From cfe7979692a31dbbde9a62874351522917cc81a8 Mon Sep 17 00:00:00 2001 From: Anthony Stanton Date: Wed, 21 Sep 2016 18:54:38 +0200 Subject: [PATCH] provider/librator: Always send required attributes on update Fixes #8966 --- .../librato/resource_librato_alert.go | 59 +++++++++---------- .../librato/resource_librato_alert_test.go | 21 +++++-- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/builtin/providers/librato/resource_librato_alert.go b/builtin/providers/librato/resource_librato_alert.go index 53d6780f8..41e2b884d 100644 --- a/builtin/providers/librato/resource_librato_alert.go +++ b/builtin/providers/librato/resource_librato_alert.go @@ -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)) } @@ -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") { diff --git a/builtin/providers/librato/resource_librato_alert_test.go b/builtin/providers/librato/resource_librato_alert_test.go index 0acf3a646..e244b6046 100644 --- a/builtin/providers/librato/resource_librato_alert_test.go +++ b/builtin/providers/librato/resource_librato_alert_test.go @@ -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"), ), @@ -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"), ), }, }, @@ -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] @@ -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 = `