2016-08-05 23:16:32 +02:00
|
|
|
package librato
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/henrikhodne/go-librato/librato"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceLibratoAlert() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceLibratoAlertCreate,
|
|
|
|
Read: resourceLibratoAlertRead,
|
|
|
|
Update: resourceLibratoAlertUpdate,
|
|
|
|
Delete: resourceLibratoAlertDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-06-05 10:50:00 +02:00
|
|
|
"name": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
2017-06-05 10:50:00 +02:00
|
|
|
"description": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-06-05 10:50:00 +02:00
|
|
|
"active": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
},
|
2017-06-05 10:50:00 +02:00
|
|
|
"rearm_seconds": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 600,
|
|
|
|
},
|
2017-06-05 10:50:00 +02:00
|
|
|
"services": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
2017-06-05 10:50:00 +02:00
|
|
|
"condition": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-06-05 10:50:00 +02:00
|
|
|
"type": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2017-06-05 10:50:00 +02:00
|
|
|
"metric_name": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2017-06-05 10:50:00 +02:00
|
|
|
"source": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-06-05 10:50:00 +02:00
|
|
|
"detect_reset": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-06-05 10:50:00 +02:00
|
|
|
"duration": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-06-05 10:50:00 +02:00
|
|
|
"threshold": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeFloat,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-06-05 10:50:00 +02:00
|
|
|
"summary_function": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceLibratoAlertConditionsHash,
|
|
|
|
},
|
2017-06-05 10:50:00 +02:00
|
|
|
"attributes": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
2017-06-06 18:29:29 +02:00
|
|
|
MaxItems: 1,
|
2016-08-05 23:16:32 +02:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-06-05 10:50:00 +02:00
|
|
|
"runbook_url": {
|
2016-08-05 23:16:32 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLibratoAlertConditionsHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["type"].(string)))
|
2016-08-31 17:44:09 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["metric_name"].(string)))
|
|
|
|
|
|
|
|
source, present := m["source"]
|
|
|
|
if present {
|
2016-08-31 22:45:03 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%s-", source.(string)))
|
2016-08-31 17:44:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-05 10:50:00 +02:00
|
|
|
detectReset, present := m["detect_reset"]
|
2016-08-31 17:44:09 +02:00
|
|
|
if present {
|
2017-06-05 10:50:00 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%t-", detectReset.(bool)))
|
2016-08-31 17:44:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
duration, present := m["duration"]
|
|
|
|
if present {
|
2016-08-31 22:45:03 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%d-", duration.(int)))
|
2016-08-31 17:44:09 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 17:19:53 +02:00
|
|
|
threshold, present := m["threshold"]
|
|
|
|
if present {
|
|
|
|
buf.WriteString(fmt.Sprintf("%f-", threshold.(float64)))
|
|
|
|
}
|
2016-08-31 17:44:09 +02:00
|
|
|
|
2017-06-05 10:50:00 +02:00
|
|
|
summaryFunction, present := m["summary_function"]
|
2016-08-31 17:44:09 +02:00
|
|
|
if present {
|
2017-06-05 10:50:00 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%s-", summaryFunction.(string)))
|
2016-08-31 17:44:09 +02:00
|
|
|
}
|
2016-08-05 23:16:32 +02:00
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLibratoAlertCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*librato.Client)
|
|
|
|
|
2017-06-06 18:29:29 +02:00
|
|
|
alert := librato.Alert{
|
|
|
|
Name: librato.String(d.Get("name").(string)),
|
2016-08-05 23:16:32 +02:00
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
alert.Description = librato.String(v.(string))
|
|
|
|
}
|
|
|
|
// GetOK returns not OK for false boolean values, use Get
|
|
|
|
alert.Active = librato.Bool(d.Get("active").(bool))
|
|
|
|
if v, ok := d.GetOk("rearm_seconds"); ok {
|
|
|
|
alert.RearmSeconds = librato.Uint(uint(v.(int)))
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("services"); ok {
|
|
|
|
vs := v.(*schema.Set)
|
|
|
|
services := make([]*string, vs.Len())
|
|
|
|
for i, serviceData := range vs.List() {
|
|
|
|
services[i] = librato.String(serviceData.(string))
|
|
|
|
}
|
|
|
|
alert.Services = services
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("condition"); ok {
|
|
|
|
vs := v.(*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)
|
|
|
|
}
|
2016-09-22 11:57:49 +02:00
|
|
|
if v, ok := conditionData["duration"].(int); ok {
|
|
|
|
condition.Duration = librato.Uint(uint(v))
|
2016-08-05 23:16:32 +02:00
|
|
|
}
|
|
|
|
if v, ok := conditionData["summary_function"].(string); ok && v != "" {
|
|
|
|
condition.SummaryFunction = librato.String(v)
|
|
|
|
}
|
|
|
|
conditions[i] = condition
|
|
|
|
}
|
|
|
|
alert.Conditions = conditions
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("attributes"); ok {
|
|
|
|
attributeData := v.([]interface{})
|
|
|
|
if len(attributeData) > 1 {
|
|
|
|
return fmt.Errorf("Only one set of attributes per alert is supported")
|
|
|
|
} else if len(attributeData) == 1 {
|
|
|
|
if attributeData[0] == nil {
|
|
|
|
return fmt.Errorf("No attributes found in attributes block")
|
|
|
|
}
|
|
|
|
attributeDataMap := attributeData[0].(map[string]interface{})
|
|
|
|
attributes := new(librato.AlertAttributes)
|
|
|
|
if v, ok := attributeDataMap["runbook_url"].(string); ok && v != "" {
|
|
|
|
attributes.RunbookURL = librato.String(v)
|
|
|
|
}
|
|
|
|
alert.Attributes = attributes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 18:29:29 +02:00
|
|
|
alertResult, _, err := client.Alerts.Create(&alert)
|
2016-08-05 23:16:32 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating Librato alert %s: %s", *alert.Name, err)
|
|
|
|
}
|
2017-03-17 17:52:38 +01:00
|
|
|
log.Printf("[INFO] Created Librato alert: %s", *alertResult)
|
2016-08-05 23:16:32 +02:00
|
|
|
|
2017-06-06 18:29:29 +02:00
|
|
|
retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError {
|
2016-08-05 23:16:32 +02:00
|
|
|
_, _, err := client.Alerts.Get(*alertResult.ID)
|
|
|
|
if err != nil {
|
|
|
|
if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 {
|
|
|
|
return resource.RetryableError(err)
|
|
|
|
}
|
|
|
|
return resource.NonRetryableError(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2017-06-06 18:29:29 +02:00
|
|
|
if retryErr != nil {
|
|
|
|
return fmt.Errorf("Error creating librato alert: %s", err)
|
|
|
|
}
|
2016-08-05 23:16:32 +02:00
|
|
|
|
2017-03-17 17:52:38 +01:00
|
|
|
d.SetId(strconv.FormatUint(uint64(*alertResult.ID), 10))
|
|
|
|
|
|
|
|
return resourceLibratoAlertRead(d, meta)
|
2016-08-05 23:16:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLibratoAlertRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*librato.Client)
|
|
|
|
id, err := strconv.ParseUint(d.Id(), 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-17 17:52:38 +01:00
|
|
|
log.Printf("[INFO] Reading Librato Alert: %d", id)
|
2016-08-05 23:16:32 +02:00
|
|
|
alert, _, err := client.Alerts.Get(uint(id))
|
|
|
|
if err != nil {
|
|
|
|
if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Error reading Librato Alert %s: %s", d.Id(), err)
|
|
|
|
}
|
2017-03-17 17:52:38 +01:00
|
|
|
log.Printf("[INFO] Received Librato Alert: %s", *alert)
|
2016-08-05 23:16:32 +02:00
|
|
|
|
2017-06-06 18:29:29 +02:00
|
|
|
d.Set("name", alert.Name)
|
2016-08-05 23:16:32 +02:00
|
|
|
|
2017-06-06 18:29:29 +02:00
|
|
|
if alert.Description != nil {
|
|
|
|
if err := d.Set("description", alert.Description); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if alert.Active != nil {
|
|
|
|
if err := d.Set("active", alert.Active); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if alert.RearmSeconds != nil {
|
|
|
|
if err := d.Set("rearm_seconds", alert.RearmSeconds); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-08-31 17:20:08 +02:00
|
|
|
|
2017-06-06 18:29:29 +02:00
|
|
|
// Since the following aren't simple terraform types (TypeList), it's best to
|
|
|
|
// catch the error returned from the d.Set() function, and handle accordingly.
|
2016-08-31 17:20:08 +02:00
|
|
|
services := resourceLibratoAlertServicesGather(d, alert.Services.([]interface{}))
|
2017-06-06 18:29:29 +02:00
|
|
|
if err := d.Set("services", schema.NewSet(schema.HashString, services)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-31 17:20:08 +02:00
|
|
|
|
|
|
|
conditions := resourceLibratoAlertConditionsGather(d, alert.Conditions)
|
2017-06-06 18:29:29 +02:00
|
|
|
if err := d.Set("condition", schema.NewSet(resourceLibratoAlertConditionsHash, conditions)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-31 17:20:08 +02:00
|
|
|
|
|
|
|
attributes := resourceLibratoAlertAttributesGather(d, alert.Attributes)
|
2017-06-06 18:29:29 +02:00
|
|
|
if err := d.Set("attributes", attributes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-05 23:16:32 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-17 17:52:38 +01:00
|
|
|
func resourceLibratoAlertServicesGather(d *schema.ResourceData, services []interface{}) []interface{} {
|
|
|
|
retServices := make([]interface{}, 0, len(services))
|
2016-08-05 23:16:32 +02:00
|
|
|
|
|
|
|
for _, s := range services {
|
|
|
|
serviceData := s.(map[string]interface{})
|
|
|
|
// ID field is returned as float64, for whatever reason
|
|
|
|
retServices = append(retServices, fmt.Sprintf("%.f", serviceData["id"]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return retServices
|
|
|
|
}
|
|
|
|
|
2017-03-17 17:52:38 +01:00
|
|
|
func resourceLibratoAlertConditionsGather(d *schema.ResourceData, conditions []librato.AlertCondition) []interface{} {
|
|
|
|
retConditions := make([]interface{}, 0, len(conditions))
|
2016-08-05 23:16:32 +02:00
|
|
|
for _, c := range conditions {
|
|
|
|
condition := make(map[string]interface{})
|
|
|
|
if c.Type != nil {
|
|
|
|
condition["type"] = *c.Type
|
|
|
|
}
|
|
|
|
if c.Threshold != nil {
|
|
|
|
condition["threshold"] = *c.Threshold
|
|
|
|
}
|
|
|
|
if c.MetricName != nil {
|
|
|
|
condition["metric_name"] = *c.MetricName
|
|
|
|
}
|
|
|
|
if c.Source != nil {
|
|
|
|
condition["source"] = *c.Source
|
|
|
|
}
|
|
|
|
if c.DetectReset != nil {
|
|
|
|
condition["detect_reset"] = *c.MetricName
|
|
|
|
}
|
|
|
|
if c.Duration != nil {
|
2017-03-17 17:52:38 +01:00
|
|
|
condition["duration"] = int(*c.Duration)
|
2016-08-05 23:16:32 +02:00
|
|
|
}
|
|
|
|
if c.SummaryFunction != nil {
|
|
|
|
condition["summary_function"] = *c.SummaryFunction
|
|
|
|
}
|
|
|
|
retConditions = append(retConditions, condition)
|
|
|
|
}
|
|
|
|
|
|
|
|
return retConditions
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flattens an attributes hash into something that flatmap.Flatten() can handle
|
|
|
|
func resourceLibratoAlertAttributesGather(d *schema.ResourceData, attributes *librato.AlertAttributes) []map[string]interface{} {
|
|
|
|
result := make([]map[string]interface{}, 0, 1)
|
|
|
|
|
|
|
|
if attributes != nil {
|
|
|
|
retAttributes := make(map[string]interface{})
|
|
|
|
if attributes.RunbookURL != nil {
|
|
|
|
retAttributes["runbook_url"] = *attributes.RunbookURL
|
|
|
|
}
|
|
|
|
result = append(result, retAttributes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLibratoAlertUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*librato.Client)
|
|
|
|
|
2017-06-06 18:29:29 +02:00
|
|
|
id, err := strconv.ParseUint(d.Id(), 10, 0)
|
2017-03-17 17:52:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-05 23:16:32 +02:00
|
|
|
alert := new(librato.Alert)
|
2016-09-21 18:54:38 +02:00
|
|
|
alert.Name = librato.String(d.Get("name").(string))
|
2017-06-06 18:29:29 +02:00
|
|
|
|
2016-08-05 23:16:32 +02:00
|
|
|
if d.HasChange("description") {
|
|
|
|
alert.Description = librato.String(d.Get("description").(string))
|
|
|
|
}
|
|
|
|
if d.HasChange("active") {
|
|
|
|
alert.Active = librato.Bool(d.Get("active").(bool))
|
|
|
|
}
|
|
|
|
if d.HasChange("rearm_seconds") {
|
2016-09-22 11:57:49 +02:00
|
|
|
alert.RearmSeconds = librato.Uint(uint(d.Get("rearm_seconds").(int)))
|
2016-08-05 23:16:32 +02:00
|
|
|
}
|
|
|
|
if d.HasChange("services") {
|
|
|
|
vs := d.Get("services").(*schema.Set)
|
|
|
|
services := make([]*string, vs.Len())
|
|
|
|
for i, serviceData := range vs.List() {
|
|
|
|
services[i] = librato.String(serviceData.(string))
|
|
|
|
}
|
|
|
|
alert.Services = services
|
|
|
|
}
|
2016-09-21 18:54:38 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2016-09-22 11:57:49 +02:00
|
|
|
if v, ok := conditionData["duration"].(int); ok {
|
|
|
|
condition.Duration = librato.Uint(uint(v))
|
2016-09-21 18:54:38 +02:00
|
|
|
}
|
|
|
|
if v, ok := conditionData["summary_function"].(string); ok && v != "" {
|
|
|
|
condition.SummaryFunction = librato.String(v)
|
2016-08-05 23:16:32 +02:00
|
|
|
}
|
2016-09-21 18:54:38 +02:00
|
|
|
conditions[i] = condition
|
2016-08-05 23:16:32 +02:00
|
|
|
alert.Conditions = conditions
|
|
|
|
}
|
|
|
|
if d.HasChange("attributes") {
|
|
|
|
attributeData := d.Get("attributes").([]interface{})
|
2017-06-06 18:29:29 +02:00
|
|
|
if attributeData[0] == nil {
|
|
|
|
return fmt.Errorf("No attributes found in attributes block")
|
2016-08-05 23:16:32 +02:00
|
|
|
}
|
2017-06-06 18:29:29 +02:00
|
|
|
attributeDataMap := attributeData[0].(map[string]interface{})
|
|
|
|
attributes := new(librato.AlertAttributes)
|
|
|
|
if v, ok := attributeDataMap["runbook_url"].(string); ok && v != "" {
|
|
|
|
attributes.RunbookURL = librato.String(v)
|
|
|
|
}
|
|
|
|
alert.Attributes = attributes
|
2016-08-05 23:16:32 +02:00
|
|
|
}
|
|
|
|
|
2017-03-17 17:52:38 +01:00
|
|
|
log.Printf("[INFO] Updating Librato alert: %s", alert)
|
2017-06-06 18:29:29 +02:00
|
|
|
_, updErr := client.Alerts.Update(uint(id), alert)
|
2017-06-05 10:50:00 +02:00
|
|
|
if updErr != nil {
|
|
|
|
return fmt.Errorf("Error updating Librato alert: %s", updErr)
|
2016-08-05 23:16:32 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 18:29:29 +02:00
|
|
|
log.Printf("[INFO] Updated Librato alert %d", id)
|
2017-03-17 17:52:38 +01:00
|
|
|
|
|
|
|
// Wait for propagation since Librato updates are eventually consistent
|
|
|
|
wait := resource.StateChangeConf{
|
|
|
|
Pending: []string{fmt.Sprintf("%t", false)},
|
|
|
|
Target: []string{fmt.Sprintf("%t", true)},
|
|
|
|
Timeout: 5 * time.Minute,
|
|
|
|
MinTimeout: 2 * time.Second,
|
|
|
|
ContinuousTargetOccurence: 5,
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
2017-06-06 18:29:29 +02:00
|
|
|
log.Printf("[DEBUG] Checking if Librato Alert %d was updated yet", id)
|
|
|
|
changedAlert, _, getErr := client.Alerts.Get(uint(id))
|
2017-06-05 10:50:00 +02:00
|
|
|
if getErr != nil {
|
|
|
|
return changedAlert, "", getErr
|
2017-03-17 17:52:38 +01:00
|
|
|
}
|
2017-06-06 18:29:29 +02:00
|
|
|
return changedAlert, "true", nil
|
2017-03-17 17:52:38 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = wait.WaitForState()
|
|
|
|
if err != nil {
|
2017-06-06 18:29:29 +02:00
|
|
|
return fmt.Errorf("Failed updating Librato Alert %d: %s", id, err)
|
2017-03-17 17:52:38 +01:00
|
|
|
}
|
|
|
|
|
2016-08-05 23:16:32 +02:00
|
|
|
return resourceLibratoAlertRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLibratoAlertDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*librato.Client)
|
|
|
|
id, err := strconv.ParseUint(d.Id(), 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting Alert: %d", id)
|
|
|
|
_, err = client.Alerts.Delete(uint(id))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting Alert: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-06-06 18:29:29 +02:00
|
|
|
retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError {
|
2016-08-05 23:16:32 +02:00
|
|
|
_, _, err := client.Alerts.Get(uint(id))
|
|
|
|
if err != nil {
|
|
|
|
if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return resource.NonRetryableError(err)
|
|
|
|
}
|
|
|
|
return resource.RetryableError(fmt.Errorf("alert still exists"))
|
|
|
|
})
|
2017-06-06 18:29:29 +02:00
|
|
|
if retryErr != nil {
|
|
|
|
return fmt.Errorf("Error deleting librato alert: %s", err)
|
|
|
|
}
|
2016-08-05 23:16:32 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|