2016-11-08 07:27:32 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2016-11-14 17:42:11 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2016-11-08 07:27:32 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Test that a service account resource can be created, updated, and destroyed
|
2018-02-23 23:14:24 +00:00
|
|
|
func TestAccServiceAccount_basic(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-11-14 17:42:11 +00:00
|
|
|
accountId := "a" + acctest.RandString(10)
|
2017-10-25 19:33:21 +00:00
|
|
|
uniqueId := ""
|
2016-11-17 17:49:22 +00:00
|
|
|
displayName := "Terraform Test"
|
|
|
|
displayName2 := "Terraform Test Update"
|
2017-11-21 17:34:32 +00:00
|
|
|
project := getTestProjectFromEnv()
|
2016-11-08 07:27:32 +00:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
// The first step creates a basic service account
|
|
|
|
resource.TestStep{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccServiceAccountBasic(accountId, displayName),
|
2016-11-08 07:27:32 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleServiceAccountExists("google_service_account.acceptance"),
|
2017-10-25 19:33:21 +00:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_service_account.acceptance", "project", project),
|
2016-11-08 07:27:32 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
// The second step updates the service account
|
|
|
|
resource.TestStep{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccServiceAccountBasic(accountId, displayName2),
|
2016-11-08 07:27:32 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-11-17 17:49:22 +00:00
|
|
|
testAccCheckGoogleServiceAccountNameModified("google_service_account.acceptance", displayName2),
|
2017-10-25 19:33:21 +00:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_service_account.acceptance", "project", project),
|
|
|
|
testAccStoreServiceAccountUniqueId(&uniqueId),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
// The third step explicitely adds the same default project to the service account configuration
|
|
|
|
// and ensure the service account is not recreated by comparing the value of its unique_id with the one from the previous step
|
|
|
|
resource.TestStep{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccServiceAccountWithProject(project, accountId, displayName2),
|
2017-10-25 19:33:21 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleServiceAccountNameModified("google_service_account.acceptance", displayName2),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_service_account.acceptance", "project", project),
|
|
|
|
resource.TestCheckResourceAttrPtr(
|
|
|
|
"google_service_account.acceptance", "unique_id", &uniqueId),
|
2016-11-08 07:27:32 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that a service account resource can be created with a policy, updated,
|
|
|
|
// and destroyed.
|
2018-02-23 23:14:24 +00:00
|
|
|
func TestAccServiceAccount_createPolicy(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-11-14 17:42:11 +00:00
|
|
|
accountId := "a" + acctest.RandString(10)
|
2016-11-17 17:49:22 +00:00
|
|
|
displayName := "Terraform Test"
|
2016-11-08 07:27:32 +00:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
// The first step creates a basic service account with an IAM policy
|
|
|
|
resource.TestStep{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccServiceAccountPolicy(accountId, getTestProjectFromEnv()),
|
2016-11-08 07:27:32 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleServiceAccountPolicyCount("google_service_account.acceptance", 1),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
// The second step updates the service account with no IAM policy
|
|
|
|
resource.TestStep{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccServiceAccountBasic(accountId, displayName),
|
2016-11-08 07:27:32 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleServiceAccountPolicyCount("google_service_account.acceptance", 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
// The final step re-applies the IAM policy
|
|
|
|
resource.TestStep{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccServiceAccountPolicy(accountId, getTestProjectFromEnv()),
|
2016-11-08 07:27:32 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleServiceAccountPolicyCount("google_service_account.acceptance", 1),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-25 19:33:21 +00:00
|
|
|
func testAccStoreServiceAccountUniqueId(uniqueId *string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
*uniqueId = s.RootModule().Resources["google_service_account.acceptance"].Primary.Attributes["unique_id"]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 07:27:32 +00:00
|
|
|
func testAccCheckGoogleServiceAccountPolicyCount(r string, n int) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
c := testAccProvider.Meta().(*Config)
|
|
|
|
p, err := getServiceAccountIamPolicy(s.RootModule().Resources[r].Primary.ID, c)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to retrieve IAM Policy for service account: %s", err)
|
|
|
|
}
|
|
|
|
if len(p.Bindings) != n {
|
|
|
|
return fmt.Errorf("The service account has %v bindings but %v were expected", len(p.Bindings), n)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGoogleServiceAccountExists(r string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[r]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 17:49:22 +00:00
|
|
|
func testAccCheckGoogleServiceAccountNameModified(r, n string) resource.TestCheckFunc {
|
2016-11-08 07:27:32 +00:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[r]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", r)
|
|
|
|
}
|
|
|
|
|
2016-11-17 17:49:22 +00:00
|
|
|
if rs.Primary.Attributes["display_name"] != n {
|
|
|
|
return fmt.Errorf("display_name is %q expected %q", rs.Primary.Attributes["display_name"], n)
|
2016-11-08 07:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:14:24 +00:00
|
|
|
func testAccServiceAccountBasic(account, name string) string {
|
2016-11-17 17:49:22 +00:00
|
|
|
t := `resource "google_service_account" "acceptance" {
|
2016-11-08 07:27:32 +00:00
|
|
|
account_id = "%v"
|
|
|
|
display_name = "%v"
|
2016-11-17 17:49:22 +00:00
|
|
|
}`
|
|
|
|
return fmt.Sprintf(t, account, name)
|
|
|
|
}
|
2016-11-08 07:27:32 +00:00
|
|
|
|
2018-02-23 23:14:24 +00:00
|
|
|
func testAccServiceAccountWithProject(project, account, name string) string {
|
2017-10-25 19:33:21 +00:00
|
|
|
t := `resource "google_service_account" "acceptance" {
|
|
|
|
project = "%v"
|
|
|
|
account_id = "%v"
|
|
|
|
display_name = "%v"
|
|
|
|
}`
|
|
|
|
return fmt.Sprintf(t, project, account, name)
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:14:24 +00:00
|
|
|
func testAccServiceAccountPolicy(account, project string) string {
|
2016-11-17 17:49:22 +00:00
|
|
|
|
|
|
|
t := `resource "google_service_account" "acceptance" {
|
2016-11-08 07:27:32 +00:00
|
|
|
account_id = "%v"
|
2016-11-14 19:12:55 +00:00
|
|
|
display_name = "%v"
|
2016-11-08 07:27:32 +00:00
|
|
|
policy_data = "${data.google_iam_policy.service_account.policy_data}"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "google_iam_policy" "service_account" {
|
|
|
|
binding {
|
|
|
|
role = "roles/iam.serviceAccountActor"
|
|
|
|
members = [
|
2016-11-14 17:42:11 +00:00
|
|
|
"serviceAccount:%v@%v.iam.gserviceaccount.com",
|
2016-11-08 07:27:32 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}`
|
2016-11-17 17:49:22 +00:00
|
|
|
|
2017-11-21 17:34:32 +00:00
|
|
|
return fmt.Sprintf(t, account, account, account, project)
|
2016-11-17 17:49:22 +00:00
|
|
|
}
|