github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 testAccCheckLBV2LoadBalancerDestroy(s *terraform.State) error { 101 config := testAccProvider.Meta().(*Config) 102 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 103 if err != nil { 104 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 105 } 106 107 for _, rs := range s.RootModule().Resources { 108 if rs.Type != "openstack_lb_loadbalancer_v2" { 109 continue 110 } 111 112 _, err := loadbalancers.Get(networkingClient, rs.Primary.ID).Extract() 113 if err == nil { 114 return fmt.Errorf("LoadBalancer still exists: %s", rs.Primary.ID) 115 } 116 } 117 118 return nil 119 } 120 121 func testAccCheckLBV2LoadBalancerExists( 122 n string, lb *loadbalancers.LoadBalancer) resource.TestCheckFunc { 123 return func(s *terraform.State) error { 124 rs, ok := s.RootModule().Resources[n] 125 if !ok { 126 return fmt.Errorf("Not found: %s", n) 127 } 128 129 if rs.Primary.ID == "" { 130 return fmt.Errorf("No ID is set") 131 } 132 133 config := testAccProvider.Meta().(*Config) 134 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 135 if err != nil { 136 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 137 } 138 139 found, err := loadbalancers.Get(networkingClient, rs.Primary.ID).Extract() 140 if err != nil { 141 return err 142 } 143 144 if found.ID != rs.Primary.ID { 145 return fmt.Errorf("Member not found") 146 } 147 148 *lb = *found 149 150 return nil 151 } 152 } 153 154 func testAccCheckLBV2LoadBalancerHasSecGroup( 155 lb *loadbalancers.LoadBalancer, sg *groups.SecGroup) resource.TestCheckFunc { 156 return func(s *terraform.State) error { 157 config := testAccProvider.Meta().(*Config) 158 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 159 if err != nil { 160 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 161 } 162 163 port, err := ports.Get(networkingClient, lb.VipPortID).Extract() 164 if err != nil { 165 return err 166 } 167 168 for _, p := range port.SecurityGroups { 169 if p == sg.ID { 170 return nil 171 } 172 } 173 174 return fmt.Errorf("LoadBalancer does not have the security group") 175 } 176 } 177 178 const testAccLBV2LoadBalancerConfig_basic = ` 179 resource "openstack_networking_network_v2" "network_1" { 180 name = "network_1" 181 admin_state_up = "true" 182 } 183 184 resource "openstack_networking_subnet_v2" "subnet_1" { 185 name = "subnet_1" 186 cidr = "192.168.199.0/24" 187 ip_version = 4 188 network_id = "${openstack_networking_network_v2.network_1.id}" 189 } 190 191 resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" { 192 name = "loadbalancer_1" 193 vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 194 } 195 ` 196 197 const testAccLBV2LoadBalancerConfig_update = ` 198 resource "openstack_networking_network_v2" "network_1" { 199 name = "network_1" 200 admin_state_up = "true" 201 } 202 203 resource "openstack_networking_subnet_v2" "subnet_1" { 204 name = "subnet_1" 205 cidr = "192.168.199.0/24" 206 ip_version = 4 207 network_id = "${openstack_networking_network_v2.network_1.id}" 208 } 209 210 resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" { 211 name = "loadbalancer_1_updated" 212 admin_state_up = "true" 213 vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 214 } 215 ` 216 217 const testAccLBV2LoadBalancer_secGroup = ` 218 resource "openstack_networking_secgroup_v2" "secgroup_1" { 219 name = "secgroup_1" 220 description = "secgroup_1" 221 } 222 223 resource "openstack_networking_secgroup_v2" "secgroup_2" { 224 name = "secgroup_2" 225 description = "secgroup_2" 226 } 227 228 resource "openstack_networking_network_v2" "network_1" { 229 name = "network_1" 230 admin_state_up = "true" 231 } 232 233 resource "openstack_networking_subnet_v2" "subnet_1" { 234 name = "subnet_1" 235 network_id = "${openstack_networking_network_v2.network_1.id}" 236 cidr = "192.168.199.0/24" 237 } 238 239 resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" { 240 name = "loadbalancer_1" 241 vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 242 security_group_ids = [ 243 "${openstack_networking_secgroup_v2.secgroup_1.id}" 244 ] 245 } 246 ` 247 248 const testAccLBV2LoadBalancer_secGroup_update1 = ` 249 resource "openstack_networking_secgroup_v2" "secgroup_1" { 250 name = "secgroup_1" 251 description = "secgroup_1" 252 } 253 254 resource "openstack_networking_secgroup_v2" "secgroup_2" { 255 name = "secgroup_2" 256 description = "secgroup_2" 257 } 258 259 resource "openstack_networking_network_v2" "network_1" { 260 name = "network_1" 261 admin_state_up = "true" 262 } 263 264 resource "openstack_networking_subnet_v2" "subnet_1" { 265 name = "subnet_1" 266 network_id = "${openstack_networking_network_v2.network_1.id}" 267 cidr = "192.168.199.0/24" 268 } 269 270 resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" { 271 name = "loadbalancer_1" 272 vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 273 security_group_ids = [ 274 "${openstack_networking_secgroup_v2.secgroup_1.id}", 275 "${openstack_networking_secgroup_v2.secgroup_2.id}" 276 ] 277 } 278 ` 279 280 const testAccLBV2LoadBalancer_secGroup_update2 = ` 281 resource "openstack_networking_secgroup_v2" "secgroup_1" { 282 name = "secgroup_1" 283 description = "secgroup_1" 284 } 285 286 resource "openstack_networking_secgroup_v2" "secgroup_2" { 287 name = "secgroup_2" 288 description = "secgroup_2" 289 } 290 291 resource "openstack_networking_network_v2" "network_1" { 292 name = "network_1" 293 admin_state_up = "true" 294 } 295 296 resource "openstack_networking_subnet_v2" "subnet_1" { 297 name = "subnet_1" 298 network_id = "${openstack_networking_network_v2.network_1.id}" 299 cidr = "192.168.199.0/24" 300 } 301 302 resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" { 303 name = "loadbalancer_1" 304 vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}" 305 security_group_ids = [ 306 "${openstack_networking_secgroup_v2.secgroup_2.id}" 307 ] 308 depends_on = ["openstack_networking_secgroup_v2.secgroup_1"] 309 } 310 `