hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.28k stars 1.72k forks source link

Failed to create a resource `google_monitoring_alert_policy` with `documentation.0.subject` #18518

Open umatare5 opened 2 months ago

umatare5 commented 2 months ago

Community Note

Terraform Version & Provider Version(s)

Terraform v1.8.5
on darwin_arm64
+ provider registry.terraform.io/hashicorp/google v5.34.0
+ provider registry.terraform.io/hashicorp/google-beta v5.34.0

Affected Resource(s)

google_monitoring_alert_policy

Terraform Configuration

Add documentation.0.subject = "baz" to Example Usage - Monitoring Alert Policy Basic.

resource "google_monitoring_alert_policy" "alert_policy" {
  display_name = "My Alert Policy"
  combiner     = "OR"
  conditions {
    display_name = "test condition"
    condition_threshold {
      filter     = "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\""
      duration   = "60s"
      comparison = "COMPARISON_GT"
      aggregations {
        alignment_period   = "60s"
        per_series_aligner = "ALIGN_RATE"
      }
    }
  }

  user_labels = {
    foo = "bar"
  }

  # Add this structure
  documentation {
    subject = "baz"
  }
}

Debug Output

No response

Expected Behavior

Create a resource successfully when run terraform apply.

Actual Behavior

terraform apply fails because API returns status code 400 as follows:

$ terraform apply
<snip>

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # google_monitoring_alert_policy.alert_policy will be created
  + resource "google_monitoring_alert_policy" "alert_policy" {
      + combiner        = "OR"
      + creation_record = (known after apply)
      + display_name    = "My Alert Policy"
      + enabled         = true
      + id              = (known after apply)
      + name            = (known after apply)
      + project         = "<masked>"

      + conditions {
          + display_name = "test condition"
          + name         = (known after apply)

          + condition_threshold {
              + comparison = "COMPARISON_GT"
              + duration   = "60s"
              + filter     = "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\""

              + aggregations {
                  + alignment_period   = "60s"
                  + per_series_aligner = "ALIGN_RATE"
                }
            }
        }

      + documentation {
          + mime_type = "text/markdown"
          + subject   = "baz"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

google_monitoring_alert_policy.alert_policy: Creating...
╷
│ Error: Error creating AlertPolicy: googleapi: Error 400: Request was missing field alert_policy.documentation.mime_type: non-empty content requires non-empty MIME type and vice versa.
│ 
│   with google_monitoring_alert_policy.alert_policy,
│   on google.tf line 1, in resource "google_monitoring_alert_policy" "alert_policy":
│    1: resource "google_monitoring_alert_policy" "alert_policy" {
│ 

This error is caused by terraform completes documentation.0.mime_type = "text/markdown" automatically.

Description

From my understanding,

So I think the completion is not necessary in this case.

How do you think about adding the condition that mime_type is only completed when the content is set?

Workaround

Set mime_type = "".

<snip>
  documentation {
+   mime_type = ""
    subject   = "baz"
  }
}

Steps to reproduce

  1. terraform apply

Important Factoids

No response

References

b/349152014

ggtisc commented 2 months ago

Confirmed issue!

According to terraform registry the documentation block content, mime_type and subject are optional. But when we are attempting to create the google_monitoring_alert_policy it triggers the next error:

Error creating AlertPolicy: googleapi: Error 400: Request was missing field alert_policy.documentation.mime_type: non-empty content requires non-empty MIME type and vice versa.

z-nand commented 1 month ago

The default value for mime_type in the Terraform client is specified here.

It's unfortunate, but removing or changing this default at this point is likely a breaking change.

Please use the workaround to create AlertPolicy.