DataDog / terraform-provider-datadog

Terraform Datadog provider
https://www.terraform.io/docs/providers/datadog/
Mozilla Public License 2.0
403 stars 379 forks source link

resource_datadog_monitor - Null attributes not being skiped even when no needed - variables.0.event_query.0.search.0.query" is required, but no │ definition was found. #2456

Closed DanielGarzaFraireDF closed 4 months ago

DanielGarzaFraireDF commented 4 months ago

Datadog Terraform Provider Version

3.39.0

Terraform Version

1.8.5

What resources or data sources are affected?

resource_datadog_monitor

Terraform Configuration Files

resource "datadog_monitor" "monitor" {
    for_each = var.monitors

    type = each.value.type
    name = each.value.name
    message = each.value.message
    escalation_message = each.value.escalation_message
    query = each.value.query
    include_tags = each.value.include_tags
    tags = each.value.tags
    priority = each.value.priority
    monitor_thresholds {
        warning = each.value.warning
        critical = each.value.critical
    }
    variables {
        event_query {
            name = each.value.variables_name
            data_source = each.value.data_source
            search {
                query = each.value.search_query
                }
            compute {
                aggregation = each.value.aggregation
                }
            }
    }
}

Relevant debug or panic output

No response

Expected Behavior

Terraform should create a list of monitors regardless of whether they have some attributes set or not

We are having this issue with this attributes:

variables {
    event_query {
        name
        data_source 
        search {
            query 
            }
        compute {
            aggregation 
            }
        }
}

Actual Behavior

Datadog is having some attributes as "required" even when the default is null and we can

The argument "variables.0.event_query.0.search.0.query" is required, but no │ definition was found.

Steps to Reproduce

resource "datadog_monitor" "monitor" {

type = “metric alert”
name = “monitor name test”
message = “message test”
escalation_message = “”
query   = "sum(last_5m):sum:postgresql.deadlocks.count{env:prod} by {host,db}.as_count() >= 2”
include_tags = false
tags = ["Terraform:true", "env:prod”]
priority = 2
monitor_thresholds {
    warning = 1
    critical = 2
}
variables {
    event_query {
        name = “”
        data_source = “”
        search {
            query = “”
            }
        compute {
            aggregation = “”
            }
        }
}

}

Important Factoids

No response

References

No response

nkzou commented 4 months ago

Sorry, but this is working as expected. We don't plan on silently skipping invalid configurations. I suggest you change your for_each to avoid including invalid event_query blocks, either by making the variables block dynamic, or having a conditional that sets variables to null if the monitor doesn't have an applicable event_query definition

DanielGarzaFraireDF commented 4 months ago

We already tried making this variables NULL when not needed by some monitors but DD its still looking for it This is the message we are receiving.

"variables.0.event_query.0.data_source" is required, but no │ definition was found"

This is in fact the real issue, not accepting the NULL for this variables

nkzou commented 4 months ago

Try using something like the following:

dynamic "variables" {
    for_each = each.value.variables_name != null ? [1] : []
    content {
      event_query {
        name        = each.value.variables_name
        data_source = each.value.data_source
        search {
          query = each.value.search_query
        }
        compute {
          aggregation = each.value.aggregation
        }
      }
    }
  }

instead of explicitly defining the variables block. This dynamic block will omit the variables block if each.value.variables_name is null.

DanielGarzaFraireDF commented 4 months ago

Thanks a lot @nkzou !

That solved our problem and now our integration is working as expected with this dynamic block

Greetings!