Skip to content

Commit

Permalink
Prevent permadiff on monitoring_uptime_check_config (#10694) (#18174)
Browse files Browse the repository at this point in the history
[upstream:19dd78e2f6cae1217073663eb1d8bb9d584190eb]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored May 17, 2024
1 parent d13b1ec commit 13b292f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
)
}

0 comments on commit 13b292f

Please sign in to comment.