github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/builtin/providers/triton/resource_vlan_test.go (about) 1 package triton 2 3 import ( 4 "context" 5 "fmt" 6 "strconv" 7 "testing" 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/triton-go" 13 ) 14 15 func TestAccTritonVLAN_basic(t *testing.T) { 16 config := testAccTritonVLAN_basic(acctest.RandIntRange(3, 2048)) 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testCheckTritonVLANDestroy, 22 Steps: []resource.TestStep{ 23 { 24 Config: config, 25 Check: resource.ComposeTestCheckFunc( 26 testCheckTritonVLANExists("triton_vlan.test"), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func TestAccTritonVLAN_update(t *testing.T) { 34 vlanNumber := acctest.RandIntRange(3, 2048) 35 preConfig := testAccTritonVLAN_basic(vlanNumber) 36 postConfig := testAccTritonVLAN_update(vlanNumber) 37 38 resource.Test(t, resource.TestCase{ 39 PreCheck: func() { testAccPreCheck(t) }, 40 Providers: testAccProviders, 41 CheckDestroy: testCheckTritonVLANDestroy, 42 Steps: []resource.TestStep{ 43 { 44 Config: preConfig, 45 Check: resource.ComposeTestCheckFunc( 46 testCheckTritonVLANExists("triton_vlan.test"), 47 resource.TestCheckResourceAttr("triton_vlan.test", "vlan_id", strconv.Itoa(vlanNumber)), 48 resource.TestCheckResourceAttr("triton_vlan.test", "name", "test-vlan"), 49 resource.TestCheckResourceAttr("triton_vlan.test", "description", "test vlan"), 50 ), 51 }, 52 53 { 54 Config: postConfig, 55 Check: resource.ComposeTestCheckFunc( 56 testCheckTritonVLANExists("triton_vlan.test"), 57 resource.TestCheckResourceAttr("triton_vlan.test", "vlan_id", strconv.Itoa(vlanNumber)), 58 resource.TestCheckResourceAttr("triton_vlan.test", "name", "test-vlan-2"), 59 resource.TestCheckResourceAttr("triton_vlan.test", "description", "test vlan 2"), 60 ), 61 }, 62 }, 63 }) 64 } 65 66 func testCheckTritonVLANExists(name string) resource.TestCheckFunc { 67 return func(s *terraform.State) error { 68 // Ensure we have enough information in state to look up in API 69 rs, ok := s.RootModule().Resources[name] 70 if !ok { 71 return fmt.Errorf("Not found: %s", name) 72 } 73 conn := testAccProvider.Meta().(*triton.Client) 74 75 id, err := resourceVLANIDInt(rs.Primary.ID) 76 if err != nil { 77 return err 78 } 79 80 resp, err := conn.Fabrics().GetFabricVLAN(context.Background(), &triton.GetFabricVLANInput{ 81 ID: id, 82 }) 83 if err != nil && triton.IsResourceNotFound(err) { 84 return fmt.Errorf("Bad: Check VLAN Exists: %s", err) 85 } else if err != nil { 86 return err 87 } 88 89 if resp == nil { 90 return fmt.Errorf("Bad: VLAN %q does not exist", rs.Primary.ID) 91 } 92 93 return nil 94 } 95 } 96 97 func testCheckTritonVLANDestroy(s *terraform.State) error { 98 conn := testAccProvider.Meta().(*triton.Client) 99 100 for _, rs := range s.RootModule().Resources { 101 if rs.Type != "triton_vlan" { 102 continue 103 } 104 105 id, err := resourceVLANIDInt(rs.Primary.ID) 106 if err != nil { 107 return err 108 } 109 110 resp, err := conn.Fabrics().GetFabricVLAN(context.Background(), &triton.GetFabricVLANInput{ 111 ID: id, 112 }) 113 if triton.IsResourceNotFound(err) { 114 return nil 115 } else if err != nil { 116 return err 117 } 118 119 if resp != nil { 120 return fmt.Errorf("Bad: VLAN %q still exists", rs.Primary.ID) 121 } 122 } 123 124 return nil 125 } 126 127 var testAccTritonVLAN_basic = func(vlanID int) string { 128 return fmt.Sprintf(`resource "triton_vlan" "test" { 129 vlan_id = %d 130 name = "test-vlan" 131 description = "test vlan" 132 }`, vlanID) 133 } 134 135 var testAccTritonVLAN_update = func(vlanID int) string { 136 return fmt.Sprintf(`resource "triton_vlan" "test" { 137 vlan_id = %d 138 name = "test-vlan-2" 139 description = "test vlan 2" 140 }`, vlanID) 141 }