-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_ip_address_reservation.go
230 lines (186 loc) · 5.96 KB
/
resource_ip_address_reservation.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
package ddcloud
import (
"fmt"
"log"
"github.com/DimensionDataResearch/dd-cloud-compute-terraform/validators"
"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
"github.com/hashicorp/terraform/helper/schema"
)
const (
resourceKeyIPAddressReservationVLANID = "vlan"
resourceKeyIPAddressReservationAddress = "address"
resourceKeyIPAddressReservationAddressType = "address_type"
addressTypeIPv4 = "ipv4"
addressTypeIPv6 = "ipv6"
)
func resourceIPAddressReservation() *schema.Resource {
return &schema.Resource{
Exists: resourceIPAddressReservationExists,
Create: resourceIPAddressReservationCreate,
Read: resourceIPAddressReservationRead,
Delete: resourceIPAddressReservationDelete,
Schema: map[string]*schema.Schema{
resourceKeyIPAddressReservationVLANID: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The Id of the VLAN in which the IP address is reserved.",
},
resourceKeyIPAddressReservationAddress: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The reserved IP address.",
},
resourceKeyIPAddressReservationAddressType: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The reserved IP address type ('ipv4' or 'ipv6').",
ValidateFunc: validators.StringIsOneOf("IP address type",
addressTypeIPv4,
addressTypeIPv6,
),
},
},
}
}
func resourceIPAddressReservationExists(data *schema.ResourceData, provider interface{}) (exists bool, err error) {
vlanID := data.Get(resourceKeyIPAddressReservationVLANID).(string)
address := data.Get(resourceKeyIPAddressReservationAddress).(string)
addressType := data.Get(resourceKeyIPAddressReservationAddressType).(string)
log.Printf("Check if IP address '%s' ('%s') is reserved in VLAN '%s'...",
address,
addressType,
vlanID,
)
providerState := provider.(*providerState)
var reservedIPAddresses map[string]compute.ReservedIPAddress
reservedIPAddresses, err = getReservedIPAddresses(vlanID, addressType, providerState)
if err != nil {
return
}
_, exists = reservedIPAddresses[address]
if exists {
log.Printf("IP address '%s' ('%s') is reserved in VLAN '%s'.",
address,
addressType,
vlanID,
)
} else {
log.Printf("IP address '%s' ('%s') is not reserved in VLAN '%s'.",
address,
addressType,
vlanID,
)
}
return
}
func resourceIPAddressReservationCreate(data *schema.ResourceData, provider interface{}) (err error) {
vlanID := data.Get(resourceKeyIPAddressReservationVLANID).(string)
address := data.Get(resourceKeyIPAddressReservationAddress).(string)
addressType := data.Get(resourceKeyIPAddressReservationAddressType).(string)
log.Printf("Reserving IP address '%s' ('%s') in VLAN '%s'...",
address,
addressType,
vlanID,
)
providerState := provider.(*providerState)
apiClient := providerState.Client()
switch addressType {
case addressTypeIPv4:
err = apiClient.ReservePrivateIPv4Address(vlanID, address)
case addressTypeIPv6:
err = apiClient.ReserveIPv6Address(vlanID, address)
default:
err = fmt.Errorf("Invalid address type '%s'", addressType)
}
if err != nil {
return
}
data.SetId(fmt.Sprintf("%s/%s",
vlanID, addressType,
))
log.Printf("Reserved IP address '%s' ('%s') in VLAN '%s'.",
address,
addressType,
vlanID,
)
return
}
func resourceIPAddressReservationRead(data *schema.ResourceData, provider interface{}) error {
// Nothing to do.
return nil
}
func resourceIPAddressReservationUpdate(data *schema.ResourceData, provider interface{}) error {
// Nothing to do.
return nil
}
func resourceIPAddressReservationDelete(data *schema.ResourceData, provider interface{}) (err error) {
vlanID := data.Get(resourceKeyIPAddressReservationVLANID).(string)
address := data.Get(resourceKeyIPAddressReservationAddress).(string)
addressType := data.Get(resourceKeyIPAddressReservationAddressType).(string)
providerState := provider.(*providerState)
apiClient := providerState.Client()
switch addressType {
case addressTypeIPv4:
err = apiClient.UnreservePrivateIPv4Address(vlanID, address)
case addressTypeIPv6:
err = apiClient.UnreserveIPv6Address(vlanID, address)
default:
err = fmt.Errorf("Invalid address type '%s'", addressType)
}
if err != nil {
return
}
data.SetId("")
return
}
func getReservedIPAddresses(vlanID string, addressType string, providerState *providerState) (map[string]compute.ReservedIPAddress, error) {
switch addressType {
case addressTypeIPv4:
return getReservedPrivateIPv4Addresses(vlanID, providerState)
case addressTypeIPv6:
return getReservedIPv6Addresses(vlanID, providerState)
default:
return nil, fmt.Errorf("Invalid address type '%s'", addressType)
}
}
func getReservedPrivateIPv4Addresses(vlanID string, providerState *providerState) (map[string]compute.ReservedIPAddress, error) {
apiClient := providerState.Client()
reservedIPAddresses := make(map[string]compute.ReservedIPAddress)
page := compute.DefaultPaging()
for {
reservations, err := apiClient.ListReservedPrivateIPv4AddressesInVLAN(vlanID)
if err != nil {
return nil, err
}
if reservations.IsEmpty() {
break
}
for _, reservedIPAddress := range reservations.Items {
reservedIPAddresses[reservedIPAddress.IPAddress] = reservedIPAddress
}
page.Next()
}
return reservedIPAddresses, nil
}
func getReservedIPv6Addresses(vlanID string, providerState *providerState) (map[string]compute.ReservedIPAddress, error) {
apiClient := providerState.Client()
reservedIPAddresses := make(map[string]compute.ReservedIPAddress)
page := compute.DefaultPaging()
for {
reservations, err := apiClient.ListReservedIPv6AddressesInVLAN(vlanID)
if err != nil {
return nil, err
}
if reservations.IsEmpty() {
break
}
for _, reservedIPAddress := range reservations.Items {
reservedIPAddresses[reservedIPAddress.IPAddress] = reservedIPAddress
}
page.Next()
}
return reservedIPAddresses, nil
}