github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/builtin/providers/librato/resource_librato_space_test.go (about) 1 package librato 2 3 import ( 4 "fmt" 5 "strconv" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "github.com/henrikhodne/go-librato/librato" 11 ) 12 13 func TestAccLibratoSpace_Basic(t *testing.T) { 14 var space librato.Space 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckLibratoSpaceDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccCheckLibratoSpaceConfig_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckLibratoSpaceExists("librato_space.foobar", &space), 25 testAccCheckLibratoSpaceAttributes(&space), 26 resource.TestCheckResourceAttr( 27 "librato_space.foobar", "name", "Foo Bar"), 28 ), 29 }, 30 }, 31 }) 32 } 33 34 func testAccCheckLibratoSpaceDestroy(s *terraform.State) error { 35 client := testAccProvider.Meta().(*librato.Client) 36 37 for _, rs := range s.RootModule().Resources { 38 if rs.Type != "librato_space" { 39 continue 40 } 41 42 id, err := strconv.ParseUint(rs.Primary.ID, 10, 0) 43 if err != nil { 44 return fmt.Errorf("ID not a number") 45 } 46 47 _, _, err = client.Spaces.Get(uint(id)) 48 49 if err == nil { 50 return fmt.Errorf("Space still exists") 51 } 52 } 53 54 return nil 55 } 56 57 func testAccCheckLibratoSpaceAttributes(space *librato.Space) resource.TestCheckFunc { 58 return func(s *terraform.State) error { 59 60 if space.Name == nil || *space.Name != "Foo Bar" { 61 return fmt.Errorf("Bad name: %s", *space.Name) 62 } 63 64 return nil 65 } 66 } 67 68 func testAccCheckLibratoSpaceExists(n string, space *librato.Space) resource.TestCheckFunc { 69 return func(s *terraform.State) error { 70 rs, ok := s.RootModule().Resources[n] 71 72 if !ok { 73 return fmt.Errorf("Not found: %s", n) 74 } 75 76 if rs.Primary.ID == "" { 77 return fmt.Errorf("No Space ID is set") 78 } 79 80 client := testAccProvider.Meta().(*librato.Client) 81 82 id, err := strconv.ParseUint(rs.Primary.ID, 10, 0) 83 if err != nil { 84 return fmt.Errorf("ID not a number") 85 } 86 87 foundSpace, _, err := client.Spaces.Get(uint(id)) 88 89 if err != nil { 90 return err 91 } 92 93 if foundSpace.ID == nil || *foundSpace.ID != uint(id) { 94 return fmt.Errorf("Space not found") 95 } 96 97 *space = *foundSpace 98 99 return nil 100 } 101 } 102 103 const testAccCheckLibratoSpaceConfig_basic = ` 104 resource "librato_space" "foobar" { 105 name = "Foo Bar" 106 }`