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

* provider/azurerm: allow updating of lb_rule resource

* check if rule is being updated rather than assuming created
* added test to cover guard against multiple rules with the same name

TF_ACC=1 go test ./builtin/providers/azurerm -v -run "TestAccAzureRMLoadBalancerRule_" -timeout 120m
=== RUN   TestAccAzureRMLoadBalancerRule_basic
--- PASS: TestAccAzureRMLoadBalancerRule_basic (157.45s)
=== RUN   TestAccAzureRMLoadBalancerRule_removal
--- PASS: TestAccAzureRMLoadBalancerRule_removal (163.67s)
=== RUN   TestAccAzureRMLoadBalancerRule_inconsistentReads
--- PASS: TestAccAzureRMLoadBalancerRule_inconsistentReads (150.00s)
=== RUN   TestAccAzureRMLoadBalancerRule_update
--- PASS: TestAccAzureRMLoadBalancerRule_update (164.20s)
=== RUN   TestAccAzureRMLoadBalancerRule_duplicateRules
--- PASS: TestAccAzureRMLoadBalancerRule_duplicateRules (137.51s)
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/azurerm	772.846s

* provider/azurerm: allow updating of lb_nat_rule resource

* check if NAT rule is being updated rather than assuming created
* added test to cover guard against multiple NAT rules with the same name

TF_ACC=1 go test ./builtin/providers/azurerm -v -run "TestAccAzureRMLoadBalancerNatRule" -timeout 120m
=== RUN   TestAccAzureRMLoadBalancerNatRule_basic
--- PASS: TestAccAzureRMLoadBalancerNatRule_basic (148.48s)
=== RUN   TestAccAzureRMLoadBalancerNatRule_removal
--- PASS: TestAccAzureRMLoadBalancerNatRule_removal (163.48s)
=== RUN   TestAccAzureRMLoadBalancerNatRule_update
--- PASS: TestAccAzureRMLoadBalancerNatRule_update (176.97s)
=== RUN   TestAccAzureRMLoadBalancerNatRule_duplicate
--- PASS: TestAccAzureRMLoadBalancerNatRule_duplicate (136.36s)
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/azurerm	625.301s

* provider/azurerm: allow updating of lb_probe resource

* check if probe is being updated rather than assuming created
* added test to cover guard against multiple probes with the same name

TF_ACC=1 go test ./builtin/providers/azurerm -v -run "TestAccAzureRMLoadBalancerProbe" -timeout 120m
=== RUN   TestAccAzureRMLoadBalancerProbe_basic
--- PASS: TestAccAzureRMLoadBalancerProbe_basic (134.53s)
=== RUN   TestAccAzureRMLoadBalancerProbe_removal
--- PASS: TestAccAzureRMLoadBalancerProbe_removal (168.06s)
=== RUN   TestAccAzureRMLoadBalancerProbe_update
--- PASS: TestAccAzureRMLoadBalancerProbe_update (175.99s)
=== RUN   TestAccAzureRMLoadBalancerProbe_duplicate
--- PASS: TestAccAzureRMLoadBalancerProbe_duplicate (139.01s)
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/azurerm	617.598s

* provider/azurerm: allow updating of lb_nat_pool resource

* check if NAT pool is being updated rather than assuming created
* added test to cover guard against multiple NAT pools with the same name

TF_ACC=1 go test ./builtin/providers/azurerm -v -run "TestAccAzureRMLoadBalancerNatPool" -timeout 120m
=== RUN   TestAccAzureRMLoadBalancerNatPool_basic
--- PASS: TestAccAzureRMLoadBalancerNatPool_basic (146.52s)
=== RUN   TestAccAzureRMLoadBalancerNatPool_removal
--- PASS: TestAccAzureRMLoadBalancerNatPool_removal (158.34s)
=== RUN   TestAccAzureRMLoadBalancerNatPool_update
--- PASS: TestAccAzureRMLoadBalancerNatPool_update (174.05s)
=== RUN   TestAccAzureRMLoadBalancerNatPool_duplicate
--- PASS: TestAccAzureRMLoadBalancerNatPool_duplicate (136.94s)
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/azurerm	615.866s
This commit is contained in:
Peter McAtominey 2016-11-11 11:09:00 +00:00 committed by Paul Stack
parent d779c71a65
commit 4996f3e289
8 changed files with 685 additions and 20 deletions

