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 #9997

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
3 changes: 3 additions & 0 deletions .changelog/5166.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
eventarc: fixed bug where resources could not be deleted out of bounds
```
5 changes: 2 additions & 3 deletions google/resource_assured_workloads_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,8 @@ func resourceAssuredWorkloadsWorkloadRead(d *schema.ResourceData, meta interface
client := NewDCLAssuredWorkloadsClient(config, userAgent, billingProject)
res, err := client.GetWorkload(context.Background(), obj)
if err != nil {
// Resource not found
d.SetId("")
return err
resourceName := fmt.Sprintf("AssuredWorkloadsWorkload %q", d.Id())
return handleNotFoundDCLError(err, d, resourceName)
}

if err = d.Set("billing_account", res.BillingAccount); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions google/resource_compute_firewall_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,8 @@ func resourceComputeFirewallPolicyRead(d *schema.ResourceData, meta interface{})
client := NewDCLComputeClient(config, userAgent, billingProject)
res, err := client.GetFirewallPolicy(context.Background(), obj)
if err != nil {
// Resource not found
d.SetId("")
return err
resourceName := fmt.Sprintf("ComputeFirewallPolicy %q", d.Id())
return handleNotFoundDCLError(err, d, resourceName)
}

if err = d.Set("parent", res.Parent); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions google/resource_compute_firewall_policy_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,8 @@ func resourceComputeFirewallPolicyAssociationRead(d *schema.ResourceData, meta i
client := NewDCLComputeClient(config, userAgent, billingProject)
res, err := client.GetFirewallPolicyAssociation(context.Background(), obj)
if err != nil {
// Resource not found
d.SetId("")
return err
resourceName := fmt.Sprintf("ComputeFirewallPolicyAssociation %q", d.Id())
return handleNotFoundDCLError(err, d, resourceName)
}

if err = d.Set("attachment_target", res.AttachmentTarget); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions google/resource_compute_firewall_policy_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,8 @@ func resourceComputeFirewallPolicyRuleRead(d *schema.ResourceData, meta interfac
client := NewDCLComputeClient(config, userAgent, billingProject)
res, err := client.GetFirewallPolicyRule(context.Background(), obj)
if err != nil {
// Resource not found
d.SetId("")
return err
resourceName := fmt.Sprintf("ComputeFirewallPolicyRule %q", d.Id())
return handleNotFoundDCLError(err, d, resourceName)
}

if err = d.Set("action", res.Action); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions google/resource_dataproc_workflow_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -1798,9 +1798,8 @@ func resourceDataprocWorkflowTemplateRead(d *schema.ResourceData, meta interface
client := NewDCLDataprocClient(config, userAgent, billingProject)
res, err := client.GetWorkflowTemplate(context.Background(), obj)
if err != nil {
// Resource not found
d.SetId("")
return err
resourceName := fmt.Sprintf("DataprocWorkflowTemplate %q", d.Id())
return handleNotFoundDCLError(err, d, resourceName)
}

if err = d.Set("jobs", flattenDataprocWorkflowTemplateJobsArray(res.Jobs)); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions google/resource_eventarc_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,8 @@ func resourceEventarcTriggerRead(d *schema.ResourceData, meta interface{}) error
client := NewDCLEventarcClient(config, userAgent, billingProject)
res, err := client.GetTrigger(context.Background(), obj)
if err != nil {
// Resource not found
d.SetId("")
return err
resourceName := fmt.Sprintf("EventarcTrigger %q", d.Id())
return handleNotFoundDCLError(err, d, resourceName)
}

if err = d.Set("destination", flattenEventarcTriggerDestination(res.Destination)); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions google/resource_privateca_certificate_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,8 @@ func resourcePrivatecaCertificateTemplateRead(d *schema.ResourceData, meta inter
client := NewDCLPrivatecaClient(config, userAgent, billingProject)
res, err := client.GetCertificateTemplate(context.Background(), obj)
if err != nil {
// Resource not found
d.SetId("")
return err
resourceName := fmt.Sprintf("PrivatecaCertificateTemplate %q", d.Id())
return handleNotFoundDCLError(err, d, resourceName)
}

if err = d.Set("location", res.Location); err != nil {
Expand Down
21 changes: 21 additions & 0 deletions google/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)
}