diff --git a/datadog/resource_datadog_monitor.go b/datadog/resource_datadog_monitor.go index 0a3151f51..4b53e9d60 100644 --- a/datadog/resource_datadog_monitor.go +++ b/datadog/resource_datadog_monitor.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "log" + "regexp" "strconv" "strings" @@ -175,6 +176,18 @@ func resourceDatadogMonitor() *schema.Resource { Type: schema.TypeBool, Optional: true, }, + "query_config": { + Type: schema.TypeMap, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "query_string": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, }, } } @@ -264,6 +277,11 @@ func buildMonitorStruct(d *schema.ResourceData) *datadog.Monitor { } if m.GetType() == logAlertMonitorType { + if queryString, ok := getQueryStringFromLogQuery(d.Get("query").(string)); ok { + o.SetQueryConfig(datadog.QueryConfig{ + QueryString: datadog.String(queryString.(string)), + }) + } if attr, ok := d.GetOk("enable_logs_sample"); ok { o.SetEnableLogsSample(attr.(bool)) } else { @@ -384,6 +402,15 @@ func resourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) error if m.GetType() == logAlertMonitorType { d.Set("enable_logs_sample", m.Options.GetEnableLogsSample()) + queryConfig := make(map[string]string) + for k, v := range map[string]string{ + "query_string": m.Options.QueryConfig.GetQueryString(), + } { + if v != "" { + queryConfig[k] = v + } + } + d.Set("query_config", queryConfig) } return nil @@ -493,6 +520,11 @@ func resourceDatadogMonitorUpdate(d *schema.ResourceData, meta interface{}) erro } // can't use m.GetType here, since it's not filled for purposes of updating if d.Get("type") == logAlertMonitorType { + if queryString, ok := getQueryStringFromLogQuery(d.Get("query").(string)); ok { + o.SetQueryConfig(datadog.QueryConfig{ + QueryString: datadog.String(queryString.(string)), + }) + } if attr, ok := d.GetOk("enable_logs_sample"); ok { o.SetEnableLogsSample(attr.(bool)) } else { @@ -565,3 +597,12 @@ func suppressDataDogFloatIntDiff(k, old, new string, d *schema.ResourceData) boo } return false } + +func getQueryStringFromLogQuery(query string) (interface{}, bool) { + matchLogQueryStrs := regexp.MustCompile(`logs\("(.*?)"\)`).FindStringSubmatch(query) + matched := (len(matchLogQueryStrs) > 1) + if !matched { + return nil, false + } + return matchLogQueryStrs[1], true +}