2015-10-01 00:08:51 +02:00
|
|
|
package librato
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2016-09-22 13:41:49 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-10-01 00:08:51 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/henrikhodne/go-librato/librato"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccLibratoSpace_Basic(t *testing.T) {
|
|
|
|
var space librato.Space
|
2016-09-22 13:41:49 +02:00
|
|
|
name := acctest.RandString(10)
|
2015-10-01 00:08:51 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckLibratoSpaceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-09-22 13:41:49 +02:00
|
|
|
{
|
|
|
|
Config: testAccCheckLibratoSpaceConfig_basic(name),
|
2015-10-01 00:08:51 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLibratoSpaceExists("librato_space.foobar", &space),
|
2016-09-22 13:41:49 +02:00
|
|
|
testAccCheckLibratoSpaceAttributes(&space, name),
|
2015-10-01 00:08:51 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-09-22 13:41:49 +02:00
|
|
|
"librato_space.foobar", "name", name),
|
2015-10-01 00:08:51 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckLibratoSpaceDestroy(s *terraform.State) error {
|
|
|
|
client := testAccProvider.Meta().(*librato.Client)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "librato_space" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := strconv.ParseUint(rs.Primary.ID, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("ID not a number")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = client.Spaces.Get(uint(id))
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Space still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-22 13:41:49 +02:00
|
|
|
func testAccCheckLibratoSpaceAttributes(space *librato.Space, name string) resource.TestCheckFunc {
|
2015-10-01 00:08:51 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
2016-09-22 13:41:49 +02:00
|
|
|
if space.Name == nil || *space.Name != name {
|
2015-10-01 00:08:51 +02:00
|
|
|
return fmt.Errorf("Bad name: %s", *space.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckLibratoSpaceExists(n string, space *librato.Space) 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 Space 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
foundSpace, _, err := client.Spaces.Get(uint(id))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundSpace.ID == nil || *foundSpace.ID != uint(id) {
|
|
|
|
return fmt.Errorf("Space not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*space = *foundSpace
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 13:41:49 +02:00
|
|
|
func testAccCheckLibratoSpaceConfig_basic(name string) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-10-01 00:08:51 +02:00
|
|
|
resource "librato_space" "foobar" {
|
2016-09-22 13:41:49 +02:00
|
|
|
name = "%s"
|
|
|
|
}`, name)
|
|
|
|
}
|