Skip to content

Commit

Permalink
Add description to service account. (#4734)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and nat-henderson committed Oct 25, 2019
1 parent 77891f3 commit 887d1b1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
17 changes: 16 additions & 1 deletion google/resource_google_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package google
import (
"fmt"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"google.golang.org/api/iam/v1"
Expand Down Expand Up @@ -40,6 +41,10 @@ func resourceGoogleServiceAccount() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"project": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -63,9 +68,11 @@ func resourceGoogleServiceAccountCreate(d *schema.ResourceData, meta interface{}
}
aid := d.Get("account_id").(string)
displayName := d.Get("display_name").(string)
description := d.Get("description").(string)

sa := &iam.ServiceAccount{
DisplayName: displayName,
Description: description,
}

r := &iam.CreateServiceAccountRequest{
Expand All @@ -79,6 +86,10 @@ func resourceGoogleServiceAccountCreate(d *schema.ResourceData, meta interface{}
}

d.SetId(sa.Name)
// This API is meant to be synchronous, but in practice it shows the old value for
// a few milliseconds after the update goes through. A second is more than enough
// time to ensure following reads are correct.
time.Sleep(time.Second)

return resourceGoogleServiceAccountRead(d, meta)
}
Expand All @@ -98,6 +109,7 @@ func resourceGoogleServiceAccountRead(d *schema.ResourceData, meta interface{})
d.Set("account_id", strings.Split(sa.Email, "@")[0])
d.Set("name", sa.Name)
d.Set("display_name", sa.DisplayName)
d.Set("description", sa.Description)
return nil
}

Expand All @@ -114,19 +126,22 @@ func resourceGoogleServiceAccountDelete(d *schema.ResourceData, meta interface{}

func resourceGoogleServiceAccountUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
if ok := d.HasChange("display_name"); ok {
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{
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)
}

return nil
Expand Down
2 changes: 2 additions & 0 deletions google/resource_google_service_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func testAccServiceAccountBasic(account, name string) string {
resource "google_service_account" "acceptance" {
account_id = "%v"
display_name = "%v"
description = "foo"
}
`, account, name)
}
Expand All @@ -105,6 +106,7 @@ resource "google_service_account" "acceptance" {
project = "%v"
account_id = "%v"
display_name = "%v"
description = "foo"
}
`, project, account, name)
}
2 changes: 2 additions & 0 deletions website/docs/r/google_service_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ The following arguments are supported:
* `display_name` - (Optional) The display name for the service account.
Can be updated without creating a new resource.

* `description` - (Optional) A text description of the service account.

* `project` - (Optional) The ID of the project that the service account will be created in.
Defaults to the provider project configuration.

Expand Down

0 comments on commit 887d1b1

Please sign in to comment.