2017-05-17 06:18:43 +02:00
|
|
|
package librato
|
|
|
|
|
|
|
|
import (
|
2017-05-17 17:33:44 +02:00
|
|
|
"encoding/json"
|
2017-05-17 06:18:43 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/henrikhodne/go-librato/librato"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceLibratoMetric() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceLibratoMetricCreate,
|
|
|
|
Read: resourceLibratoMetricRead,
|
|
|
|
Update: resourceLibratoMetricUpdate,
|
|
|
|
Delete: resourceLibratoMetricDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-05-25 20:09:04 +02:00
|
|
|
"name": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: false,
|
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"type": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"display_name": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"description": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"period": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"composite": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"attributes": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
2017-05-25 20:09:04 +02:00
|
|
|
MaxItems: 1,
|
2017-05-17 06:18:43 +02:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-05-25 20:09:04 +02:00
|
|
|
"color": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"display_max": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"display_min": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"display_units_long": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"display_units_short": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"display_stacked": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2017-05-25 20:09:04 +02:00
|
|
|
Default: false,
|
2017-05-17 06:18:43 +02:00
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"created_by_ua": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeString,
|
2017-05-18 22:13:11 +02:00
|
|
|
Computed: true,
|
2017-05-17 06:18:43 +02:00
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"gap_detection": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-25 20:09:04 +02:00
|
|
|
"aggregate": {
|
2017-05-17 06:18:43 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLibratoMetricCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*librato.Client)
|
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
metric := librato.Metric{
|
|
|
|
Name: librato.String(d.Get("name").(string)),
|
|
|
|
Type: librato.String(d.Get("type").(string)),
|
2017-05-17 06:18:43 +02:00
|
|
|
}
|
|
|
|
if a, ok := d.GetOk("display_name"); ok {
|
|
|
|
metric.DisplayName = librato.String(a.(string))
|
|
|
|
}
|
|
|
|
if a, ok := d.GetOk("description"); ok {
|
|
|
|
metric.Description = librato.String(a.(string))
|
|
|
|
}
|
|
|
|
if a, ok := d.GetOk("period"); ok {
|
2017-05-25 20:09:04 +02:00
|
|
|
metric.Period = librato.Uint(uint(a.(int)))
|
2017-05-17 06:18:43 +02:00
|
|
|
}
|
2017-05-18 15:37:16 +02:00
|
|
|
if a, ok := d.GetOk("composite"); ok {
|
|
|
|
metric.Composite = librato.String(a.(string))
|
|
|
|
}
|
2017-05-17 06:18:43 +02:00
|
|
|
|
|
|
|
if a, ok := d.GetOk("attributes"); ok {
|
2017-05-18 15:37:16 +02:00
|
|
|
|
2017-05-17 06:18:43 +02:00
|
|
|
attributeData := a.([]interface{})
|
|
|
|
attributeDataMap := attributeData[0].(map[string]interface{})
|
|
|
|
attributes := new(librato.MetricAttributes)
|
|
|
|
|
|
|
|
if v, ok := attributeDataMap["color"].(string); ok && v != "" {
|
|
|
|
attributes.Color = librato.String(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["display_max"].(string); ok && v != "" {
|
|
|
|
attributes.DisplayMax = librato.String(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["display_min"].(string); ok && v != "" {
|
|
|
|
attributes.DisplayMin = librato.String(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["display_units_long"].(string); ok && v != "" {
|
|
|
|
attributes.DisplayUnitsLong = *librato.String(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["display_units_short"].(string); ok && v != "" {
|
|
|
|
attributes.DisplayUnitsShort = *librato.String(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["created_by_ua"].(string); ok && v != "" {
|
|
|
|
attributes.CreatedByUA = *librato.String(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["display_stacked"].(bool); ok {
|
|
|
|
attributes.DisplayStacked = *librato.Bool(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["gap_detection"].(bool); ok {
|
|
|
|
attributes.GapDetection = *librato.Bool(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["aggregate"].(bool); ok {
|
|
|
|
attributes.Aggregate = *librato.Bool(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
metric.Attributes = attributes
|
|
|
|
}
|
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
_, err := client.Metrics.Edit(&metric)
|
2017-05-17 06:18:43 +02:00
|
|
|
if err != nil {
|
2017-05-17 17:33:44 +02:00
|
|
|
log.Printf("[INFO] ERROR creating Metric: %s", err)
|
2017-05-18 15:37:16 +02:00
|
|
|
return fmt.Errorf("Error creating Librato metric: %s", err)
|
2017-05-17 06:18:43 +02:00
|
|
|
}
|
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError {
|
2017-05-17 06:18:43 +02:00
|
|
|
_, _, err := client.Metrics.Get(*metric.Name)
|
|
|
|
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-05-25 20:09:04 +02:00
|
|
|
if retryErr != nil {
|
|
|
|
return fmt.Errorf("Error creating Librato metric: %s", retryErr)
|
|
|
|
}
|
2017-05-17 06:18:43 +02:00
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
d.SetId(*metric.Name)
|
|
|
|
return resourceLibratoMetricRead(d, meta)
|
2017-05-17 06:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLibratoMetricRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*librato.Client)
|
|
|
|
|
|
|
|
id := d.Id()
|
|
|
|
|
|
|
|
log.Printf("[INFO] Reading Librato Metric: %s", id)
|
|
|
|
metric, _, err := client.Metrics.Get(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 Metric %s: %s", id, err)
|
|
|
|
}
|
2017-05-17 17:33:44 +02:00
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
d.Set("name", metric.Name)
|
|
|
|
d.Set("type", metric.Type)
|
2017-05-17 06:18:43 +02:00
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
if metric.Description != nil {
|
|
|
|
d.Set("description", metric.Description)
|
|
|
|
}
|
2017-05-17 06:18:43 +02:00
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
if metric.DisplayName != nil {
|
|
|
|
d.Set("display_name", metric.DisplayName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if metric.Period != nil {
|
|
|
|
d.Set("period", metric.Period)
|
|
|
|
}
|
2017-05-17 06:18:43 +02:00
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
if metric.Composite != nil {
|
|
|
|
d.Set("composite", metric.Composite)
|
|
|
|
}
|
2017-05-17 06:18:43 +02:00
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
attributes := metricAttributesGather(d, metric.Attributes)
|
|
|
|
|
|
|
|
// Since attributes isn't a simple terraform type (TypeList), it's best to
|
|
|
|
// catch the error returned from the d.Set() function, and handle accordingly.
|
|
|
|
if err := d.Set("attributes", attributes); err != nil {
|
2017-05-17 06:18:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLibratoMetricUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*librato.Client)
|
|
|
|
|
|
|
|
id := d.Id()
|
|
|
|
|
2017-05-17 06:18:43 +02:00
|
|
|
metric := new(librato.Metric)
|
2017-05-25 20:09:04 +02:00
|
|
|
metric.Name = librato.String(id)
|
2017-05-17 06:18:43 +02:00
|
|
|
|
|
|
|
if d.HasChange("type") {
|
|
|
|
metric.Type = librato.String(d.Get("type").(string))
|
|
|
|
}
|
|
|
|
if d.HasChange("description") {
|
|
|
|
metric.Description = librato.String(d.Get("description").(string))
|
|
|
|
}
|
|
|
|
if d.HasChange("display_name") {
|
|
|
|
metric.DisplayName = librato.String(d.Get("display_name").(string))
|
|
|
|
}
|
|
|
|
if d.HasChange("period") {
|
2017-05-25 20:09:04 +02:00
|
|
|
metric.Period = librato.Uint(uint(d.Get("period").(int)))
|
2017-05-17 06:18:43 +02:00
|
|
|
}
|
|
|
|
if d.HasChange("composite") {
|
|
|
|
metric.Composite = librato.String(d.Get("composite").(string))
|
|
|
|
}
|
|
|
|
if d.HasChange("attributes") {
|
|
|
|
attributeData := d.Get("attributes").([]interface{})
|
|
|
|
attributeDataMap := attributeData[0].(map[string]interface{})
|
|
|
|
attributes := new(librato.MetricAttributes)
|
|
|
|
|
|
|
|
if v, ok := attributeDataMap["color"].(string); ok && v != "" {
|
|
|
|
attributes.Color = librato.String(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["display_max"].(string); ok && v != "" {
|
|
|
|
attributes.DisplayMax = librato.String(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["display_min"].(string); ok && v != "" {
|
|
|
|
attributes.DisplayMin = librato.String(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["display_units_long"].(string); ok && v != "" {
|
|
|
|
attributes.DisplayUnitsLong = *librato.String(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["display_units_short"].(string); ok && v != "" {
|
|
|
|
attributes.DisplayUnitsShort = *librato.String(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["created_by_ua"].(string); ok && v != "" {
|
|
|
|
attributes.CreatedByUA = *librato.String(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["display_stacked"].(bool); ok {
|
|
|
|
attributes.DisplayStacked = *librato.Bool(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["gap_detection"].(bool); ok {
|
|
|
|
attributes.GapDetection = *librato.Bool(v)
|
|
|
|
}
|
|
|
|
if v, ok := attributeDataMap["aggregate"].(bool); ok {
|
|
|
|
attributes.Aggregate = *librato.Bool(v)
|
|
|
|
}
|
|
|
|
metric.Attributes = attributes
|
|
|
|
}
|
|
|
|
|
2017-05-18 15:37:16 +02:00
|
|
|
log.Printf("[INFO] Updating Librato metric: %v", structToString(metric))
|
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
_, err := client.Metrics.Edit(metric)
|
2017-05-17 06:18:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating Librato metric: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
log.Printf("[INFO] Updated Librato metric %s", id)
|
2017-05-17 06:18:43 +02: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-05-25 20:09:04 +02:00
|
|
|
log.Printf("[INFO] Checking if Librato Metric %s was updated yet", id)
|
|
|
|
changedMetric, _, getErr := client.Metrics.Get(id)
|
2017-05-17 06:18:43 +02:00
|
|
|
if getErr != nil {
|
|
|
|
return changedMetric, "", getErr
|
|
|
|
}
|
2017-05-25 20:09:04 +02:00
|
|
|
return changedMetric, "true", nil
|
2017-05-17 06:18:43 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = wait.WaitForState()
|
|
|
|
if err != nil {
|
2017-05-25 20:09:04 +02:00
|
|
|
log.Printf("[INFO] ERROR - Failed updating Librato Metric %s: %s", id, err)
|
|
|
|
return fmt.Errorf("Failed updating Librato Metric %s: %s", id, err)
|
2017-05-17 06:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return resourceLibratoMetricRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLibratoMetricDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*librato.Client)
|
|
|
|
|
|
|
|
id := d.Id()
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting Metric: %s", id)
|
|
|
|
_, err := client.Metrics.Delete(id)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting Metric: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-05-18 15:37:16 +02:00
|
|
|
log.Printf("[INFO] Verifying Metric %s deleted", id)
|
2017-05-25 20:09:04 +02:00
|
|
|
retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError {
|
2017-05-18 15:37:16 +02:00
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
log.Printf("[INFO] Getting Metric %s", id)
|
2017-05-17 06:18:43 +02:00
|
|
|
_, _, err := client.Metrics.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 {
|
2017-05-25 20:09:04 +02:00
|
|
|
log.Printf("[INFO] Metric %s not found, removing from state", id)
|
2017-05-17 06:18:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-05-17 17:33:44 +02:00
|
|
|
log.Printf("[INFO] non-retryable error attempting to Get metric: %s", err)
|
2017-05-17 06:18:43 +02:00
|
|
|
return resource.NonRetryableError(err)
|
|
|
|
}
|
2017-05-18 15:37:16 +02:00
|
|
|
|
|
|
|
log.Printf("[INFO] retryable error attempting to Get metric: %s", id)
|
2017-05-17 06:18:43 +02:00
|
|
|
return resource.RetryableError(fmt.Errorf("metric still exists"))
|
|
|
|
})
|
2017-05-25 20:09:04 +02:00
|
|
|
if retryErr != nil {
|
|
|
|
return fmt.Errorf("Error deleting librato metric: %s", retryErr)
|
2017-05-18 15:37:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flattens an attributes hash into something that flatmap.Flatten() can handle
|
|
|
|
func metricAttributesGather(d *schema.ResourceData, attributes *librato.MetricAttributes) []map[string]interface{} {
|
|
|
|
result := make([]map[string]interface{}, 0, 1)
|
|
|
|
|
|
|
|
if attributes != nil {
|
|
|
|
retAttributes := make(map[string]interface{})
|
|
|
|
if attributes.Color != nil {
|
|
|
|
retAttributes["color"] = *attributes.Color
|
|
|
|
}
|
|
|
|
if attributes.DisplayMax != nil {
|
|
|
|
retAttributes["display_max"] = attributes.DisplayMax
|
|
|
|
}
|
|
|
|
if attributes.DisplayMin != nil {
|
|
|
|
retAttributes["display_min"] = attributes.DisplayMin
|
|
|
|
}
|
|
|
|
if attributes.DisplayUnitsLong != "" {
|
|
|
|
retAttributes["display_units_long"] = attributes.DisplayUnitsLong
|
|
|
|
}
|
|
|
|
if attributes.DisplayUnitsShort != "" {
|
|
|
|
retAttributes["display_units_short"] = attributes.DisplayUnitsShort
|
|
|
|
}
|
|
|
|
if attributes.CreatedByUA != "" {
|
|
|
|
retAttributes["created_by_ua"] = attributes.CreatedByUA
|
|
|
|
}
|
|
|
|
retAttributes["display_stacked"] = attributes.DisplayStacked || false
|
|
|
|
retAttributes["gap_detection"] = attributes.GapDetection || false
|
|
|
|
retAttributes["aggregate"] = attributes.Aggregate || false
|
|
|
|
|
|
|
|
result = append(result, retAttributes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:33:44 +02:00
|
|
|
func structToString(i interface{}) string {
|
|
|
|
s, _ := json.Marshal(i)
|
|
|
|
return string(s)
|
|
|
|
}
|