Skip to content

Commit

Permalink
Removed guard around operation WithResponse method & improved error h…
Browse files Browse the repository at this point in the history
…andling (GoogleCloudPlatform#8762)

* Removed guard around operation WithResponse method

Resolved hashicorp/terraform-provider-google#15618

* Handle missing resource inside response more gracefully
  • Loading branch information
melinath authored and joelkattapuram committed Sep 20, 2023
1 parent ce63529 commit 9d182e4
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions mmv1/templates/terraform/operation.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
package <%= product_ns.downcase -%>

import (
<% if async.result.resource_inside_response -%>
"encoding/json"
<% end -%>
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -83,7 +82,6 @@ func create<%= product_name %>Waiter(config *transport_tpg.Config, op map[string
return w, nil
}

<% if async.result.resource_inside_response -%>
<%# Not all APIs will need a WithResponse operation, but it's hard to check whether
they will or not since it involves iterating over all resources.
Might as well just nolint it so we can pass the linter checks.
Expand All @@ -97,9 +95,12 @@ func <%= product_name.camelize() -%>OperationWaitTimeWithResponse(config *transp
if err := tpgresource.OperationWait(w, activity, timeout, config.PollInterval); err != nil {
return err
}
return json.Unmarshal([]byte(w.CommonOperationWaiter.Op.Response), response)
rawResponse := []byte(w.CommonOperationWaiter.Op.Response)
if len(rawResponse) == 0 {
return errors.New("`resource` not set in operation response")
}
return json.Unmarshal(rawResponse, response)
}
<% end -%>

func <%= product_name.camelize() -%>OperationWaitTime(config *transport_tpg.Config, op map[string]interface{}, <% if has_project -%> project,<% end -%> activity, userAgent string, timeout time.Duration) error {
if val, ok := op["name"]; !ok || val == "" {
Expand Down

0 comments on commit 9d182e4

Please sign in to comment.