Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent permadiff on monitoring_uptime_check_config #18174

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
)
}