-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_address_list.go
300 lines (254 loc) · 10.1 KB
/
resource_address_list.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
package ddcloud
import (
"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
"github.com/hashicorp/terraform/helper/schema"
"log"
)
const (
resourceKeyAddressListNetworkDomainID = "networkdomain"
resourceKeyAddressListName = "name"
resourceKeyAddressListDescription = "description"
resourceKeyAddressListIPVersion = "ip_version"
resourceKeyAddressListAddresses = "addresses"
resourceKeyAddressListAddress = "address"
resourceKeyAddressListAddressBegin = "begin"
resourceKeyAddressListAddressEnd = "end"
resourceKeyAddressListAddressNetwork = "network"
resourceKeyAddressListAddressPrefixSize = "prefix_size"
resourceKeyAddressListChildIDs = "child_lists"
)
func resourceAddressList() *schema.Resource {
return &schema.Resource{
Exists: resourceAddressListExists,
Create: resourceAddressListCreate,
Read: resourceAddressListRead,
Update: resourceAddressListUpdate,
Delete: resourceAddressListDelete,
Schema: map[string]*schema.Schema{
resourceKeyAddressListNetworkDomainID: &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
Description: "The Id of the network domain in which the address list rule applies",
},
resourceKeyAddressListName: &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
Description: "A name for the address list",
},
resourceKeyAddressListDescription: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "A description for the firewall rule",
},
resourceKeyAddressListIPVersion: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The IP version (IPv4 or IPv6) used by the address list",
},
resourceKeyAddressListAddress: &schema.Schema{
Type: schema.TypeList,
Optional: true,
Description: "Complex IP addresses or networks included in the address list",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
resourceKeyAddressListAddressBegin: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The address or starting address for an address range",
},
resourceKeyAddressListAddressEnd: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The end address for an address range",
},
resourceKeyAddressListAddressNetwork: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The base address for an IP network",
ConflictsWith: []string{
resourceKeyAddressListAddress + "." + resourceKeyAddressListAddressBegin,
resourceKeyAddressListAddress + "." + resourceKeyAddressListAddressEnd,
},
},
resourceKeyAddressListAddressPrefixSize: &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Description: "The prefix size for an IP network",
ConflictsWith: []string{
resourceKeyAddressListAddress + "." + resourceKeyAddressListAddressBegin,
resourceKeyAddressListAddress + "." + resourceKeyAddressListAddressEnd,
},
},
},
},
ConflictsWith: []string{resourceKeyAddressListAddresses},
},
resourceKeyAddressListAddresses: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Description: "Simple IP addresses included in the address list",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
ConflictsWith: []string{resourceKeyAddressListAddress},
},
resourceKeyAddressListChildIDs: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Description: "The Ids of child address lists included in the address list",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
},
},
}
}
// Check if an address list resource exists.
func resourceAddressListExists(data *schema.ResourceData, provider interface{}) (bool, error) {
log.Printf("ResourceAddressListExists")
addressListID := data.Id()
client := provider.(*providerState).Client()
addressList, err := client.GetIPAddressList(addressListID)
exists := addressList != nil
return exists, err
}
// Create an address list resource.
func resourceAddressListCreate(data *schema.ResourceData, provider interface{}) error {
log.Printf("resourceAddressListCreate")
propertyHelper := propertyHelper(data)
networkDomainID := data.Get(resourceKeyAddressListNetworkDomainID).(string)
name := data.Get(resourceKeyAddressListName).(string)
description := data.Get(resourceKeyAddressListDescription).(string)
ipVersion := data.Get(resourceKeyAddressListIPVersion).(string)
childListIDs := propertyHelper.GetStringSetItems(resourceKeyAddressListChildIDs)
var addressListEntries []compute.IPAddressListEntry
if propertyHelper.HasProperty(resourceKeyAddressListAddresses) {
// Address list entries from a simple set of IP addresses.
simpleAddresses := propertyHelper.GetStringSetItems(resourceKeyAddressListAddresses)
for _, simpleAddress := range simpleAddresses {
addressListEntries = append(addressListEntries, compute.IPAddressListEntry{
Begin: simpleAddress,
})
}
} else { // Default for backward compatibility
// Raw address list entries.
addressListEntries = propertyHelper.GetAddressListAddresses()
}
log.Printf("Create address list '%s' in network domain '%s'.", name, networkDomainID)
client := provider.(*providerState).Client()
addressListID, err := client.CreateIPAddressList(name, description, ipVersion, networkDomainID, addressListEntries, childListIDs)
if err != nil {
return err
}
data.SetId(addressListID)
log.Printf("Successfully created address list '%s'.", addressListID)
return nil
}
// Read an address list resource.
func resourceAddressListRead(data *schema.ResourceData, provider interface{}) error {
addressListID := data.Id()
client := provider.(*providerState).Client()
addressList, err := client.GetIPAddressList(addressListID)
if err != nil {
return err
}
if addressList == nil {
data.SetId("") // Mark as deleted.
}
childListIDs := make([]string, len(addressList.ChildLists))
for index, childList := range addressList.ChildLists {
childListIDs[index] = childList.ID
}
propertyHelper := propertyHelper(data)
data.Set(resourceKeyAddressListDescription, addressList.Description)
propertyHelper.SetStringSetItems(resourceKeyAddressListChildIDs, childListIDs)
if propertyHelper.HasProperty(resourceKeyAddressListAddresses) {
// Note that if the address list now has complex entries (rather than the simple ones configured), then we won't pick that up here.
// TODO: Modify this logic to switch over to complex addresses if resource state indicates it's necessary
// For example, if addressListEntry.End or addressListEntry.PrefixSize is populated, then we need to switch over to complex ports.
var addresses []string
for _, addressListEntry := range addressList.Addresses {
addresses = append(addresses, addressListEntry.Begin)
}
propertyHelper.SetStringSetItems(resourceKeyAddressListAddresses, addresses)
} else { // Default for backward compatibility
propertyHelper.SetAddressListAddresses(addressList.Addresses)
}
//TODO: Implement set resourcekey address. At the moment changes in address will not get detected for change
return nil
}
// Update an address list resource.
func resourceAddressListUpdate(data *schema.ResourceData, provider interface{}) error {
log.Printf("resourceAddressListUpdate")
addressListID := data.Id()
networkDomainID := data.Get(resourceKeyAddressListNetworkDomainID).(string)
log.Printf("Update address list '%s' in network domain '%s'.", addressListID, networkDomainID)
client := provider.(*providerState).Client()
addressList, err := client.GetIPAddressList(addressListID)
if err != nil {
return err
}
if addressList == nil {
log.Printf("Address list '%s' not found in network domain '%s' (will treat as deleted).", addressListID, networkDomainID)
data.SetId("") // Mark as deleted.
return nil
}
propertyHelper := propertyHelper(data)
editRequest := addressList.BuildEditRequest()
if data.HasChange(resourceKeyAddressListDescription) {
editRequest.Description = data.Get(resourceKeyAddressListDescription).(string)
}
if data.HasChange(resourceKeyAddressListAddress) || data.HasChange(resourceKeyAddressListAddresses) {
var addressListEntries []compute.IPAddressListEntry
if propertyHelper.HasProperty(resourceKeyAddressListAddresses) {
// Address list entries from a simple set of IP addresses.
simpleAddresses := propertyHelper.GetStringSetItems(resourceKeyAddressListAddresses)
for _, simpleAddress := range simpleAddresses {
addressListEntries = append(addressListEntries, compute.IPAddressListEntry{
Begin: simpleAddress,
})
}
} else { // Default for backward compatibility
// Raw address list entries.
addressListEntries = propertyHelper.GetAddressListAddresses()
}
editRequest.Addresses = addressListEntries
}
if data.HasChange(resourceKeyAddressListChildIDs) {
editRequest.ChildListIDs = propertyHelper.GetStringSetItems(resourceKeyAddressListChildIDs)
}
err = client.EditIPAddressList(editRequest)
if err != nil {
return err
}
log.Printf("Updated address list '%s'.", addressListID)
return nil
}
// Delete an address list resource.
func resourceAddressListDelete(data *schema.ResourceData, provider interface{}) error {
log.Printf("resourceAddressListDelete")
addressListID := data.Id()
networkDomainID := data.Get(resourceKeyAddressListNetworkDomainID).(string)
client := provider.(*providerState).Client()
addressList, err := client.GetIPAddressList(addressListID)
if err != nil {
return err
}
if addressList == nil {
log.Printf("Address list '%s' not found in network domain '%s' (will treat as deleted).", addressListID, networkDomainID)
return nil
}
err = client.DeleteIPAddressList(addressListID)
if err != nil {
return err
}
log.Printf("Successfully deleted address list '%s' in network domain '%s'.", addressListID, networkDomainID)
return nil
}