2016-10-07 20:14:26 +02:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-10-17 17:16:26 +02:00
|
|
|
"os"
|
2016-10-07 20:14:26 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/network"
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAzureRMLoadBalancerNatPool_basic(t *testing.T) {
|
|
|
|
var lb network.LoadBalancer
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
natPoolName := fmt.Sprintf("NatPool-%d", ri)
|
|
|
|
|
2016-10-17 17:16:26 +02:00
|
|
|
subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
|
|
|
|
natPool_id := fmt.Sprintf(
|
|
|
|
"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/inboundNatPools/%s",
|
|
|
|
subscriptionID, ri, ri, natPoolName)
|
|
|
|
|
2016-10-07 20:14:26 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
|
|
testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
|
2016-10-17 17:16:26 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_lb_nat_pool.test", "id", natPool_id),
|
2016-10-07 20:14:26 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureRMLoadBalancerNatPool_removal(t *testing.T) {
|
|
|
|
var lb network.LoadBalancer
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
natPoolName := fmt.Sprintf("NatPool-%d", ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
|
|
testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: testAccAzureRMLoadBalancerNatPool_removal(ri),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
|
|
testCheckAzureRMLoadBalancerNatPoolNotExists(natPoolName, &lb),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:09:00 +01:00
|
|
|
func TestAccAzureRMLoadBalancerNatPool_update(t *testing.T) {
|
|
|
|
var lb network.LoadBalancer
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
natPoolName := fmt.Sprintf("NatPool-%d", ri)
|
|
|
|
natPool2Name := fmt.Sprintf("NatPool-%d", acctest.RandInt())
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAzureRMLoadBalancerNatPool_multiplePools(ri, natPoolName, natPool2Name),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
|
|
testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
|
|
|
|
testCheckAzureRMLoadBalancerNatPoolExists(natPool2Name, &lb),
|
|
|
|
resource.TestCheckResourceAttr("azurerm_lb_nat_pool.test2", "backend_port", "3390"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: testAccAzureRMLoadBalancerNatPool_multiplePoolsUpdate(ri, natPoolName, natPool2Name),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
|
|
testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
|
|
|
|
testCheckAzureRMLoadBalancerNatPoolExists(natPool2Name, &lb),
|
|
|
|
resource.TestCheckResourceAttr("azurerm_lb_nat_pool.test2", "backend_port", "3391"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-12 14:57:07 +01:00
|
|
|
func TestAccAzureRMLoadBalancerNatPool_reapply(t *testing.T) {
|
2016-11-11 12:09:00 +01:00
|
|
|
var lb network.LoadBalancer
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
natPoolName := fmt.Sprintf("NatPool-%d", ri)
|
|
|
|
|
2017-01-12 14:57:07 +01:00
|
|
|
deleteNatPoolState := func(s *terraform.State) error {
|
|
|
|
return s.Remove("azurerm_lb_nat_pool.test")
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:09:00 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
2017-01-12 14:57:07 +01:00
|
|
|
Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
|
|
testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
|
|
|
|
deleteNatPoolState,
|
|
|
|
),
|
|
|
|
ExpectNonEmptyPlan: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
|
2016-11-11 12:09:00 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
|
|
testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-31 16:55:17 +01:00
|
|
|
func TestAccAzureRMLoadBalancerNatPool_disappears(t *testing.T) {
|
|
|
|
var lb network.LoadBalancer
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
natPoolName := fmt.Sprintf("NatPool-%d", ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
|
|
|
|
testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
|
|
|
|
testCheckAzureRMLoadBalancerNatPoolDisappears(natPoolName, &lb),
|
|
|
|
),
|
|
|
|
ExpectNonEmptyPlan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-07 20:14:26 +02:00
|
|
|
func testCheckAzureRMLoadBalancerNatPoolExists(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
_, _, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
|
|
|
|
if !exists {
|
|
|
|
return fmt.Errorf("A NAT Pool with name %q cannot be found.", natPoolName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckAzureRMLoadBalancerNatPoolNotExists(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
_, _, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
|
|
|
|
if exists {
|
|
|
|
return fmt.Errorf("A NAT Pool with name %q has been found.", natPoolName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-31 16:55:17 +01:00
|
|
|
func testCheckAzureRMLoadBalancerNatPoolDisappears(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
|
|
|
|
|
|
|
|
_, i, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
|
|
|
|
if !exists {
|
|
|
|
return fmt.Errorf("A Nat Pool with name %q cannot be found.", natPoolName)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPools := *lb.LoadBalancerPropertiesFormat.InboundNatPools
|
|
|
|
pools := append(currentPools[:i], currentPools[i+1:]...)
|
|
|
|
lb.LoadBalancerPropertiesFormat.InboundNatPools = &pools
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(*lb.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Get(id.ResourceGroup, *lb.Name, "")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-07 20:14:26 +02:00
|
|
|
func testAccAzureRMLoadBalancerNatPool_basic(rInt int, natPoolName string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "azurerm_resource_group" "test" {
|
|
|
|
name = "acctestrg-%d"
|
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_public_ip" "test" {
|
|
|
|
name = "test-ip-%d"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
public_ip_address_allocation = "static"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_lb" "test" {
|
|
|
|
name = "arm-test-loadbalancer-%d"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
|
|
|
|
frontend_ip_configuration {
|
|
|
|
name = "one-%d"
|
|
|
|
public_ip_address_id = "${azurerm_public_ip.test.id}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_lb_nat_pool" "test" {
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
loadbalancer_id = "${azurerm_lb.test.id}"
|
|
|
|
name = "%s"
|
|
|
|
protocol = "Tcp"
|
|
|
|
frontend_port_start = 80
|
|
|
|
frontend_port_end = 81
|
|
|
|
backend_port = 3389
|
|
|
|
frontend_ip_configuration_name = "one-%d"
|
|
|
|
}
|
|
|
|
|
|
|
|
`, rInt, rInt, rInt, rInt, natPoolName, rInt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccAzureRMLoadBalancerNatPool_removal(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "azurerm_resource_group" "test" {
|
|
|
|
name = "acctestrg-%d"
|
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_public_ip" "test" {
|
|
|
|
name = "test-ip-%d"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
public_ip_address_allocation = "static"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_lb" "test" {
|
|
|
|
name = "arm-test-loadbalancer-%d"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
|
|
|
|
frontend_ip_configuration {
|
|
|
|
name = "one-%d"
|
|
|
|
public_ip_address_id = "${azurerm_public_ip.test.id}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, rInt, rInt, rInt, rInt)
|
|
|
|
}
|
2016-11-11 12:09:00 +01:00
|
|
|
|
|
|
|
func testAccAzureRMLoadBalancerNatPool_multiplePools(rInt int, natPoolName, natPool2Name string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "azurerm_resource_group" "test" {
|
|
|
|
name = "acctestrg-%d"
|
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_public_ip" "test" {
|
|
|
|
name = "test-ip-%d"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
public_ip_address_allocation = "static"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_lb" "test" {
|
|
|
|
name = "arm-test-loadbalancer-%d"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
|
|
|
|
frontend_ip_configuration {
|
|
|
|
name = "one-%d"
|
|
|
|
public_ip_address_id = "${azurerm_public_ip.test.id}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_lb_nat_pool" "test" {
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
loadbalancer_id = "${azurerm_lb.test.id}"
|
|
|
|
name = "%s"
|
|
|
|
protocol = "Tcp"
|
|
|
|
frontend_port_start = 80
|
|
|
|
frontend_port_end = 81
|
|
|
|
backend_port = 3389
|
|
|
|
frontend_ip_configuration_name = "one-%d"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_lb_nat_pool" "test2" {
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
loadbalancer_id = "${azurerm_lb.test.id}"
|
|
|
|
name = "%s"
|
|
|
|
protocol = "Tcp"
|
|
|
|
frontend_port_start = 82
|
|
|
|
frontend_port_end = 83
|
|
|
|
backend_port = 3390
|
|
|
|
frontend_ip_configuration_name = "one-%d"
|
|
|
|
}
|
|
|
|
|
|
|
|
`, rInt, rInt, rInt, rInt, natPoolName, rInt, natPool2Name, rInt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccAzureRMLoadBalancerNatPool_multiplePoolsUpdate(rInt int, natPoolName, natPool2Name string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "azurerm_resource_group" "test" {
|
|
|
|
name = "acctestrg-%d"
|
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_public_ip" "test" {
|
|
|
|
name = "test-ip-%d"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
public_ip_address_allocation = "static"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_lb" "test" {
|
|
|
|
name = "arm-test-loadbalancer-%d"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
|
|
|
|
frontend_ip_configuration {
|
|
|
|
name = "one-%d"
|
|
|
|
public_ip_address_id = "${azurerm_public_ip.test.id}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_lb_nat_pool" "test" {
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
loadbalancer_id = "${azurerm_lb.test.id}"
|
|
|
|
name = "%s"
|
|
|
|
protocol = "Tcp"
|
|
|
|
frontend_port_start = 80
|
|
|
|
frontend_port_end = 81
|
|
|
|
backend_port = 3389
|
|
|
|
frontend_ip_configuration_name = "one-%d"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_lb_nat_pool" "test2" {
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
loadbalancer_id = "${azurerm_lb.test.id}"
|
|
|
|
name = "%s"
|
|
|
|
protocol = "Tcp"
|
|
|
|
frontend_port_start = 82
|
|
|
|
frontend_port_end = 83
|
|
|
|
backend_port = 3391
|
|
|
|
frontend_ip_configuration_name = "one-%d"
|
|
|
|
}
|
|
|
|
|
|
|
|
`, rInt, rInt, rInt, rInt, natPoolName, rInt, natPool2Name, rInt)
|
|
|
|
}
|