Skip to content

Commit

Permalink
fix provider to Optional fields
Browse files Browse the repository at this point in the history
make syntax backwards compatible

suppress *_display changes
  • Loading branch information
platinummonkey committed Aug 13, 2019
1 parent 3444a4c commit 354e721
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 62 deletions.
45 changes: 32 additions & 13 deletions datadog/resource_datadog_service_level_objective.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func resourceDatadogServiceLevelObjective() *schema.Resource {
},
"target_display": {
Type: schema.TypeString,
Required: false,
DiffSuppressFunc: suppressDataDogFloatIntDiff,
Optional: true,
DiffSuppressFunc: suppressDataDogSLODisplayValueDiff,
},
"warning": {
Type: schema.TypeFloat,
Expand All @@ -68,8 +68,8 @@ func resourceDatadogServiceLevelObjective() *schema.Resource {
},
"warning_display": {
Type: schema.TypeString,
Required: false,
DiffSuppressFunc: suppressDataDogFloatIntDiff,
Optional: true,
DiffSuppressFunc: suppressDataDogSLODisplayValueDiff,
},
},
},
Expand Down Expand Up @@ -168,39 +168,40 @@ func buildServiceLevelObjectiveStruct(d *schema.ResourceData) *datadog.ServiceLe
}
}

if attr, ok := d.GetOk("thresholds"); ok {
if _, ok := d.GetOk("thresholds"); ok {
numThresholds := d.Get("thresholds.#").(int)
sloThresholds := make(datadog.ServiceLevelObjectiveThresholds, 0)
for _, rawThreshold := range attr.([]interface{}) {
threshold := rawThreshold.(map[string]interface{})
for i := 0; i < numThresholds; i++ {
prefix := fmt.Sprintf("thresholds.%d.", i)
t := datadog.ServiceLevelObjectiveThreshold{}
if tf, ok := threshold["timeframe"]; ok {

if tf, ok := d.GetOk(prefix + "timeframe"); ok {
t.TimeFrame = datadog.String(tf.(string))
}

if targetValue, ok := threshold["target"]; ok {
if targetValue, ok := d.GetOk(prefix + "target"); ok {
if f, ok := floatOk(targetValue); ok {
t.Target = datadog.Float64(f)
}
}

if warningValue, ok := threshold["warning"]; ok {
if warningValue, ok := d.GetOk(prefix + "warning"); ok {
if f, ok := floatOk(warningValue); ok {
t.Warning = datadog.Float64(f)
}
}

if targetDisplayValue, ok := threshold["target_display"]; ok {
if targetDisplayValue, ok := d.GetOk(prefix + "target_display"); ok {
if s, ok := targetDisplayValue.(string); ok && strings.TrimSpace(s) != "" {
t.TargetDisplay = datadog.String(strings.TrimSpace(targetDisplayValue.(string)))
}
}

if warningDisplayValue, ok := threshold["warning_display"]; ok {
if warningDisplayValue, ok := d.GetOk(prefix + "warning_display"); ok {
if s, ok := warningDisplayValue.(string); ok && strings.TrimSpace(s) != "" {
t.WarningDisplay = datadog.String(strings.TrimSpace(warningDisplayValue.(string)))
}
}

sloThresholds = append(sloThresholds, &t)
}
sort.Sort(sloThresholds)
Expand Down Expand Up @@ -463,3 +464,21 @@ func resourceDatadogServiceLevelObjectiveImport(d *schema.ResourceData, meta int
}
return []*schema.ResourceData{d}, nil
}

// Ignore any diff that results from the mix of *_display string values from the
// DataDog API.
func suppressDataDogSLODisplayValueDiff(k, old, new string, d *schema.ResourceData) bool {
sloType := d.Get("type")
if sloType == datadog.ServiceLevelObjectiveTypeMonitor {
// always suppress monitor type, this is controlled via API.
return false
}

// metric type otherwise
if old == "" || new == "" {
// always suppress if not specified
return true
}

return suppressDataDogFloatIntDiff(k, old, new, d)
}
42 changes: 20 additions & 22 deletions datadog/resource_datadog_service_level_objective_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ resource "datadog_service_level_objective" "foo" {
denominator = "sum:my.metric{*}.as_count()"
}
thresholds = [
{
timeframe = "7d"
target = 99.5
warning = 99.8
},
{
timeframe = "30d"
target = 99
}
]
thresholds {
timeframe = "7d"
target = 99.5
warning = 99.8
}
thresholds {
timeframe = "30d"
target = 99
}
tags = ["foo:bar", "baz"]
}
Expand All @@ -47,17 +46,16 @@ resource "datadog_service_level_objective" "foo" {
denominator = "sum:my.metric{type:good}.as_count() + sum:my.metric{type:bad}.as_count()"
}
thresholds = [
{
timeframe = "7d"
target = 99.5
warning = 99.8
},
{
timeframe = "30d"
target = 98
}
]
thresholds {
timeframe = "7d"
target = 99.5
warning = 99.8
}
thresholds {
timeframe = "30d"
target = 98
}
tags = ["foo:bar", "baz"]
}
Expand Down
53 changes: 26 additions & 27 deletions website/docs/r/service_level_objective.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ resource "datadog_service_level_objective" "foo" {
denominator = "sum:my.custom.count.metric{*}.as_count()"
}
thresholds = [
{
timeframe = "7d"
target = 99.9
warning = 99.99
target_display = "99.900"
warning_display = "99.990"
},
{
timeframe = "30d"
target = 99.9
warning = 99.99
target_display = "99.900"
warning_display = "99.990"
}
thresholds {
timeframe = "7d"
target = 99.9
warning = 99.99
target_display = "99.900"
warning_display = "99.990"
}
thresholds {
timeframe = "30d"
target = 99.9
warning = 99.99
target_display = "99.900"
warning_display = "99.990"
}
]
tags = ["foo:bar", "baz"]
Expand All @@ -54,18 +54,17 @@ resource "datadog_service_level_objective" "bar" {
description = "My custom monitor SLO"
monitor_ids = [1, 2, 3]
thresholds = [
{
timeframe = "7d"
target = 99.9
warning = 99.99
},
{
timeframe = "30d"
target = 99.9
warning = 99.99
}
]
thresholds {
timeframe = "7d"
target = 99.9
warning = 99.99
}
thresholds {
timeframe = "30d"
target = 99.9
warning = 99.99
}
tags = ["foo:bar", "baz"]
}
Expand Down

0 comments on commit 354e721

Please sign in to comment.