github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/openstack/resource_openstack_lb_pool_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/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups" 11 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" 12 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/monitors" 13 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/pools" 14 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/vips" 15 "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" 16 "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" 17 ) 18 19 func TestAccLBV1Pool_basic(t *testing.T) { 20 var pool pools.Pool 21 22 resource.Test(t, resource.TestCase{ 23 PreCheck: func() { testAccPreCheck(t) }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckLBV1PoolDestroy, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: testAccLBV1Pool_basic, 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckLBV1PoolExists("openstack_lb_pool_v1.pool_1", &pool), 31 resource.TestCheckResourceAttr("openstack_lb_pool_v1.pool_1", "lb_provider", "haproxy"), 32 ), 33 }, 34 resource.TestStep{ 35 Config: testAccLBV1Pool_update, 36 Check: resource.ComposeTestCheckFunc( 37 resource.TestCheckResourceAttr("openstack_lb_pool_v1.pool_1", "name", "pool_1"), 38 ), 39 }, 40 }, 41 }) 42 } 43 44 func TestAccLBV1Pool_fullstack(t *testing.T) { 45 var instance1, instance2 servers.Server 46 var monitor monitors.Monitor 47 var network networks.Network 48 var pool pools.Pool 49 var secgroup secgroups.SecurityGroup 50 var subnet subnets.Subnet 51 var vip vips.VirtualIP 52 53 resource.Test(t, resource.TestCase{ 54 PreCheck: func() { testAccPreCheck(t) }, 55 Providers: testAccProviders, 56 CheckDestroy: testAccCheckLBV1PoolDestroy, 57 Steps: []resource.TestStep{ 58 resource.TestStep{ 59 Config: testAccLBV1Pool_fullstack_1, 60 Check: resource.ComposeTestCheckFunc( 61 testAccCheckNetworkingV2NetworkExists("openstack_networking_network_v2.network_1", &network), 62 testAccCheckNetworkingV2SubnetExists("openstack_networking_subnet_v2.subnet_1", &subnet), 63 testAccCheckComputeV2SecGroupExists("openstack_compute_secgroup_v2.secgroup_1", &secgroup), 64 testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance1), 65 testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_2", &instance2), 66 testAccCheckLBV1PoolExists("openstack_lb_pool_v1.pool_1", &pool), 67 testAccCheckLBV1MonitorExists("openstack_lb_monitor_v1.monitor_1", &monitor), 68 testAccCheckLBV1VIPExists("openstack_lb_vip_v1.vip_1", &vip), 69 ), 70 }, 71 resource.TestStep{ 72 Config: testAccLBV1Pool_fullstack_2, 73 Check: resource.ComposeTestCheckFunc( 74 testAccCheckNetworkingV2NetworkExists("openstack_networking_network_v2.network_1", &network), 75 testAccCheckNetworkingV2SubnetExists("openstack_networking_subnet_v2.subnet_1", &subnet), 76 testAccCheckComputeV2SecGroupExists("openstack_compute_secgroup_v2.secgroup_1", &secgroup), 77 testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance1), 78 testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_2", &instance2), 79 testAccCheckLBV1PoolExists("openstack_lb_pool_v1.pool_1", &pool), 80 testAccCheckLBV1MonitorExists("openstack_lb_monitor_v1.monitor_1", &monitor), 81 testAccCheckLBV1VIPExists("openstack_lb_vip_v1.vip_1", &vip), 82 ), 83 }, 84 }, 85 }) 86 } 87 88 func testAccCheckLBV1PoolDestroy(s *terraform.State) error { 89 config := testAccProvider.Meta().(*Config) 90 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 91 if err != nil { 92 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 93 } 94 95 for _, rs := range s.RootModule().Resources { 96 if rs.Type != "openstack_lb_pool_v1" { 97 continue 98 } 99 100 _, err := pools.Get(networkingClient, rs.Primary.ID).Extract() 101 if err == nil { 102 return fmt.Errorf("LB Pool still exists") 103 } 104 } 105 106 return nil 107 } 108 109 func testAccCheckLBV1PoolExists(n string, pool *pools.Pool) resource.TestCheckFunc { 110 return func(s *terraform.State) error { 111 rs, ok := s.RootModule().Resources[n] 112 if !ok { 113 return fmt.Errorf("Not found: %s", n) 114 } 115 116 if rs.Primary.ID == "" { 117 return fmt.Errorf("No ID is set") 118 } 119 120 config := testAccProvider.Meta().(*Config) 121 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 122 if err != nil { 123 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 124 } 125 126 found, err := pools.Get(networkingClient, rs.Primary.ID).Extract() 127 if err != nil { 128 return err 129 } 130 131 if found.ID != rs.Primary.ID { 132 return fmt.Errorf("Pool not found") 133 } 134 135 *pool = *found 136 137 return nil 138 } 139 } 140 141 const testAccLBV1Pool_basic = ` 142 resource "openstack_networking_network_v2" "network_1" { 143 name = "network_1" 144 admin_state_up = "true" 145 } 146 147 resource "openstack_networking_subnet_v2" "subnet_1" { 148 cidr = "192.168.199.0/24" 149 ip_version = 4 150 network_id = "${openstack_networking_network_v2.network_1.id}" 151 } 152 153 resource "openstack_lb_pool_v1" "pool_1" { 154 name = "pool_1" 155 protocol = "HTTP" 156 lb_method = "ROUND_ROBIN" 157 lb_provider = "haproxy" 158 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 159 } 160 ` 161 162 const testAccLBV1Pool_update = ` 163 resource "openstack_networking_network_v2" "network_1" { 164 name = "network_1" 165 admin_state_up = "true" 166 } 167 168 resource "openstack_networking_subnet_v2" "subnet_1" { 169 cidr = "192.168.199.0/24" 170 ip_version = 4 171 network_id = "${openstack_networking_network_v2.network_1.id}" 172 } 173 174 resource "openstack_lb_pool_v1" "pool_1" { 175 name = "pool_1" 176 protocol = "HTTP" 177 lb_method = "ROUND_ROBIN" 178 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 179 } 180 ` 181 182 const testAccLBV1Pool_fullstack_1 = ` 183 name = "network_1" 184 admin_state_up = "true" 185 resource "openstack_networking_network_v2" "network_1" { 186 } 187 188 resource "openstack_networking_subnet_v2" "subnet_1" { 189 cidr = "192.168.199.0/24" 190 ip_version = 4 191 network_id = "${openstack_networking_network_v2.network_1.id}" 192 } 193 194 resource "openstack_compute_secgroup_v2" "secgroup_1" { 195 name = "secgroup_1" 196 description = "Rules for secgroup_1" 197 198 rule { 199 from_port = -1 200 to_port = -1 201 ip_protocol = "icmp" 202 cidr = "0.0.0.0/0" 203 } 204 205 rule { 206 from_port = 80 207 to_port = 80 208 ip_protocol = "tcp" 209 cidr = "0.0.0.0/0" 210 } 211 } 212 213 resource "openstack_compute_instance_v2" "instance_1" { 214 name = "instance_1" 215 security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"] 216 217 network { 218 uuid = "${openstack_networking_network_v2.network_1.id}" 219 } 220 } 221 222 resource "openstack_compute_instance_v2" "instance_2" { 223 name = "instance_2" 224 security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"] 225 226 network { 227 uuid = "${openstack_networking_network_v2.network_1.id}" 228 } 229 } 230 231 resource "openstack_lb_monitor_v1" "monitor_1" { 232 type = "TCP" 233 delay = 30 234 timeout = 5 235 max_retries = 3 236 admin_state_up = "true" 237 } 238 239 resource "openstack_lb_pool_v1" "pool_1" { 240 name = "pool_1" 241 protocol = "TCP" 242 lb_method = "ROUND_ROBIN" 243 monitor_ids = ["${openstack_lb_monitor_v1.monitor_1.id}"] 244 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 245 } 246 247 resource "openstack_lb_member_v1" "member_1" { 248 port = 80 249 admin_state_up = true 250 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 251 address = "${openstack_compute_instance_v2.instance_1.access_ip_v4}" 252 } 253 254 resource "openstack_lb_member_v1" "member_2" { 255 port = 80 256 admin_state_up = true 257 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 258 address = "${openstack_compute_instance_v2.instance_2.access_ip_v4}" 259 } 260 261 resource "openstack_lb_vip_v1" "vip_1" { 262 name = "vip_1" 263 protocol = "TCP" 264 port = 80 265 admin_state_up = true 266 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 267 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 268 } 269 ` 270 271 const testAccLBV1Pool_fullstack_2 = ` 272 resource "openstack_networking_network_v2" "network_1" { 273 name = "network_1" 274 admin_state_up = "true" 275 } 276 277 resource "openstack_networking_subnet_v2" "subnet_1" { 278 cidr = "192.168.199.0/24" 279 ip_version = 4 280 network_id = "${openstack_networking_network_v2.network_1.id}" 281 } 282 283 resource "openstack_compute_secgroup_v2" "secgroup_1" { 284 name = "secgroup_1" 285 description = "Rules for secgroup_1" 286 287 rule { 288 from_port = -1 289 to_port = -1 290 ip_protocol = "icmp" 291 cidr = "0.0.0.0/0" 292 } 293 294 rule { 295 from_port = 80 296 to_port = 80 297 ip_protocol = "tcp" 298 cidr = "0.0.0.0/0" 299 } 300 } 301 302 resource "openstack_compute_instance_v2" "instance_1" { 303 name = "instance_1" 304 security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"] 305 306 network { 307 uuid = "${openstack_networking_network_v2.network_1.id}" 308 } 309 } 310 311 resource "openstack_compute_instance_v2" "instance_2" { 312 name = "instance_2" 313 security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"] 314 user_data = "#cloud-config\ndisable_root: false" 315 316 network { 317 uuid = "${openstack_networking_network_v2.network_1.id}" 318 } 319 } 320 321 resource "openstack_lb_monitor_v1" "monitor_1" { 322 type = "TCP" 323 delay = 30 324 timeout = 5 325 max_retries = 3 326 admin_state_up = "true" 327 } 328 329 resource "openstack_lb_pool_v1" "pool_1" { 330 name = "pool_1" 331 protocol = "TCP" 332 lb_method = "ROUND_ROBIN" 333 monitor_ids = ["${openstack_lb_monitor_v1.monitor_1.id}"] 334 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 335 } 336 337 resource "openstack_lb_member_v1" "member_1" { 338 port = 80 339 admin_state_up = true 340 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 341 address = "${openstack_compute_instance_v2.instance_1.access_ip_v4}" 342 } 343 344 resource "openstack_lb_member_v1" "member_2" { 345 port = 80 346 admin_state_up = true 347 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 348 address = "${openstack_compute_instance_v2.instance_2.access_ip_v4}" 349 } 350 351 resource "openstack_lb_vip_v1" "vip_1" { 352 name = "vip_1" 353 protocol = "TCP" 354 port = 80 355 admin_state_up = true 356 subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 357 pool_id = "${openstack_lb_pool_v1.pool_1.id}" 358 } 359 `