github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudstack/resource_cloudstack_loadbalancer_test.go (about) 1 package cloudstack 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "github.com/xanzy/go-cloudstack/cloudstack" 11 ) 12 13 func TestAccCloudStackLoadBalancerRule_basic(t *testing.T) { 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 CheckDestroy: testAccCheckCloudStackLoadBalancerRuleDestroy, 18 Steps: []resource.TestStep{ 19 resource.TestStep{ 20 Config: testAccCloudStackLoadBalancerRule_basic, 21 Check: resource.ComposeTestCheckFunc( 22 testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil), 23 resource.TestCheckResourceAttr( 24 "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"), 25 resource.TestCheckResourceAttr( 26 "cloudstack_loadbalancer_rule.foo", "algorithm", "roundrobin"), 27 resource.TestCheckResourceAttr( 28 "cloudstack_loadbalancer_rule.foo", "public_port", "80"), 29 resource.TestCheckResourceAttr( 30 "cloudstack_loadbalancer_rule.foo", "private_port", "80"), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccCloudStackLoadBalancerRule_update(t *testing.T) { 38 var id string 39 40 resource.Test(t, resource.TestCase{ 41 PreCheck: func() { testAccPreCheck(t) }, 42 Providers: testAccProviders, 43 CheckDestroy: testAccCheckCloudStackLoadBalancerRuleDestroy, 44 Steps: []resource.TestStep{ 45 resource.TestStep{ 46 Config: testAccCloudStackLoadBalancerRule_basic, 47 Check: resource.ComposeTestCheckFunc( 48 testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", &id), 49 resource.TestCheckResourceAttr( 50 "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"), 51 resource.TestCheckResourceAttr( 52 "cloudstack_loadbalancer_rule.foo", "algorithm", "roundrobin"), 53 resource.TestCheckResourceAttr( 54 "cloudstack_loadbalancer_rule.foo", "public_port", "80"), 55 resource.TestCheckResourceAttr( 56 "cloudstack_loadbalancer_rule.foo", "private_port", "80"), 57 ), 58 }, 59 60 resource.TestStep{ 61 Config: testAccCloudStackLoadBalancerRule_update, 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", &id), 64 resource.TestCheckResourceAttr( 65 "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"), 66 resource.TestCheckResourceAttr( 67 "cloudstack_loadbalancer_rule.foo", "algorithm", "leastconn"), 68 resource.TestCheckResourceAttr( 69 "cloudstack_loadbalancer_rule.foo", "public_port", "80"), 70 resource.TestCheckResourceAttr( 71 "cloudstack_loadbalancer_rule.foo", "private_port", "80"), 72 ), 73 }, 74 }, 75 }) 76 } 77 78 func TestAccCloudStackLoadBalancerRule_forcenew(t *testing.T) { 79 resource.Test(t, resource.TestCase{ 80 PreCheck: func() { testAccPreCheck(t) }, 81 Providers: testAccProviders, 82 CheckDestroy: testAccCheckCloudStackLoadBalancerRuleDestroy, 83 Steps: []resource.TestStep{ 84 resource.TestStep{ 85 Config: testAccCloudStackLoadBalancerRule_basic, 86 Check: resource.ComposeTestCheckFunc( 87 testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil), 88 resource.TestCheckResourceAttr( 89 "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"), 90 resource.TestCheckResourceAttr( 91 "cloudstack_loadbalancer_rule.foo", "algorithm", "roundrobin"), 92 resource.TestCheckResourceAttr( 93 "cloudstack_loadbalancer_rule.foo", "public_port", "80"), 94 resource.TestCheckResourceAttr( 95 "cloudstack_loadbalancer_rule.foo", "private_port", "80"), 96 ), 97 }, 98 99 resource.TestStep{ 100 Config: testAccCloudStackLoadBalancerRule_forcenew, 101 Check: resource.ComposeTestCheckFunc( 102 testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil), 103 resource.TestCheckResourceAttr( 104 "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"), 105 resource.TestCheckResourceAttr( 106 "cloudstack_loadbalancer_rule.foo", "algorithm", "leastconn"), 107 resource.TestCheckResourceAttr( 108 "cloudstack_loadbalancer_rule.foo", "public_port", "443"), 109 resource.TestCheckResourceAttr( 110 "cloudstack_loadbalancer_rule.foo", "private_port", "443"), 111 ), 112 }, 113 }, 114 }) 115 } 116 117 func TestAccCloudStackLoadBalancerRule_vpc(t *testing.T) { 118 resource.Test(t, resource.TestCase{ 119 PreCheck: func() { testAccPreCheck(t) }, 120 Providers: testAccProviders, 121 CheckDestroy: testAccCheckCloudStackLoadBalancerRuleDestroy, 122 Steps: []resource.TestStep{ 123 resource.TestStep{ 124 Config: testAccCloudStackLoadBalancerRule_vpc, 125 Check: resource.ComposeTestCheckFunc( 126 testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil), 127 resource.TestCheckResourceAttr( 128 "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"), 129 resource.TestCheckResourceAttr( 130 "cloudstack_loadbalancer_rule.foo", "algorithm", "roundrobin"), 131 resource.TestCheckResourceAttr( 132 "cloudstack_loadbalancer_rule.foo", "public_port", "80"), 133 resource.TestCheckResourceAttr( 134 "cloudstack_loadbalancer_rule.foo", "private_port", "80"), 135 ), 136 }, 137 }, 138 }) 139 } 140 141 func TestAccCloudStackLoadBalancerRule_vpc_update(t *testing.T) { 142 resource.Test(t, resource.TestCase{ 143 PreCheck: func() { testAccPreCheck(t) }, 144 Providers: testAccProviders, 145 CheckDestroy: testAccCheckCloudStackLoadBalancerRuleDestroy, 146 Steps: []resource.TestStep{ 147 resource.TestStep{ 148 Config: testAccCloudStackLoadBalancerRule_vpc, 149 Check: resource.ComposeTestCheckFunc( 150 testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil), 151 resource.TestCheckResourceAttr( 152 "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"), 153 resource.TestCheckResourceAttr( 154 "cloudstack_loadbalancer_rule.foo", "algorithm", "roundrobin"), 155 resource.TestCheckResourceAttr( 156 "cloudstack_loadbalancer_rule.foo", "public_port", "80"), 157 resource.TestCheckResourceAttr( 158 "cloudstack_loadbalancer_rule.foo", "private_port", "80"), 159 ), 160 }, 161 162 resource.TestStep{ 163 Config: testAccCloudStackLoadBalancerRule_vpc_update, 164 Check: resource.ComposeTestCheckFunc( 165 testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil), 166 resource.TestCheckResourceAttr( 167 "cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"), 168 resource.TestCheckResourceAttr( 169 "cloudstack_loadbalancer_rule.foo", "algorithm", "leastconn"), 170 resource.TestCheckResourceAttr( 171 "cloudstack_loadbalancer_rule.foo", "public_port", "443"), 172 resource.TestCheckResourceAttr( 173 "cloudstack_loadbalancer_rule.foo", "private_port", "443"), 174 ), 175 }, 176 }, 177 }) 178 } 179 180 func testAccCheckCloudStackLoadBalancerRuleExist(n string, id *string) resource.TestCheckFunc { 181 return func(s *terraform.State) error { 182 rs, ok := s.RootModule().Resources[n] 183 if !ok { 184 return fmt.Errorf("Not found: %s", n) 185 } 186 187 if rs.Primary.ID == "" { 188 return fmt.Errorf("No loadbalancer rule ID is set") 189 } 190 191 if id != nil { 192 if *id != "" && *id != rs.Primary.ID { 193 return fmt.Errorf("Resource ID has changed!") 194 } 195 196 *id = rs.Primary.ID 197 } 198 199 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 200 _, count, err := cs.LoadBalancer.GetLoadBalancerRuleByID(rs.Primary.ID) 201 202 if err != nil { 203 return err 204 } 205 206 if count == 0 { 207 return fmt.Errorf("Loadbalancer rule %s not found", n) 208 } 209 210 return nil 211 } 212 } 213 214 func testAccCheckCloudStackLoadBalancerRuleDestroy(s *terraform.State) error { 215 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 216 217 for _, rs := range s.RootModule().Resources { 218 if rs.Type != "cloudstack_loadbalancer_rule" { 219 continue 220 } 221 222 if rs.Primary.ID == "" { 223 return fmt.Errorf("No Loadbalancer rule ID is set") 224 } 225 226 for k, id := range rs.Primary.Attributes { 227 if !strings.Contains(k, "uuid") { 228 continue 229 } 230 231 _, _, err := cs.LoadBalancer.GetLoadBalancerRuleByID(id) 232 if err == nil { 233 return fmt.Errorf("Loadbalancer rule %s still exists", rs.Primary.ID) 234 } 235 } 236 } 237 238 return nil 239 } 240 241 var testAccCloudStackLoadBalancerRule_basic = fmt.Sprintf(` 242 resource "cloudstack_instance" "foobar1" { 243 name = "terraform-server1" 244 display_name = "terraform" 245 service_offering= "%s" 246 network = "%s" 247 template = "%s" 248 zone = "%s" 249 expunge = true 250 } 251 252 resource "cloudstack_loadbalancer_rule" "foo" { 253 name = "terraform-lb" 254 ipaddress = "%s" 255 # network omitted, inferred from IP 256 algorithm = "roundrobin" 257 public_port = 80 258 private_port = 80 259 members = ["${cloudstack_instance.foobar1.id}"] 260 } 261 262 # attempt to create dependent firewall rule 263 # this will clash if cloudstack creates the implicit rule as it does by default 264 resource "cloudstack_firewall" "foo" { 265 ipaddress = "${cloudstack_loadbalancer_rule.foo.ipaddress}" 266 rule { 267 source_cidr = "0.0.0.0/0" 268 protocol = "tcp" 269 ports = ["${cloudstack_loadbalancer_rule.foo.public_port}"] 270 } 271 } 272 `, 273 CLOUDSTACK_SERVICE_OFFERING_1, 274 CLOUDSTACK_NETWORK_1, 275 CLOUDSTACK_TEMPLATE, 276 CLOUDSTACK_ZONE, 277 CLOUDSTACK_PUBLIC_IPADDRESS) 278 279 var testAccCloudStackLoadBalancerRule_update = fmt.Sprintf(` 280 resource "cloudstack_instance" "foobar1" { 281 name = "terraform-server1" 282 display_name = "terraform" 283 service_offering= "%s" 284 network = "%s" 285 template = "%s" 286 zone = "%s" 287 expunge = true 288 } 289 290 resource "cloudstack_loadbalancer_rule" "foo" { 291 name = "terraform-lb-update" 292 ipaddress = "%s" 293 # network omitted, inferred from IP 294 algorithm = "leastconn" 295 public_port = 80 296 private_port = 80 297 members = ["${cloudstack_instance.foobar1.id}"] 298 } 299 300 # attempt to create dependent firewall rule 301 # this will clash if cloudstack creates the implicit rule as it does by default 302 resource "cloudstack_firewall" "foo" { 303 ipaddress = "${cloudstack_loadbalancer_rule.foo.ipaddress}" 304 rule { 305 source_cidr = "0.0.0.0/0" 306 protocol = "tcp" 307 ports = ["${cloudstack_loadbalancer_rule.foo.public_port}"] 308 } 309 } 310 `, 311 CLOUDSTACK_SERVICE_OFFERING_1, 312 CLOUDSTACK_NETWORK_1, 313 CLOUDSTACK_TEMPLATE, 314 CLOUDSTACK_ZONE, 315 CLOUDSTACK_PUBLIC_IPADDRESS) 316 317 var testAccCloudStackLoadBalancerRule_forcenew = fmt.Sprintf(` 318 resource "cloudstack_instance" "foobar1" { 319 name = "terraform-server1" 320 display_name = "terraform" 321 service_offering= "%s" 322 network = "%s" 323 template = "%s" 324 zone = "%s" 325 expunge = true 326 } 327 328 resource "cloudstack_loadbalancer_rule" "foo" { 329 name = "terraform-lb-update" 330 ipaddress = "%s" 331 # network omitted, inferred from IP 332 algorithm = "leastconn" 333 public_port = 443 334 private_port = 443 335 members = ["${cloudstack_instance.foobar1.id}"] 336 } 337 338 # attempt to create dependent firewall rule 339 # this will clash if cloudstack creates the implicit rule as it does by default 340 resource "cloudstack_firewall" "foo" { 341 ipaddress = "${cloudstack_loadbalancer_rule.foo.ipaddress}" 342 rule { 343 source_cidr = "0.0.0.0/0" 344 protocol = "tcp" 345 ports = ["${cloudstack_loadbalancer_rule.foo.public_port}"] 346 } 347 } 348 `, 349 CLOUDSTACK_SERVICE_OFFERING_1, 350 CLOUDSTACK_NETWORK_1, 351 CLOUDSTACK_TEMPLATE, 352 CLOUDSTACK_ZONE, 353 CLOUDSTACK_PUBLIC_IPADDRESS) 354 355 var testAccCloudStackLoadBalancerRule_vpc = fmt.Sprintf(` 356 resource "cloudstack_vpc" "foobar" { 357 name = "terraform-vpc" 358 cidr = "%s" 359 vpc_offering = "%s" 360 zone = "%s" 361 } 362 363 resource "cloudstack_network" "foo" { 364 name = "terraform-network" 365 cidr = "%s" 366 network_offering = "%s" 367 vpc = "${cloudstack_vpc.foobar.name}" 368 zone = "${cloudstack_vpc.foobar.zone}" 369 } 370 371 resource "cloudstack_ipaddress" "foo" { 372 vpc = "${cloudstack_vpc.foobar.name}" 373 } 374 375 resource "cloudstack_instance" "foobar1" { 376 name = "terraform-server1" 377 display_name = "terraform" 378 service_offering= "%s" 379 network = "${cloudstack_network.foo.name}" 380 template = "%s" 381 zone = "${cloudstack_network.foo.zone}" 382 user_data = "foobar\nfoo\nbar" 383 expunge = true 384 } 385 386 resource "cloudstack_loadbalancer_rule" "foo" { 387 name = "terraform-lb" 388 ipaddress = "${cloudstack_ipaddress.foo.ipaddress}" 389 algorithm = "roundrobin" 390 network = "${cloudstack_network.foo.id}" 391 public_port = 80 392 private_port = 80 393 members = ["${cloudstack_instance.foobar1.id}"] 394 }`, 395 CLOUDSTACK_VPC_CIDR_1, 396 CLOUDSTACK_VPC_OFFERING, 397 CLOUDSTACK_ZONE, 398 CLOUDSTACK_VPC_NETWORK_CIDR, 399 CLOUDSTACK_VPC_NETWORK_OFFERING, 400 CLOUDSTACK_SERVICE_OFFERING_1, 401 CLOUDSTACK_TEMPLATE) 402 403 var testAccCloudStackLoadBalancerRule_vpc_update = fmt.Sprintf(` 404 resource "cloudstack_vpc" "foobar" { 405 name = "terraform-vpc" 406 cidr = "%s" 407 vpc_offering = "%s" 408 zone = "%s" 409 } 410 411 resource "cloudstack_network" "foo" { 412 name = "terraform-network" 413 cidr = "%s" 414 network_offering = "%s" 415 vpc = "${cloudstack_vpc.foobar.name}" 416 zone = "${cloudstack_vpc.foobar.zone}" 417 } 418 419 resource "cloudstack_ipaddress" "foo" { 420 vpc = "${cloudstack_vpc.foobar.name}" 421 } 422 423 resource "cloudstack_instance" "foobar1" { 424 name = "terraform-server1" 425 display_name = "terraform" 426 service_offering= "%s" 427 network = "${cloudstack_network.foo.name}" 428 template = "%s" 429 zone = "${cloudstack_network.foo.zone}" 430 user_data = "foobar\nfoo\nbar" 431 expunge = true 432 } 433 434 resource "cloudstack_instance" "foobar2" { 435 name = "terraform-server2" 436 display_name = "terraform" 437 service_offering= "%s" 438 network = "${cloudstack_network.foo.name}" 439 template = "%s" 440 zone = "${cloudstack_network.foo.zone}" 441 user_data = "foobar\nfoo\nbar" 442 expunge = true 443 } 444 445 resource "cloudstack_loadbalancer_rule" "foo" { 446 name = "terraform-lb-update" 447 ipaddress = "${cloudstack_ipaddress.foo.ipaddress}" 448 algorithm = "leastconn" 449 network = "${cloudstack_network.foo.id}" 450 public_port = 443 451 private_port = 443 452 members = ["${cloudstack_instance.foobar2.id}", "${cloudstack_instance.foobar1.id}"] 453 }`, 454 CLOUDSTACK_VPC_CIDR_1, 455 CLOUDSTACK_VPC_OFFERING, 456 CLOUDSTACK_ZONE, 457 CLOUDSTACK_VPC_NETWORK_CIDR, 458 CLOUDSTACK_VPC_NETWORK_OFFERING, 459 CLOUDSTACK_SERVICE_OFFERING_1, 460 CLOUDSTACK_TEMPLATE, 461 CLOUDSTACK_SERVICE_OFFERING_1, 462 CLOUDSTACK_TEMPLATE)