-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathresource_cloudstack_network_offering.go
More file actions
456 lines (389 loc) · 13 KB
/
Copy pathresource_cloudstack_network_offering.go
File metadata and controls
456 lines (389 loc) · 13 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package cloudstack
import (
"fmt"
"log"
"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceCloudStackNetworkOffering() *schema.Resource {
return &schema.Resource{
Create: resourceCloudStackNetworkOfferingCreate,
Read: resourceCloudStackNetworkOfferingRead,
Update: resourceCloudStackNetworkOfferingUpdate,
Delete: resourceCloudStackNetworkOfferingDelete,
Importer: &schema.ResourceImporter{
State: resourceCloudStackNetworkOfferingImport,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"display_text": {
Type: schema.TypeString,
Required: true,
},
"guest_ip_type": {
Type: schema.TypeString,
Required: true,
},
"traffic_type": {
Type: schema.TypeString,
Required: true,
},
"domain_id": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "the ID of the containing domain(s), null for public offerings",
ForceNew: true,
},
"network_rate": {
Type: schema.TypeInt,
Optional: true,
Description: "data transfer rate in megabits per second allowed",
ForceNew: true,
},
"network_mode": {
Type: schema.TypeString,
Optional: true,
Description: "Indicates the mode with which the network will operate. Valid option: NATTED or ROUTED",
ForceNew: true,
},
"max_connections": {
Type: schema.TypeInt,
Optional: true,
Description: "maximum number of concurrent connections supported by the network offering",
ForceNew: true,
},
"conserve_mode": {
Type: schema.TypeBool,
Optional: true,
Description: "true if the network offering is IP conserve mode enabled",
ForceNew: true,
},
"enable": {
Type: schema.TypeBool,
Optional: true,
Description: "set to true if the offering is to be enabled during creation. Default is false",
ForceNew: true,
},
"for_vpc": {
Type: schema.TypeBool,
Optional: true,
Description: "true if network offering is meant to be used for VPC, false otherwise.",
ForceNew: true,
},
"for_nsx": {
Type: schema.TypeBool,
Optional: true,
Description: "true if network offering is meant to be used for NSX, false otherwise",
ForceNew: true,
},
"internet_protocol": {
Type: schema.TypeString,
Optional: true,
Description: "The internet protocol of network offering. Options are ipv4 and dualstack. Default is ipv4. dualstack will create a network offering that supports both IPv4 and IPv6",
ForceNew: true,
},
"routing_mode": {
Type: schema.TypeString,
Optional: true,
Description: "the routing mode for the network offering. Supported types are: Static or Dynamic.",
ForceNew: true,
},
"specify_vlan": {
Type: schema.TypeBool,
Optional: true,
Description: "true if network offering supports vlans, false otherwise",
ForceNew: true,
},
"supported_services": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "the list of supported services",
ForceNew: true,
},
"service_provider_list": {
Type: schema.TypeMap,
Optional: true,
Description: "provider to service mapping. If not specified, the provider for the service will be mapped to the default provider on the physical network",
ForceNew: true,
},
"specify_ip_ranges": {
Type: schema.TypeBool,
Optional: true,
Description: "true if network offering supports specifying ip ranges; defaulted to false if not specified",
ForceNew: true,
},
"specify_as_number": {
Type: schema.TypeBool,
Optional: true,
Description: "true if network offering supports choosing AS number",
ForceNew: true,
},
},
}
}
func resourceCloudStackNetworkOfferingCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
name := d.Get("name").(string)
display_text := d.Get("display_text").(string)
guest_ip_type := d.Get("guest_ip_type").(string)
traffic_type := d.Get("traffic_type").(string)
// Create a new parameter struct
p := cs.NetworkOffering.NewCreateNetworkOfferingParams(display_text, guest_ip_type, name, traffic_type)
if guest_ip_type == "Shared" {
p.SetSpecifyvlan(true)
p.SetSpecifyipranges(true)
}
if v, ok := d.GetOk("domain_id"); ok {
p.SetDomainid(v.([]string))
}
if v, ok := d.GetOk("network_rate"); ok {
p.SetNetworkrate(v.(int))
}
if v, ok := d.GetOk("network_mode"); ok {
p.SetNetworkmode(v.(string))
}
if v, ok := d.GetOk("max_connections"); ok {
p.SetMaxconnections(v.(int))
}
if v, ok := d.GetOk("conserve_mode"); ok {
p.SetConservemode(v.(bool))
}
if v, ok := d.GetOk("enable"); ok {
p.SetEnable(v.(bool))
}
if v, ok := d.GetOk("for_vpc"); ok {
p.SetForvpc(v.(bool))
}
if v, ok := d.GetOk("for_nsx"); ok {
p.SetFornsx(v.(bool))
}
if v, ok := d.GetOk("internet_protocol"); ok {
p.SetInternetprotocol(v.(string))
}
if v, ok := d.GetOk("routing_mode"); ok {
p.SetRoutingmode(v.(string))
}
if v, ok := d.GetOk("specify_vlan"); ok {
p.SetSpecifyvlan(v.(bool))
}
var supported_services []string
if v, ok := d.GetOk("supported_services"); ok {
for _, supported_service := range v.(*schema.Set).List() {
supported_services = append(supported_services, supported_service.(string))
}
}
p.SetSupportedservices(supported_services)
if v, ok := d.GetOk("service_provider_list"); ok {
m := make(map[string]string)
for key, value := range v.(map[string]interface{}) {
m[key] = value.(string)
}
p.SetServiceproviderlist(m)
}
if v, ok := d.GetOk("specify_ip_ranges"); ok {
p.SetSpecifyipranges(v.(bool))
}
if v, ok := d.GetOk("specify_as_number"); ok {
p.SetSpecifyasnumber(v.(bool))
}
log.Printf("[DEBUG] Creating Network Offering %s", name)
n, err := cs.NetworkOffering.CreateNetworkOffering(p)
if err != nil {
return err
}
log.Printf("[DEBUG] Network Offering %s successfully created", name)
d.SetId(n.Id)
return resourceCloudStackNetworkOfferingRead(d, meta)
}
func resourceCloudStackNetworkOfferingUpdate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
name := d.Get("name").(string)
// Check if the name is changed and if so, update the network offering
if d.HasChange("name") {
log.Printf("[DEBUG] Name changed for %s, starting update", name)
// Create a new parameter struct
p := cs.NetworkOffering.NewUpdateNetworkOfferingParams()
// Set the new name
p.SetName(d.Get("name").(string))
// Update the name
_, err := cs.NetworkOffering.UpdateNetworkOffering(p)
if err != nil {
return fmt.Errorf(
"Error updating the name for network offering %s: %s", name, err)
}
}
// Check if the display text is changed and if so, update the network offering
if d.HasChange("display_text") {
log.Printf("[DEBUG] Display text changed for %s, starting update", name)
// Create a new parameter struct
p := cs.NetworkOffering.NewUpdateNetworkOfferingParams()
// Set the new display text
p.SetDisplaytext(d.Get("display_text").(string))
// Update the display text
_, err := cs.NetworkOffering.UpdateNetworkOffering(p)
if err != nil {
return fmt.Errorf(
"Error updating the display text for network offering %s: %s", name, err)
}
}
// Check if maxconnections is changed and if so, update the network offering
if d.HasChange("max_connections") {
log.Printf("[DEBUG] Max connections changed for %s, starting update", name)
// Create a new parameter struct
p := cs.NetworkOffering.NewUpdateNetworkOfferingParams()
// Set the new max connections
p.SetMaxconnections(d.Get("max_connections").(int))
// Update the max connections
_, err := cs.NetworkOffering.UpdateNetworkOffering(p)
if err != nil {
return fmt.Errorf(
"Error updating the max connections for network offering %s: %s", name, err)
}
}
// Check if the domain id is changed and if so, update the network offering
if d.HasChange("domain_id") {
log.Printf("[DEBUG] Domain id changed for %s, starting update", name)
// Create a new parameter struct
p := cs.NetworkOffering.NewUpdateNetworkOfferingParams()
// Set the new domain id
p.SetDomainid(d.Get("domain_id").(string))
// Update the traffic type
_, err := cs.NetworkOffering.UpdateNetworkOffering(p)
if err != nil {
return fmt.Errorf(
"Error updating the traffic type for network offering %s: %s", name, err)
}
}
return resourceCloudStackInstanceRead(d, meta)
}
func resourceCloudStackNetworkOfferingDelete(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Create a new parameter struct
p := cs.NetworkOffering.NewDeleteNetworkOfferingParams(d.Id())
_, err := cs.NetworkOffering.DeleteNetworkOffering(p)
if err != nil {
return fmt.Errorf("Error deleting Network Offering: %s", err)
}
return nil
}
func resourceCloudStackNetworkOfferingImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
cs := meta.(*cloudstack.CloudStackClient)
// Read looks the offering up by name, so resolve the name from the ID first
n, count, err := cs.NetworkOffering.GetNetworkOfferingByID(d.Id())
if err != nil {
if count == 0 {
return nil, fmt.Errorf("network offering with ID %s does not exist", d.Id())
}
return nil, err
}
d.Set("name", n.Name)
return []*schema.ResourceData{d}, nil
}
func resourceCloudStackNetworkOfferingRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
log.Printf("[DEBUG] Retrieving Network Offering %s", d.Get("name").(string))
// Get the Network Offering details
n, count, err := cs.NetworkOffering.GetNetworkOfferingByName(d.Get("name").(string))
if err != nil {
if count == 0 {
log.Printf("[DEBUG] Network Offering %s does no longer exist", d.Get("name").(string))
d.SetId("")
return nil
}
return err
}
d.SetId(n.Id)
d.Set("name", n.Name)
d.Set("display_text", n.Displaytext)
d.Set("guest_ip_type", n.Guestiptype)
d.Set("traffic_type", n.Traffictype)
if _, ok := d.GetOk("network_rate"); ok {
d.Set("network_rate", n.Networkrate)
}
if _, ok := d.GetOk("network_mode"); ok {
// Only set if CloudStack supports and returns this field (4.20.0+)
// Older versions may not support network_mode
if n.Networkmode != "" {
d.Set("network_mode", n.Networkmode)
}
}
if _, ok := d.GetOk("conserve_mode"); ok {
d.Set("conserve_mode", n.Conservemode)
}
if _, ok := d.GetOk("enable"); ok {
d.Set("enable", n.State == "Enabled")
}
if _, ok := d.GetOk("for_vpc"); ok {
d.Set("for_vpc", n.Forvpc)
}
if _, ok := d.GetOk("for_nsx"); ok {
d.Set("for_nsx", n.Fornsx)
}
if _, ok := d.GetOk("specify_vlan"); ok {
d.Set("specify_vlan", n.Specifyvlan)
}
if _, ok := d.GetOk("specify_ip_ranges"); ok {
d.Set("specify_ip_ranges", n.Specifyipranges)
}
if _, ok := d.GetOk("specify_as_number"); ok {
d.Set("specify_as_number", n.Specifyasnumber)
}
if _, ok := d.GetOk("internet_protocol"); ok {
d.Set("internet_protocol", n.Internetprotocol)
}
if _, ok := d.GetOk("routing_mode"); ok {
// Only set if CloudStack supports and returns this field (4.20.0+)
// Older versions may not support routing_mode
if n.Routingmode != "" {
d.Set("routing_mode", n.Routingmode)
}
}
if _, ok := d.GetOk("max_connections"); ok {
log.Printf("[DEBUG] Max connections configured: %d, CloudStack returned: %d", d.Get("max_connections").(int), n.Maxconnections)
if n.Maxconnections > 0 {
d.Set("max_connections", n.Maxconnections)
}
}
if _, ok := d.GetOk("supported_services"); ok && len(n.Service) > 0 {
services := make([]string, len(n.Service))
for i, service := range n.Service {
services[i] = service.Name
}
d.Set("supported_services", services)
}
if _, ok := d.GetOk("service_provider_list"); ok && len(n.Service) > 0 {
serviceProviders := make(map[string]string)
for _, service := range n.Service {
if len(service.Provider) > 0 {
serviceProviders[service.Name] = service.Provider[0].Name
}
}
d.Set("service_provider_list", serviceProviders)
}
return nil
}