Skip to content

Commit a085c8d

Browse files
stack72jen20
authored andcommitted
provider/azurerm: Add Load Balancer resources (hashicorp#9199)
* provider/azurerm: Add AzureRM Loadbalancer resource Adds support for the elusive Azure LoadBalancer * [x] `azurerm_lb` * [x] `azurerm_lb_backend_address_pool` * [x] `azurerm_lb_rule` * [x] `azurerm_lb_nat_rule` * [x] `azurerm_lb_probe` * [x] `azurerm_lb_nat_pool` Test Results: ``` make testacc TEST=./builtin/providers/azurerm TESTARGS='-run=TestAccAzureRMLoadbalancer' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) TF_ACC=1 go test ./builtin/providers/azurerm -v -run=TestAccAzureRMLoadbalancer -timeout 120m === RUN TestAccAzureRMLoadbalancerBackEndAddressPool_basic --- PASS: TestAccAzureRMLoadbalancerBackEndAddressPool_basic (207.26s) === RUN TestAccAzureRMLoadbalancerBackEndAddressPool_removal --- PASS: TestAccAzureRMLoadbalancerBackEndAddressPool_removal (165.89s) === RUN TestAccAzureRMLoadbalancerNatRule_basic --- PASS: TestAccAzureRMLoadbalancerNatRule_basic (179.30s) === RUN TestAccAzureRMLoadbalancerNatRule_removal --- PASS: TestAccAzureRMLoadbalancerNatRule_removal (180.73s) === RUN TestAccAzureRMLoadbalancerRule_basic --- PASS: TestAccAzureRMLoadbalancerRule_basic (170.40s) === RUN TestAccAzureRMLoadbalancerRule_removal --- PASS: TestAccAzureRMLoadbalancerRule_removal (204.23s) === RUN TestAccAzureRMLoadbalancer_basic --- PASS: TestAccAzureRMLoadbalancer_basic (136.03s) === RUN TestAccAzureRMLoadbalancer_frontEndConfig --- PASS: TestAccAzureRMLoadbalancer_frontEndConfig (214.47s) === RUN TestAccAzureRMLoadbalancer_tags --- PASS: TestAccAzureRMLoadbalancer_tags (215.52s) === RUN TestAccAzureRMLoadbalancerProbe_basic --- PASS: TestAccAzureRMLoadbalancerProbe_basic (183.36s) === RUN TestAccAzureRMLoadbalancerProbe_removal --- PASS: TestAccAzureRMLoadbalancerProbe_removal (185.86s) === RUN TestAccAzureRMLoadbalancerNatPool_basic --- PASS: TestAccAzureRMLoadbalancerNatPool_basic (161.47s) === RUN TestAccAzureRMLoadbalancerNatPool_removal --- PASS: TestAccAzureRMLoadbalancerNatPool_removal (167.38s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 1673.852s ``` * provider/azurerm: Documentation for the ARM LB resources
1 parent 42c2fe5 commit a085c8d

22 files changed

Lines changed: 3220 additions & 3 deletions
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"strings"
7+
8+
"github.com/Azure/azure-sdk-for-go/arm/network"
9+
"github.com/hashicorp/errwrap"
10+
"github.com/hashicorp/terraform/helper/resource"
11+
)
12+
13+
func resourceGroupAndLBNameFromId(loadBalancerId string) (string, string, error) {
14+
id, err := parseAzureResourceID(loadBalancerId)
15+
if err != nil {
16+
return "", "", err
17+
}
18+
name := id.Path["loadBalancers"]
19+
resGroup := id.ResourceGroup
20+
21+
return resGroup, name, nil
22+
}
23+
24+
func retrieveLoadBalancerById(loadBalancerId string, meta interface{}) (*network.LoadBalancer, bool, error) {
25+
loadBalancerClient := meta.(*ArmClient).loadBalancerClient
26+
27+
resGroup, name, err := resourceGroupAndLBNameFromId(loadBalancerId)
28+
if err != nil {
29+
return nil, false, errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
30+
}
31+
32+
resp, err := loadBalancerClient.Get(resGroup, name, "")
33+
if err != nil {
34+
if resp.StatusCode == http.StatusNotFound {
35+
return nil, false, nil
36+
}
37+
return nil, false, fmt.Errorf("Error making Read request on Azure LoadBalancer %s: %s", name, err)
38+
}
39+
40+
return &resp, true, nil
41+
}
42+
43+
func findLoadBalancerBackEndAddressPoolByName(lb *network.LoadBalancer, name string) (*network.BackendAddressPool, int, bool) {
44+
if lb == nil || lb.Properties == nil || lb.Properties.BackendAddressPools == nil {
45+
return nil, -1, false
46+
}
47+
48+
for i, apc := range *lb.Properties.BackendAddressPools {
49+
if apc.Name != nil && *apc.Name == name {
50+
return &apc, i, true
51+
}
52+
}
53+
54+
return nil, -1, false
55+
}
56+
57+
func findLoadBalancerFrontEndIpConfigurationByName(lb *network.LoadBalancer, name string) (*network.FrontendIPConfiguration, int, bool) {
58+
if lb == nil || lb.Properties == nil || lb.Properties.FrontendIPConfigurations == nil {
59+
return nil, -1, false
60+
}
61+
62+
for i, feip := range *lb.Properties.FrontendIPConfigurations {
63+
if feip.Name != nil && *feip.Name == name {
64+
return &feip, i, true
65+
}
66+
}
67+
68+
return nil, -1, false
69+
}
70+
71+
func findLoadBalancerRuleByName(lb *network.LoadBalancer, name string) (*network.LoadBalancingRule, int, bool) {
72+
if lb == nil || lb.Properties == nil || lb.Properties.LoadBalancingRules == nil {
73+
return nil, -1, false
74+
}
75+
76+
for i, lbr := range *lb.Properties.LoadBalancingRules {
77+
if lbr.Name != nil && *lbr.Name == name {
78+
return &lbr, i, true
79+
}
80+
}
81+
82+
return nil, -1, false
83+
}
84+
85+
func findLoadBalancerNatRuleByName(lb *network.LoadBalancer, name string) (*network.InboundNatRule, int, bool) {
86+
if lb == nil || lb.Properties == nil || lb.Properties.InboundNatRules == nil {
87+
return nil, -1, false
88+
}
89+
90+
for i, nr := range *lb.Properties.InboundNatRules {
91+
if nr.Name != nil && *nr.Name == name {
92+
return &nr, i, true
93+
}
94+
}
95+
96+
return nil, -1, false
97+
}
98+
99+
func findLoadBalancerNatPoolByName(lb *network.LoadBalancer, name string) (*network.InboundNatPool, int, bool) {
100+
if lb == nil || lb.Properties == nil || lb.Properties.InboundNatPools == nil {
101+
return nil, -1, false
102+
}
103+
104+
for i, np := range *lb.Properties.InboundNatPools {
105+
if np.Name != nil && *np.Name == name {
106+
return &np, i, true
107+
}
108+
}
109+
110+
return nil, -1, false
111+
}
112+
113+
func findLoadBalancerProbeByName(lb *network.LoadBalancer, name string) (*network.Probe, int, bool) {
114+
if lb == nil || lb.Properties == nil || lb.Properties.Probes == nil {
115+
return nil, -1, false
116+
}
117+
118+
for i, p := range *lb.Properties.Probes {
119+
if p.Name != nil && *p.Name == name {
120+
return &p, i, true
121+
}
122+
}
123+
124+
return nil, -1, false
125+
}
126+
127+
func loadbalancerStateRefreshFunc(client *ArmClient, resourceGroupName string, loadbalancer string) resource.StateRefreshFunc {
128+
return func() (interface{}, string, error) {
129+
res, err := client.loadBalancerClient.Get(resourceGroupName, loadbalancer, "")
130+
if err != nil {
131+
return nil, "", fmt.Errorf("Error issuing read request in loadbalancerStateRefreshFunc to Azure ARM for LoadBalancer '%s' (RG: '%s'): %s", loadbalancer, resourceGroupName, err)
132+
}
133+
134+
return res, *res.Properties.ProvisioningState, nil
135+
}
136+
}
137+
138+
func validateLoadBalancerPrivateIpAddressAllocation(v interface{}, k string) (ws []string, errors []error) {
139+
value := strings.ToLower(v.(string))
140+
if value != "static" && value != "dynamic" {
141+
errors = append(errors, fmt.Errorf("LoadBalancer Allocations can only be Static or Dynamic"))
142+
}
143+
return
144+
}

