Skip to content

Commit bbde053

Browse files
pmcatomineystack72
authored andcommitted
provider/azurerm: support import for load balancer and sub resources (hashicorp#11610)
implemented ResourceImporter for sub resources which extracts the lb id deprecated location on each sub resource as it was unused
1 parent db9e671 commit bbde053

19 files changed

Lines changed: 423 additions & 26 deletions
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
)
10+
11+
func TestAccAzureRMLoadBalancerBackEndAddressPool_importBasic(t *testing.T) {
12+
resourceName := "azurerm_lb_backend_address_pool.test"
13+
14+
ri := acctest.RandInt()
15+
addressPoolName := fmt.Sprintf("%d-address-pool", ri)
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
21+
Steps: []resource.TestStep{
22+
resource.TestStep{
23+
Config: testAccAzureRMLoadBalancerBackEndAddressPool_basic(ri, addressPoolName),
24+
},
25+
26+
resource.TestStep{
27+
ResourceName: resourceName,
28+
ImportState: true,
29+
ImportStateVerify: true,
30+
// location is deprecated and was never actually used
31+
ImportStateVerifyIgnore: []string{"location"},
32+
},
33+
},
34+
})
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
)
10+
11+
func TestAccAzureRMLoadBalancerNatPool_importBasic(t *testing.T) {
12+
resourceName := "azurerm_lb_nat_pool.test"
13+
14+
ri := acctest.RandInt()
15+
natPoolName := fmt.Sprintf("NatPool-%d", ri)
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
21+
Steps: []resource.TestStep{
22+
resource.TestStep{
23+
Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
24+
},
25+
26+
resource.TestStep{
27+
ResourceName: resourceName,
28+
ImportState: true,
29+
ImportStateVerify: true,
30+
// location is deprecated and was never actually used
31+
ImportStateVerifyIgnore: []string{"location"},
32+
},
33+
},
34+
})
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
)
10+
11+
func TestAccAzureRMLoadBalancerNatRule_importBasic(t *testing.T) {
12+
resourceName := "azurerm_lb_nat_rule.test"
13+
14+
ri := acctest.RandInt()
15+
natRuleName := fmt.Sprintf("NatRule-%d", ri)
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
21+
Steps: []resource.TestStep{
22+
resource.TestStep{
23+
Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName),
24+
},
25+
26+
resource.TestStep{
27+
ResourceName: resourceName,
28+
ImportState: true,
29+
ImportStateVerify: true,
30+
// location is deprecated and was never actually used
31+
ImportStateVerifyIgnore: []string{"location"},
32+
},
33+
},
34+
})
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
)
10+
11+
func TestAccAzureRMLoadBalancerProbe_importBasic(t *testing.T) {
12+
resourceName := "azurerm_lb_probe.test"
13+
14+
ri := acctest.RandInt()
15+
probeName := fmt.Sprintf("probe-%d", ri)
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
21+
Steps: []resource.TestStep{
22+
resource.TestStep{
23+
Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName),
24+
},
25+
26+
resource.TestStep{
27+
ResourceName: resourceName,
28+
ImportState: true,
29+
ImportStateVerify: true,
30+
// location is deprecated and was never actually used
31+
ImportStateVerifyIgnore: []string{"location"},
32+
},
33+
},
34+
})
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
)
10+
11+
func TestAccAzureRMLoadBalancerRule_importBasic(t *testing.T) {
12+
resourceName := "azurerm_lb_rule.test"
13+
14+
ri := acctest.RandInt()
15+
lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
21+
Steps: []resource.TestStep{
22+
resource.TestStep{
23+
Config: testAccAzureRMLoadBalancerRule_basic(ri, lbRuleName),
24+
},
25+
26+
resource.TestStep{
27+
ResourceName: resourceName,
28+
ImportState: true,
29+
ImportStateVerify: true,
30+
// location is deprecated and was never actually used
31+
ImportStateVerifyIgnore: []string{"location"},
32+
},
33+
},
34+
})
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package azurerm
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform/helper/acctest"
7+
"github.com/hashicorp/terraform/helper/resource"
8+
)
9+
10+
func TestAccAzureRMLoadBalancer_importBasic(t *testing.T) {
11+
resourceName := "azurerm_lb.test"
12+
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
Providers: testAccProviders,
16+
CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
17+
Steps: []resource.TestStep{
18+
resource.TestStep{
19+
Config: testAccAzureRMLoadBalancer_basic(acctest.RandInt()),
20+
},
21+
22+
resource.TestStep{
23+
ResourceName: resourceName,
24+
ImportState: true,
25+
ImportStateVerify: true,
26+
},
27+
},
28+
})
29+
}

builtin/providers/azurerm/loadbalancer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package azurerm
33
import (
44
"fmt"
55
"net/http"
6+
"regexp"
67
"strings"
78

89
"github.com/Azure/azure-sdk-for-go/arm/network"
910
"github.com/hashicorp/errwrap"
1011
"github.com/hashicorp/terraform/helper/resource"
12+
"github.com/hashicorp/terraform/helper/schema"
1113
)
1214

