From 15c8a5bfdc6b0c541c67128cf4359d17f8ec4a60 Mon Sep 17 00:00:00 2001 From: VenelinMartinov Date: Thu, 16 May 2024 23:43:45 +0100 Subject: [PATCH] Prevent permadiff on monitoring_uptime_check_config (#10694) --- .../monitoring/UptimeCheckConfig.yaml | 1 + .../monitoring_uptime_check_config.go.erb | 9 +++ ...rce_monitoring_uptime_check_config_test.go | 61 +++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/mmv1/products/monitoring/UptimeCheckConfig.yaml b/mmv1/products/monitoring/UptimeCheckConfig.yaml index 5396ed6f9068..fddd4a0bcf9f 100644 --- a/mmv1/products/monitoring/UptimeCheckConfig.yaml +++ b/mmv1/products/monitoring/UptimeCheckConfig.yaml @@ -462,6 +462,7 @@ properties: name: 'labels' immutable: true required: true + diff_suppress_func: resourceMonitoringUptimeCheckConfigMonitoredResourceLabelsDiffSuppress description: Values for all of the labels listed in the associated monitored resource descriptor. For example, Compute Engine VM instances use the diff --git a/mmv1/templates/terraform/constants/monitoring_uptime_check_config.go.erb b/mmv1/templates/terraform/constants/monitoring_uptime_check_config.go.erb index b10157904afa..8b22300db65c 100644 --- a/mmv1/templates/terraform/constants/monitoring_uptime_check_config.go.erb +++ b/mmv1/templates/terraform/constants/monitoring_uptime_check_config.go.erb @@ -1,3 +1,12 @@ func resourceMonitoringUptimeCheckConfigHttpCheckPathDiffSuppress(k, old, new string, d *schema.ResourceData) bool { return old == "/"+new +} + +func resourceMonitoringUptimeCheckConfigMonitoredResourceLabelsDiffSuppress(k, old, new string, d *schema.ResourceData) bool { + // GCP adds the project_id to the labels if unset. + // We want to suppress the diff if not set in the config. + if strings.HasSuffix(k, "project_id") && new == "" && old != "" { + return true + } + return false } \ No newline at end of file diff --git a/mmv1/third_party/terraform/services/monitoring/resource_monitoring_uptime_check_config_test.go b/mmv1/third_party/terraform/services/monitoring/resource_monitoring_uptime_check_config_test.go index 07139f808a25..213d6d1a87d1 100644 --- a/mmv1/third_party/terraform/services/monitoring/resource_monitoring_uptime_check_config_test.go +++ b/mmv1/third_party/terraform/services/monitoring/resource_monitoring_uptime_check_config_test.go @@ -41,6 +41,34 @@ func TestAccMonitoringUptimeCheckConfig_update(t *testing.T) { }) } +func TestAccMonitoringUptimeCheckConfig_noProjectId(t *testing.T) { + t.Parallel() + host := "192.168.1.1" + suffix := acctest.RandString(t, 4) + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckMonitoringUptimeCheckConfigDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccMonitoringUptimeCheckConfig_noProjectId(suffix, host), + }, + { + ResourceName: "google_monitoring_uptime_check_config.http", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"http_check.0.auth_info.0.password"}, + }, + { + Config: testAccMonitoringUptimeCheckConfig_noProjectId(suffix, host), + PlanOnly: true, + ResourceName: "google_monitoring_uptime_check_config.http", + }, + }, + }) +} + // The second update should force a recreation of the uptime check because 'monitored_resource' isn't // updatable in place func TestAccMonitoringUptimeCheckConfig_changeNonUpdatableFields(t *testing.T) { @@ -180,3 +208,36 @@ resource "google_monitoring_uptime_check_config" "http" { `, suffix, project, host, content, json_path, json_path_matcher, ) } + +func testAccMonitoringUptimeCheckConfig_noProjectId(suffix, host string) string { + return fmt.Sprintf(` +resource "google_monitoring_uptime_check_config" "http" { + display_name = "http-uptime-check-%s" + timeout = "60s" + period = "60s" + + http_check { + path = "/mypath" + port = "8010" + request_method = "GET" + auth_info { + username = "name" + password = "mypassword" + } + } + + monitored_resource { + type = "uptime_url" + labels = { + host = "%s" + } + } + + content_matchers { + content = "example" + matcher = "CONTAINS_STRING" + } +} +`, suffix, host, + ) +}