Skip to content

Commit

Permalink
Handle scenarios where the dcl resource can't be found on READ (#5166)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottSuarez authored Sep 2, 2021
1 parent 2f48ca5 commit 6f745ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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

0 comments on commit 6f745ae

Please sign in to comment.