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

Handle location tag bindings of removed resources #11857

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"strings"
"time"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
"google.golang.org/api/googleapi"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -180,7 +182,7 @@ func resourceTagsLocationTagBindingRead(d *schema.ResourceData, meta interface{}
UserAgent: userAgent,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("TagsLocationTagBinding %q", d.Id()))
return transport_tpg.HandleNotFoundError(transformTagsLocationTagBindingReadError(err), d, fmt.Sprintf("TagsLocationTagBinding %q", d.Id()))
}
log.Printf("[DEBUG] Skipping res with name for import = %#v,)", res)

Expand All @@ -195,7 +197,7 @@ func resourceTagsLocationTagBindingRead(d *schema.ResourceData, meta interface{}
for pageToken != "" {
url, err = transport_tpg.AddQueryParams(url, map[string]string{"pageToken": fmt.Sprintf("%s", res["nextPageToken"])})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("TagsLocationTagBinding %q", d.Id()))
return transport_tpg.HandleNotFoundError(transformTagsLocationTagBindingReadError(err), d, fmt.Sprintf("TagsLocationTagBinding %q", d.Id()))
}
resp, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Expand Down Expand Up @@ -386,3 +388,27 @@ func resourceTagsLocationTagBindingFindNestedObjectInList(d *schema.ResourceData
}
return -1, nil, nil
}

func transformTagsLocationTagBindingReadError(err error) error {
if gErr, ok := errwrap.GetType(err, &googleapi.Error{}).(*googleapi.Error); ok && gErr.Code == 403 {
for _, detail := range gErr.Details {
if detailMap, ok := detail.(map[string]interface{}); ok {
if detailType, ok := detailMap["@type"].(string); ok && detailType == "type.googleapis.com/google.rpc.ResourceInfo" {
if description, ok := detailMap["description"].(string); ok && strings.Contains(description, "(or the resource may not exist in this location)") {
// This error occurs when either the tag binding parent does not exist, or permission is denied. It is
// deliberately ambiguous so that existence information is not revealed to the caller. However, for
// the Read function, we can only assume that the membership does not exist, and proceed with attempting
// other operations. Since HandleNotFoundError(...) expects an error code of 404 when a resource does not
// exist, to get the desired behavior, we modify the error code to be 404.
gErr.Code = 404
}
}
}
}

log.Printf("[DEBUG] Transformed TagsLocationTagBinding error")
return gErr
}

return err
}
Loading