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 scenarios where the dcl resource can't be found on READ #5166

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tpgtools/handwritten/tpgtools_utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
package google

import (
"fmt"
"log"

dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func oldValue(old, new interface{}) interface{} {
return old
}

func handleNotFoundDCLError(err error, d *schema.ResourceData, resourceName string) error {
if dcl.IsNotFound(err) {
log.Printf("[WARN] Removing %s because it's gone", resourceName)
// The resource doesn't exist anymore
d.SetId("")
return nil
}

return errwrap.Wrapf(
fmt.Sprintf("Error when reading or editing %s: {{err}}", resourceName), err)
}
5 changes: 2 additions & 3 deletions tpgtools/templates/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,8 @@ func resource{{$.PathType}}Read(d *schema.ResourceData, meta interface{}) error
client := NewDCL{{$.ProductType}}Client(config, userAgent, billingProject)
res, err := client.Get{{$.Type}}(context.Background(), obj)
if err != nil {
// Resource not found
d.SetId("")
return err
resourceName := fmt.Sprintf("{{$.PathType}} %q", d.Id())
return handleNotFoundDCLError(err, d, resourceName)
}

{{ range $v := .Properties -}}
Expand Down