Skip to content

Commit

Permalink
#148 Fix log monitor UI issue: generate queryConfig.queryString from …
Browse files Browse the repository at this point in the history
…query (#199)

* #148 Fix log monitor UI issue: generate queryConfig.queryString from query

* #148 make schema query_config computed

* refator match log queryString
  • Loading branch information
geekeren authored and Slavek Kabrda committed May 15, 2019
1 parent 0231646 commit 7aade75
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions datadog/resource_datadog_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -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,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 7aade75

Please sign in to comment.