Skip to content

Commit

Permalink
Service account description uses patch for updates. (#2650)
Browse files Browse the repository at this point in the history
Merged PR #2650.
  • Loading branch information
nat-henderson authored and modular-magician committed Nov 12, 2019
1 parent 35cba66 commit 88c0178
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
34 changes: 21 additions & 13 deletions third_party/terraform/resources/resource_google_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,31 @@ func resourceGoogleServiceAccountDelete(d *schema.ResourceData, meta interface{}

func resourceGoogleServiceAccountUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
if d.HasChange("display_name") || d.HasChange("description") {
sa, err := config.clientIAM.Projects.ServiceAccounts.Get(d.Id()).Do()
if err != nil {
return fmt.Errorf("Error retrieving service account %q: %s", d.Id(), err)
}
_, err = config.clientIAM.Projects.ServiceAccounts.Update(d.Id(),
&iam.ServiceAccount{
sa, err := config.clientIAM.Projects.ServiceAccounts.Get(d.Id()).Do()
if err != nil {
return fmt.Errorf("Error retrieving service account %q: %s", d.Id(), err)
}
updateMask := make([]string, 0)
if d.HasChange("description") {
updateMask = append(updateMask, "description")
}
if d.HasChange("display_name") {
updateMask = append(updateMask, "display_name")
}
_, err = config.clientIAM.Projects.ServiceAccounts.Patch(d.Id(),
&iam.PatchServiceAccountRequest{
UpdateMask: strings.Join(updateMask, ","),
ServiceAccount: &iam.ServiceAccount{
DisplayName: d.Get("display_name").(string),
Description: d.Get("description").(string),
Etag: sa.Etag,
}).Do()
if err != nil {
return fmt.Errorf("Error updating service account %q: %s", d.Id(), err)
}
// See comment in Create.
time.Sleep(time.Second)
},
}).Do()
if err != nil {
return err
}
// See comment in Create.
time.Sleep(time.Second)

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func TestAccServiceAccount_basic(t *testing.T) {
uniqueId := ""
displayName := "Terraform Test"
displayName2 := "Terraform Test Update"
desc := "test description"
desc2 := "test description update"
project := getTestProjectFromEnv()
expectedEmail := fmt.Sprintf("%s@%s.iam.gserviceaccount.com", accountId, project)
resource.Test(t, resource.TestCase{
Expand All @@ -25,7 +27,7 @@ func TestAccServiceAccount_basic(t *testing.T) {
Steps: []resource.TestStep{
// The first step creates a basic service account
{
Config: testAccServiceAccountBasic(accountId, displayName),
Config: testAccServiceAccountBasic(accountId, displayName, desc),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_service_account.acceptance", "project", project),
Expand All @@ -51,7 +53,7 @@ func TestAccServiceAccount_basic(t *testing.T) {
},
// The second step updates the service account
{
Config: testAccServiceAccountBasic(accountId, displayName2),
Config: testAccServiceAccountBasic(accountId, displayName2, desc2),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_service_account.acceptance", "project", project),
Expand Down Expand Up @@ -90,14 +92,14 @@ func testAccStoreServiceAccountUniqueId(uniqueId *string) resource.TestCheckFunc
}
}

func testAccServiceAccountBasic(account, name string) string {
func testAccServiceAccountBasic(account, name, desc string) string {
return fmt.Sprintf(`
resource "google_service_account" "acceptance" {
account_id = "%v"
display_name = "%v"
description = "foo"
description = "%v"
}
`, account, name)
`, account, name, desc)
}

func testAccServiceAccountWithProject(project, account, name string) string {
Expand Down

0 comments on commit 88c0178

Please sign in to comment.