View File

@ -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 {

View File

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

View File

@ -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 {

View File

@ -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"
@ -67,6 +69,63 @@ func TestAccAzureRMLoadBalancerNatRule_removal(t *testing.T) {
})
}
func TestAccAzureRMLoadBalancerNatRule_update(t *testing.T) {
var lb network.LoadBalancer
ri := acctest.RandInt()
natRuleName := fmt.Sprintf("NatRule-%d", ri)
natRule2Name := fmt.Sprintf("NatRule-%d", acctest.RandInt())
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLoadBalancerNatRule_multipleRules(ri, natRuleName, natRule2Name),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
testCheckAzureRMLoadBalancerNatRuleExists(natRule2Name, &lb),
resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "frontend_port", "3390"),
resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "backend_port", "3390"),
),
},
{
Config: testAccAzureRMLoadBalancerNatRule_multipleRulesUpdate(ri, natRuleName, natRule2Name),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
testCheckAzureRMLoadBalancerNatRuleExists(natRule2Name, &lb),
resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "frontend_port", "3391"),
resource.TestCheckResourceAttr("azurerm_lb_nat_rule.test2", "backend_port", "3391"),
),
},
},
})
}
func TestAccAzureRMLoadBalancerNatRule_duplicate(t *testing.T) {
var lb network.LoadBalancer
ri := acctest.RandInt()
natRuleName := fmt.Sprintf("NatRule-%d", ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLoadBalancerNatRule_multipleRules(ri, natRuleName, natRuleName),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
),
ExpectError: regexp.MustCompile(fmt.Sprintf("A NAT Rule with name %q already exists.", natRuleName)),
},
},
})
}
func testCheckAzureRMLoadBalancerNatRuleExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName)
@ -154,3 +213,103 @@ resource "azurerm_lb" "test" {
}
`, rInt, rInt, rInt, rInt)
}
func testAccAzureRMLoadBalancerNatRule_multipleRules(rInt int, natRuleName, natRule2Name 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_rule" "test" {
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
loadbalancer_id = "${azurerm_lb.test.id}"
name = "%s"
protocol = "Tcp"
frontend_port = 3389
backend_port = 3389
frontend_ip_configuration_name = "one-%d"
}
resource "azurerm_lb_nat_rule" "test2" {
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
loadbalancer_id = "${azurerm_lb.test.id}"
name = "%s"
protocol = "Tcp"
frontend_port = 3390
backend_port = 3390
frontend_ip_configuration_name = "one-%d"
}
`, rInt, rInt, rInt, rInt, natRuleName, rInt, natRule2Name, rInt)
}
func testAccAzureRMLoadBalancerNatRule_multipleRulesUpdate(rInt int, natRuleName, natRule2Name 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_rule" "test" {
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
loadbalancer_id = "${azurerm_lb.test.id}"
name = "%s"
protocol = "Tcp"
frontend_port = 3389
backend_port = 3389
frontend_ip_configuration_name = "one-%d"
}
resource "azurerm_lb_nat_rule" "test2" {
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
loadbalancer_id = "${azurerm_lb.test.id}"
name = "%s"
protocol = "Tcp"
frontend_port = 3391
backend_port = 3391
frontend_ip_configuration_name = "one-%d"
}
`, rInt, rInt, rInt, rInt, natRuleName, rInt, natRule2Name, rInt)
}

View File

@ -102,17 +102,23 @@ func resourceArmLoadBalancerProbeCreate(d *schema.ResourceData, meta interface{}
return nil
}
_, _, exists = findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string))
if exists {
return fmt.Errorf("A Probe with name %q already exists.", d.Get("name").(string))
}
newProbe, err := expandAzureRmLoadBalancerProbe(d, loadBalancer)
if err != nil {
return errwrap.Wrapf("Error Expanding Probe {{err}}", err)
}
probes := append(*loadBalancer.Properties.Probes, *newProbe)
existingProbe, existingProbeIndex, exists := findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string))
if exists {
if d.Id() == *existingProbe.ID {
// this probe is being updated remove old copy from the slice
probes = append(probes[:existingProbeIndex], probes[existingProbeIndex+1:]...)
} else {
return fmt.Errorf("A Probe with name %q already exists.", d.Get("name").(string))
}
}
loadBalancer.Properties.Probes = &probes
resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
if err != nil {

View File

@ -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"
@ -67,6 +69,61 @@ func TestAccAzureRMLoadBalancerProbe_removal(t *testing.T) {
})
}
func TestAccAzureRMLoadBalancerProbe_update(t *testing.T) {
var lb network.LoadBalancer
ri := acctest.RandInt()
probeName := fmt.Sprintf("probe-%d", ri)
probe2Name := fmt.Sprintf("probe-%d", acctest.RandInt())
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLoadBalancerProbe_multipleProbes(ri, probeName, probe2Name),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
testCheckAzureRMLoadBalancerProbeExists(probe2Name, &lb),
resource.TestCheckResourceAttr("azurerm_lb_probe.test2", "port", "80"),
),
},
{
Config: testAccAzureRMLoadBalancerProbe_multipleProbesUpdate(ri, probeName, probe2Name),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
testCheckAzureRMLoadBalancerProbeExists(probe2Name, &lb),
resource.TestCheckResourceAttr("azurerm_lb_probe.test2", "port", "8080"),
),
},
},
})
}
func TestAccAzureRMLoadBalancerProbe_duplicate(t *testing.T) {
var lb network.LoadBalancer
ri := acctest.RandInt()
probeName := fmt.Sprintf("probe-%d", ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLoadBalancerProbe_multipleProbes(ri, probeName, probeName),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
),
ExpectError: regexp.MustCompile(fmt.Sprintf("A Probe with name %q already exists.", probeName)),
},
},
})
}
func testCheckAzureRMLoadBalancerProbeExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, _, exists := findLoadBalancerProbeByName(lb, natRuleName)
@ -150,3 +207,89 @@ resource "azurerm_lb" "test" {
}
`, rInt, rInt, rInt, rInt)
}
func testAccAzureRMLoadBalancerProbe_multipleProbes(rInt int, probeName, probe2Name 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_probe" "test" {
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
loadbalancer_id = "${azurerm_lb.test.id}"
name = "%s"
port = 22
}
resource "azurerm_lb_probe" "test2" {
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
loadbalancer_id = "${azurerm_lb.test.id}"
name = "%s"
port = 80
}
`, rInt, rInt, rInt, rInt, probeName, probe2Name)
}
func testAccAzureRMLoadBalancerProbe_multipleProbesUpdate(rInt int, probeName, probe2Name 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_probe" "test" {
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
loadbalancer_id = "${azurerm_lb.test.id}"
name = "%s"
port = 22
}
resource "azurerm_lb_probe" "test2" {
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
loadbalancer_id = "${azurerm_lb.test.id}"
name = "%s"
port = 8080
}
`, rInt, rInt, rInt, rInt, probeName, probe2Name)
}

