/
resource_vcd_ip_space_ip_allocation.go
338 lines (288 loc) · 11.6 KB
/
resource_vcd_ip_space_ip_allocation.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
333
334
335
336
337
338
package vcd
import (
"context"
"fmt"
"log"
"regexp"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/go-vcloud-director/v2/types/v56"
"github.com/vmware/go-vcloud-director/v2/util"
)
func resourceVcdIpAllocation() *schema.Resource {
return &schema.Resource{
CreateContext: resourceVcdIpAllocationCreate,
ReadContext: resourceVcdIpAllocationRead,
UpdateContext: resourceVcdIpAllocationUpdate,
DeleteContext: resourceVcdIpAllocationDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceVcdIpAllocationImport,
},
Schema: map[string]*schema.Schema{
"org_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Org ID for IP Allocation",
},
"ip_space_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "IP Space ID for IP Allocation",
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Type of allocation. One of `FLOATING_IP``, `IP_PREFIX`",
ValidateFunc: validation.StringInSlice([]string{"FLOATING_IP", "IP_PREFIX"}, false),
},
"usage_state": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Can be set to 'USED_MANUAL' to mark the IP Allocation for manual use",
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Custom description can only be set when usage_state is set to 'USED_MANUAL'",
},
"prefix_length": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Computed: true,
Description: "Required if 'type' is IP_PREFIX and no custom 'value` is provided",
ConflictsWith: []string{"value"},
},
"value": {
Type: schema.TypeString,
Optional: true,
Description: "IP address or CIDR to use. (VCD 10.4.2+)",
ConflictsWith: []string{"prefix_length"},
ForceNew: true, // Once a particular IP or Prefix is allocated - its changes are ignored by the API
// API supports allocation of IP ranges (e.g. 10.10.10.1-10.10.10.3), but this
// results in multiple separate allocations with separate IDs which goes against
// Terraform principle. We have 'quantity' field disabled for the same reason.
// Users can define multiple instances of this resource to allocate IP ranges
ValidateFunc: validation.StringDoesNotMatch(
regexp.MustCompile("-"), // having a hyphen '-' in the value means that it is an IP range
"This resource does not support allocating IP ranges due to Terraform resources map to single entity. "+
"Please use multiple resource instances to allocate multiple IP addresses",
),
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
Description: "IP address or CIDR",
},
"used_by_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of entity that is using this allocation",
},
"allocation_date": {
Type: schema.TypeString,
Computed: true,
Description: "Allocation date in ISO 8601 format (e.g. 2023-06-07T09:57:58.721Z)",
},
"ip": {
Type: schema.TypeString,
Computed: true,
Description: "IP address part",
},
// The resource supports 'quantity' parameter, however it goes against Terraform concept
// of - "one resource:one entity" mapping therefore it is left commented for now unless
// a real use case appears and we have to hack around Terraform schema
// "quantity": {
// Type: schema.TypeString,
// Optional: true,
// Default: "1",
// ForceNew: true,
// Description: "Number of entries to allocate",
// },
},
}
}
func resourceVcdIpAllocationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[TRACE] IP Space IP Allocation creation initiated")
vcdClient := meta.(*VCDClient)
orgId := d.Get("org_id").(string)
org, err := vcdClient.GetOrgById(orgId)
if err != nil {
return diag.Errorf("error getting Org by ID: %s", err)
}
ipSpaceId := d.Get("ip_space_id").(string)
ipSpace, err := vcdClient.GetIpSpaceById(ipSpaceId)
if err != nil {
return diag.Errorf("error getting IP Space by ID '%s': %s", ipSpaceId, err)
}
allocationConfig := types.IpSpaceIpAllocationRequest{
Type: d.Get("type").(string),
Quantity: addrOf(1),
}
prefixLength := d.Get("prefix_length").(string)
if prefixLength != "" {
intPrefixLength, _ := strconv.Atoi(prefixLength)
allocationConfig.PrefixLength = &intPrefixLength
}
if d.Get("value").(string) != "" {
// API throws an exception if this is specified for older versions
if vcdClient.Client.APIVCDMaxVersionIs("< 37.2") {
return diag.Errorf("'value' can only be specified on VCD 10.4.2+")
}
allocationConfig.Value = d.Get("value").(string)
allocationConfig.Quantity = nil // Quantity field must not be set when 'value' is specified
}
allocation, err := ipSpace.AllocateIp(orgId, org.Org.Name, &allocationConfig)
if err != nil {
return diag.Errorf("error allocating IP: %s", err)
}
d.SetId(allocation[0].ID)
dSet(d, "ip_address", allocation[0].Value)
// Perform manual reservation if there is a request for USED_MANUAL (it always needs a separate
// API call)
// * UNUSED - the allocated IP is current not being used in the system.
// * USED - the allocated IP is currently in use in the system. An allocated IP address or IP
// Prefix is considered used if it is being used in network services such as NAT rule or in Org
// VDC network definition.
// * USED_MANUAL - manual usage reservation with custom description
// If user specified
usageState := d.Get("usage_state").(string)
if usageState == "USED_MANUAL" {
// Retrieve IP Allocation object
ipSpaceAllocation, err := org.GetIpSpaceAllocationById(ipSpaceId, allocation[0].ID)
if err != nil {
return diag.Errorf("error retrieving IP Space IP Allocation after request: %s", err)
}
ipAllocationUpdate := &types.IpSpaceIpAllocation{
ID: allocation[0].ID,
Type: d.Get("type").(string),
UsageState: usageState,
Description: d.Get("description").(string),
Value: ipSpaceAllocation.IpSpaceIpAllocation.Value,
}
_, err = ipSpaceAllocation.Update(ipAllocationUpdate)
if err != nil {
return diag.Errorf("error updating IP Space IP Allocation after creation: %s", err)
}
}
return resourceVcdIpAllocationRead(ctx, d, meta)
}
func resourceVcdIpAllocationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[TRACE] IP Space IP Allocation update initiated")
vcdClient := meta.(*VCDClient)
orgId := d.Get("org_id").(string)
ipSpaceId := d.Get("ip_space_id").(string)
org, err := vcdClient.GetOrgById(orgId)
if err != nil {
return diag.Errorf("error getting Org by ID: %s", err)
}
ipAllocation, err := org.GetIpSpaceAllocationById(ipSpaceId, d.Id())
if err != nil {
return diag.Errorf("error retrieving IP Allocation: %s", err)
}
if d.HasChange("usage_state") || d.HasChange("description") {
ipAllocation.IpSpaceIpAllocation.UsageState = d.Get("usage_state").(string)
ipAllocation.IpSpaceIpAllocation.Description = d.Get("description").(string)
_, err = ipAllocation.Update(ipAllocation.IpSpaceIpAllocation)
if err != nil {
return diag.Errorf("error updating IP Space IP Allocation: %s", err)
}
}
return resourceVcdIpAllocationRead(ctx, d, meta)
}
func resourceVcdIpAllocationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[TRACE] IP Space IP Allocation read initiated")
vcdClient := meta.(*VCDClient)
orgId := d.Get("org_id").(string)
ipSpaceId := d.Get("ip_space_id").(string)
org, err := vcdClient.GetOrgById(orgId)
if err != nil {
return diag.Errorf("error getting Org by id: %s", err)
}
ipAllocation, err := org.GetIpSpaceAllocationById(ipSpaceId, d.Id())
if err != nil {
return diag.Errorf("error getting IP Space IP Allocation: %s", err)
}
dSet(d, "description", ipAllocation.IpSpaceIpAllocation.Description)
dSet(d, "type", ipAllocation.IpSpaceIpAllocation.Type)
if ipAllocation.IpSpaceIpAllocation.OrgRef != nil {
dSet(d, "org_id", ipAllocation.IpSpaceIpAllocation.OrgRef.ID)
}
if ipAllocation.IpSpaceIpAllocation.UsedByRef != nil {
dSet(d, "used_by_id", ipAllocation.IpSpaceIpAllocation.UsedByRef.ID)
}
dSet(d, "allocation_date", ipAllocation.IpSpaceIpAllocation.AllocationDate)
dSet(d, "usage_state", ipAllocation.IpSpaceIpAllocation.UsageState)
dSet(d, "ip_address", ipAllocation.IpSpaceIpAllocation.Value)
// When IP Prefix is allocated, the returned value is in CIDR format (e.g. 192.168.1.0/24), and
// although it can be split using Terraform native functions, we're adding a convenience layer for
// users by splitting this address into IP and prefix length
if ipAllocation.IpSpaceIpAllocation.Type == "IP_PREFIX" {
splitCidr := strings.Split(ipAllocation.IpSpaceIpAllocation.Value, "/")
if len(splitCidr) == 2 {
dSet(d, "ip", splitCidr[0])
dSet(d, "prefix_length", splitCidr[1])
} else {
util.Logger.Printf("[TRACE] resourceVcdIpAllocationRead Unable to store split CIDR (value %s, split length %d)\n",
ipAllocation.IpSpaceIpAllocation.Value, len(ipAllocation.IpSpaceIpAllocation.Value))
}
}
if ipAllocation.IpSpaceIpAllocation.Type == "FLOATING_IP" {
dSet(d, "ip", ipAllocation.IpSpaceIpAllocation.Value)
}
return nil
}
func resourceVcdIpAllocationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[TRACE] IP Space IP Allocation deletion initiated")
vcdClient := meta.(*VCDClient)
orgId := d.Get("org_id").(string)
ipSpaceId := d.Get("ip_space_id").(string)
org, err := vcdClient.GetOrgById(orgId)
if err != nil {
return diag.Errorf("error getting Org by id: %s", err)
}
ipAllocation, err := org.GetIpSpaceAllocationById(ipSpaceId, d.Id())
if err != nil {
return diag.Errorf("error getting IP Space IP Allocation: %s", err)
}
err = ipAllocation.Delete()
if err != nil {
return diag.Errorf("error deleting IP Space IP Allocation: %s", err)
}
return nil
}
func resourceVcdIpAllocationImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
log.Printf("[TRACE] IP Allocation import initiated")
resourceURI := strings.SplitN(d.Id(), ImportSeparator, 4)
if len(resourceURI) != 4 {
return nil, fmt.Errorf("resource name must be specified as org-name.ip-space-name.ip-allocation-type.ip-allocation-ip")
}
orgName, ipSpaceName, ipAllocationType, ipAllocationIp := resourceURI[0], resourceURI[1], resourceURI[2], resourceURI[3]
vcdClient := meta.(*VCDClient)
org, err := vcdClient.GetOrgByName(orgName)
if err != nil {
return nil, fmt.Errorf("error retrieving Org '%s': %s", orgName, err)
}
ipSpace, err := vcdClient.GetIpSpaceByName(ipSpaceName)
if err != nil {
return nil, fmt.Errorf("error retrieving IP Space %s: %s ", ipSpaceName, err)
}
ipAllocation, err := org.GetIpSpaceAllocationByTypeAndValue(ipSpace.IpSpace.ID, ipAllocationType, ipAllocationIp, nil)
if err != nil {
return nil, fmt.Errorf("error retrieving IP Allocation: %s", err)
}
// Only setting Org because VDC is a deprecated field. `owner_id` is set by resourceVcdNsxtEdgeGatewayRead by itself
dSet(d, "org_id", org.Org.ID)
dSet(d, "ip_space_id", ipSpace.IpSpace.ID)
dSet(d, "type", ipSpace.IpSpace.Type)
d.SetId(ipAllocation.IpSpaceIpAllocation.ID)
return []*schema.ResourceData{d}, nil
}