github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/openstack/resource_openstack_lb_vip_v1_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 10 "github.com/rackspace/gophercloud/openstack/networking/v2/extensions/lbaas/vips" 11 ) 12 13 func TestAccLBV1VIP_basic(t *testing.T) { 14 var vip vips.VirtualIP 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckLBV1VIPDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccLBV1VIP_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckLBV1VIPExists(t, "openstack_lb_vip_v1.vip_1", &vip), 25 ), 26 }, 27 resource.TestStep{ 28 Config: testAccLBV1VIP_update, 29 Check: resource.ComposeTestCheckFunc( 30 resource.TestCheckResourceAttr("openstack_lb_vip_v1.vip_1", "name", "tf_test_lb_vip_updated"), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func testAccCheckLBV1VIPDestroy(s *terraform.State) error { 38 config := testAccProvider.Meta().(*Config) 39 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 40 if err != nil { 41 return fmt.Errorf("(testAccCheckLBV1VIPDestroy) Error creating OpenStack networking client: %s", err) 42 } 43 44 for _, rs := range s.RootModule().Resources { 45 if rs.Type != "openstack_lb_vip_v1" { 46 continue 47 } 48 49 _, err := vips.Get(networkingClient, rs.Primary.ID).Extract() 50 if err == nil { 51 return fmt.Errorf("LB VIP still exists") 52 } 53 } 54 55 return nil 56 } 57 58 func testAccCheckLBV1VIPExists(t *testing.T, n string, vip *vips.VirtualIP) resource.TestCheckFunc { 59 return func(s *terraform.State) error { 60 rs, ok := s.RootModule().Resources[n] 61 if !ok { 62 return fmt.Errorf("Not found: %s", n) 63 } 64 65 if rs.Primary.ID == "" { 66 return fmt.Errorf("No ID is set") 67 } 68 69 config := testAccProvider.Meta().(*Config) 70 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 71 if err != nil { 72 return fmt.Errorf("(testAccCheckLBV1VIPExists) Error creating OpenStack networking client: %s", err) 73 } 74 75 found, err := vips.Get(networkingClient, rs.Primary.ID).Extract() 76 if err != nil { 77 return err 78 } 79 80 if found.ID != rs.Primary.ID { 81 return fmt.Errorf("VIP not found") 82 } 83 84 *vip = *found 85 86 return nil 87 } 88 } 89 90 var testAccLBV1VIP_basic = fmt.Sprintf(` 91 resource "openstack_networking_network_v2" "network_1" { 92 region = "%s" 93 name = "network_1" 94 admin_state_up = "true" 95 } 96 97 resource "openstack_networking_subnet_v2" "subnet_1" { 98 region = "%s" 99 network_id = "${openstack_networking_network_v2.network_1.id}" 100 cidr = "192.168.199.0/24" 101 ip_version = 4 102 } 103 104 resource "openstack_lb_pool_v1" "pool_1" { 105 region = "%s" 106 name = "tf_test_lb_pool" 107 protocol = "HTTP" 108 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 109 lb_method = "ROUND_ROBIN" 110 } 111 112 resource "openstack_lb_vip_v1" "vip_1" { 113 region = "RegionOne" 114 name = "tf_test_lb_vip" 115 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 116 protocol = "HTTP" 117 port = 80 118 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 119 persistence { 120 type = "SOURCE_IP" 121 } 122 }`, 123 OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME) 124 125 var testAccLBV1VIP_update = fmt.Sprintf(` 126 resource "openstack_networking_network_v2" "network_1" { 127 region = "%s" 128 name = "network_1" 129 admin_state_up = "true" 130 } 131 132 resource "openstack_networking_subnet_v2" "subnet_1" { 133 region = "%s" 134 network_id = "${openstack_networking_network_v2.network_1.id}" 135 cidr = "192.168.199.0/24" 136 ip_version = 4 137 } 138 139 resource "openstack_lb_pool_v1" "pool_1" { 140 region = "%s" 141 name = "tf_test_lb_pool" 142 protocol = "HTTP" 143 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 144 lb_method = "ROUND_ROBIN" 145 } 146 147 resource "openstack_lb_vip_v1" "vip_1" { 148 region = "RegionOne" 149 name = "tf_test_lb_vip_updated" 150 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 151 protocol = "HTTP" 152 port = 80 153 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 154 persistence { 155 type = "SOURCE_IP" 156 } 157 }`, 158 OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME)