Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/azurerm: allow updating load balancer sub-resources #10016

Merged
merged 4 commits into from
Nov 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions builtin/providers/azurerm/resource_arm_loadbalancer_nat_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,23 @@ func resourceArmLoadBalancerNatPoolCreate(d *schema.ResourceData, meta interface
return nil
}

_, _, exists = findLoadBalancerNatPoolByName(loadBalancer, d.Get("name").(string))
if exists {
return fmt.Errorf("A NAT Pool with name %q already exists.", d.Get("name").(string))
}

newNatPool, err := expandAzureRmLoadBalancerNatPool(d, loadBalancer)
if err != nil {
return errwrap.Wrapf("Error Expanding NAT Pool {{err}}", err)
}

natPools := append(*loadBalancer.Properties.InboundNatPools, *newNatPool)

existingNatPool, existingNatPoolIndex, exists := findLoadBalancerNatPoolByName(loadBalancer, d.Get("name").(string))
if exists {
if d.Id() == *existingNatPool.ID {
// this probe is being updated remove old copy from the slice
natPools = append(natPools[:existingNatPoolIndex], natPools[existingNatPoolIndex+1:]...)
} else {
return fmt.Errorf("A NAT Pool with name %q already exists.", d.Get("name").(string))
}
}

loadBalancer.Properties.InboundNatPools = &natPools
resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
if err != nil {
Expand Down
161 changes: 161 additions & 0 deletions builtin/providers/azurerm/resource_arm_loadbalancer_nat_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"testing"

"regexp"

"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -67,6 +69,61 @@ func TestAccAzureRMLoadBalancerNatPool_removal(t *testing.T) {
})
}

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"),
),
},
},
})
}

func TestAccAzureRMLoadBalancerNatPool_duplicate(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_multiplePools(ri, natPoolName, natPoolName),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
),
ExpectError: regexp.MustCompile(fmt.Sprintf("A NAT Pool with name %q already exists.", natPoolName)),
},
},
})
}

func testCheckAzureRMLoadBalancerNatPoolExists(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, _, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
Expand Down Expand Up @@ -155,3 +212,107 @@ resource "azurerm_lb" "test" {
}
`, rInt, rInt, rInt, rInt)
}

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)
}
16 changes: 11 additions & 5 deletions builtin/providers/azurerm/resource_arm_loadbalancer_nat_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,23 @@ func resourceArmLoadBalancerNatRuleCreate(d *schema.ResourceData, meta interface
return nil
}

_, _, exists = findLoadBalancerNatRuleByName(loadBalancer, d.Get("name").(string))
if exists {
return fmt.Errorf("A NAT Rule with name %q already exists.", d.Get("name").(string))
}

newNatRule, err := expandAzureRmLoadBalancerNatRule(d, loadBalancer)
if err != nil {
return errwrap.Wrapf("Error Expanding NAT Rule {{err}}", err)
}

natRules := append(*loadBalancer.Properties.InboundNatRules, *newNatRule)

existingNatRule, existingNatRuleIndex, exists := findLoadBalancerNatRuleByName(loadBalancer, d.Get("name").(string))
if exists {
if d.Id() == *existingNatRule.ID {
// this probe is being updated remove old copy from the slice
natRules = append(natRules[:existingNatRuleIndex], natRules[existingNatRuleIndex+1:]...)
} else {
return fmt.Errorf("A NAT Rule with name %q already exists.", d.Get("name").(string))
}
}

loadBalancer.Properties.InboundNatRules = &natRules
resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
if err != nil {
Expand Down
Loading