github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azurerm/resource_arm_loadbalancer_nat_pool_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/Azure/azure-sdk-for-go/arm/network" 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAzureRMLoadBalancerNatPool_basic(t *testing.T) { 15 var lb network.LoadBalancer 16 ri := acctest.RandInt() 17 natPoolName := fmt.Sprintf("NatPool-%d", ri) 18 19 subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID") 20 natPool_id := fmt.Sprintf( 21 "/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/inboundNatPools/%s", 22 subscriptionID, ri, ri, natPoolName) 23 24 resource.Test(t, resource.TestCase{ 25 PreCheck: func() { testAccPreCheck(t) }, 26 Providers: testAccProviders, 27 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 28 Steps: []resource.TestStep{ 29 { 30 Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName), 31 Check: resource.ComposeTestCheckFunc( 32 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 33 testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb), 34 resource.TestCheckResourceAttr( 35 "azurerm_lb_nat_pool.test", "id", natPool_id), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func TestAccAzureRMLoadBalancerNatPool_removal(t *testing.T) { 43 var lb network.LoadBalancer 44 ri := acctest.RandInt() 45 natPoolName := fmt.Sprintf("NatPool-%d", ri) 46 47 resource.Test(t, resource.TestCase{ 48 PreCheck: func() { testAccPreCheck(t) }, 49 Providers: testAccProviders, 50 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 51 Steps: []resource.TestStep{ 52 { 53 Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName), 54 Check: resource.ComposeTestCheckFunc( 55 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 56 testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb), 57 ), 58 }, 59 { 60 Config: testAccAzureRMLoadBalancerNatPool_removal(ri), 61 Check: resource.ComposeTestCheckFunc( 62 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 63 testCheckAzureRMLoadBalancerNatPoolNotExists(natPoolName, &lb), 64 ), 65 }, 66 }, 67 }) 68 } 69 70 func TestAccAzureRMLoadBalancerNatPool_update(t *testing.T) { 71 var lb network.LoadBalancer 72 ri := acctest.RandInt() 73 natPoolName := fmt.Sprintf("NatPool-%d", ri) 74 natPool2Name := fmt.Sprintf("NatPool-%d", acctest.RandInt()) 75 76 resource.Test(t, resource.TestCase{ 77 PreCheck: func() { testAccPreCheck(t) }, 78 Providers: testAccProviders, 79 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 80 Steps: []resource.TestStep{ 81 { 82 Config: testAccAzureRMLoadBalancerNatPool_multiplePools(ri, natPoolName, natPool2Name), 83 Check: resource.ComposeTestCheckFunc( 84 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 85 testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb), 86 testCheckAzureRMLoadBalancerNatPoolExists(natPool2Name, &lb), 87 resource.TestCheckResourceAttr("azurerm_lb_nat_pool.test2", "backend_port", "3390"), 88 ), 89 }, 90 { 91 Config: testAccAzureRMLoadBalancerNatPool_multiplePoolsUpdate(ri, natPoolName, natPool2Name), 92 Check: resource.ComposeTestCheckFunc( 93 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 94 testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb), 95 testCheckAzureRMLoadBalancerNatPoolExists(natPool2Name, &lb), 96 resource.TestCheckResourceAttr("azurerm_lb_nat_pool.test2", "backend_port", "3391"), 97 ), 98 }, 99 }, 100 }) 101 } 102 103 func TestAccAzureRMLoadBalancerNatPool_reapply(t *testing.T) { 104 var lb network.LoadBalancer 105 ri := acctest.RandInt() 106 natPoolName := fmt.Sprintf("NatPool-%d", ri) 107 108 deleteNatPoolState := func(s *terraform.State) error { 109 return s.Remove("azurerm_lb_nat_pool.test") 110 } 111 112 resource.Test(t, resource.TestCase{ 113 PreCheck: func() { testAccPreCheck(t) }, 114 Providers: testAccProviders, 115 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 116 Steps: []resource.TestStep{ 117 { 118 Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName), 119 Check: resource.ComposeTestCheckFunc( 120 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 121 testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb), 122 deleteNatPoolState, 123 ), 124 ExpectNonEmptyPlan: true, 125 }, 126 { 127 Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName), 128 Check: resource.ComposeTestCheckFunc( 129 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 130 testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb), 131 ), 132 }, 133 }, 134 }) 135 } 136 137 func TestAccAzureRMLoadBalancerNatPool_disappears(t *testing.T) { 138 var lb network.LoadBalancer 139 ri := acctest.RandInt() 140 natPoolName := fmt.Sprintf("NatPool-%d", ri) 141 142 resource.Test(t, resource.TestCase{ 143 PreCheck: func() { testAccPreCheck(t) }, 144 Providers: testAccProviders, 145 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 146 Steps: []resource.TestStep{ 147 { 148 Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName), 149 Check: resource.ComposeTestCheckFunc( 150 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 151 testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb), 152 testCheckAzureRMLoadBalancerNatPoolDisappears(natPoolName, &lb), 153 ), 154 ExpectNonEmptyPlan: true, 155 }, 156 }, 157 }) 158 } 159 160 func testCheckAzureRMLoadBalancerNatPoolExists(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc { 161 return func(s *terraform.State) error { 162 _, _, exists := findLoadBalancerNatPoolByName(lb, natPoolName) 163 if !exists { 164 return fmt.Errorf("A NAT Pool with name %q cannot be found.", natPoolName) 165 } 166 167 return nil 168 } 169 } 170 171 func testCheckAzureRMLoadBalancerNatPoolNotExists(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc { 172 return func(s *terraform.State) error { 173 _, _, exists := findLoadBalancerNatPoolByName(lb, natPoolName) 174 if exists { 175 return fmt.Errorf("A NAT Pool with name %q has been found.", natPoolName) 176 } 177 178 return nil 179 } 180 } 181 182 func testCheckAzureRMLoadBalancerNatPoolDisappears(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc { 183 return func(s *terraform.State) error { 184 conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient 185 186 _, i, exists := findLoadBalancerNatPoolByName(lb, natPoolName) 187 if !exists { 188 return fmt.Errorf("A Nat Pool with name %q cannot be found.", natPoolName) 189 } 190 191 currentPools := *lb.LoadBalancerPropertiesFormat.InboundNatPools 192 pools := append(currentPools[:i], currentPools[i+1:]...) 193 lb.LoadBalancerPropertiesFormat.InboundNatPools = &pools 194 195 id, err := parseAzureResourceID(*lb.ID) 196 if err != nil { 197 return err 198 } 199 200 _, error := conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{})) 201 err = <-error 202 if err != nil { 203 return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err) 204 } 205 206 _, err = conn.Get(id.ResourceGroup, *lb.Name, "") 207 return err 208 } 209 } 210 211 func testAccAzureRMLoadBalancerNatPool_basic(rInt int, natPoolName string) string { 212 return fmt.Sprintf(` 213 resource "azurerm_resource_group" "test" { 214 name = "acctestrg-%d" 215 location = "West US" 216 } 217 218 resource "azurerm_public_ip" "test" { 219 name = "test-ip-%d" 220 location = "West US" 221 resource_group_name = "${azurerm_resource_group.test.name}" 222 public_ip_address_allocation = "static" 223 } 224 225 resource "azurerm_lb" "test" { 226 name = "arm-test-loadbalancer-%d" 227 location = "West US" 228 resource_group_name = "${azurerm_resource_group.test.name}" 229 230 frontend_ip_configuration { 231 name = "one-%d" 232 public_ip_address_id = "${azurerm_public_ip.test.id}" 233 } 234 } 235 236 resource "azurerm_lb_nat_pool" "test" { 237 location = "West US" 238 resource_group_name = "${azurerm_resource_group.test.name}" 239 loadbalancer_id = "${azurerm_lb.test.id}" 240 name = "%s" 241 protocol = "Tcp" 242 frontend_port_start = 80 243 frontend_port_end = 81 244 backend_port = 3389 245 frontend_ip_configuration_name = "one-%d" 246 } 247 248 `, rInt, rInt, rInt, rInt, natPoolName, rInt) 249 } 250 251 func testAccAzureRMLoadBalancerNatPool_removal(rInt int) string { 252 return fmt.Sprintf(` 253 resource "azurerm_resource_group" "test" { 254 name = "acctestrg-%d" 255 location = "West US" 256 } 257 258 resource "azurerm_public_ip" "test" { 259 name = "test-ip-%d" 260 location = "West US" 261 resource_group_name = "${azurerm_resource_group.test.name}" 262 public_ip_address_allocation = "static" 263 } 264 265 resource "azurerm_lb" "test" { 266 name = "arm-test-loadbalancer-%d" 267 location = "West US" 268 resource_group_name = "${azurerm_resource_group.test.name}" 269 270 frontend_ip_configuration { 271 name = "one-%d" 272 public_ip_address_id = "${azurerm_public_ip.test.id}" 273 } 274 } 275 `, rInt, rInt, rInt, rInt) 276 } 277 278 func testAccAzureRMLoadBalancerNatPool_multiplePools(rInt int, natPoolName, natPool2Name string) string { 279 return fmt.Sprintf(` 280 resource "azurerm_resource_group" "test" { 281 name = "acctestrg-%d" 282 location = "West US" 283 } 284 285 resource "azurerm_public_ip" "test" { 286 name = "test-ip-%d" 287 location = "West US" 288 resource_group_name = "${azurerm_resource_group.test.name}" 289 public_ip_address_allocation = "static" 290 } 291 292 resource "azurerm_lb" "test" { 293 name = "arm-test-loadbalancer-%d" 294 location = "West US" 295 resource_group_name = "${azurerm_resource_group.test.name}" 296 297 frontend_ip_configuration { 298 name = "one-%d" 299 public_ip_address_id = "${azurerm_public_ip.test.id}" 300 } 301 } 302 303 resource "azurerm_lb_nat_pool" "test" { 304 location = "West US" 305 resource_group_name = "${azurerm_resource_group.test.name}" 306 loadbalancer_id = "${azurerm_lb.test.id}" 307 name = "%s" 308 protocol = "Tcp" 309 frontend_port_start = 80 310 frontend_port_end = 81 311 backend_port = 3389 312 frontend_ip_configuration_name = "one-%d" 313 } 314 315 resource "azurerm_lb_nat_pool" "test2" { 316 location = "West US" 317 resource_group_name = "${azurerm_resource_group.test.name}" 318 loadbalancer_id = "${azurerm_lb.test.id}" 319 name = "%s" 320 protocol = "Tcp" 321 frontend_port_start = 82 322 frontend_port_end = 83 323 backend_port = 3390 324 frontend_ip_configuration_name = "one-%d" 325 } 326 327 `, rInt, rInt, rInt, rInt, natPoolName, rInt, natPool2Name, rInt) 328 } 329 330 func testAccAzureRMLoadBalancerNatPool_multiplePoolsUpdate(rInt int, natPoolName, natPool2Name string) string { 331 return fmt.Sprintf(` 332 resource "azurerm_resource_group" "test" { 333 name = "acctestrg-%d" 334 location = "West US" 335 } 336 337 resource "azurerm_public_ip" "test" { 338 name = "test-ip-%d" 339 location = "West US" 340 resource_group_name = "${azurerm_resource_group.test.name}" 341 public_ip_address_allocation = "static" 342 } 343 344 resource "azurerm_lb" "test" { 345 name = "arm-test-loadbalancer-%d" 346 location = "West US" 347 resource_group_name = "${azurerm_resource_group.test.name}" 348 349 frontend_ip_configuration { 350 name = "one-%d" 351 public_ip_address_id = "${azurerm_public_ip.test.id}" 352 } 353 } 354 355 resource "azurerm_lb_nat_pool" "test" { 356 location = "West US" 357 resource_group_name = "${azurerm_resource_group.test.name}" 358 loadbalancer_id = "${azurerm_lb.test.id}" 359 name = "%s" 360 protocol = "Tcp" 361 frontend_port_start = 80 362 frontend_port_end = 81 363 backend_port = 3389 364 frontend_ip_configuration_name = "one-%d" 365 } 366 367 resource "azurerm_lb_nat_pool" "test2" { 368 location = "West US" 369 resource_group_name = "${azurerm_resource_group.test.name}" 370 loadbalancer_id = "${azurerm_lb.test.id}" 371 name = "%s" 372 protocol = "Tcp" 373 frontend_port_start = 82 374 frontend_port_end = 83 375 backend_port = 3391 376 frontend_ip_configuration_name = "one-%d" 377 } 378 379 `, rInt, rInt, rInt, rInt, natPoolName, rInt, natPool2Name, rInt) 380 }