github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/triton/resource_fabric_test.go (about) 1 package triton 2 3 import ( 4 "fmt" 5 "strconv" 6 "testing" 7 "time" 8 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 "github.com/joyent/gosdc/cloudapi" 13 ) 14 15 func TestAccTritonFabric_basic(t *testing.T) { 16 fabricName := fmt.Sprintf("acctest-%d", acctest.RandInt()) 17 config := fmt.Sprintf(testAccTritonFabric_basic, fabricName) 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testCheckTritonFabricDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: config, 26 Check: resource.ComposeTestCheckFunc( 27 testCheckTritonFabricExists("triton_fabric.test"), 28 func(*terraform.State) error { 29 time.Sleep(10 * time.Second) 30 return nil 31 }, 32 ), 33 }, 34 }, 35 }) 36 } 37 38 func testCheckTritonFabricExists(name string) resource.TestCheckFunc { 39 return func(s *terraform.State) error { 40 // Ensure we have enough information in state to look up in API 41 rs, ok := s.RootModule().Resources[name] 42 if !ok { 43 return fmt.Errorf("Not found: %s", name) 44 } 45 conn := testAccProvider.Meta().(*cloudapi.Client) 46 47 id, err := strconv.ParseInt(rs.Primary.Attributes["vlan_id"], 10, 16) 48 if err != nil { 49 return err 50 } 51 52 fabric, err := conn.GetFabricNetwork(int16(id), rs.Primary.ID) 53 if err != nil { 54 return fmt.Errorf("Bad: Check Fabric Exists: %s", err) 55 } 56 57 if fabric == nil { 58 return fmt.Errorf("Bad: Fabric %q does not exist", rs.Primary.ID) 59 } 60 61 return nil 62 } 63 } 64 65 func testCheckTritonFabricDestroy(s *terraform.State) error { 66 conn := testAccProvider.Meta().(*cloudapi.Client) 67 68 for _, rs := range s.RootModule().Resources { 69 if rs.Type != "triton_fabric" { 70 continue 71 } 72 73 id, err := strconv.ParseInt(rs.Primary.Attributes["vlan_id"], 10, 16) 74 if err != nil { 75 return err 76 } 77 78 fabric, err := conn.GetFabricNetwork(int16(id), rs.Primary.ID) 79 if err != nil { 80 return nil 81 } 82 83 if fabric != nil { 84 return fmt.Errorf("Bad: Fabric %q still exists", rs.Primary.ID) 85 } 86 } 87 88 return nil 89 } 90 91 var testAccTritonFabric_basic = ` 92 resource "triton_fabric" "test" { 93 name = "%s" 94 description = "test network" 95 vlan_id = 2 # every DC seems to have a vlan 2 available 96 97 subnet = "10.0.0.0/22" 98 gateway = "10.0.0.1" 99 provision_start_ip = "10.0.0.5" 100 provision_end_ip = "10.0.3.250" 101 102 resolvers = ["8.8.8.8", "8.8.4.4"] 103 } 104 `