github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/openstack/resource_openstack_lb_loadbalancer_v2_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 11 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/loadbalancers" 12 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups" 13 "github.com/gophercloud/gophercloud/openstack/networking/v2/ports" 14 ) 15 16 func TestAccLBV2LoadBalancer_basic(t *testing.T) { 17 var lb loadbalancers.LoadBalancer 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckLBV2LoadBalancerDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccLBV2LoadBalancerConfig_basic, 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckLBV2LoadBalancerExists("openstack_lb_loadbalancer_v2.loadbalancer_1", &lb), 28 ), 29 }, 30 resource.TestStep{ 31 Config: testAccLBV2LoadBalancerConfig_update, 32 Check: resource.ComposeTestCheckFunc( 33 resource.TestCheckResourceAttr( 34 "openstack_lb_loadbalancer_v2.loadbalancer_1", "name", "loadbalancer_1_updated"), 35 resource.TestMatchResourceAttr( 36 "openstack_lb_loadbalancer_v2.loadbalancer_1", "vip_port_id", 37 regexp.MustCompile("^[a-f0-9-]+")), 38 ), 39 }, 40 }, 41 }) 42 } 43 44 func TestAccLBV2LoadBalancer_secGroup(t *testing.T) { 45 var lb loadbalancers.LoadBalancer 46 var sg_1, sg_2 groups.SecGroup 47 48 resource.Test(t, resource.TestCase{ 49 PreCheck: func() { testAccPreCheck(t) }, 50 Providers: testAccProviders, 51 CheckDestroy: testAccCheckLBV2LoadBalancerDestroy, 52 Steps: []resource.TestStep{ 53 resource.TestStep{ 54 Config: testAccLBV2LoadBalancer_secGroup, 55 Check: resource.ComposeTestCheckFunc( 56 testAccCheckLBV2LoadBalancerExists( 57 "openstack_lb_loadbalancer_v2.loadbalancer_1", &lb), 58 testAccCheckNetworkingV2SecGroupExists( 59 "openstack_networking_secgroup_v2.secgroup_1", &sg_1), 60 testAccCheckNetworkingV2SecGroupExists( 61 "openstack_networking_secgroup_v2.secgroup_1", &sg_2), 62 resource.TestCheckResourceAttr( 63 "openstack_lb_loadbalancer_v2.loadbalancer_1", "security_group_ids.#", "1"), 64 testAccCheckLBV2LoadBalancerHasSecGroup(&lb, &sg_1), 65 ), 66 }, 67 resource.TestStep{ 68 Config: testAccLBV2LoadBalancer_secGroup_update1, 69 Check: resource.ComposeTestCheckFunc( 70 testAccCheckLBV2LoadBalancerExists( 71 "openstack_lb_loadbalancer_v2.loadbalancer_1", &lb), 72 testAccCheckNetworkingV2SecGroupExists( 73 "openstack_networking_secgroup_v2.secgroup_2", &sg_1), 74 testAccCheckNetworkingV2SecGroupExists( 75 "openstack_networking_secgroup_v2.secgroup_2", &sg_2), 76 resource.TestCheckResourceAttr( 77 "openstack_lb_loadbalancer_v2.loadbalancer_1", "security_group_ids.#", "2"), 78 testAccCheckLBV2LoadBalancerHasSecGroup(&lb, &sg_1), 79 testAccCheckLBV2LoadBalancerHasSecGroup(&lb, &sg_2), 80 ), 81 }, 82 resource.TestStep{ 83 Config: testAccLBV2LoadBalancer_secGroup_update2, 84 Check: resource.ComposeTestCheckFunc( 85 testAccCheckLBV2LoadBalancerExists( 86 "openstack_lb_loadbalancer_v2.loadbalancer_1", &lb), 87 testAccCheckNetworkingV2SecGroupExists( 88 "openstack_networking_secgroup_v2.secgroup_2", &sg_1), 89 testAccCheckNetworkingV2SecGroupExists( 90 "openstack_networking_secgroup_v2.secgroup_2", &sg_2), 91 resource.TestCheckResourceAttr( 92 "openstack_lb_loadbalancer_v2.loadbalancer_1", "security_group_ids.#", "1"), 93 testAccCheckLBV2LoadBalancerHasSecGroup(&lb, &sg_2), 94 ), 95 }, 96 }, 97 }) 98 } 99 100 func TestAccLBV2LoadBalancer_timeout(t *testing.T) { 101 var lb loadbalancers.LoadBalancer 102 103 resource.Test(t, resource.TestCase{ 104 PreCheck: func() { testAccPreCheck(t) }, 105 Providers: testAccProviders, 106 CheckDestroy: testAccCheckLBV2LoadBalancerDestroy, 107 Steps: []resource.TestStep{ 108 resource.TestStep{ 109 Config: testAccLBV2LoadBalancerConfig_timeout, 110 Check: resource.ComposeTestCheckFunc( 111 testAccCheckLBV2LoadBalancerExists("openstack_lb_loadbalancer_v2.loadbalancer_1", &lb), 112 ), 113 }, 114 }, 115 }) 116 } 117 118 func testAccCheckLBV2LoadBalancerDestroy(s *terraform.State) error { 119 config := testAccProvider.Meta().(*Config) 120 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 121 if err != nil { 122 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 123 } 124 125 for _, rs := range s.RootModule().Resources { 126 if rs.Type != "openstack_lb_loadbalancer_v2" { 127 continue 128 } 129 130 _, err := loadbalancers.Get(networkingClient, rs.Primary.ID).Extract() 131 if err == nil { 132 return fmt.Errorf("LoadBalancer still exists: %s", rs.Primary.ID) 133 } 134 } 135 136 return nil 137 } 138 139 func testAccCheckLBV2LoadBalancerExists( 140 n string, lb *loadbalancers.LoadBalancer) resource.TestCheckFunc { 141 return func(s *terraform.State) error { 142 rs, ok := s.RootModule().Resources[n] 143 if !ok { 144 return fmt.Errorf("Not found: %s", n) 145 } 146 147 if rs.Primary.ID == "" { 148 return fmt.Errorf("No ID is set") 149 } 150 151 config := testAccProvider.Meta().(*Config) 152 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 153 if err != nil { 154 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 155 } 156 157 found, err := loadbalancers.Get(networkingClient, rs.Primary.ID).Extract() 158 if err != nil { 159 return err 160 } 161 162 if found.ID != rs.Primary.ID { 163 return fmt.Errorf("Member not found") 164 } 165 166 *lb = *found 167 168 return nil 169 } 170 } 171 172 func testAccCheckLBV2LoadBalancerHasSecGroup( 173 lb *loadbalancers.LoadBalancer, sg *groups.SecGroup) resource.TestCheckFunc { 174 return func(s *terraform.State) error { 175 config := testAccProvider.Meta().(*Config) 176 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 177 if err != nil { 178 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 179 } 180 181 port, err := ports.Get(networkingClient, lb.VipPortID).Extract() 182 if err != nil { 183 return err 184 } 185 186 for _, p := range port.SecurityGroups { 187 if p == sg.ID { 188 return nil 189 } 190 } 191 192 return fmt.Errorf("LoadBalancer does not have the security group") 193 } 194 } 195 196 const testAccLBV2LoadBalancerConfig_basic = ` 197 resource "openstack_networking_network_v2" "network_1" { 198 name = "network_1" 199 admin_state_up = "true" 200 } 201 202 resource "openstack_networking_subnet_v2" "subnet_1" { 203 name = "subnet_1" 204 cidr = "192.168.199.0/24" 205 ip_version = 4 206 network_id = "${openstack_networking_network_v2.network_1.id}" 207 } 208 209 resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" { 210 name = "loadbalancer_1" 211 loadbalancer_provider = "haproxy" 212 vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 213 } 214 ` 215 216 const testAccLBV2LoadBalancerConfig_update = ` 217 resource "openstack_networking_network_v2" "network_1" { 218 name = "network_1" 219 admin_state_up = "true" 220 } 221 222 resource "openstack_networking_subnet_v2" "subnet_1" { 223 name = "subnet_1" 224 cidr = "192.168.199.0/24" 225 ip_version = 4 226 network_id = "${openstack_networking_network_v2.network_1.id}" 227 } 228 229 resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" { 230 name = "loadbalancer_1_updated" 231 loadbalancer_provider = "haproxy" 232 admin_state_up = "true" 233 vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 234 } 235 ` 236 237 const testAccLBV2LoadBalancer_secGroup = ` 238 resource "openstack_networking_secgroup_v2" "secgroup_1" { 239 name = "secgroup_1" 240 description = "secgroup_1" 241 } 242 243 resource "openstack_networking_secgroup_v2" "secgroup_2" { 244 name = "secgroup_2" 245 description = "secgroup_2" 246 } 247 248 resource "openstack_networking_network_v2" "network_1" { 249 name = "network_1" 250 admin_state_up = "true" 251 } 252 253 resource "openstack_networking_subnet_v2" "subnet_1" { 254 name = "subnet_1" 255 network_id = "${openstack_networking_network_v2.network_1.id}" 256 cidr = "192.168.199.0/24" 257 } 258 259 resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" { 260 name = "loadbalancer_1" 261 vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 262 security_group_ids = [ 263 "${openstack_networking_secgroup_v2.secgroup_1.id}" 264 ] 265 } 266 ` 267 268 const testAccLBV2LoadBalancer_secGroup_update1 = ` 269 resource "openstack_networking_secgroup_v2" "secgroup_1" { 270 name = "secgroup_1" 271 description = "secgroup_1" 272 } 273 274 resource "openstack_networking_secgroup_v2" "secgroup_2" { 275 name = "secgroup_2" 276 description = "secgroup_2" 277 } 278 279 resource "openstack_networking_network_v2" "network_1" { 280 name = "network_1" 281 admin_state_up = "true" 282 } 283 284 resource "openstack_networking_subnet_v2" "subnet_1" { 285 name = "subnet_1" 286 network_id = "${openstack_networking_network_v2.network_1.id}" 287 cidr = "192.168.199.0/24" 288 } 289 290 resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" { 291 name = "loadbalancer_1" 292 vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 293 security_group_ids = [ 294 "${openstack_networking_secgroup_v2.secgroup_1.id}", 295 "${openstack_networking_secgroup_v2.secgroup_2.id}" 296 ] 297 } 298 ` 299 300 const testAccLBV2LoadBalancer_secGroup_update2 = ` 301 resource "openstack_networking_secgroup_v2" "secgroup_1" { 302 name = "secgroup_1" 303 description = "secgroup_1" 304 } 305 306 resource "openstack_networking_secgroup_v2" "secgroup_2" { 307 name = "secgroup_2" 308 description = "secgroup_2" 309 } 310 311 resource "openstack_networking_network_v2" "network_1" { 312 name = "network_1" 313 admin_state_up = "true" 314 } 315 316 resource "openstack_networking_subnet_v2" "subnet_1" { 317 name = "subnet_1" 318 network_id = "${openstack_networking_network_v2.network_1.id}" 319 cidr = "192.168.199.0/24" 320 } 321 322 resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" { 323 name = "loadbalancer_1" 324 vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 325 security_group_ids = [ 326 "${openstack_networking_secgroup_v2.secgroup_2.id}" 327 ] 328 depends_on = ["openstack_networking_secgroup_v2.secgroup_1"] 329 } 330 ` 331 332 const testAccLBV2LoadBalancerConfig_timeout = ` 333 resource "openstack_networking_network_v2" "network_1" { 334 name = "network_1" 335 admin_state_up = "true" 336 } 337 338 resource "openstack_networking_subnet_v2" "subnet_1" { 339 name = "subnet_1" 340 cidr = "192.168.199.0/24" 341 ip_version = 4 342 network_id = "${openstack_networking_network_v2.network_1.id}" 343 } 344 345 resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" { 346 name = "loadbalancer_1" 347 loadbalancer_provider = "haproxy" 348 vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 349 350 timeouts { 351 create = "5m" 352 delete = "5m" 353 } 354 } 355 `