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.33k stars 1.73k forks source link

Error creating Monitoring Alert Policy - Expected type 'Duration' but got 'String' #9576

Open Luke-Scott opened 3 years ago

Luke-Scott commented 3 years ago

Community Note

Terraform Version

Terraform v1.0.2 provider registry.terraform.io/hashicorp/google v3.75.0

Affected Resource(s)

resource "google_monitoring_alert_policy"

Terraform Configuration Files

resource "google_monitoring_alert_policy" "alert_policy" {
  display_name = "test_alert"
  combiner     = "OR"
  conditions {
    display_name = "condition"
    condition_monitoring_query_language {
      query    = <<EOT
      fetch cloud_run_revision
| metric 'run.googleapis.com/request_count'
| filter
    resource.project_id == 'luke-test-project-291019'
    &&
    (resource.location == 'europe-west2'
      && resource.service_name == 'github-event-handler-production')
      && (metric.response_code_class == '2xx')
| align rate("300s")
| every "300s"
| group_by [metric.response_code_class],
    [value_request_count_percentile: percentile(value.request_count, 99)]
| condition val() > 0 '1/s'
      EOT 
      duration = "300s"
    }
  }
  user_labels = {
    owner = "sre"
  }
}

Expected Behavior

As you can see from the Terraform Plan output it will create one resource - an alerting policy within GCP's Monitoring.

Screenshot 2021-07-16 at 16 34 17

Actual Behavior

Screenshot 2021-07-16 at 16 31 33

Expected type 'Duration' but got 'String'.

It complains that the duration argument needs to be in the data type 'Duration' and not a string. Is it possible to cast a Duration data type? However, if I then remove the string and make it an integer I get the following error.

Screenshot 2021-07-16 at 16 40 42

Illegal duration format; duration must end with 's'

Doesn't that then mean it has to be a string? It seems to contradict itself.

Here is what's on the Terraform Docs for Duration:

Screenshot 2021-07-16 at 16 42 19

Steps to Reproduce

terraform apply

edwardmedia commented 3 years ago

This appears to be an API bug. Duration in below request is working

{
 "combiner": "OR",
 "conditions": [
  {
   "conditionThreshold": {
    "aggregations": [
     {
      "alignmentPeriod": "60s",
      "perSeriesAligner": "ALIGN_RATE"
     }
    ],
    "comparison": "COMPARISON_GT",
    "duration": "60s",
    "filter": "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\""
   },
   "displayName": "test condition"
  }
 ],
 "displayName": "issue9576_alert2",
 "enabled": true,
 "userLabels": {
  "foo": "bar"
 }
}