Cleanup and slight refactors

This commit is contained in:
Chris Johnson 2017-05-18 16:13:11 -04:00
parent 45c93cfb07
commit 78b61ad5f8
2 changed files with 21 additions and 39 deletions

View File

@ -18,8 +18,6 @@ const (
metricTypeComposite = "composite" metricTypeComposite = "composite"
) )
var metricTypes = []string{metricTypeGauge, metricTypeCounter, metricTypeComposite}
func resourceLibratoMetric() *schema.Resource { func resourceLibratoMetric() *schema.Resource {
return &schema.Resource{ return &schema.Resource{
Create: resourceLibratoMetricCreate, Create: resourceLibratoMetricCreate,
@ -84,7 +82,7 @@ func resourceLibratoMetric() *schema.Resource {
}, },
"created_by_ua": &schema.Schema{ "created_by_ua": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Computed: true,
}, },
"gap_detection": &schema.Schema{ "gap_detection": &schema.Schema{
Type: schema.TypeBool, Type: schema.TypeBool,
@ -102,8 +100,6 @@ func resourceLibratoMetric() *schema.Resource {
} }
func resourceLibratoMetricCreate(d *schema.ResourceData, meta interface{}) error { func resourceLibratoMetricCreate(d *schema.ResourceData, meta interface{}) error {
log.Println("[INFO] Creating Librato metric")
client := meta.(*librato.Client) client := meta.(*librato.Client)
metric := new(librato.Metric) metric := new(librato.Metric)
@ -192,8 +188,6 @@ func resourceLibratoMetricCreate(d *schema.ResourceData, meta interface{}) error
} }
func resourceLibratoMetricRead(d *schema.ResourceData, meta interface{}) error { func resourceLibratoMetricRead(d *schema.ResourceData, meta interface{}) error {
log.Println("[INFO] Reading Librato metric")
client := meta.(*librato.Client) client := meta.(*librato.Client)
id := d.Id() id := d.Id()
@ -214,8 +208,6 @@ func resourceLibratoMetricRead(d *schema.ResourceData, meta interface{}) error {
} }
func resourceLibratoMetricUpdate(d *schema.ResourceData, meta interface{}) error { func resourceLibratoMetricUpdate(d *schema.ResourceData, meta interface{}) error {
log.Println("[INFO] Updating Librato metric")
client := meta.(*librato.Client) client := meta.(*librato.Client)
metricID := d.Id() metricID := d.Id()
@ -338,8 +330,6 @@ func resourceLibratoMetricUpdate(d *schema.ResourceData, meta interface{}) error
} }
func resourceLibratoMetricDelete(d *schema.ResourceData, meta interface{}) error { func resourceLibratoMetricDelete(d *schema.ResourceData, meta interface{}) error {
log.Println("[INFO] Deleting Librato metric")
client := meta.(*librato.Client) client := meta.(*librato.Client)
id := d.Id() id := d.Id()

View File

@ -14,7 +14,7 @@ import (
func TestAccLibratoCounterMetric(t *testing.T) { func TestAccLibratoCounterMetric(t *testing.T) {
var metric librato.Metric var metric librato.Metric
name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10)) name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
counter := "counter" typ := "counter"
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
@ -22,22 +22,22 @@ func TestAccLibratoCounterMetric(t *testing.T) {
CheckDestroy: testAccCheckLibratoMetricDestroy, CheckDestroy: testAccCheckLibratoMetricDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
{ {
Config: counterMetricConfig(name, counter, fmt.Sprintf("A test %s metric", counter)), Config: counterMetricConfig(name, typ, fmt.Sprintf("A test %s metric", typ)),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric), testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name), testAccCheckLibratoMetricName(&metric, name),
testAccCheckLibratoMetricType(&metric, []string{"gauge", "counter", "composite"}), testAccCheckLibratoMetricType(&metric, typ),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name), "librato_metric.foobar", "name", name),
), ),
}, },
{ {
PreConfig: sleep(t, 15), PreConfig: sleep(t, 15),
Config: counterMetricConfig(name, counter, fmt.Sprintf("An updated test %s metric", counter)), Config: counterMetricConfig(name, typ, fmt.Sprintf("An updated test %s metric", typ)),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric), testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name), testAccCheckLibratoMetricName(&metric, name),
testAccCheckLibratoMetricType(&metric, []string{"gauge", "counter", "composite"}), testAccCheckLibratoMetricType(&metric, typ),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name), "librato_metric.foobar", "name", name),
), ),
@ -49,7 +49,7 @@ func TestAccLibratoCounterMetric(t *testing.T) {
func TestAccLibratoGaugeMetric(t *testing.T) { func TestAccLibratoGaugeMetric(t *testing.T) {
var metric librato.Metric var metric librato.Metric
name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10)) name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
gauge := "gauge" typ := "gauge"
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
@ -57,22 +57,22 @@ func TestAccLibratoGaugeMetric(t *testing.T) {
CheckDestroy: testAccCheckLibratoMetricDestroy, CheckDestroy: testAccCheckLibratoMetricDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
{ {
Config: gaugeMetricConfig(name, gauge, fmt.Sprintf("A test %s metric", gauge)), Config: gaugeMetricConfig(name, typ, fmt.Sprintf("A test %s metric", typ)),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric), testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name), testAccCheckLibratoMetricName(&metric, name),
testAccCheckLibratoMetricType(&metric, []string{"gauge", "counter", "composite"}), testAccCheckLibratoMetricType(&metric, typ),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name), "librato_metric.foobar", "name", name),
), ),
}, },
{ {
PreConfig: sleep(t, 15), PreConfig: sleep(t, 15),
Config: gaugeMetricConfig(name, gauge, fmt.Sprintf("An updated test %s metric", gauge)), Config: gaugeMetricConfig(name, typ, fmt.Sprintf("An updated test %s metric", typ)),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric), testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name), testAccCheckLibratoMetricName(&metric, name),
testAccCheckLibratoMetricType(&metric, []string{"gauge", "counter", "composite"}), testAccCheckLibratoMetricType(&metric, typ),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name), "librato_metric.foobar", "name", name),
), ),
@ -84,7 +84,7 @@ func TestAccLibratoGaugeMetric(t *testing.T) {
func TestAccLibratoCompositeMetric(t *testing.T) { func TestAccLibratoCompositeMetric(t *testing.T) {
var metric librato.Metric var metric librato.Metric
name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10)) name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
composite := "composite" typ := "composite"
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
@ -92,22 +92,22 @@ func TestAccLibratoCompositeMetric(t *testing.T) {
CheckDestroy: testAccCheckLibratoMetricDestroy, CheckDestroy: testAccCheckLibratoMetricDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
{ {
Config: compositeMetricConfig(name, composite, fmt.Sprintf("A test %s metric", composite)), Config: compositeMetricConfig(name, typ, fmt.Sprintf("A test %s metric", typ)),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric), testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name), testAccCheckLibratoMetricName(&metric, name),
testAccCheckLibratoMetricType(&metric, []string{"gauge", "counter", "composite"}), testAccCheckLibratoMetricType(&metric, typ),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name), "librato_metric.foobar", "name", name),
), ),
}, },
{ {
PreConfig: sleep(t, 15), PreConfig: sleep(t, 15),
Config: compositeMetricConfig(name, composite, fmt.Sprintf("An updated test %s metric", composite)), Config: compositeMetricConfig(name, typ, fmt.Sprintf("An updated test %s metric", typ)),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric), testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name), testAccCheckLibratoMetricName(&metric, name),
testAccCheckLibratoMetricType(&metric, []string{"gauge", "counter", "composite"}), testAccCheckLibratoMetricType(&metric, typ),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name), "librato_metric.foobar", "name", name),
), ),
@ -144,14 +144,9 @@ func testAccCheckLibratoMetricName(metric *librato.Metric, name string) resource
} }
} }
func testAccCheckLibratoMetricType(metric *librato.Metric, validTypes []string) resource.TestCheckFunc { func testAccCheckLibratoMetricType(metric *librato.Metric, wantType string) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
m := make(map[string]bool) if metric.Type == nil || *metric.Type != wantType {
for _, v := range validTypes {
m[v] = true
}
if !m[*metric.Type] {
return fmt.Errorf("Bad metric type: %s", *metric.Type) return fmt.Errorf("Bad metric type: %s", *metric.Type)
} }
@ -196,8 +191,7 @@ func counterMetricConfig(name, typ, desc string) string {
type = "%s" type = "%s"
description = "%s" description = "%s"
attributes { attributes {
display_stacked = true, display_stacked = true
created_by_ua = "go-librato/0.1"
} }
}`, name, typ, desc)) }`, name, typ, desc))
} }
@ -209,8 +203,7 @@ func gaugeMetricConfig(name, typ, desc string) string {
type = "%s" type = "%s"
description = "%s" description = "%s"
attributes { attributes {
display_stacked = true, display_stacked = true
created_by_ua = "go-librato/0.1"
} }
}`, name, typ, desc)) }`, name, typ, desc))
} }
@ -223,8 +216,7 @@ func compositeMetricConfig(name, typ, desc string) string {
description = "%s" description = "%s"
composite = "s(\"librato.cpu.percent.user\", {\"environment\" : \"prod\", \"service\": \"api\"})" composite = "s(\"librato.cpu.percent.user\", {\"environment\" : \"prod\", \"service\": \"api\"})"
attributes { attributes {
display_stacked = true, display_stacked = true
created_by_ua = "go-librato/0.1"
} }
}`, name, typ, desc)) }`, name, typ, desc))
} }