View File

@ -123,17 +123,23 @@ func resourceArmLoadBalancerRuleCreate(d *schema.ResourceData, meta interface{})
return nil
}
_, _, exists = findLoadBalancerRuleByName(loadBalancer, d.Get("name").(string))
if exists {
return fmt.Errorf("A LoadBalancer Rule with name %q already exists.", d.Get("name").(string))
}
newLbRule, err := expandAzureRmLoadBalancerRule(d, loadBalancer)
if err != nil {
return errwrap.Wrapf("Error Exanding LoadBalancer Rule {{err}}", err)
}
lbRules := append(*loadBalancer.Properties.LoadBalancingRules, *newLbRule)
existingRule, existingRuleIndex, exists := findLoadBalancerRuleByName(loadBalancer, d.Get("name").(string))
if exists {
if d.Id() == *existingRule.ID {
// this rule is being updated remove old copy from the slice
lbRules = append(lbRules[:existingRuleIndex], lbRules[existingRuleIndex+1:]...)
} else {
return fmt.Errorf("A LoadBalancer Rule with name %q already exists.", d.Get("name").(string))
}
}
loadBalancer.Properties.LoadBalancingRules = &lbRules
resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
if err != nil {

View File

@ -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"
@ -149,6 +151,82 @@ func TestAccAzureRMLoadBalancerRule_inconsistentReads(t *testing.T) {
})
}
func TestAccAzureRMLoadBalancerRule_update(t *testing.T) {
var lb network.LoadBalancer
ri := acctest.RandInt()
lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
lbRule2Name := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
lbRuleID := fmt.Sprintf(
"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/loadBalancingRules/%s",
subscriptionID, ri, ri, lbRuleName)
lbRule2ID := fmt.Sprintf(
"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/loadBalancingRules/%s",
subscriptionID, ri, ri, lbRule2Name)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLoadBalancerRule_multipleRules(ri, lbRuleName, lbRule2Name),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
testCheckAzureRMLoadBalancerRuleExists(lbRule2Name, &lb),
resource.TestCheckResourceAttr("azurerm_lb_rule.test", "id", lbRuleID),
resource.TestCheckResourceAttr("azurerm_lb_rule.test2", "id", lbRule2ID),
resource.TestCheckResourceAttr("azurerm_lb_rule.test2", "frontend_port", "3390"),
resource.TestCheckResourceAttr("azurerm_lb_rule.test2", "backend_port", "3390"),
),
},
{
Config: testAccAzureRMLoadBalancerRule_multipleRulesUpdate(ri, lbRuleName, lbRule2Name),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
testCheckAzureRMLoadBalancerRuleExists(lbRule2Name, &lb),
resource.TestCheckResourceAttr("azurerm_lb_rule.test", "id", lbRuleID),
resource.TestCheckResourceAttr("azurerm_lb_rule.test2", "id", lbRule2ID),
resource.TestCheckResourceAttr("azurerm_lb_rule.test2", "frontend_port", "3391"),
resource.TestCheckResourceAttr("azurerm_lb_rule.test2", "backend_port", "3391"),
),
},
},
})
}
func TestAccAzureRMLoadBalancerRule_duplicateRules(t *testing.T) {
var lb network.LoadBalancer
ri := acctest.RandInt()
lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
lbRuleID := fmt.Sprintf(
"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/loadBalancingRules/%s",
subscriptionID, ri, ri, lbRuleName)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMLoadBalancerRule_multipleRules(ri, lbRuleName, lbRuleName),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
resource.TestCheckResourceAttr("azurerm_lb_rule.test", "id", lbRuleID),
),
ExpectError: regexp.MustCompile(fmt.Sprintf("A LoadBalancer Rule with name %q already exists.", lbRuleName)),
},
},
})
}
func testCheckAzureRMLoadBalancerRuleExists(lbRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, _, exists := findLoadBalancerRuleByName(lb, lbRuleName)
@ -291,3 +369,103 @@ resource "azurerm_lb_rule" "test" {
}
`, rInt, rInt, rInt, rInt, backendPoolName, probeName, lbRuleName, rInt)
}
func testAccAzureRMLoadBalancerRule_multipleRules(rInt int, lbRuleName, lbRule2Name 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_rule" "test" {
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
loadbalancer_id = "${azurerm_lb.test.id}"
name = "%s"
protocol = "Udp"
frontend_port = 3389
backend_port = 3389
frontend_ip_configuration_name = "one-%d"
}
resource "azurerm_lb_rule" "test2" {
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
loadbalancer_id = "${azurerm_lb.test.id}"
name = "%s"
protocol = "Udp"
frontend_port = 3390
backend_port = 3390
frontend_ip_configuration_name = "one-%d"
}
`, rInt, rInt, rInt, rInt, lbRuleName, rInt, lbRule2Name, rInt)
}
func testAccAzureRMLoadBalancerRule_multipleRulesUpdate(rInt int, lbRuleName, lbRule2Name 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_rule" "test" {
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
loadbalancer_id = "${azurerm_lb.test.id}"
name = "%s"
protocol = "Udp"
frontend_port = 3389
backend_port = 3389
frontend_ip_configuration_name = "one-%d"
}
resource "azurerm_lb_rule" "test2" {
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
loadbalancer_id = "${azurerm_lb.test.id}"
name = "%s"
protocol = "Udp"
frontend_port = 3391
backend_port = 3391
frontend_ip_configuration_name = "one-%d"
}
`, rInt, rInt, rInt, rInt, lbRuleName, rInt, lbRule2Name, rInt)
}