/
datasource_vcd_ip_space_ip_allocation.go
124 lines (111 loc) · 3.65 KB
/
datasource_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
package vcd
import (
"context"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func datasourceVcdIpAllocation() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceVcdIpAllocationRead,
Schema: map[string]*schema.Schema{
"org_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Org ID",
},
"ip_space_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "IP Space ID",
},
"ip_address": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "IP Address or Prefix of the allocation",
},
"type": {
Type: schema.TypeString,
Required: true,
Description: "Type of IP Allocation. One of 'FLOATING_IP' or 'IP_PREFIX'",
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "IP Allocation Description",
},
"prefix_length": {
Type: schema.TypeString,
Computed: true,
Description: "Exposed only if 'type' is 'IP_PREFIX'",
},
"usage_state": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "One of 'UNUSED', 'USED', 'USED_MANUAL",
},
"used_by_id": {
Type: schema.TypeString,
Computed: true,
Description: "An ID of entity this IP Allocation is assigned to",
},
"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: "Allocated IP address",
},
},
}
}
func datasourceVcdIpAllocationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[TRACE] IP Space IP Allocation DS read initiated")
vcdClient := meta.(*VCDClient)
orgId := d.Get("org_id").(string)
ipSpaceId := d.Get("ip_space_id").(string)
ipAddress := d.Get("ip_address").(string)
ipAllocationType := d.Get("type").(string)
org, err := vcdClient.GetOrgById(orgId)
if err != nil {
return diag.Errorf("error getting Org by id: %s", err)
}
ipAllocation, err := org.GetIpSpaceAllocationByTypeAndValue(ipSpaceId, ipAllocationType, ipAddress, nil)
if err != nil {
return diag.Errorf("error getting 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)
// 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])
}
}
if ipAllocation.IpSpaceIpAllocation.Type == "FLOATING_IP" {
dSet(d, "ip", ipAllocation.IpSpaceIpAllocation.Value)
}
d.SetId(ipAllocation.IpSpaceIpAllocation.ID)
return nil
}