Skip to content

Commit

Permalink
provider/librator: Always send required attributes on update
Browse files Browse the repository at this point in the history
Fixes #8966
  • Loading branch information
elblivion authored and stack72 committed Sep 22, 2016
1 parent 48a53e2 commit bd43f41
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
59 changes: 28 additions & 31 deletions resource_librato_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,7 @@ func resourceLibratoAlertUpdate(d *schema.ResourceData, meta interface{}) error
}

alert := new(librato.Alert)
if d.HasChange("name") {
alert.Name = librato.String(d.Get("name").(string))
}
alert.Name = librato.String(d.Get("name").(string))
if d.HasChange("description") {
alert.Description = librato.String(d.Get("description").(string))
}
Expand All @@ -355,35 +353,34 @@ func resourceLibratoAlertUpdate(d *schema.ResourceData, meta interface{}) error
}
alert.Services = services
}
if d.HasChange("condition") {
vs := d.Get("condition").(*schema.Set)
conditions := make([]librato.AlertCondition, vs.Len())
for i, conditionDataM := range vs.List() {
conditionData := conditionDataM.(map[string]interface{})
var condition librato.AlertCondition
if v, ok := conditionData["type"].(string); ok && v != "" {
condition.Type = librato.String(v)
}
if v, ok := conditionData["threshold"].(float64); ok && !math.IsNaN(v) {
condition.Threshold = librato.Float(v)
}
if v, ok := conditionData["metric_name"].(string); ok && v != "" {
condition.MetricName = librato.String(v)
}
if v, ok := conditionData["source"].(string); ok && v != "" {
condition.Source = librato.String(v)
}
if v, ok := conditionData["detect_reset"].(bool); ok {
condition.DetectReset = librato.Bool(v)
}
if v, ok := conditionData["duration"].(uint); ok {
condition.Duration = librato.Uint(v)
}
if v, ok := conditionData["summary_function"].(string); ok && v != "" {
condition.SummaryFunction = librato.String(v)
}
conditions[i] = condition

vs := d.Get("condition").(*schema.Set)
conditions := make([]librato.AlertCondition, vs.Len())
for i, conditionDataM := range vs.List() {
conditionData := conditionDataM.(map[string]interface{})
var condition librato.AlertCondition
if v, ok := conditionData["type"].(string); ok && v != "" {
condition.Type = librato.String(v)
}
if v, ok := conditionData["threshold"].(float64); ok && !math.IsNaN(v) {
condition.Threshold = librato.Float(v)
}
if v, ok := conditionData["metric_name"].(string); ok && v != "" {
condition.MetricName = librato.String(v)
}
if v, ok := conditionData["source"].(string); ok && v != "" {
condition.Source = librato.String(v)
}
if v, ok := conditionData["detect_reset"].(bool); ok {
condition.DetectReset = librato.Bool(v)
}
if v, ok := conditionData["duration"].(uint); ok {
condition.Duration = librato.Uint(v)
}
if v, ok := conditionData["summary_function"].(string); ok && v != "" {
condition.SummaryFunction = librato.String(v)
}
conditions[i] = condition
alert.Conditions = conditions
}
if d.HasChange("attributes") {
Expand Down
21 changes: 16 additions & 5 deletions resource_librato_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestAccLibratoAlert_Updated(t *testing.T) {
Config: testAccCheckLibratoAlertConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
testAccCheckLibratoAlertName(&alert, "FooBar"),
testAccCheckLibratoAlertDescription(&alert, "A Test Alert"),
resource.TestCheckResourceAttr(
"librato_alert.foobar", "name", "FooBar"),
),
Expand All @@ -73,9 +73,9 @@ func TestAccLibratoAlert_Updated(t *testing.T) {
Config: testAccCheckLibratoAlertConfig_new_value,
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
testAccCheckLibratoAlertName(&alert, "BarBaz"),
testAccCheckLibratoAlertDescription(&alert, "A modified Test Alert"),
resource.TestCheckResourceAttr(
"librato_alert.foobar", "name", "BarBaz"),
"librato_alert.foobar", "description", "A modified Test Alert"),
),
},
},
Expand Down Expand Up @@ -116,6 +116,17 @@ func testAccCheckLibratoAlertName(alert *librato.Alert, name string) resource.Te
}
}

func testAccCheckLibratoAlertDescription(alert *librato.Alert, description string) resource.TestCheckFunc {
return func(s *terraform.State) error {

if alert.Description == nil || *alert.Description != description {
return fmt.Errorf("Bad description: %s", *alert.Description)
}

return nil
}
}

func testAccCheckLibratoAlertExists(n string, alert *librato.Alert) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -159,8 +170,8 @@ resource "librato_alert" "foobar" {

const testAccCheckLibratoAlertConfig_new_value = `
resource "librato_alert" "foobar" {
name = "BarBaz"
description = "A Test Alert"
name = "FooBar"
description = "A modified Test Alert"
}`

const testAccCheckLibratoAlertConfig_full = `
Expand Down

0 comments on commit bd43f41

Please sign in to comment.