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