Skip to content

Commit

Permalink
add polling to handle inconsistent result (#4642) (#8817)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Mar 31, 2021
1 parent 1fdfc23 commit 8578b5d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/4642.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
storage: fixed intermittent error in `google_storage_hmac_key`
```
53 changes: 53 additions & 0 deletions google/resource_storage_hmac_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"google.golang.org/api/googleapi"
)

func resourceStorageHmacKey() *schema.Resource {
Expand Down Expand Up @@ -167,11 +168,63 @@ func resourceStorageHmacKeyCreate(d *schema.ResourceData, meta interface{}) erro

d.SetId(id)

err = PollingWaitTime(resourceStorageHmacKeyPollRead(d, meta), PollCheckForExistence, "Creating HmacKey", d.Timeout(schema.TimeoutCreate), 1)
if err != nil {
return fmt.Errorf("Error waiting to create HmacKey: %s", err)
}

log.Printf("[DEBUG] Finished creating HmacKey %q: %#v", d.Id(), res)

return resourceStorageHmacKeyRead(d, meta)
}

func resourceStorageHmacKeyPollRead(d *schema.ResourceData, meta interface{}) PollReadFunc {
return func() (map[string]interface{}, error) {
config := meta.(*Config)

url, err := replaceVars(d, config, "{{StorageBasePath}}projects/{{project}}/hmacKeys/{{access_id}}")
if err != nil {
return nil, err
}

billingProject := ""

project, err := getProject(d, config)
if err != nil {
return nil, fmt.Errorf("Error fetching project for HmacKey: %s", err)
}
billingProject = project

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return nil, err
}

res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
if err != nil {
return res, err
}
res, err = resourceStorageHmacKeyDecoder(d, meta, res)
if err != nil {
return nil, err
}
if res == nil {
// Decoded object not found, spoof a 404 error for poll
return nil, &googleapi.Error{
Code: 404,
Message: "could not find object StorageHmacKey",
}
}

return res, nil
}
}

func resourceStorageHmacKeyRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
Expand Down

0 comments on commit 8578b5d

Please sign in to comment.