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 Fix multiple loadbalancer resource IDs #9401

Merged
merged 10 commits into from
Oct 18, 2016
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,18 @@ func resourceArmLoadBalancerBackendAddressPoolCreate(d *schema.ResourceData, met
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
}

d.SetId(*read.ID)
var pool_id string
for _, BackendAddressPool := range *(*read.Properties).BackendAddressPools {
if *BackendAddressPool.Name == d.Get("name").(string) {
pool_id = *BackendAddressPool.ID
}
}

if pool_id != "" {
d.SetId(pool_id)
} else {
return fmt.Errorf("Error can not find created loadbalacner backend address pool id %s", pool_id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loadbalacner => LoadBalancer

}

log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName)
stateConf := &resource.StateChangeConf{
Expand All @@ -117,7 +128,7 @@ func resourceArmLoadBalancerBackendAddressPoolCreate(d *schema.ResourceData, met
}

func resourceArmLoadBalancerBackendAddressPoolRead(d *schema.ResourceData, meta interface{}) error {
loadBalancer, exists, err := retrieveLoadBalancerById(d.Id(), meta)
loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
if err != nil {
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurerm

import (
"fmt"
"os"
"testing"

"github.com/Azure/azure-sdk-for-go/arm/network"
Expand All @@ -15,6 +16,11 @@ func TestAccAzureRMLoadBalancerBackEndAddressPool_basic(t *testing.T) {
ri := acctest.RandInt()
addressPoolName := fmt.Sprintf("%d-address-pool", ri)

subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we expose this via the provider?

backendAddressPool_id := fmt.Sprintf(
"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/backendAddressPools/%s",
subscriptionID, ri, ri, addressPoolName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -25,6 +31,8 @@ func TestAccAzureRMLoadBalancerBackEndAddressPool_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName, &lb),
resource.TestCheckResourceAttr(
"azurerm_lb_backend_address_pool.test", "id", backendAddressPool_id),
),
},
},
Expand Down
15 changes: 13 additions & 2 deletions builtin/providers/azurerm/resource_arm_loadbalancer_nat_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,18 @@ func resourceArmLoadBalancerNatPoolCreate(d *schema.ResourceData, meta interface
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
}

d.SetId(*read.ID)
var natPool_id string
for _, InboundNatPool := range *(*read.Properties).InboundNatPools {
if *InboundNatPool.Name == d.Get("name").(string) {
natPool_id = *InboundNatPool.ID
}
}

if natPool_id != "" {
d.SetId(natPool_id)
} else {
return fmt.Errorf("Error can not find created loadbalancer nat pool id %s", natPool_id)
}

log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName)
stateConf := &resource.StateChangeConf{
Expand All @@ -139,7 +150,7 @@ func resourceArmLoadBalancerNatPoolCreate(d *schema.ResourceData, meta interface
}

func resourceArmLoadBalancerNatPoolRead(d *schema.ResourceData, meta interface{}) error {
loadBalancer, exists, err := retrieveLoadBalancerById(d.Id(), meta)
loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
if err != nil {
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurerm

import (
"fmt"
"os"
"testing"

"github.com/Azure/azure-sdk-for-go/arm/network"
Expand All @@ -15,6 +16,11 @@ func TestAccAzureRMLoadBalancerNatPool_basic(t *testing.T) {
ri := acctest.RandInt()
natPoolName := fmt.Sprintf("NatPool-%d", ri)

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)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -25,6 +31,8 @@ func TestAccAzureRMLoadBalancerNatPool_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
resource.TestCheckResourceAttr(
"azurerm_lb_nat_pool.test", "id", natPool_id),
),
},
},
Expand Down
15 changes: 13 additions & 2 deletions builtin/providers/azurerm/resource_arm_loadbalancer_nat_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,18 @@ func resourceArmLoadBalancerNatRuleCreate(d *schema.ResourceData, meta interface
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
}

d.SetId(*read.ID)
var natRule_id string
for _, InboundNatRule := range *(*read.Properties).InboundNatRules {
if *InboundNatRule.Name == d.Get("name").(string) {
natRule_id = *InboundNatRule.ID
}
}

if natRule_id != "" {
d.SetId(natRule_id)
} else {
return fmt.Errorf("Error can not find created loadbalancer nat rule id %s", natRule_id)
}

log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName)
stateConf := &resource.StateChangeConf{
Expand All @@ -139,7 +150,7 @@ func resourceArmLoadBalancerNatRuleCreate(d *schema.ResourceData, meta interface
}

func resourceArmLoadBalancerNatRuleRead(d *schema.ResourceData, meta interface{}) error {
loadBalancer, exists, err := retrieveLoadBalancerById(d.Id(), meta)
loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
if err != nil {
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurerm

import (
"fmt"
"os"
"testing"

"github.com/Azure/azure-sdk-for-go/arm/network"
Expand All @@ -15,6 +16,11 @@ func TestAccAzureRMLoadBalancerNatRule_basic(t *testing.T) {
ri := acctest.RandInt()
natRuleName := fmt.Sprintf("NatRule-%d", ri)

subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
natRule_id := fmt.Sprintf(
"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/inboundNatRules/%s",
subscriptionID, ri, ri, natRuleName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -25,6 +31,8 @@ func TestAccAzureRMLoadBalancerNatRule_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb),
resource.TestCheckResourceAttr(
"azurerm_lb_nat_rule.test", "id", natRule_id),
),
},
},
Expand Down
15 changes: 13 additions & 2 deletions builtin/providers/azurerm/resource_arm_loadbalancer_probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,18 @@ func resourceArmLoadBalancerProbeCreate(d *schema.ResourceData, meta interface{}
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
}

d.SetId(*read.ID)
var createdProbe_id string
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use retrieveLoadBalancerById() here again then findLoadBalancerProbeByName(), but it's another call to the API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to read the response here and avoid the additional round trip.

for _, Probe := range *(*read.Properties).Probes {
if *Probe.Name == d.Get("name").(string) {
createdProbe_id = *Probe.ID
}
}

if createdProbe_id != "" {
d.SetId(createdProbe_id)
} else {
return fmt.Errorf("Error can not find created probe id %s", createdProbe_id)
}

log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName)
stateConf := &resource.StateChangeConf{
Expand All @@ -145,7 +156,7 @@ func resourceArmLoadBalancerProbeCreate(d *schema.ResourceData, meta interface{}
}

func resourceArmLoadBalancerProbeRead(d *schema.ResourceData, meta interface{}) error {
loadBalancer, exists, err := retrieveLoadBalancerById(d.Id(), meta)
loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
if err != nil {
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurerm

import (
"fmt"
"os"
"testing"

"github.com/Azure/azure-sdk-for-go/arm/network"
Expand All @@ -15,6 +16,11 @@ func TestAccAzureRMLoadBalancerProbe_basic(t *testing.T) {
ri := acctest.RandInt()
probeName := fmt.Sprintf("probe-%d", ri)

subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
probe_id := fmt.Sprintf(
"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/probes/%s",
subscriptionID, ri, ri, probeName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -25,6 +31,8 @@ func TestAccAzureRMLoadBalancerProbe_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
resource.TestCheckResourceAttr(
"azurerm_lb_probe.test", "id", probe_id),
),
},
},
Expand Down
15 changes: 13 additions & 2 deletions builtin/providers/azurerm/resource_arm_loadbalancer_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,18 @@ func resourceArmLoadBalancerRuleCreate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
}

d.SetId(*read.ID)
var rule_id string
for _, LoadBalancingRule := range *(*read.Properties).LoadBalancingRules {
if *LoadBalancingRule.Name == d.Get("name").(string) {
rule_id = *LoadBalancingRule.ID
}
}

if rule_id != "" {
d.SetId(rule_id)
} else {
return fmt.Errorf("Error can not find created loadbalancer rule id %s", rule_id)
}

log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName)
stateConf := &resource.StateChangeConf{
Expand All @@ -166,7 +177,7 @@ func resourceArmLoadBalancerRuleCreate(d *schema.ResourceData, meta interface{})
}

func resourceArmLoadBalancerRuleRead(d *schema.ResourceData, meta interface{}) error {
loadBalancer, exists, err := retrieveLoadBalancerById(d.Id(), meta)
loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
if err != nil {
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurerm

import (
"fmt"
"os"
"testing"

"github.com/Azure/azure-sdk-for-go/arm/network"
Expand Down Expand Up @@ -63,6 +64,11 @@ func TestAccAzureRMLoadBalancerRule_basic(t *testing.T) {
ri := acctest.RandInt()
lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))

subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
lbRule_id := 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,
Expand All @@ -73,6 +79,8 @@ func TestAccAzureRMLoadBalancerRule_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
resource.TestCheckResourceAttr(
"azurerm_lb_rule.test", "id", lbRule_id),
),
},
},
Expand Down