2016-08-05 23:16:32 +02:00
|
|
|
package librato
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/henrikhodne/go-librato/librato"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccLibratoAlert_Basic(t *testing.T) {
|
|
|
|
var alert librato.Alert
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckLibratoAlertDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckLibratoAlertConfig_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
|
|
|
|
testAccCheckLibratoAlertName(&alert, "FooBar"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"librato_alert.foobar", "name", "FooBar"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccLibratoAlert_Full(t *testing.T) {
|
|
|
|
var alert librato.Alert
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckLibratoAlertDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckLibratoAlertConfig_full,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
|
|
|
|
testAccCheckLibratoAlertName(&alert, "FooBar"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"librato_alert.foobar", "name", "FooBar"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccLibratoAlert_Updated(t *testing.T) {
|
|
|
|
var alert librato.Alert
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckLibratoAlertDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckLibratoAlertConfig_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
|
2016-09-21 18:54:38 +02:00
|
|
|
testAccCheckLibratoAlertDescription(&alert, "A Test Alert"),
|
2016-08-05 23:16:32 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"librato_alert.foobar", "name", "FooBar"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckLibratoAlertConfig_new_value,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLibratoAlertExists("librato_alert.foobar", &alert),
|
2016-09-21 18:54:38 +02:00
|
|
|
testAccCheckLibratoAlertDescription(&alert, "A modified Test Alert"),
|
2016-08-05 23:16:32 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-09-21 18:54:38 +02:00
|
|
|
"librato_alert.foobar", "description", "A modified Test Alert"),
|
2016-08-05 23:16:32 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckLibratoAlertDestroy(s *terraform.State) error {
|
|
|
|
client := testAccProvider.Meta().(*librato.Client)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "librato_alert" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("ID not a number")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = client.Alerts.Get(uint(id))
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Alert still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckLibratoAlertName(alert *librato.Alert, name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if alert.Name == nil || *alert.Name != name {
|
|
|
|
return fmt.Errorf("Bad name: %s", *alert.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 18:54:38 +02:00
|
|
|
func testAccCheckLibratoAlertDescription(alert *librato.Alert, description string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if alert.Description == nil || *alert.Description != description {
|
|
|
|
return fmt.Errorf("Bad description: %s", *alert.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 23:16:32 +02:00
|
|
|
func testAccCheckLibratoAlertExists(n string, alert *librato.Alert) 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 Alert ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := testAccProvider.Meta().(*librato.Client)
|
|
|
|
|
|
|
|
id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("ID not a number")
|
|
|
|
}
|
|
|
|
|
|
|
|
foundAlert, _, err := client.Alerts.Get(uint(id))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundAlert.ID == nil || *foundAlert.ID != uint(id) {
|
|
|
|
return fmt.Errorf("Alert not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*alert = *foundAlert
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccCheckLibratoAlertConfig_basic = `
|
|
|
|
resource "librato_alert" "foobar" {
|
|
|
|
name = "FooBar"
|
|
|
|
description = "A Test Alert"
|
|
|
|
}`
|
|
|
|
|
|
|
|
const testAccCheckLibratoAlertConfig_new_value = `
|
|
|
|
resource "librato_alert" "foobar" {
|
2016-09-21 18:54:38 +02:00
|
|
|
name = "FooBar"
|
|
|
|
description = "A modified Test Alert"
|
2016-08-05 23:16:32 +02:00
|
|
|
}`
|
|
|
|
|
|
|
|
const testAccCheckLibratoAlertConfig_full = `
|
|
|
|
resource "librato_service" "foobar" {
|
|
|
|
title = "Foo Bar"
|
|
|
|
type = "mail"
|
|
|
|
settings = <<EOF
|
|
|
|
{
|
|
|
|
"addresses": "admin@example.com"
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "librato_alert" "foobar" {
|
|
|
|
name = "FooBar"
|
|
|
|
description = "A Test Alert"
|
|
|
|
services = [ "${librato_service.foobar.id}" ]
|
|
|
|
condition {
|
|
|
|
type = "above"
|
|
|
|
threshold = 10
|
|
|
|
metric_name = "librato.cpu.percent.idle"
|
|
|
|
}
|
|
|
|
attributes {
|
|
|
|
runbook_url = "https://www.youtube.com/watch?v=oHg5SJYRHA0"
|
|
|
|
}
|
|
|
|
active = false
|
|
|
|
rearm_seconds = 300
|
|
|
|
}`
|