Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…rom TypeMap to TypeList and validate
  • Loading branch information
platinummonkey committed Aug 13, 2019
1 parent 354e721 commit 1eb55bc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
25 changes: 15 additions & 10 deletions datadog/resource_datadog_service_level_objective.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ func resourceDatadogServiceLevelObjective() *schema.Resource {

// Metric-Based SLO
"query": {
Type: schema.TypeMap,
// we use TypeList here because of https://github.com/hashicorp/terraform/issues/6215/
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"monitor_ids", "monitor_search", "groups"},
Description: "The metric query of good / total events",
Expand Down Expand Up @@ -238,9 +240,11 @@ func buildServiceLevelObjectiveStruct(d *schema.ResourceData) *datadog.ServiceLe
}
default:
// query type
slo.Query = &datadog.ServiceLevelObjectiveMetricQuery{
Numerator: datadog.String(d.Get("query.numerator").(string)),
Denominator: datadog.String(d.Get("query.denominator").(string)),
if _, ok := d.GetOk("query.0"); ok {
slo.Query = &datadog.ServiceLevelObjectiveMetricQuery{
Numerator: datadog.String(d.Get("query.0.numerator").(string)),
Denominator: datadog.String(d.Get("query.0.denominator").(string)),
}
}
}

Expand Down Expand Up @@ -353,7 +357,7 @@ func resourceDatadogServiceLevelObjectiveRead(d *schema.ResourceData, meta inter
q := slo.GetQuery()
query["numerator"] = q.GetNumerator()
query["denominator"] = q.GetDenominator()
d.Set("query", query)
d.Set("query", []map[string]interface{}{query})
}

return nil
Expand Down Expand Up @@ -402,11 +406,12 @@ func resourceDatadogServiceLevelObjectiveUpdate(d *schema.ResourceData, meta int
default:
// metric type
if attr, ok := d.GetOk("query"); ok {
query := attr.(map[string]interface{})
slo.SetQuery(datadog.ServiceLevelObjectiveMetricQuery{
Numerator: datadog.String(query["numerator"].(string)),
Denominator: datadog.String(query["denominator"].(string)),
})
if query, ok := attr.([]map[string]interface{}); ok && len(query) == 1 {
slo.SetQuery(datadog.ServiceLevelObjectiveMetricQuery{
Numerator: datadog.String(query[0]["numerator"].(string)),
Denominator: datadog.String(query[0]["denominator"].(string)),
})
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions datadog/resource_datadog_service_level_objective_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ resource "datadog_service_level_objective" "foo" {
name = "name for metric SLO foo"
type = "metric"
description = "some description about foo SLO"
query = {
query {
numerator = "sum:my.metric{type:good}.as_count()"
denominator = "sum:my.metric{*}.as_count()"
}
Expand All @@ -41,7 +41,7 @@ resource "datadog_service_level_objective" "foo" {
name = "updated name for metric SLO foo"
type = "metric"
description = "some updated description about foo SLO"
query = {
query {
numerator = "sum:my.metric{type:good}.as_count()"
denominator = "sum:my.metric{type:good}.as_count() + sum:my.metric{type:bad}.as_count()"
}
Expand Down Expand Up @@ -80,9 +80,9 @@ func TestAccDatadogServiceLevelObjective_Basic(t *testing.T) {
resource.TestCheckResourceAttr(
"datadog_service_level_objective.foo", "type", "metric"),
resource.TestCheckResourceAttr(
"datadog_service_level_objective.foo", "query.numerator", "sum:my.metric{type:good}.as_count()"),
"datadog_service_level_objective.foo", "query.0.numerator", "sum:my.metric{type:good}.as_count()"),
resource.TestCheckResourceAttr(
"datadog_service_level_objective.foo", "query.denominator", "sum:my.metric{*}.as_count()"),
"datadog_service_level_objective.foo", "query.0.denominator", "sum:my.metric{*}.as_count()"),
// Thresholds are a TypeList, that are sorted by timeframe alphabetically.
resource.TestCheckResourceAttr(
"datadog_service_level_objective.foo", "thresholds.#", "2"),
Expand Down Expand Up @@ -119,9 +119,9 @@ func TestAccDatadogServiceLevelObjective_Basic(t *testing.T) {
resource.TestCheckResourceAttr(
"datadog_service_level_objective.foo", "type", "metric"),
resource.TestCheckResourceAttr(
"datadog_service_level_objective.foo", "query.numerator", "sum:my.metric{type:good}.as_count()"),
"datadog_service_level_objective.foo", "query.0.numerator", "sum:my.metric{type:good}.as_count()"),
resource.TestCheckResourceAttr(
"datadog_service_level_objective.foo", "query.denominator", "sum:my.metric{type:good}.as_count() + sum:my.metric{type:bad}.as_count()"),
"datadog_service_level_objective.foo", "query.0.denominator", "sum:my.metric{type:good}.as_count() + sum:my.metric{type:bad}.as_count()"),
// Thresholds are a TypeList, that are sorted by timeframe alphabetically.
resource.TestCheckResourceAttr(
"datadog_service_level_objective.foo", "thresholds.#", "2"),
Expand Down

0 comments on commit 1eb55bc

Please sign in to comment.