Skip to content

Commit

Permalink
provider/azurerm: support import for load balancer and sub resources (#…
Browse files Browse the repository at this point in the history
…11610)

implemented ResourceImporter for sub resources which extracts the lb id
deprecated location on each sub resource as it was unused
  • Loading branch information
pmcatominey authored and stack72 committed Feb 1, 2017
1 parent db9e671 commit bbde053
Show file tree
Hide file tree
Showing 19 changed files with 423 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMLoadBalancerBackEndAddressPool_importBasic(t *testing.T) {
resourceName := "azurerm_lb_backend_address_pool.test"

ri := acctest.RandInt()
addressPoolName := fmt.Sprintf("%d-address-pool", ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAzureRMLoadBalancerBackEndAddressPool_basic(ri, addressPoolName),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
// location is deprecated and was never actually used
ImportStateVerifyIgnore: []string{"location"},
},
},
})
}
35 changes: 35 additions & 0 deletions builtin/providers/azurerm/import_arm_loadbalancer_nat_pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMLoadBalancerNatPool_importBasic(t *testing.T) {
resourceName := "azurerm_lb_nat_pool.test"

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{
resource.TestStep{
Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
// location is deprecated and was never actually used
ImportStateVerifyIgnore: []string{"location"},
},
},
})
}
35 changes: 35 additions & 0 deletions builtin/providers/azurerm/import_arm_loadbalancer_nat_rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMLoadBalancerNatRule_importBasic(t *testing.T) {
resourceName := "azurerm_lb_nat_rule.test"

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{
resource.TestStep{
Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
// location is deprecated and was never actually used
ImportStateVerifyIgnore: []string{"location"},
},
},
})
}
35 changes: 35 additions & 0 deletions builtin/providers/azurerm/import_arm_loadbalancer_probe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMLoadBalancerProbe_importBasic(t *testing.T) {
resourceName := "azurerm_lb_probe.test"

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{
resource.TestStep{
Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
// location is deprecated and was never actually used
ImportStateVerifyIgnore: []string{"location"},
},
},
})
}
35 changes: 35 additions & 0 deletions builtin/providers/azurerm/import_arm_loadbalancer_rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMLoadBalancerRule_importBasic(t *testing.T) {
resourceName := "azurerm_lb_rule.test"

ri := acctest.RandInt()
lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAzureRMLoadBalancerRule_basic(ri, lbRuleName),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
// location is deprecated and was never actually used
ImportStateVerifyIgnore: []string{"location"},
},
},
})
}
29 changes: 29 additions & 0 deletions builtin/providers/azurerm/import_arm_loadbalancer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package azurerm

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMLoadBalancer_importBasic(t *testing.T) {
resourceName := "azurerm_lb.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAzureRMLoadBalancer_basic(acctest.RandInt()),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
23 changes: 23 additions & 0 deletions builtin/providers/azurerm/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package azurerm
import (
"fmt"
"net/http"
"regexp"
"strings"

"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceGroupAndLBNameFromId(loadBalancerId string) (string, string, error) {
Expand Down Expand Up @@ -142,3 +144,24 @@ func validateLoadBalancerPrivateIpAddressAllocation(v interface{}, k string) (ws
}
return
}

// sets the loadbalancer_id in the ResourceData from the sub resources full id
func loadBalancerSubResourceStateImporter(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
r, err := regexp.Compile(`.+\/loadBalancers\/.+?\/`)
if err != nil {
return nil, err
}

lbID := strings.TrimSuffix(r.FindString(d.Id()), "/")
parsed, err := parseAzureResourceID(lbID)
if err != nil {
return nil, fmt.Errorf("unable to parse loadbalancer id from %s", d.Id())
}

if parsed.Path["loadBalancers"] == "" {
return nil, fmt.Errorf("parsed ID is invalid")
}

d.Set("loadbalancer_id", lbID)
return []*schema.ResourceData{d}, nil
}
12 changes: 12 additions & 0 deletions builtin/providers/azurerm/resource_arm_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func resourceArmLoadBalancer() *schema.Resource {
Read: resourecArmLoadBalancerRead,
Update: resourceArmLoadBalancerCreate,
Delete: resourceArmLoadBalancerDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -147,6 +150,11 @@ func resourceArmLoadBalancerCreate(d *schema.ResourceData, meta interface{}) err
}

func resourecArmLoadBalancerRead(d *schema.ResourceData, meta interface{}) error {
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

loadBalancer, exists, err := retrieveLoadBalancerById(d.Id(), meta)
if err != nil {
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
Expand All @@ -157,6 +165,10 @@ func resourecArmLoadBalancerRead(d *schema.ResourceData, meta interface{}) error
return nil
}

d.Set("name", loadBalancer.Name)
d.Set("location", loadBalancer.Location)
d.Set("resource_group_name", id.ResourceGroup)

if loadBalancer.LoadBalancerPropertiesFormat != nil && loadBalancer.LoadBalancerPropertiesFormat.FrontendIPConfigurations != nil {
d.Set("frontend_ip_configuration", flattenLoadBalancerFrontendIpConfiguration(loadBalancer.LoadBalancerPropertiesFormat.FrontendIPConfigurations))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func resourceArmLoadBalancerBackendAddressPool() *schema.Resource {
Create: resourceArmLoadBalancerBackendAddressPoolCreate,
Read: resourceArmLoadBalancerBackendAddressPoolRead,
Delete: resourceArmLoadBalancerBackendAddressPoolDelete,
Importer: &schema.ResourceImporter{
State: loadBalancerSubResourceStateImporter,
},

Schema: map[string]*schema.Schema{
"name": {
Expand All @@ -25,7 +28,14 @@ func resourceArmLoadBalancerBackendAddressPool() *schema.Resource {
ForceNew: true,
},

"location": locationSchema(),
"location": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
StateFunc: azureRMNormalizeLocation,
DiffSuppressFunc: azureRMSuppressLocationDiff,
Deprecated: "location is no longer used",
},

"resource_group_name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -130,42 +140,48 @@ func resourceArmLoadBalancerBackendAddressPoolCreate(d *schema.ResourceData, met
}

func resourceArmLoadBalancerBackendAddressPoolRead(d *schema.ResourceData, meta interface{}) error {
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
name := id.Path["backendAddressPools"]

loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
if err != nil {
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
}
if !exists {
d.SetId("")
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", name)
return nil
}

config, _, exists := findLoadBalancerBackEndAddressPoolByName(loadBalancer, d.Get("name").(string))
config, _, exists := findLoadBalancerBackEndAddressPoolByName(loadBalancer, name)
if !exists {
d.SetId("")
log.Printf("[INFO] LoadBalancer Backend Address Pool %q not found. Removing from state", d.Get("name").(string))
log.Printf("[INFO] LoadBalancer Backend Address Pool %q not found. Removing from state", name)
return nil
}

d.Set("name", config.Name)
d.Set("resource_group_name", id.ResourceGroup)

var backend_ip_configurations []string
if config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations != nil {
backend_ip_configurations := make([]string, 0, len(*config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations))
for _, backendConfig := range *config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations {
backend_ip_configurations = append(backend_ip_configurations, *backendConfig.ID)
}

d.Set("backend_ip_configurations", backend_ip_configurations)
}
d.Set("backend_ip_configurations", backend_ip_configurations)

var load_balancing_rules []string
if config.BackendAddressPoolPropertiesFormat.LoadBalancingRules != nil {
load_balancing_rules := make([]string, 0, len(*config.BackendAddressPoolPropertiesFormat.LoadBalancingRules))
for _, rule := range *config.BackendAddressPoolPropertiesFormat.LoadBalancingRules {
load_balancing_rules = append(load_balancing_rules, *rule.ID)
}

d.Set("backend_ip_configurations", load_balancing_rules)
}
d.Set("load_balancing_rules", load_balancing_rules)

return nil
}
Expand Down
Loading

0 comments on commit bbde053

Please sign in to comment.