github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_loadbalancer_backend_address_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 TestAccAzureRMLoadBalancerBackEndAddressPool_basic(t *testing.T) { 15 var lb network.LoadBalancer 16 ri := acctest.RandInt() 17 addressPoolName := fmt.Sprintf("%d-address-pool", ri) 18 19 subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID") 20 backendAddressPool_id := fmt.Sprintf( 21 "/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/backendAddressPools/%s", 22 subscriptionID, ri, ri, addressPoolName) 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: testAccAzureRMLoadBalancerBackEndAddressPool_basic(ri, addressPoolName), 31 Check: resource.ComposeTestCheckFunc( 32 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 33 testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName, &lb), 34 resource.TestCheckResourceAttr( 35 "azurerm_lb_backend_address_pool.test", "id", backendAddressPool_id), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func TestAccAzureRMLoadBalancerBackEndAddressPool_removal(t *testing.T) { 43 var lb network.LoadBalancer 44 ri := acctest.RandInt() 45 addressPoolName := fmt.Sprintf("%d-address-pool", 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: testAccAzureRMLoadBalancerBackEndAddressPool_removal(ri), 54 Check: resource.ComposeTestCheckFunc( 55 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 56 testCheckAzureRMLoadBalancerBackEndAddressPoolNotExists(addressPoolName, &lb), 57 ), 58 }, 59 }, 60 }) 61 } 62 63 func TestAccAzureRMLoadBalancerBackEndAddressPool_reapply(t *testing.T) { 64 var lb network.LoadBalancer 65 ri := acctest.RandInt() 66 addressPoolName := fmt.Sprintf("%d-address-pool", ri) 67 68 deleteAddressPoolState := func(s *terraform.State) error { 69 return s.Remove("azurerm_lb_backend_address_pool.test") 70 } 71 72 resource.Test(t, resource.TestCase{ 73 PreCheck: func() { testAccPreCheck(t) }, 74 Providers: testAccProviders, 75 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 76 Steps: []resource.TestStep{ 77 { 78 Config: testAccAzureRMLoadBalancerBackEndAddressPool_basic(ri, addressPoolName), 79 Check: resource.ComposeTestCheckFunc( 80 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 81 testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName, &lb), 82 deleteAddressPoolState, 83 ), 84 ExpectNonEmptyPlan: true, 85 }, 86 { 87 Config: testAccAzureRMLoadBalancerBackEndAddressPool_basic(ri, addressPoolName), 88 Check: resource.ComposeTestCheckFunc( 89 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 90 testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName, &lb), 91 ), 92 }, 93 }, 94 }) 95 } 96 97 func TestAccAzureRMLoadBalancerBackEndAddressPool_disappears(t *testing.T) { 98 var lb network.LoadBalancer 99 ri := acctest.RandInt() 100 addressPoolName := fmt.Sprintf("%d-address-pool", ri) 101 102 resource.Test(t, resource.TestCase{ 103 PreCheck: func() { testAccPreCheck(t) }, 104 Providers: testAccProviders, 105 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 106 Steps: []resource.TestStep{ 107 { 108 Config: testAccAzureRMLoadBalancerBackEndAddressPool_basic(ri, addressPoolName), 109 Check: resource.ComposeTestCheckFunc( 110 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 111 testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName, &lb), 112 testCheckAzureRMLoadBalancerBackEndAddressPoolDisappears(addressPoolName, &lb), 113 ), 114 ExpectNonEmptyPlan: true, 115 }, 116 }, 117 }) 118 } 119 120 func testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc { 121 return func(s *terraform.State) error { 122 _, _, exists := findLoadBalancerBackEndAddressPoolByName(lb, addressPoolName) 123 if !exists { 124 return fmt.Errorf("A BackEnd Address Pool with name %q cannot be found.", addressPoolName) 125 } 126 127 return nil 128 } 129 } 130 131 func testCheckAzureRMLoadBalancerBackEndAddressPoolNotExists(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc { 132 return func(s *terraform.State) error { 133 _, _, exists := findLoadBalancerBackEndAddressPoolByName(lb, addressPoolName) 134 if exists { 135 return fmt.Errorf("A BackEnd Address Pool with name %q has been found.", addressPoolName) 136 } 137 138 return nil 139 } 140 } 141 142 func testCheckAzureRMLoadBalancerBackEndAddressPoolDisappears(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc { 143 return func(s *terraform.State) error { 144 conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient 145 146 _, i, exists := findLoadBalancerBackEndAddressPoolByName(lb, addressPoolName) 147 if !exists { 148 return fmt.Errorf("A BackEnd Address Pool with name %q cannot be found.", addressPoolName) 149 } 150 151 currentPools := *lb.LoadBalancerPropertiesFormat.BackendAddressPools 152 pools := append(currentPools[:i], currentPools[i+1:]...) 153 lb.LoadBalancerPropertiesFormat.BackendAddressPools = &pools 154 155 id, err := parseAzureResourceID(*lb.ID) 156 if err != nil { 157 return err 158 } 159 160 _, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{})) 161 if err != nil { 162 return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err) 163 } 164 165 _, err = conn.Get(id.ResourceGroup, *lb.Name, "") 166 return err 167 } 168 } 169 170 func testAccAzureRMLoadBalancerBackEndAddressPool_basic(rInt int, addressPoolName string) string { 171 return fmt.Sprintf(` 172 resource "azurerm_resource_group" "test" { 173 name = "acctestrg-%d" 174 location = "West US" 175 } 176 177 resource "azurerm_public_ip" "test" { 178 name = "test-ip-%d" 179 location = "West US" 180 resource_group_name = "${azurerm_resource_group.test.name}" 181 public_ip_address_allocation = "static" 182 } 183 184 resource "azurerm_lb" "test" { 185 name = "arm-test-loadbalancer-%d" 186 location = "West US" 187 resource_group_name = "${azurerm_resource_group.test.name}" 188 189 frontend_ip_configuration { 190 name = "one-%d" 191 public_ip_address_id = "${azurerm_public_ip.test.id}" 192 } 193 } 194 195 resource "azurerm_lb_backend_address_pool" "test" { 196 location = "West US" 197 resource_group_name = "${azurerm_resource_group.test.name}" 198 loadbalancer_id = "${azurerm_lb.test.id}" 199 name = "%s" 200 } 201 202 `, rInt, rInt, rInt, rInt, addressPoolName) 203 } 204 205 func testAccAzureRMLoadBalancerBackEndAddressPool_removal(rInt int) string { 206 return fmt.Sprintf(` 207 resource "azurerm_resource_group" "test" { 208 name = "acctestrg-%d" 209 location = "West US" 210 } 211 212 resource "azurerm_public_ip" "test" { 213 name = "test-ip-%d" 214 location = "West US" 215 resource_group_name = "${azurerm_resource_group.test.name}" 216 public_ip_address_allocation = "static" 217 } 218 219 resource "azurerm_lb" "test" { 220 name = "arm-test-loadbalancer-%d" 221 location = "West US" 222 resource_group_name = "${azurerm_resource_group.test.name}" 223 224 frontend_ip_configuration { 225 name = "one-%d" 226 public_ip_address_id = "${azurerm_public_ip.test.id}" 227 } 228 } 229 `, rInt, rInt, rInt, rInt) 230 }