builtin/providers/azurerm/provider.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,17 @@ func Provider() terraform.ResourceProvider {
4646

4747
ResourcesMap: map[string]*schema.Resource{
4848
// These resources use the Azure ARM SDK
49-
"azurerm_availability_set": resourceArmAvailabilitySet(),
50-
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
51-
"azurerm_cdn_profile": resourceArmCdnProfile(),
49+
"azurerm_availability_set": resourceArmAvailabilitySet(),
50+
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
51+
"azurerm_cdn_profile": resourceArmCdnProfile(),
52+
53+
"azurerm_lb": resourceArmLoadBalancer(),
54+
"azurerm_lb_backend_address_pool": resourceArmLoadBalancerBackendAddressPool(),
55+
"azurerm_lb_nat_rule": resourceArmLoadBalancerNatRule(),
56+
"azurerm_lb_nat_pool": resourceArmLoadBalancerNatPool(),
57+
"azurerm_lb_probe": resourceArmLoadBalancerProbe(),
58+
"azurerm_lb_rule": resourceArmLoadBalancerRule(),
59+
5260
"azurerm_local_network_gateway": resourceArmLocalNetworkGateway(),
5361
"azurerm_network_interface": resourceArmNetworkInterface(),
5462
"azurerm_network_security_group": resourceArmNetworkSecurityGroup(),

0 commit comments

Comments
 (0)