diff --git a/mmv1/products/monitoring/UptimeCheckConfig.yaml b/mmv1/products/monitoring/UptimeCheckConfig.yaml index 5da0d11df199..b83d6cf5fd87 100644 --- a/mmv1/products/monitoring/UptimeCheckConfig.yaml +++ b/mmv1/products/monitoring/UptimeCheckConfig.yaml @@ -72,6 +72,7 @@ custom_code: !ruby/object:Provider::Terraform::CustomCode constants: templates/terraform/constants/monitoring_uptime_check_config.go.erb custom_import: templates/terraform/custom_import/self_link_as_name.erb post_create: templates/terraform/post_create/set_computed_name.erb + custom_delete: templates/terraform/custom_delete/monitoring_uptime_check_config.go.erb properties: - !ruby/object:Api::Type::String name: name diff --git a/mmv1/templates/terraform/custom_delete/monitoring_uptime_check_config.go.erb b/mmv1/templates/terraform/custom_delete/monitoring_uptime_check_config.go.erb new file mode 100644 index 000000000000..737359dceee3 --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/monitoring_uptime_check_config.go.erb @@ -0,0 +1,55 @@ +billingProject := "" + +project, err := tpgresource.GetProject(d, config) +if err != nil { + return fmt.Errorf("Error fetching project for UptimeCheckConfig: %s", err) +} +billingProject = project + +lockName, err := tpgresource.ReplaceVars(d, config, "stackdriver/groups/{{project}}") +if err != nil { + return err +} +transport_tpg.MutexStore.Lock(lockName) +defer transport_tpg.MutexStore.Unlock(lockName) + +url, err := tpgresource.ReplaceVars(d, config, "{{MonitoringBasePath}}v3/{{name}}") +if err != nil { + return err +} + +var obj map[string]interface{} +log.Printf("[DEBUG] Deleting UptimeCheckConfig %q", d.Id()) + +// err == nil indicates that the billing_project value was found +if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "DELETE", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutDelete), + ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.IsMonitoringConcurrentEditError}, +}) + + +<%# + The generated DELETE method returns a generic error for 400. + Need to include a help message about deleting associated Alert Policies. +-%> + +if err != nil { + if transport_tpg.IsGoogleApiErrorWithCode(err, 400) { + err := fmt.Errorf("%w - please ensure all associated Alert Policies are deleted.", err) + return errwrap.Wrapf("Error when reading or editing UptimeCheckConfig: {{err}}", err) + } + return transport_tpg.HandleNotFoundError(err, d, "UptimeCheckConfig") +} + +log.Printf("[DEBUG] Finished deleting UptimeCheckConfig %q: %#v", d.Id(), res) +return nil \ No newline at end of file diff --git a/mmv1/third_party/terraform/services/monitoring/resource_monitoring_uptime_check_config_sweeper.go b/mmv1/third_party/terraform/services/monitoring/resource_monitoring_uptime_check_config_sweeper.go new file mode 100644 index 000000000000..915ebbe3f1ca --- /dev/null +++ b/mmv1/third_party/terraform/services/monitoring/resource_monitoring_uptime_check_config_sweeper.go @@ -0,0 +1,137 @@ + +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** Type: MMv1 *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package monitoring + +import ( + "context" + "log" + "strings" + "testing" + + "github.com/hashicorp/terraform-provider-google/google/envvar" + "github.com/hashicorp/terraform-provider-google/google/sweeper" + "github.com/hashicorp/terraform-provider-google/google/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" +) + +func init() { + sweeper.AddTestSweepers("MonitoringUptimeCheckConfig", testSweepMonitoringUptimeCheckConfig) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepMonitoringUptimeCheckConfig(region string) error { + resourceName := "MonitoringUptimeCheckConfig" + log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName) + + config, err := sweeper.SharedConfigForRegion(region) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error getting shared config for region: %s", err) + return err + } + + err = config.LoadAndValidate(context.Background()) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err) + return err + } + + t := &testing.T{} + billingId := envvar.GetTestBillingAccountFromEnv(t) + + // Setup variables to replace in list template + d := &tpgresource.ResourceDataMock{ + FieldsInSchema: map[string]interface{}{ + "project": config.Project, + "region": region, + "location": region, + "zone": "-", + "billing_account": billingId, + }, + } + + listTemplate := strings.Split("https://monitoring.googleapis.com/v3/projects/{{project}}/uptimeCheckConfigs", "?")[0] + listUrl, err := tpgresource.ReplaceVars(d, config, listTemplate) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err) + return nil + } + + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: config.Project, + RawURL: listUrl, + UserAgent: config.UserAgent, + }) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", listUrl, err) + return nil + } + + resourceList, ok := res["uptimeCheckConfigs"] + if !ok { + log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.") + return nil + } + + rl := resourceList.([]interface{}) + + log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName) + // Keep count of items that aren't sweepable for logging. + nonPrefixCount := 0 + for _, ri := range rl { + obj := ri.(map[string]interface{}) + if obj["name"] == nil { + log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName) + return nil + } + + name := tpgresource.GetResourceNameFromSelfLink(obj["name"].(string)) + // Skip resources that shouldn't be sweeped + if !sweeper.IsSweepableTestResource(name) { + nonPrefixCount++ + continue + } + + deleteTemplate := "https://monitoring.googleapis.com/v3/{{name}}" + deleteUrl, err := tpgresource.ReplaceVars(d, config, deleteTemplate) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err) + return nil + } + deleteUrl = deleteUrl + name + + // Don't wait on operations as we may have a lot to delete + _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "DELETE", + Project: config.Project, + RawURL: deleteUrl, + UserAgent: config.UserAgent, + }) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err) + } else { + log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, name) + } + } + + if nonPrefixCount > 0 { + log.Printf("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped.", nonPrefixCount) + } + + return nil +}