Resolve review feedback

This commit is contained in:
Evan Brown 2016-11-17 09:49:22 -08:00
parent 6257dfe221
commit d267c3587a
2 changed files with 24 additions and 22 deletions

View File

@ -178,9 +178,7 @@ func resourceGoogleServiceAccountUpdate(d *schema.ResourceData, meta interface{}
newPString = "{}" newPString = "{}"
} }
oldPStringf, _ := json.MarshalIndent(oldPString, "", " ") log.Printf("[DEBUG]: Old policy: %q\nNew policy: %q", string(oldPString), string(newPString))
newPStringf, _ := json.MarshalIndent(newPString, "", " ")
log.Printf("[DEBUG]: Old policy: %v\nNew policy: %v", string(oldPStringf), string(newPStringf))
var oldPolicy, newPolicy iam.Policy var oldPolicy, newPolicy iam.Policy
if err = json.Unmarshal([]byte(newPString), &newPolicy); err != nil { if err = json.Unmarshal([]byte(newPString), &newPolicy); err != nil {

View File

@ -9,30 +9,27 @@ import (
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
var (
displayName = "Terraform Test"
displayName2 = "Terraform Test Update"
)
// Test that a service account resource can be created, updated, and destroyed // Test that a service account resource can be created, updated, and destroyed
func TestAccGoogleServiceAccount_basic(t *testing.T) { func TestAccGoogleServiceAccount_basic(t *testing.T) {
accountId := "a" + acctest.RandString(10) accountId := "a" + acctest.RandString(10)
displayName := "Terraform Test"
displayName2 := "Terraform Test Update"
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
// The first step creates a basic service account // The first step creates a basic service account
resource.TestStep{ resource.TestStep{
Config: fmt.Sprintf(testAccGoogleServiceAccount_basic, accountId, displayName), Config: testAccGoogleServiceAccountBasic(accountId, displayName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleServiceAccountExists("google_service_account.acceptance"), testAccCheckGoogleServiceAccountExists("google_service_account.acceptance"),
), ),
}, },
// The second step updates the service account // The second step updates the service account
resource.TestStep{ resource.TestStep{
Config: fmt.Sprintf(testAccGoogleServiceAccount_basic, accountId, displayName2), Config: testAccGoogleServiceAccountBasic(accountId, displayName2),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleServiceAccountNameModified("google_service_account.acceptance"), testAccCheckGoogleServiceAccountNameModified("google_service_account.acceptance", displayName2),
), ),
}, },
}, },
@ -43,27 +40,28 @@ func TestAccGoogleServiceAccount_basic(t *testing.T) {
// and destroyed. // and destroyed.
func TestAccGoogleServiceAccount_createPolicy(t *testing.T) { func TestAccGoogleServiceAccount_createPolicy(t *testing.T) {
accountId := "a" + acctest.RandString(10) accountId := "a" + acctest.RandString(10)
displayName := "Terraform Test"
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
// The first step creates a basic service account with an IAM policy // The first step creates a basic service account with an IAM policy
resource.TestStep{ resource.TestStep{
Config: fmt.Sprintf(testAccGoogleServiceAccount_policy, accountId, displayName, accountId, projectId), Config: testAccGoogleServiceAccountPolicy(accountId, projectId),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleServiceAccountPolicyCount("google_service_account.acceptance", 1), testAccCheckGoogleServiceAccountPolicyCount("google_service_account.acceptance", 1),
), ),
}, },
// The second step updates the service account with no IAM policy // The second step updates the service account with no IAM policy
resource.TestStep{ resource.TestStep{
Config: fmt.Sprintf(testAccGoogleServiceAccount_basic, accountId, displayName), Config: testAccGoogleServiceAccountBasic(accountId, displayName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleServiceAccountPolicyCount("google_service_account.acceptance", 0), testAccCheckGoogleServiceAccountPolicyCount("google_service_account.acceptance", 0),
), ),
}, },
// The final step re-applies the IAM policy // The final step re-applies the IAM policy
resource.TestStep{ resource.TestStep{
Config: fmt.Sprintf(testAccGoogleServiceAccount_policy, accountId, displayName, accountId, projectId), Config: testAccGoogleServiceAccountPolicy(accountId, projectId),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleServiceAccountPolicyCount("google_service_account.acceptance", 1), testAccCheckGoogleServiceAccountPolicyCount("google_service_account.acceptance", 1),
), ),
@ -101,29 +99,32 @@ func testAccCheckGoogleServiceAccountExists(r string) resource.TestCheckFunc {
} }
} }
func testAccCheckGoogleServiceAccountNameModified(r string) resource.TestCheckFunc { func testAccCheckGoogleServiceAccountNameModified(r, n string) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[r] rs, ok := s.RootModule().Resources[r]
if !ok { if !ok {
return fmt.Errorf("Not found: %s", r) return fmt.Errorf("Not found: %s", r)
} }
if rs.Primary.Attributes["display_name"] != displayName2 { if rs.Primary.Attributes["display_name"] != n {
return fmt.Errorf("display_name is %q expected %q", rs.Primary.Attributes["display_name"], displayName2) return fmt.Errorf("display_name is %q expected %q", rs.Primary.Attributes["display_name"], n)
} }
return nil return nil
} }
} }
var testAccGoogleServiceAccount_basic = ` func testAccGoogleServiceAccountBasic(account, name string) string {
resource "google_service_account" "acceptance" { t := `resource "google_service_account" "acceptance" {
account_id = "%v" account_id = "%v"
display_name = "%v" display_name = "%v"
}` }`
return fmt.Sprintf(t, account, name)
}
var testAccGoogleServiceAccount_policy = ` func testAccGoogleServiceAccountPolicy(account, name string) string {
resource "google_service_account" "acceptance" {
t := `resource "google_service_account" "acceptance" {
account_id = "%v" account_id = "%v"
display_name = "%v" display_name = "%v"
policy_data = "${data.google_iam_policy.service_account.policy_data}" policy_data = "${data.google_iam_policy.service_account.policy_data}"
@ -137,3 +138,6 @@ data "google_iam_policy" "service_account" {
] ]
} }
}` }`
return fmt.Sprintf(t, account, name, account, projectId)
}