Enable CRUD of metrics

This commit is contained in:
Chris Johnson 2017-05-17 00:18:43 -04:00
parent 0c649fcbb7
commit 002c9985d3
2 changed files with 533 additions and 0 deletions

View File

@ -0,0 +1,411 @@
package librato
import (
"fmt"
"log"
"reflect"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/henrikhodne/go-librato/librato"
)
const (
metricTypeGauge = "gauge"
metricTypeCounter = "counter"
metricTypeComposite = "composite"
)
var metricTypes = []string{metricTypeGauge, metricTypeCounter, metricTypeComposite}
func resourceLibratoMetric() *schema.Resource {
return &schema.Resource{
Create: resourceLibratoMetricCreate,
Read: resourceLibratoMetricRead,
Update: resourceLibratoMetricUpdate,
Delete: resourceLibratoMetricDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"display_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"period": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"composite": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"attributes": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"color": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"display_max": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"display_min": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"display_units_long": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"display_units_short": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"display_stacked": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"created_by_ua": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"gap_detection": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"aggregate": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
},
},
},
},
}
}
func resourceLibratoMetricCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*librato.Client)
metric := new(librato.Metric)
if a, ok := d.GetOk("name"); ok {
metric.Name = librato.String(a.(string))
}
if a, ok := d.GetOk("display_name"); ok {
metric.DisplayName = librato.String(a.(string))
}
if a, ok := d.GetOk("type"); ok {
metric.Type = librato.String(a.(string))
}
if a, ok := d.GetOk("description"); ok {
metric.Description = librato.String(a.(string))
}
if a, ok := d.GetOk("period"); ok {
metric.Period = librato.Uint(a.(uint))
}
if a, ok := d.GetOk("attributes"); ok {
attributeData := a.([]interface{})
if len(attributeData) > 1 {
return fmt.Errorf("Only one set of attributes per alert is supported")
}
if len(attributeData) == 1 && attributeData[0] == nil {
return fmt.Errorf("No attributes found in attributes block")
}
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
}
_, err := client.Metrics.Edit(metric)
if err != nil {
return fmt.Errorf("Error creating Librato service: %s", err)
}
resource.Retry(1*time.Minute, func() *resource.RetryError {
_, _, 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
})
return resourceLibratoMetricReadResult(d, metric)
}
func resourceLibratoMetricReadResult(d *schema.ResourceData, metric *librato.Metric) error {
d.SetId(*metric.Name)
d.Set("id", *metric.Name)
d.Set("name", *metric.Name)
d.Set("type", *metric.Type)
if metric.Description != nil {
d.Set("description", *metric.Description)
}
if metric.DisplayName != nil {
d.Set("display_name", *metric.DisplayName)
}
if metric.Period != nil {
d.Set("period", *metric.Period)
}
if metric.Composite != nil {
d.Set("composite", *metric.Composite)
}
attributes := resourceLibratoMetricAttributesGather(d, metric.Attributes)
d.Set("attributes", attributes)
return nil
}
// Flattens an attributes hash into something that flatmap.Flatten() can handle
func resourceLibratoMetricAttributesGather(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
}
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)
}
log.Printf("[INFO] Received Librato Metric: %+v", *metric)
return resourceLibratoMetricReadResult(d, metric)
}
func resourceLibratoMetricUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*librato.Client)
metricID := d.Id()
// Just to have whole object for comparison before/after update
fullMetric, _, err := client.Metrics.Get(metricID)
if err != nil {
return err
}
metric := new(librato.Metric)
metric.Name = librato.String(d.Get("name").(string))
if d.HasChange("type") {
metric.Type = librato.String(d.Get("type").(string))
fullMetric.Type = metric.Type
}
if d.HasChange("description") {
metric.Description = librato.String(d.Get("description").(string))
fullMetric.Description = metric.Description
}
if d.HasChange("display_name") {
metric.DisplayName = librato.String(d.Get("display_name").(string))
fullMetric.DisplayName = metric.DisplayName
}
if d.HasChange("period") {
metric.Period = librato.Uint(d.Get("period").(uint))
fullMetric.Period = metric.Period
}
if d.HasChange("composite") {
metric.Composite = librato.String(d.Get("composite").(string))
fullMetric.Composite = metric.Composite
}
if d.HasChange("attributes") {
attributeData := d.Get("attributes").([]interface{})
if len(attributeData) > 1 {
return fmt.Errorf("Only one set of attributes per alert is supported")
}
if len(attributeData) == 1 && attributeData[0] == nil {
return fmt.Errorf("No attributes found in attributes block")
}
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
fullMetric.Attributes = attributes
}
log.Printf("[INFO] Updating Librato metric: %v", metric)
_, err = client.Metrics.Edit(metric)
if err != nil {
return fmt.Errorf("Error updating Librato metric: %s", err)
}
log.Printf("[INFO] Updated Librato metric %s", metricID)
// 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) {
log.Printf("[DEBUG] Checking if Librato Metric %s was updated yet", metricID)
changedMetric, _, getErr := client.Metrics.Get(metricID)
if getErr != nil {
return changedMetric, "", getErr
}
isEqual := reflect.DeepEqual(*fullMetric, *changedMetric)
log.Printf("[DEBUG] Updated Librato Metric %s match: %t", metricID, isEqual)
return changedMetric, fmt.Sprintf("%t", isEqual), nil
},
}
_, err = wait.WaitForState()
if err != nil {
return fmt.Errorf("Failed updating Librato Metric %s: %s", metricID, err)
}
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)
}
resource.Retry(1*time.Minute, func() *resource.RetryError {
_, _, err := client.Metrics.Get(id)
if err != nil {
if errResp, ok := err.(*librato.ErrorResponse); ok && errResp.Response.StatusCode == 404 {
return nil
}
log.Printf("[DEBUG] unknown error attempting to Get metric: %s", err)
return resource.NonRetryableError(err)
}
return resource.RetryableError(fmt.Errorf("metric still exists"))
})
d.SetId("")
return nil
}

