provider/librator: Always send required attributes on update

Fixes #8966
This commit is contained in:
Anthony Stanton 2016-09-21 18:54:38 +02:00 committed by stack72
parent 23009ec837
commit cfe7979692
No known key found for this signature in database
GPG Key ID: 8619A619B085CB16
2 changed files with 44 additions and 36 deletions

View File

@ -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") {

View File

@ -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 = `