forked from hashicorp/terraform-provider-azurerm
/
resource_arm_network_interface_nat_rule_association.go
293 lines (236 loc) · 10.4 KB
/
resource_arm_network_interface_nat_rule_association.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package azurerm
import (
"fmt"
"log"
"strings"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-08-01/network"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmNetworkInterfaceNatRuleAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceArmNetworkInterfaceNatRuleAssociationCreate,
Read: resourceArmNetworkInterfaceNatRuleAssociationRead,
Delete: resourceArmNetworkInterfaceNatRuleAssociationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"network_interface_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},
"ip_configuration_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},
"nat_rule_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},
},
}
}
func resourceArmNetworkInterfaceNatRuleAssociationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).ifaceClient
ctx := meta.(*ArmClient).StopContext
log.Printf("[INFO] preparing arguments for Network Interface <-> Load Balancer NAT Rule Association creation.")
networkInterfaceId := d.Get("network_interface_id").(string)
ipConfigurationName := d.Get("ip_configuration_name").(string)
natRuleId := d.Get("nat_rule_id").(string)
id, err := parseAzureResourceID(networkInterfaceId)
if err != nil {
return err
}
networkInterfaceName := id.Path["networkInterfaces"]
resourceGroup := id.ResourceGroup
azureRMLockByName(networkInterfaceName, networkInterfaceResourceName)
defer azureRMUnlockByName(networkInterfaceName, networkInterfaceResourceName)
read, err := client.Get(ctx, resourceGroup, networkInterfaceName, "")
if err != nil {
if utils.ResponseWasNotFound(read.Response) {
return fmt.Errorf("Network Interface %q (Resource Group %q) was not found!", networkInterfaceName, resourceGroup)
}
return fmt.Errorf("Error retrieving Network Interface %q (Resource Group %q): %+v", networkInterfaceName, resourceGroup, err)
}
props := read.InterfacePropertiesFormat
if props == nil {
return fmt.Errorf("Error: `properties` was nil for Network Interface %q (Resource Group %q)", networkInterfaceName, resourceGroup)
}
ipConfigs := props.IPConfigurations
if ipConfigs == nil {
return fmt.Errorf("Error: `properties.IPConfigurations` was nil for Network Interface %q (Resource Group %q)", networkInterfaceName, resourceGroup)
}
c := azure.FindNetworkInterfaceIPConfiguration(props.IPConfigurations, ipConfigurationName)
if c == nil {
return fmt.Errorf("Error: IP Configuration %q was not found on Network Interface %q (Resource Group %q)", ipConfigurationName, networkInterfaceName, resourceGroup)
}
config := *c
p := config.InterfaceIPConfigurationPropertiesFormat
if p == nil {
return fmt.Errorf("Error: `IPConfiguration.properties` was nil for Network Interface %q (Resource Group %q)", networkInterfaceName, resourceGroup)
}
rules := make([]network.InboundNatRule, 0)
// first double-check it doesn't exist
if p.LoadBalancerInboundNatRules != nil {
for _, existingRule := range *p.LoadBalancerInboundNatRules {
if id := existingRule.ID; id != nil {
if *id == natRuleId {
// TODO: switch to using the common error once https://github.com/terraform-providers/terraform-provider-azurerm/pull/1746 is merged
return fmt.Errorf("A Network Interface <-> Load Balancer NAT Rule association exists between %q and %q - please import it!", networkInterfaceId, natRuleId)
}
rules = append(rules, existingRule)
}
}
}
rule := network.InboundNatRule{
ID: utils.String(natRuleId),
}
rules = append(rules, rule)
p.LoadBalancerInboundNatRules = &rules
props.IPConfigurations = azure.UpdateNetworkInterfaceIPConfiguration(config, props.IPConfigurations)
future, err := client.CreateOrUpdate(ctx, resourceGroup, networkInterfaceName, read)
if err != nil {
return fmt.Errorf("Error updating NAT Rule Association for Network Interface %q (Resource Group %q): %+v", networkInterfaceName, resourceGroup, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion of NAT Rule Association for NIC %q (Resource Group %q): %+v", networkInterfaceName, resourceGroup, err)
}
resourceId := fmt.Sprintf("%s/ipConfigurations/%s|%s", networkInterfaceId, ipConfigurationName, natRuleId)
d.SetId(resourceId)
return resourceArmNetworkInterfaceNatRuleAssociationRead(d, meta)
}
func resourceArmNetworkInterfaceNatRuleAssociationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).ifaceClient
ctx := meta.(*ArmClient).StopContext
splitId := strings.Split(d.Id(), "|")
if len(splitId) != 2 {
return fmt.Errorf("Expected ID to be in the format {networkInterfaceId}/ipConfigurations/{ipConfigurationName}|{natRuleId} but got %q", d.Id())
}
nicID, err := parseAzureResourceID(splitId[0])
if err != nil {
return err
}
ipConfigurationName := nicID.Path["ipConfigurations"]
networkInterfaceName := nicID.Path["networkInterfaces"]
resourceGroup := nicID.ResourceGroup
natRuleId := splitId[1]
read, err := client.Get(ctx, resourceGroup, networkInterfaceName, "")
if err != nil {
if utils.ResponseWasNotFound(read.Response) {
return fmt.Errorf("Network Interface %q (Resource Group %q) was not found!", networkInterfaceName, resourceGroup)
}
return fmt.Errorf("Error retrieving Network Interface %q (Resource Group %q): %+v", networkInterfaceName, resourceGroup, err)
}
nicProps := read.InterfacePropertiesFormat
if nicProps == nil {
return fmt.Errorf("Error: `properties` was nil for Network Interface %q (Resource Group %q)", networkInterfaceName, resourceGroup)
}
ipConfigs := nicProps.IPConfigurations
if ipConfigs == nil {
return fmt.Errorf("Error: `properties.IPConfigurations` was nil for Network Interface %q (Resource Group %q)", networkInterfaceName, resourceGroup)
}
c := azure.FindNetworkInterfaceIPConfiguration(nicProps.IPConfigurations, ipConfigurationName)
if c == nil {
log.Printf("IP Configuration %q was not found in Network Interface %q (Resource Group %q) - removing from state!", ipConfigurationName, networkInterfaceName, resourceGroup)
d.SetId("")
return nil
}
config := *c
found := false
if props := config.InterfaceIPConfigurationPropertiesFormat; props != nil {
if rules := props.LoadBalancerInboundNatRules; rules != nil {
for _, rule := range *rules {
if rule.ID == nil {
continue
}
if *rule.ID == natRuleId {
found = true
break
}
}
}
}
if !found {
log.Printf("[DEBUG] Association between Network Interface %q (Resource Group %q) and Load Balancer NAT Rule %q was not found - removing from state!", networkInterfaceName, resourceGroup, natRuleId)
d.SetId("")
return nil
}
d.Set("ip_configuration_name", ipConfigurationName)
d.Set("nat_rule_id", natRuleId)
if id := read.ID; id != nil {
d.Set("network_interface_id", *id)
}
return nil
}
func resourceArmNetworkInterfaceNatRuleAssociationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).ifaceClient
ctx := meta.(*ArmClient).StopContext
splitId := strings.Split(d.Id(), "|")
if len(splitId) != 2 {
return fmt.Errorf("Expected ID to be in the format {networkInterfaceId}/ipConfigurations/{ipConfigurationName}|{natRuleId} but got %q", d.Id())
}
nicID, err := parseAzureResourceID(splitId[0])
if err != nil {
return err
}
ipConfigurationName := nicID.Path["ipConfigurations"]
networkInterfaceName := nicID.Path["networkInterfaces"]
resourceGroup := nicID.ResourceGroup
natRuleId := splitId[1]
azureRMLockByName(networkInterfaceName, networkInterfaceResourceName)
defer azureRMUnlockByName(networkInterfaceName, networkInterfaceResourceName)
read, err := client.Get(ctx, resourceGroup, networkInterfaceName, "")
if err != nil {
if utils.ResponseWasNotFound(read.Response) {
return fmt.Errorf("Network Interface %q (Resource Group %q) was not found!", networkInterfaceName, resourceGroup)
}
return fmt.Errorf("Error retrieving Network Interface %q (Resource Group %q): %+v", networkInterfaceName, resourceGroup, err)
}
nicProps := read.InterfacePropertiesFormat
if nicProps == nil {
return fmt.Errorf("Error: `properties` was nil for Network Interface %q (Resource Group %q)", networkInterfaceName, resourceGroup)
}
ipConfigs := nicProps.IPConfigurations
if ipConfigs == nil {
return fmt.Errorf("Error: `properties.IPConfigurations` was nil for Network Interface %q (Resource Group %q)", networkInterfaceName, resourceGroup)
}
c := azure.FindNetworkInterfaceIPConfiguration(nicProps.IPConfigurations, ipConfigurationName)
if c == nil {
return fmt.Errorf("Error: IP Configuration %q was not found on Network Interface %q (Resource Group %q)", ipConfigurationName, networkInterfaceName, resourceGroup)
}
config := *c
props := config.InterfaceIPConfigurationPropertiesFormat
if props == nil {
return fmt.Errorf("Error: Properties for IPConfiguration %q was nil for Network Interface %q (Resource Group %q)", ipConfigurationName, networkInterfaceName, resourceGroup)
}
updatedRules := make([]network.InboundNatRule, 0)
if existingRules := props.LoadBalancerInboundNatRules; existingRules != nil {
for _, rule := range *existingRules {
if rule.ID == nil {
continue
}
if *rule.ID != natRuleId {
updatedRules = append(updatedRules, rule)
}
}
}
props.LoadBalancerInboundNatRules = &updatedRules
nicProps.IPConfigurations = azure.UpdateNetworkInterfaceIPConfiguration(config, nicProps.IPConfigurations)
future, err := client.CreateOrUpdate(ctx, resourceGroup, networkInterfaceName, read)
if err != nil {
return fmt.Errorf("Error removing NAT Rule Association for Network Interface %q (Resource Group %q): %+v", networkInterfaceName, resourceGroup, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for removal of NAT Rule Association for NIC %q (Resource Group %q): %+v", networkInterfaceName, resourceGroup, err)
}
return nil
}