View File

@ -0,0 +1,122 @@
package librato
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/henrikhodne/go-librato/librato"
)
func TestAccLibratoMetric_Basic(t *testing.T) {
var metric librato.Metric
name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
typ := "counter"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibratoMetricDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckLibratoMetricConfig(name, typ),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name),
testAccCheckLibratoMetricType(&metric, []string{"gauge", "counter", "composite"}),
resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name),
),
},
},
})
}
func testAccCheckLibratoMetricDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*librato.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "librato_metric" {
continue
}
_, _, err := client.Metrics.Get(rs.Primary.ID)
if err == nil {
return fmt.Errorf("Metric still exists")
}
}
return nil
}
func testAccCheckLibratoMetricConfig(name, typ string) string {
return strings.TrimSpace(fmt.Sprintf(`
resource "librato_metric" "foobar" {
name = "%s"
type = "%s"
description = "A test composite metric"
composite = "s(\"librato.cpu.percent.user\", {\"environment\" : \"prod\", \"service\": \"api\"})"
attributes {
display_stacked = true,
created_by_ua = "go-librato/0.1"
}
}`, name, typ))
}
func testAccCheckLibratoMetricName(metric *librato.Metric, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if metric.Name == nil || *metric.Name != name {
return fmt.Errorf("Bad name: %s", *metric.Name)
}
return nil
}
}
func testAccCheckLibratoMetricType(metric *librato.Metric, validTypes []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
m := make(map[string]bool)
for _, v := range validTypes {
m[v] = true
}
if !m[*metric.Type] {
return fmt.Errorf("Bad metric type: %s", *metric.Type)
}
return nil
}
}
func testAccCheckLibratoMetricExists(n string, metric *librato.Metric) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Metric ID is set")
}
client := testAccProvider.Meta().(*librato.Client)
foundMetric, _, err := client.Metrics.Get(rs.Primary.ID)
if err != nil {
return err
}
if foundMetric.Name == nil || *foundMetric.Name != rs.Primary.ID {
return fmt.Errorf("Metric not found")
}
*metric = *foundMetric
return nil
}
}