2017-05-17 06:18:43 +02:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
func TestAccLibratoMetrics(t *testing.T) {
|
2017-05-17 06:18:43 +02:00
|
|
|
var metric librato.Metric
|
2017-05-25 20:09:04 +02:00
|
|
|
|
2017-05-17 06:18:43 +02:00
|
|
|
name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
|
2017-05-18 22:13:11 +02:00
|
|
|
typ := "counter"
|
2017-05-25 20:09:04 +02:00
|
|
|
desc1 := fmt.Sprintf("A test %s metric", typ)
|
|
|
|
desc2 := fmt.Sprintf("An updated test %s metric", typ)
|
2017-05-17 06:18:43 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckLibratoMetricDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
2017-05-25 20:09:04 +02:00
|
|
|
Config: counterMetricConfig(name, typ, desc1),
|
2017-05-18 15:37:16 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
|
|
|
|
testAccCheckLibratoMetricName(&metric, name),
|
2017-05-18 22:13:11 +02:00
|
|
|
testAccCheckLibratoMetricType(&metric, typ),
|
2017-05-18 15:37:16 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"librato_metric.foobar", "name", name),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PreConfig: sleep(t, 15),
|
2017-05-25 20:09:04 +02:00
|
|
|
Config: counterMetricConfig(name, typ, desc2),
|
2017-05-18 15:37:16 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
|
|
|
|
testAccCheckLibratoMetricName(&metric, name),
|
2017-05-18 22:13:11 +02:00
|
|
|
testAccCheckLibratoMetricType(&metric, typ),
|
2017-05-25 20:09:04 +02:00
|
|
|
testAccCheckLibratoMetricDescription(&metric, desc2),
|
2017-05-18 15:37:16 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"librato_metric.foobar", "name", name),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
name = fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
|
|
|
|
typ = "gauge"
|
|
|
|
desc1 = fmt.Sprintf("A test %s metric", typ)
|
|
|
|
desc2 = fmt.Sprintf("An updated test %s metric", typ)
|
2017-05-18 15:37:16 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckLibratoMetricDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
2017-05-25 20:09:04 +02:00
|
|
|
Config: gaugeMetricConfig(name, typ, desc1),
|
2017-05-18 15:37:16 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
|
|
|
|
testAccCheckLibratoMetricName(&metric, name),
|
2017-05-18 22:13:11 +02:00
|
|
|
testAccCheckLibratoMetricType(&metric, typ),
|
2017-05-18 15:37:16 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"librato_metric.foobar", "name", name),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PreConfig: sleep(t, 15),
|
2017-05-25 20:09:04 +02:00
|
|
|
Config: gaugeMetricConfig(name, typ, desc2),
|
2017-05-18 15:37:16 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
|
|
|
|
testAccCheckLibratoMetricName(&metric, name),
|
2017-05-18 22:13:11 +02:00
|
|
|
testAccCheckLibratoMetricType(&metric, typ),
|
2017-05-25 20:09:04 +02:00
|
|
|
testAccCheckLibratoMetricDescription(&metric, desc2),
|
2017-05-18 15:37:16 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"librato_metric.foobar", "name", name),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
name = fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
|
|
|
|
typ = "composite"
|
|
|
|
desc1 = fmt.Sprintf("A test %s metric", typ)
|
|
|
|
desc2 = fmt.Sprintf("An updated test %s metric", typ)
|
2017-05-18 15:37:16 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckLibratoMetricDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
2017-05-25 20:09:04 +02:00
|
|
|
Config: compositeMetricConfig(name, typ, desc1),
|
2017-05-18 15:37:16 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
|
|
|
|
testAccCheckLibratoMetricName(&metric, name),
|
2017-05-18 22:13:11 +02:00
|
|
|
testAccCheckLibratoMetricType(&metric, typ),
|
2017-05-18 15:37:16 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"librato_metric.foobar", "name", name),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PreConfig: sleep(t, 15),
|
2017-05-25 20:09:04 +02:00
|
|
|
Config: compositeMetricConfig(name, typ, desc2),
|
2017-05-17 06:18:43 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
|
|
|
|
testAccCheckLibratoMetricName(&metric, name),
|
2017-05-18 22:13:11 +02:00
|
|
|
testAccCheckLibratoMetricType(&metric, typ),
|
2017-05-25 20:09:04 +02:00
|
|
|
testAccCheckLibratoMetricDescription(&metric, desc2),
|
2017-05-17 06:18:43 +02:00
|
|
|
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 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 20:09:04 +02:00
|
|
|
func testAccCheckLibratoMetricDescription(metric *librato.Metric, desc string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if metric.Description == nil || *metric.Description != desc {
|
|
|
|
return fmt.Errorf("Bad description: %s", *metric.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-18 22:13:11 +02:00
|
|
|
func testAccCheckLibratoMetricType(metric *librato.Metric, wantType string) resource.TestCheckFunc {
|
2017-05-17 06:18:43 +02:00
|
|
|
return func(s *terraform.State) error {
|
2017-05-18 22:13:11 +02:00
|
|
|
if metric.Type == nil || *metric.Type != wantType {
|
2017-05-17 06:18:43 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2017-05-18 15:37:16 +02:00
|
|
|
|
|
|
|
func counterMetricConfig(name, typ, desc string) string {
|
|
|
|
return strings.TrimSpace(fmt.Sprintf(`
|
|
|
|
resource "librato_metric" "foobar" {
|
|
|
|
name = "%s"
|
|
|
|
type = "%s"
|
|
|
|
description = "%s"
|
|
|
|
attributes {
|
2017-05-18 22:13:11 +02:00
|
|
|
display_stacked = true
|
2017-05-18 15:37:16 +02:00
|
|
|
}
|
|
|
|
}`, name, typ, desc))
|
|
|
|
}
|
|
|
|
|
|
|
|
func gaugeMetricConfig(name, typ, desc string) string {
|
|
|
|
return strings.TrimSpace(fmt.Sprintf(`
|
|
|
|
resource "librato_metric" "foobar" {
|
|
|
|
name = "%s"
|
|
|
|
type = "%s"
|
|
|
|
description = "%s"
|
|
|
|
attributes {
|
2017-05-18 22:13:11 +02:00
|
|
|
display_stacked = true
|
2017-05-18 15:37:16 +02:00
|
|
|
}
|
|
|
|
}`, name, typ, desc))
|
|
|
|
}
|
|
|
|
|
|
|
|
func compositeMetricConfig(name, typ, desc string) string {
|
|
|
|
return strings.TrimSpace(fmt.Sprintf(`
|
|
|
|
resource "librato_metric" "foobar" {
|
|
|
|
name = "%s"
|
|
|
|
type = "%s"
|
|
|
|
description = "%s"
|
|
|
|
composite = "s(\"librato.cpu.percent.user\", {\"environment\" : \"prod\", \"service\": \"api\"})"
|
|
|
|
attributes {
|
2017-05-18 22:13:11 +02:00
|
|
|
display_stacked = true
|
2017-05-18 15:37:16 +02:00
|
|
|
}
|
|
|
|
}`, name, typ, desc))
|
|
|
|
}
|