Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry to service account creation #2074

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/3513.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
iam: Fixed an issue where `google_service_account` shows an error after creating the resource
```
18 changes: 13 additions & 5 deletions google-beta/resource_google_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func resourceGoogleServiceAccount() *schema.Resource {
Importer: &schema.ResourceImporter{
State: resourceGoogleServiceAccountImport,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"email": {
Type: schema.TypeString,
Expand Down Expand Up @@ -83,10 +86,15 @@ 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)

err = retryTimeDuration(func() (operr error) {
_, saerr := config.clientIAM.Projects.ServiceAccounts.Get(d.Id()).Do()
return saerr
}, d.Timeout(schema.TimeoutCreate), isNotFoundRetryableError("service account creation"))

if err != nil {
return fmt.Errorf("Error reading service account after creation: %s", err)
}

return resourceGoogleServiceAccountRead(d, meta)
}
Expand Down Expand Up @@ -146,7 +154,7 @@ func resourceGoogleServiceAccountUpdate(d *schema.ResourceData, meta interface{}
if err != nil {
return err
}
// See comment in Create.
// API tends to be asynchronous
time.Sleep(time.Second)

return nil
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/google_service_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ exported:

* `unique_id` - The unique id of the service account.

## Timeouts

This resource provides the following
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:

- `create` - Default is 5 minutes.

## Import

Service accounts can be imported using their URI, e.g.
Expand Down