1315
func resourceGroupAndLBNameFromId(loadBalancerId string) (string, string, error) {
@@ -142,3 +144,24 @@ func validateLoadBalancerPrivateIpAddressAllocation(v interface{}, k string) (ws
142144
}
143145
return
144146
}
147+
148+
// sets the loadbalancer_id in the ResourceData from the sub resources full id
149+
func loadBalancerSubResourceStateImporter(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
150+
r, err := regexp.Compile(`.+\/loadBalancers\/.+?\/`)
151+
if err != nil {
152+
return nil, err
153+
}
154+
155+
lbID := strings.TrimSuffix(r.FindString(d.Id()), "/")
156+
parsed, err := parseAzureResourceID(lbID)
157+
if err != nil {
158+
return nil, fmt.Errorf("unable to parse loadbalancer id from %s", d.Id())
159+
}
160+
161+
if parsed.Path["loadBalancers"] == "" {
162+
return nil, fmt.Errorf("parsed ID is invalid")
163+
}
164+
165+
d.Set("loadbalancer_id", lbID)
166+
return []*schema.ResourceData{d}, nil
167+
}

builtin/providers/azurerm/resource_arm_loadbalancer.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ func resourceArmLoadBalancer() *schema.Resource {
1818
Read: resourecArmLoadBalancerRead,
1919
Update: resourceArmLoadBalancerCreate,
2020
Delete: resourceArmLoadBalancerDelete,
21+
Importer: &schema.ResourceImporter{
22+
State: schema.ImportStatePassthrough,
23+
},
2124

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

149152
func resourecArmLoadBalancerRead(d *schema.ResourceData, meta interface{}) error {
153+
id, err := parseAzureResourceID(d.Id())
154+
if err != nil {
155+
return err
156+
}
157+
150158
loadBalancer, exists, err := retrieveLoadBalancerById(d.Id(), meta)
151159
if err != nil {
152160
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
@@ -157,6 +165,10 @@ func resourecArmLoadBalancerRead(d *schema.ResourceData, meta interface{}) error
157165
return nil
158166
}
159167

168+
d.Set("name", loadBalancer.Name)
169+
d.Set("location", loadBalancer.Location)
170+
d.Set("resource_group_name", id.ResourceGroup)
171+
160172
if loadBalancer.LoadBalancerPropertiesFormat != nil && loadBalancer.LoadBalancerPropertiesFormat.FrontendIPConfigurations != nil {
161173
d.Set("frontend_ip_configuration", flattenLoadBalancerFrontendIpConfiguration(loadBalancer.LoadBalancerPropertiesFormat.FrontendIPConfigurations))
162174
}

builtin/providers/azurerm/resource_arm_loadbalancer_backend_address_pool.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ func resourceArmLoadBalancerBackendAddressPool() *schema.Resource {
1717
Create: resourceArmLoadBalancerBackendAddressPoolCreate,
1818
Read: resourceArmLoadBalancerBackendAddressPoolRead,
1919
Delete: resourceArmLoadBalancerBackendAddressPoolDelete,
20+
Importer: &schema.ResourceImporter{
21+
State: loadBalancerSubResourceStateImporter,
22+
},
2023

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

28-
"location": locationSchema(),
31+
"location": {
32+
Type: schema.TypeString,
33+
ForceNew: true,
34+
Optional: true,
35+
StateFunc: azureRMNormalizeLocation,
36+
DiffSuppressFunc: azureRMSuppressLocationDiff,
37+
Deprecated: "location is no longer used",
38+
},
2939

3040
"resource_group_name": {
3141
Type: schema.TypeString,
@@ -130,42 +140,48 @@ func resourceArmLoadBalancerBackendAddressPoolCreate(d *schema.ResourceData, met
130140
}
131141

132142
func resourceArmLoadBalancerBackendAddressPoolRead(d *schema.ResourceData, meta interface{}) error {
143+
id, err := parseAzureResourceID(d.Id())
144+
if err != nil {
145+
return err
146+
}
147+
name := id.Path["backendAddressPools"]
148+
133149
loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
134150
if err != nil {
135151
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
136152
}
137153
if !exists {
138154
d.SetId("")
139-
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
155+
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", name)
140156
return nil
141157
}
142158

143-
config, _, exists := findLoadBalancerBackEndAddressPoolByName(loadBalancer, d.Get("name").(string))
159+
config, _, exists := findLoadBalancerBackEndAddressPoolByName(loadBalancer, name)
144160
if !exists {
145161
d.SetId("")
146-
log.Printf("[INFO] LoadBalancer Backend Address Pool %q not found. Removing from state", d.Get("name").(string))
162+
log.Printf("[INFO] LoadBalancer Backend Address Pool %q not found. Removing from state", name)
147163
return nil
148164
}
149165

150166
d.Set("name", config.Name)
167+
d.Set("resource_group_name", id.ResourceGroup)
151168

169+
var backend_ip_configurations []string
152170
if config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations != nil {
153-
backend_ip_configurations := make([]string, 0, len(*config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations))
154171
for _, backendConfig := range *config.BackendAddressPoolPropertiesFormat.BackendIPConfigurations {
155172
backend_ip_configurations = append(backend_ip_configurations, *backendConfig.ID)
156173
}
157174

158-
d.Set("backend_ip_configurations", backend_ip_configurations)
159175
}
176+
d.Set("backend_ip_configurations", backend_ip_configurations)
160177

178+
var load_balancing_rules []string
161179
if config.BackendAddressPoolPropertiesFormat.LoadBalancingRules != nil {
162-
load_balancing_rules := make([]string, 0, len(*config.BackendAddressPoolPropertiesFormat.LoadBalancingRules))
163180
for _, rule := range *config.BackendAddressPoolPropertiesFormat.LoadBalancingRules {
164181
load_balancing_rules = append(load_balancing_rules, *rule.ID)
165182
}
166-
167-
d.Set("backend_ip_configurations", load_balancing_rules)
168183
}
184+
d.Set("load_balancing_rules", load_balancing_rules)
169185

170186
return nil
171187
}

0 commit comments

Comments
 (0)