From 13b292f883f82cc6961cb621e6adfc6b355a482f Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 17 May 2024 10:51:56 -0700 Subject: [PATCH] Prevent permadiff on monitoring_uptime_check_config (#10694) (#18174) [upstream:19dd78e2f6cae1217073663eb1d8bb9d584190eb] Signed-off-by: Modular Magician --- ...resource_monitoring_uptime_check_config.go | 20 ++++-- ...rce_monitoring_uptime_check_config_test.go | 61 +++++++++++++++++++ 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/google/services/monitoring/resource_monitoring_uptime_check_config.go b/google/services/monitoring/resource_monitoring_uptime_check_config.go index 436538bd3ee..b3b0bdb6fbf 100644 --- a/google/services/monitoring/resource_monitoring_uptime_check_config.go +++ b/google/services/monitoring/resource_monitoring_uptime_check_config.go @@ -38,6 +38,15 @@ func resourceMonitoringUptimeCheckConfigHttpCheckPathDiffSuppress(k, old, new st 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 +} + func ResourceMonitoringUptimeCheckConfig() *schema.Resource { return &schema.Resource{ Create: resourceMonitoringUptimeCheckConfigCreate, @@ -287,11 +296,12 @@ uptime checks: Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "labels": { - Type: schema.TypeMap, - Required: true, - ForceNew: true, - Description: `Values for all of the labels listed in the associated monitored resource descriptor. For example, Compute Engine VM instances use the labels 'project_id', 'instance_id', and 'zone'.`, - Elem: &schema.Schema{Type: schema.TypeString}, + Type: schema.TypeMap, + Required: true, + ForceNew: true, + DiffSuppressFunc: resourceMonitoringUptimeCheckConfigMonitoredResourceLabelsDiffSuppress, + Description: `Values for all of the labels listed in the associated monitored resource descriptor. For example, Compute Engine VM instances use the labels 'project_id', 'instance_id', and 'zone'.`, + Elem: &schema.Schema{Type: schema.TypeString}, }, "type": { Type: schema.TypeString, diff --git a/google/services/monitoring/resource_monitoring_uptime_check_config_test.go b/google/services/monitoring/resource_monitoring_uptime_check_config_test.go index 6455016963c..1411e44a3c8 100644 --- a/google/services/monitoring/resource_monitoring_uptime_check_config_test.go +++ b/google/services/monitoring/resource_monitoring_uptime_check_config_test.go @@ -43,6 +43,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) { @@ -182,3 +210,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, + ) +}