github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_loadbalancer_nat_rule_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 TestAccAzureRMLoadBalancerNatRule_basic(t *testing.T) { 15 var lb network.LoadBalancer 16 ri := acctest.RandInt() 17 natRuleName := fmt.Sprintf("NatRule-%d", ri) 18 19 subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID") 20 natRule_id := fmt.Sprintf( 21 "/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/inboundNatRules/%s", 22 subscriptionID, ri, ri, natRuleName) 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: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName), 31 Check: resource.ComposeTestCheckFunc( 32 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 33 testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb), 34 resource.TestCheckResourceAttr( 35 "azurerm_lb_nat_rule.test", "id", natRule_id), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func TestAccAzureRMLoadBalancerNatRule_removal(t *testing.T) { 43 var lb network.LoadBalancer 44 ri := acctest.RandInt() 45 natRuleName := fmt.Sprintf("NatRule-%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: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName), 54 Check: resource.ComposeTestCheckFunc( 55 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 56 testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb), 57 ), 58 }, 59 { 60 Config: testAccAzureRMLoadBalancerNatRule_removal(ri), 61 Check: resource.ComposeTestCheckFunc( 62 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 63 testCheckAzureRMLoadBalancerNatRuleNotExists(natRuleName, &lb), 64 ), 65 }, 66 }, 67 }) 68 } 69 70 func TestAccAzureRMLoadBalancerNatRule_update(t *testing.T) { 71 var lb network.LoadBalancer 72 ri := acctest.RandInt() 73 natRuleName := fmt.Sprintf("NatRule-%d", ri) 74 natRule2Name := fmt.Sprintf("NatRule-%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: testAccAzureRMLoadBalancerNatRule_multipleRules(ri, natRuleName, natRule2Name), 83 Check: resource.ComposeTestCheckFunc( 84 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 85 testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb), 86 testCheckAzureRMLoadBalancerNatRuleExists(natRule2Name, &lb), 87 resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "frontend_port", "3390"), 88 resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "backend_port", "3390"), 89 ), 90 }, 91 { 92 Config: testAccAzureRMLoadBalancerNatRule_multipleRulesUpdate(ri, natRuleName, natRule2Name), 93 Check: resource.ComposeTestCheckFunc( 94 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 95 testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb), 96 testCheckAzureRMLoadBalancerNatRuleExists(natRule2Name, &lb), 97 resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "frontend_port", "3391"), 98 resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "backend_port", "3391"), 99 ), 100 }, 101 }, 102 }) 103 } 104 105 func TestAccAzureRMLoadBalancerNatRule_reapply(t *testing.T) { 106 var lb network.LoadBalancer 107 ri := acctest.RandInt() 108 natRuleName := fmt.Sprintf("NatRule-%d", ri) 109 110 deleteNatRuleState := func(s *terraform.State) error { 111 return s.Remove("azurerm_lb_nat_rule.test") 112 } 113 114 resource.Test(t, resource.TestCase{ 115 PreCheck: func() { testAccPreCheck(t) }, 116 Providers: testAccProviders, 117 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 118 Steps: []resource.TestStep{ 119 { 120 Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName), 121 Check: resource.ComposeTestCheckFunc( 122 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 123 testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb), 124 deleteNatRuleState, 125 ), 126 ExpectNonEmptyPlan: true, 127 }, 128 { 129 Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName), 130 Check: resource.ComposeTestCheckFunc( 131 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 132 testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb), 133 ), 134 }, 135 }, 136 }) 137 } 138 139 func TestAccAzureRMLoadBalancerNatRule_disappears(t *testing.T) { 140 var lb network.LoadBalancer 141 ri := acctest.RandInt() 142 natRuleName := fmt.Sprintf("NatRule-%d", ri) 143 144 resource.Test(t, resource.TestCase{ 145 PreCheck: func() { testAccPreCheck(t) }, 146 Providers: testAccProviders, 147 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 148 Steps: []resource.TestStep{ 149 { 150 Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName), 151 Check: resource.ComposeTestCheckFunc( 152 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 153 testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb), 154 testCheckAzureRMLoadBalancerNatRuleDisappears(natRuleName, &lb), 155 ), 156 ExpectNonEmptyPlan: true, 157 }, 158 }, 159 }) 160 } 161 162 func testCheckAzureRMLoadBalancerNatRuleExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc { 163 return func(s *terraform.State) error { 164 _, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName) 165 if !exists { 166 return fmt.Errorf("A NAT Rule with name %q cannot be found.", natRuleName) 167 } 168 169 return nil 170 } 171 } 172 173 func testCheckAzureRMLoadBalancerNatRuleNotExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc { 174 return func(s *terraform.State) error { 175 _, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName) 176 if exists { 177 return fmt.Errorf("A NAT Rule with name %q has been found.", natRuleName) 178 } 179 180 return nil 181 } 182 } 183 184 func testCheckAzureRMLoadBalancerNatRuleDisappears(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc { 185 return func(s *terraform.State) error { 186 conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient 187 188 _, i, exists := findLoadBalancerNatRuleByName(lb, natRuleName) 189 if !exists { 190 return fmt.Errorf("A Nat Rule with name %q cannot be found.", natRuleName) 191 } 192 193 currentRules := *lb.LoadBalancerPropertiesFormat.InboundNatRules 194 rules := append(currentRules[:i], currentRules[i+1:]...) 195 lb.LoadBalancerPropertiesFormat.InboundNatRules = &rules 196 197 id, err := parseAzureResourceID(*lb.ID) 198 if err != nil { 199 return err 200 } 201 202 _, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{})) 203 if err != nil { 204 return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err) 205 } 206 207 _, err = conn.Get(id.ResourceGroup, *lb.Name, "") 208 return err 209 } 210 } 211 212 func testAccAzureRMLoadBalancerNatRule_basic(rInt int, natRuleName string) string { 213 return fmt.Sprintf(` 214 resource "azurerm_resource_group" "test" { 215 name = "acctestrg-%d" 216 location = "West US" 217 } 218 219 resource "azurerm_public_ip" "test" { 220 name = "test-ip-%d" 221 location = "West US" 222 resource_group_name = "${azurerm_resource_group.test.name}" 223 public_ip_address_allocation = "static" 224 } 225 226 resource "azurerm_lb" "test" { 227 name = "arm-test-loadbalancer-%d" 228 location = "West US" 229 resource_group_name = "${azurerm_resource_group.test.name}" 230 231 frontend_ip_configuration { 232 name = "one-%d" 233 public_ip_address_id = "${azurerm_public_ip.test.id}" 234 } 235 } 236 237 resource "azurerm_lb_nat_rule" "test" { 238 location = "West US" 239 resource_group_name = "${azurerm_resource_group.test.name}" 240 loadbalancer_id = "${azurerm_lb.test.id}" 241 name = "%s" 242 protocol = "Tcp" 243 frontend_port = 3389 244 backend_port = 3389 245 frontend_ip_configuration_name = "one-%d" 246 } 247 248 `, rInt, rInt, rInt, rInt, natRuleName, rInt) 249 } 250 251 func testAccAzureRMLoadBalancerNatRule_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 testAccAzureRMLoadBalancerNatRule_multipleRules(rInt int, natRuleName, natRule2Name 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_rule" "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 = 3389 310 backend_port = 3389 311 frontend_ip_configuration_name = "one-%d" 312 } 313 314 resource "azurerm_lb_nat_rule" "test2" { 315 location = "West US" 316 resource_group_name = "${azurerm_resource_group.test.name}" 317 loadbalancer_id = "${azurerm_lb.test.id}" 318 name = "%s" 319 protocol = "Tcp" 320 frontend_port = 3390 321 backend_port = 3390 322 frontend_ip_configuration_name = "one-%d" 323 } 324 325 `, rInt, rInt, rInt, rInt, natRuleName, rInt, natRule2Name, rInt) 326 } 327 328 func testAccAzureRMLoadBalancerNatRule_multipleRulesUpdate(rInt int, natRuleName, natRule2Name string) string { 329 return fmt.Sprintf(` 330 resource "azurerm_resource_group" "test" { 331 name = "acctestrg-%d" 332 location = "West US" 333 } 334 335 resource "azurerm_public_ip" "test" { 336 name = "test-ip-%d" 337 location = "West US" 338 resource_group_name = "${azurerm_resource_group.test.name}" 339 public_ip_address_allocation = "static" 340 } 341 342 resource "azurerm_lb" "test" { 343 name = "arm-test-loadbalancer-%d" 344 location = "West US" 345 resource_group_name = "${azurerm_resource_group.test.name}" 346 347 frontend_ip_configuration { 348 name = "one-%d" 349 public_ip_address_id = "${azurerm_public_ip.test.id}" 350 } 351 } 352 353 resource "azurerm_lb_nat_rule" "test" { 354 location = "West US" 355 resource_group_name = "${azurerm_resource_group.test.name}" 356 loadbalancer_id = "${azurerm_lb.test.id}" 357 name = "%s" 358 protocol = "Tcp" 359 frontend_port = 3389 360 backend_port = 3389 361 frontend_ip_configuration_name = "one-%d" 362 } 363 364 resource "azurerm_lb_nat_rule" "test2" { 365 location = "West US" 366 resource_group_name = "${azurerm_resource_group.test.name}" 367 loadbalancer_id = "${azurerm_lb.test.id}" 368 name = "%s" 369 protocol = "Tcp" 370 frontend_port = 3391 371 backend_port = 3391 372 frontend_ip_configuration_name = "one-%d" 373 } 374 375 `, rInt, rInt, rInt, rInt, natRuleName, rInt, natRule2Name, rInt) 376 }