Skip to content

Commit

Permalink
Cleanup and slight refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
wchrisjohnson committed May 25, 2017
1 parent 8d2cb8a commit 445658a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 39 deletions.
12 changes: 1 addition & 11 deletions resource_librato_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const (
metricTypeComposite = "composite"
)

var metricTypes = []string{metricTypeGauge, metricTypeCounter, metricTypeComposite}

func resourceLibratoMetric() *schema.Resource {
return &schema.Resource{
Create: resourceLibratoMetricCreate,
Expand Down Expand Up @@ -84,7 +82,7 @@ func resourceLibratoMetric() *schema.Resource {
},
"created_by_ua": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"gap_detection": &schema.Schema{
Type: schema.TypeBool,
Expand All @@ -102,8 +100,6 @@ func resourceLibratoMetric() *schema.Resource {
}

func resourceLibratoMetricCreate(d *schema.ResourceData, meta interface{}) error {
log.Println("[INFO] Creating Librato metric")

client := meta.(*librato.Client)

metric := new(librato.Metric)
Expand Down Expand Up @@ -192,8 +188,6 @@ func resourceLibratoMetricCreate(d *schema.ResourceData, meta interface{}) error
}

func resourceLibratoMetricRead(d *schema.ResourceData, meta interface{}) error {
log.Println("[INFO] Reading Librato metric")

client := meta.(*librato.Client)

id := d.Id()
Expand All @@ -214,8 +208,6 @@ func resourceLibratoMetricRead(d *schema.ResourceData, meta interface{}) error {
}

func resourceLibratoMetricUpdate(d *schema.ResourceData, meta interface{}) error {
log.Println("[INFO] Updating Librato metric")

client := meta.(*librato.Client)

metricID := d.Id()
Expand Down Expand Up @@ -338,8 +330,6 @@ func resourceLibratoMetricUpdate(d *schema.ResourceData, meta interface{}) error
}

func resourceLibratoMetricDelete(d *schema.ResourceData, meta interface{}) error {
log.Println("[INFO] Deleting Librato metric")

client := meta.(*librato.Client)

id := d.Id()
Expand Down
48 changes: 20 additions & 28 deletions resource_librato_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ import (
func TestAccLibratoCounterMetric(t *testing.T) {
var metric librato.Metric
name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
counter := "counter"
typ := "counter"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibratoMetricDestroy,
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(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name),
testAccCheckLibratoMetricType(&metric, []string{"gauge", "counter", "composite"}),
testAccCheckLibratoMetricType(&metric, typ),
resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name),
),
},
{
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(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name),
testAccCheckLibratoMetricType(&metric, []string{"gauge", "counter", "composite"}),
testAccCheckLibratoMetricType(&metric, typ),
resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name),
),
Expand All @@ -49,30 +49,30 @@ func TestAccLibratoCounterMetric(t *testing.T) {
func TestAccLibratoGaugeMetric(t *testing.T) {
var metric librato.Metric
name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
gauge := "gauge"
typ := "gauge"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibratoMetricDestroy,
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(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name),
testAccCheckLibratoMetricType(&metric, []string{"gauge", "counter", "composite"}),
testAccCheckLibratoMetricType(&metric, typ),
resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name),
),
},
{
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(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name),
testAccCheckLibratoMetricType(&metric, []string{"gauge", "counter", "composite"}),
testAccCheckLibratoMetricType(&metric, typ),
resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name),
),
Expand All @@ -84,30 +84,30 @@ func TestAccLibratoGaugeMetric(t *testing.T) {
func TestAccLibratoCompositeMetric(t *testing.T) {
var metric librato.Metric
name := fmt.Sprintf("tftest-metric-%s", acctest.RandString(10))
composite := "composite"
typ := "composite"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibratoMetricDestroy,
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(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name),
testAccCheckLibratoMetricType(&metric, []string{"gauge", "counter", "composite"}),
testAccCheckLibratoMetricType(&metric, typ),
resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name),
),
},
{
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(
testAccCheckLibratoMetricExists("librato_metric.foobar", &metric),
testAccCheckLibratoMetricName(&metric, name),
testAccCheckLibratoMetricType(&metric, []string{"gauge", "counter", "composite"}),
testAccCheckLibratoMetricType(&metric, typ),
resource.TestCheckResourceAttr(
"librato_metric.foobar", "name", name),
),
Expand Down Expand Up @@ -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 {
m := make(map[string]bool)
for _, v := range validTypes {
m[v] = true
}

if !m[*metric.Type] {
if metric.Type == nil || *metric.Type != wantType {
return fmt.Errorf("Bad metric type: %s", *metric.Type)
}

Expand Down Expand Up @@ -196,8 +191,7 @@ func counterMetricConfig(name, typ, desc string) string {
type = "%s"
description = "%s"
attributes {
display_stacked = true,
created_by_ua = "go-librato/0.1"
display_stacked = true
}
}`, name, typ, desc))
}
Expand All @@ -209,8 +203,7 @@ func gaugeMetricConfig(name, typ, desc string) string {
type = "%s"
description = "%s"
attributes {
display_stacked = true,
created_by_ua = "go-librato/0.1"
display_stacked = true
}
}`, name, typ, desc))
}
Expand All @@ -223,8 +216,7 @@ func compositeMetricConfig(name, typ, desc string) string {
description = "%s"
composite = "s(\"librato.cpu.percent.user\", {\"environment\" : \"prod\", \"service\": \"api\"})"
attributes {
display_stacked = true,
created_by_ua = "go-librato/0.1"
display_stacked = true
}
}`, name, typ, desc))
}

0 comments on commit 445658a

Please sign in to comment.