diff --git a/.changelog/5166.txt b/.changelog/5166.txt new file mode 100644 index 00000000000..1386cea3910 --- /dev/null +++ b/.changelog/5166.txt @@ -0,0 +1,3 @@ +```release-note:bug +eventarc: fixed bug where resources could not be deleted out of bounds +``` diff --git a/google/resource_assured_workloads_workload.go b/google/resource_assured_workloads_workload.go index 952419f7266..4590eb54118 100644 --- a/google/resource_assured_workloads_workload.go +++ b/google/resource_assured_workloads_workload.go @@ -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 { diff --git a/google/resource_compute_firewall_policy.go b/google/resource_compute_firewall_policy.go index 334185f522c..9ba5084b71d 100644 --- a/google/resource_compute_firewall_policy.go +++ b/google/resource_compute_firewall_policy.go @@ -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 { diff --git a/google/resource_compute_firewall_policy_association.go b/google/resource_compute_firewall_policy_association.go index d2aee598810..b8bc48a3632 100644 --- a/google/resource_compute_firewall_policy_association.go +++ b/google/resource_compute_firewall_policy_association.go @@ -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 { diff --git a/google/resource_compute_firewall_policy_rule.go b/google/resource_compute_firewall_policy_rule.go index d63914d6198..240c1e8e37d 100644 --- a/google/resource_compute_firewall_policy_rule.go +++ b/google/resource_compute_firewall_policy_rule.go @@ -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 { diff --git a/google/resource_dataproc_workflow_template.go b/google/resource_dataproc_workflow_template.go index 25a456b294e..e8950492d43 100644 --- a/google/resource_dataproc_workflow_template.go +++ b/google/resource_dataproc_workflow_template.go @@ -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 { diff --git a/google/resource_eventarc_trigger.go b/google/resource_eventarc_trigger.go index 15fec496031..6841e008aef 100644 --- a/google/resource_eventarc_trigger.go +++ b/google/resource_eventarc_trigger.go @@ -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 { diff --git a/google/resource_privateca_certificate_template.go b/google/resource_privateca_certificate_template.go index 5701b19bf06..ccb7815454e 100644 --- a/google/resource_privateca_certificate_template.go +++ b/google/resource_privateca_certificate_template.go @@ -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 { diff --git a/google/tpgtools_utils.go b/google/tpgtools_utils.go index 261c679524d..cdcb287d27b 100644 --- a/google/tpgtools_utils.go +++ b/google/tpgtools_utils.go @@ -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) +}