-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_port_list.go
275 lines (226 loc) · 8.45 KB
/
resource_port_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
package ddcloud
import (
"log"
"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
"github.com/hashicorp/terraform/helper/schema"
)
const (
resourceKeyPortListNetworkDomainID = "networkdomain"
resourceKeyPortListName = "name"
resourceKeyPortListDescription = "description"
resourceKeyPortListPort = "port"
resourceKeyPortListPorts = "ports"
resourceKeyPortListPortBegin = "begin"
resourceKeyPortListPortEnd = "end"
resourceKeyPortListChildIDs = "child_lists"
)
func resourcePortList() *schema.Resource {
return &schema.Resource{
Exists: resourcePortListExists,
Create: resourcePortListCreate,
Read: resourcePortListRead,
Update: resourcePortListUpdate,
Delete: resourcePortListDelete,
Schema: map[string]*schema.Schema{
resourceKeyPortListNetworkDomainID: &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
Description: "The Id of the network domain in which the port list rule applies",
},
resourceKeyPortListName: &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
Description: "A name for the port list",
},
resourceKeyPortListDescription: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "A description for the firewall rule",
},
resourceKeyPortListPort: &schema.Schema{
Type: schema.TypeList,
Optional: true,
Description: "Ports included in the port list",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
resourceKeyPortListPortBegin: &schema.Schema{
Type: schema.TypeInt,
Required: true,
Description: "The port (or starting port, for a port range)",
},
resourceKeyPortListPortEnd: &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 0,
Description: "The ending port (for a port range",
},
},
},
ConflictsWith: []string{resourceKeyPortListPorts},
},
resourceKeyPortListPorts: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Description: "Simple ports included in the port list",
Elem: &schema.Schema{
Type: schema.TypeInt,
},
ConflictsWith: []string{resourceKeyPortListPort},
},
resourceKeyPortListChildIDs: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Description: "The Ids of child port lists included in the port list",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
},
},
}
}
// Check if a port list resource exists.
func resourcePortListExists(data *schema.ResourceData, provider interface{}) (bool, error) {
portListID := data.Id()
log.Printf("Check if port list '%s' exists.", portListID)
client := provider.(*providerState).Client()
portList, err := client.GetPortList(portListID)
exists := (portList != nil)
log.Printf("Port list '%s' exists: %t", portListID, true)
return exists, err
}
// Create a port list resource.
func resourcePortListCreate(data *schema.ResourceData, provider interface{}) error {
propertyHelper := propertyHelper(data)
networkDomainID := data.Get(resourceKeyPortListNetworkDomainID).(string)
name := data.Get(resourceKeyPortListName).(string)
description := data.Get(resourceKeyPortListDescription).(string)
childListIDs := propertyHelper.GetStringSetItems(resourceKeyPortListChildIDs)
var portListEntries []compute.PortListEntry
if propertyHelper.HasProperty(resourceKeyPortListPorts) {
// Port list entries from a simple set of ports.
simplePorts := propertyHelper.GetIntSetItems(resourceKeyPortListPorts)
for _, simplePort := range simplePorts {
portListEntries = append(portListEntries, compute.PortListEntry{
Begin: simplePort,
})
}
} else { // Default for backward compatibility
// Raw port list entries.
portListEntries = propertyHelper.GetPortListPorts()
}
log.Printf("Create port list '%s' in network domain '%s'.", name, networkDomainID)
client := provider.(*providerState).Client()
portListID, err := client.CreatePortList(name, description, networkDomainID, portListEntries, childListIDs)
if err != nil {
return err
}
data.SetId(portListID)
log.Printf("Successfully created port list '%s'.", portListID)
return nil
}
// Read a port list resource.
func resourcePortListRead(data *schema.ResourceData, provider interface{}) error {
portListID := data.Id()
networkDomainID := data.Get(resourceKeyPortListNetworkDomainID).(string)
log.Printf("Read port list '%s' in network domain '%s'.", portListID, networkDomainID)
client := provider.(*providerState).Client()
portList, err := client.GetPortList(portListID)
if err != nil {
return err
}
if portList == nil {
log.Printf("Port list '%s' not found in network domain '%s' (will treat as deleted).", portListID, networkDomainID)
data.SetId("") // Mark as deleted.
}
childListIDs := make([]string, len(portList.ChildLists))
for index, childList := range portList.ChildLists {
childListIDs[index] = childList.ID
}
propertyHelper := propertyHelper(data)
data.Set(resourceKeyPortListDescription, portList.Description)
propertyHelper.SetStringSetItems(resourceKeyPortListChildIDs, childListIDs)
if propertyHelper.HasProperty(resourceKeyPortListPorts) {
// Note that if the port 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 ports if resource state indicates it's necessary
// For example, if portListEntry.End is populated, then we need to switch over to complex ports.
var ports []int
for _, portListEntry := range portList.Ports {
ports = append(ports, portListEntry.Begin)
}
propertyHelper.SetIntSetItems(resourceKeyPortListPorts, ports)
} else { // Default for backward compatibility
propertyHelper.SetPortListPorts(portList.Ports)
}
return nil
}
// Update a port list resource.
func resourcePortListUpdate(data *schema.ResourceData, provider interface{}) error {
portListID := data.Id()
networkDomainID := data.Get(resourceKeyPortListNetworkDomainID).(string)
log.Printf("Update port list '%s' in network domain '%s'.", portListID, networkDomainID)
client := provider.(*providerState).Client()
portList, err := client.GetPortList(portListID)
if err != nil {
return err
}
if portList == nil {
log.Printf("Port list '%s' not found in network domain '%s' (will treat as deleted).", portListID, networkDomainID)
data.SetId("") // Mark as deleted.
return nil
}
propertyHelper := propertyHelper(data)
editRequest := portList.BuildEditRequest()
if data.HasChange(resourceKeyPortListDescription) {
editRequest.Description = data.Get(resourceKeyPortListDescription).(string)
}
if data.HasChange(resourceKeyPortListPort) || data.HasChange(resourceKeyPortListPorts) {
var portListEntries []compute.PortListEntry
if propertyHelper.HasProperty(resourceKeyPortListPorts) {
// Port list entries from a simple set of IP ports.
simplePorts := propertyHelper.GetIntSetItems(resourceKeyPortListPorts)
for _, simplePort := range simplePorts {
portListEntries = append(portListEntries, compute.PortListEntry{
Begin: simplePort,
})
}
} else { // Default for backward compatibility
// Raw port list entries.
portListEntries = propertyHelper.GetPortListPorts()
}
editRequest.Ports = portListEntries
}
if data.HasChange(resourceKeyPortListChildIDs) {
editRequest.ChildListIDs = propertyHelper.GetStringSetItems(resourceKeyPortListChildIDs)
}
err = client.EditPortList(portListID, editRequest)
if err != nil {
return err
}
log.Printf("Updated port list '%s'.", portListID)
return nil
}
// Delete a port list resource.
func resourcePortListDelete(data *schema.ResourceData, provider interface{}) error {
portListID := data.Id()
networkDomainID := data.Get(resourceKeyPortListNetworkDomainID).(string)
log.Printf("Delete port list '%s' in network domain '%s'.", portListID, networkDomainID)
client := provider.(*providerState).Client()
portList, err := client.GetPortList(portListID)
if err != nil {
return err
}
if portList == nil {
log.Printf("Port list '%s' not found in network domain '%s' (will treat as deleted).", portListID, networkDomainID)
return nil
}
err = client.DeletePortList(portListID)
if err != nil {
return err
}
log.Printf("Successfully deleted port list '%s' in network domain '%s'.", portListID, networkDomainID)
return nil
}