forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_firewall.go
332 lines (271 loc) · 10.2 KB
/
resource_arm_firewall.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package azurerm
import (
"fmt"
"log"
"regexp"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-04-01/network"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
var azureFirewallResourceName = "azurerm_firewall"
func resourceArmFirewall() *schema.Resource {
return &schema.Resource{
Create: resourceArmFirewallCreateUpdate,
Read: resourceArmFirewallRead,
Update: resourceArmFirewallCreateUpdate,
Delete: resourceArmFirewallDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAzureFirewallName,
},
"location": locationSchema(),
"resource_group_name": resourceGroupNameSchema(),
"ip_configuration": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},
"subnet_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateResourceID,
},
"internal_public_ip_address_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateResourceID,
},
"private_ip_address": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"tags": tagsSchema(),
},
}
}
func resourceArmFirewallCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).azureFirewallsClient
ctx := meta.(*ArmClient).StopContext
log.Printf("[INFO] preparing arguments for AzureRM Azure Firewall creation")
name := d.Get("name").(string)
location := azureRMNormalizeLocation(d.Get("location").(string))
resourceGroup := d.Get("resource_group_name").(string)
tags := d.Get("tags").(map[string]interface{})
ipConfigs, subnetToLock, vnetToLock, err := expandArmFirewallIPConfigurations(d)
if err != nil {
return fmt.Errorf("Error Building list of Azure Firewall IP Configurations: %+v", err)
}
azureRMLockByName(name, azureFirewallResourceName)
defer azureRMUnlockByName(name, azureFirewallResourceName)
azureRMLockMultipleByName(subnetToLock, subnetResourceName)
defer azureRMUnlockMultipleByName(subnetToLock, subnetResourceName)
azureRMLockMultipleByName(vnetToLock, virtualNetworkResourceName)
defer azureRMUnlockMultipleByName(vnetToLock, virtualNetworkResourceName)
parameters := network.AzureFirewall{
Location: &location,
Tags: expandTags(tags),
AzureFirewallPropertiesFormat: &network.AzureFirewallPropertiesFormat{
IPConfigurations: ipConfigs,
},
}
future, err := client.CreateOrUpdate(ctx, resourceGroup, name, parameters)
if err != nil {
return fmt.Errorf("Error creating/updating Azure Firewall %q (Resource Group %q): %+v", name, resourceGroup, err)
}
err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
return fmt.Errorf("Error waiting for creation/update of Azure Firewall %q (Resource Group %q): %+v", name, resourceGroup, err)
}
read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving Azure Firewall %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Azure Firewall %q (Resource Group %q) ID", name, resourceGroup)
}
d.SetId(*read.ID)
return resourceArmFirewallRead(d, meta)
}
func resourceArmFirewallRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).azureFirewallsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["azureFirewalls"]
read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(read.Response) {
log.Printf("[DEBUG] Firewall %q was not found in Resource Group %q - removing from state!", name, resourceGroup)
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure Firewall %q (Resource Group %q): %+v", name, resourceGroup, err)
}
d.Set("name", read.Name)
d.Set("resource_group_name", resourceGroup)
if location := read.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}
if props := read.AzureFirewallPropertiesFormat; props != nil {
ipConfigs := flattenArmFirewallIPConfigurations(props.IPConfigurations)
if err := d.Set("ip_configuration", ipConfigs); err != nil {
return fmt.Errorf("Error setting `ip_configuration`: %+v", err)
}
}
flattenAndSetTags(d, read.Tags)
return nil
}
func resourceArmFirewallDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).azureFirewallsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["azureFirewalls"]
read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(read.Response) {
// deleted outside of TF
log.Printf("[DEBUG] Firewall %q was not found in Resource Group %q - assuming removed!", name, resourceGroup)
return nil
}
return fmt.Errorf("Error retrieving Firewall %q (Resource Group %q): %+v", name, resourceGroup, err)
}
subnetNamesToLock := make([]string, 0)
virtualNetworkNamesToLock := make([]string, 0)
if props := read.AzureFirewallPropertiesFormat; props != nil {
if configs := props.IPConfigurations; configs != nil {
for _, config := range *configs {
if config.Subnet == nil || config.Subnet.ID == nil {
continue
}
parsedSubnetId, err := parseAzureResourceID(*config.Subnet.ID)
if err != nil {
return err
}
subnetName := parsedSubnetId.Path["subnets"]
if !sliceContainsValue(subnetNamesToLock, subnetName) {
subnetNamesToLock = append(subnetNamesToLock, subnetName)
}
virtualNetworkName := parsedSubnetId.Path["virtualNetworks"]
if !sliceContainsValue(virtualNetworkNamesToLock, virtualNetworkName) {
virtualNetworkNamesToLock = append(virtualNetworkNamesToLock, virtualNetworkName)
}
}
}
}
azureRMLockByName(name, azureFirewallResourceName)
defer azureRMUnlockByName(name, azureFirewallResourceName)
azureRMLockMultipleByName(&subnetNamesToLock, subnetResourceName)
defer azureRMUnlockMultipleByName(&subnetNamesToLock, subnetResourceName)
azureRMLockMultipleByName(&virtualNetworkNamesToLock, virtualNetworkResourceName)
defer azureRMUnlockMultipleByName(&virtualNetworkNamesToLock, virtualNetworkResourceName)
future, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error deleting Azure Firewall %q (Resource Group %q): %+v", name, resourceGroup, err)
}
err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
return fmt.Errorf("Error waiting for the deletion of Azure Firewall %q (Resource Group %q): %+v", name, resourceGroup, err)
}
return err
}
func expandArmFirewallIPConfigurations(d *schema.ResourceData) (*[]network.AzureFirewallIPConfiguration, *[]string, *[]string, error) {
configs := d.Get("ip_configuration").([]interface{})
ipConfigs := make([]network.AzureFirewallIPConfiguration, 0)
subnetNamesToLock := make([]string, 0)
virtualNetworkNamesToLock := make([]string, 0)
for _, configRaw := range configs {
data := configRaw.(map[string]interface{})
name := data["name"].(string)
subnetId := data["subnet_id"].(string)
intPubID := data["internal_public_ip_address_id"].(string)
subnetID, err := parseAzureResourceID(subnetId)
if err != nil {
return nil, nil, nil, err
}
subnetName := subnetID.Path["subnets"]
virtualNetworkName := subnetID.Path["virtualNetworks"]
if !sliceContainsValue(subnetNamesToLock, subnetName) {
subnetNamesToLock = append(subnetNamesToLock, subnetName)
}
if !sliceContainsValue(virtualNetworkNamesToLock, virtualNetworkName) {
virtualNetworkNamesToLock = append(virtualNetworkNamesToLock, virtualNetworkName)
}
ipConfig := network.AzureFirewallIPConfiguration{
Name: utils.String(name),
AzureFirewallIPConfigurationPropertiesFormat: &network.AzureFirewallIPConfigurationPropertiesFormat{
Subnet: &network.SubResource{
ID: utils.String(subnetId),
},
InternalPublicIPAddress: &network.SubResource{
ID: utils.String(intPubID),
},
},
}
ipConfigs = append(ipConfigs, ipConfig)
}
return &ipConfigs, &subnetNamesToLock, &virtualNetworkNamesToLock, nil
}
func flattenArmFirewallIPConfigurations(input *[]network.AzureFirewallIPConfiguration) []interface{} {
result := make([]interface{}, 0)
if input == nil {
return result
}
for _, v := range *input {
afIPConfig := make(map[string]interface{})
props := v.AzureFirewallIPConfigurationPropertiesFormat
if props == nil {
continue
}
if name := v.Name; name != nil {
afIPConfig["name"] = *name
}
if subnet := props.Subnet; subnet != nil {
if id := subnet.ID; id != nil {
afIPConfig["subnet_id"] = *id
}
}
if ipAddress := props.PrivateIPAddress; ipAddress != nil {
afIPConfig["private_ip_address"] = *ipAddress
}
if pip := props.PublicIPAddress; pip != nil {
if id := pip.ID; id != nil {
afIPConfig["internal_public_ip_address_id"] = *id
}
}
result = append(result, afIPConfig)
}
return result
}
func validateAzureFirewallName(v interface{}, k string) (ws []string, es []error) {
value := v.(string)
// From the Portal:
// The name must begin with a letter or number, end with a letter, number or underscore, and may contain only letters, numbers, underscores, periods, or hyphens.
if matched := regexp.MustCompile(`^[0-9a-zA-Z]([0-9a-zA-Z.\_-]{0,}[0-9a-zA-Z_])?$`).Match([]byte(value)); !matched {
es = append(es, fmt.Errorf("%q must begin with a letter or number, end with a letter, number or underscore, and may contain only letters, numbers, underscores, periods, or hyphens.", k))
}
return ws, es
}