forked from peterbale/terraform-provider-phpipam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_address.go
341 lines (320 loc) · 9.34 KB
/
resource_address.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
339
340
341
package main
import (
"errors"
"fmt"
"log"
"strconv"
"sync"
"github.com/hashicorp/terraform/helper/schema"
"github.com/peterbale/go-phpipam"
)
var addonLock sync.Mutex
// AddressInformation struct to define ipaddress data
type AddressInformation struct {
Hostname string
IP string
Section string
Subnet string
Broadcast string
Gateway string
BitMask string
Index string
}
func resourcePhpIPAMAddress() *schema.Resource {
return &schema.Resource{
Create: resourcePhpIPAMAddressCreate,
Read: resourcePhpIPAMAddressRead,
Update: resourcePhpIPAMAddressrUpdate,
Delete: resourcePhpIPAMAddressDelete,
Schema: map[string]*schema.Schema{
"hostname": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"section": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"subnet": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"ip_address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"broadcast": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"gateway": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"bitmask": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"index": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func (c *Client) findSectionID(section string) (string, error) {
var sectionID string
sections, err := c.PhpIPAMClient.GetSections()
if err != nil {
return sectionID, err
}
for _, element := range sections.Data {
if element.Name == section {
sectionID = element.ID
}
}
if len(sectionID) == 0 {
return sectionID, errors.New("Section Not Found")
}
return sectionID, nil
}
func (c *Client) findSubnetID(sectionID string, subnet string) (string, error) {
var subnetID string
subnets, err := c.PhpIPAMClient.GetSectionsSubnets(sectionID)
if err != nil {
return subnetID, err
}
for _, element := range subnets.Data {
if element.Description == subnet {
subnetID = element.ID
}
}
if len(subnetID) == 0 {
return subnetID, errors.New("Subnet Not Found")
}
return subnetID, nil
}
func (c *Client) findExistingAddress(hostname string, index string) (string, error) {
var address string
var totalFoundAddresses int
addresses, err := c.PhpIPAMClient.GetAddressSearch(hostname)
if err != nil {
return address, err
}
totalFoundAddresses = len(addresses.Data)
if totalFoundAddresses == 0 {
return address, nil
}
if len(index) > 0 {
totalFoundAddresses = 0
for _, i := range addresses.Data {
if i.Description == index {
totalFoundAddresses++
if totalFoundAddresses > 1 {
return address, errors.New("Multiple Indexed Addresses Found")
}
address = i.IP
}
}
} else if totalFoundAddresses > 1 {
return address, errors.New("Multiple Addresses Found")
} else {
address = addresses.Data[0].IP
}
return address, nil
}
func (c *Client) getAddressID(address string) (string, error) {
var addressID string
addressSearchIP, err := c.PhpIPAMClient.GetAddressSearchIP(address)
if err != nil {
return addressID, err
}
if len(addressSearchIP.Data) != 1 {
return addressID, errors.New("Address Over Allocated")
}
return addressSearchIP.Data[0].ID, nil
}
func (c *Client) getAddressInformation(addressID string) (*AddressInformation, error) {
addressInformation := new(AddressInformation)
var subnetID, sectionID string
addressData, err := c.PhpIPAMClient.GetAddress(addressID)
if err != nil {
return nil, err
}
if addressData.Code == 200 {
addressInformation.Hostname = addressData.Data.Hostname
addressInformation.IP = addressData.Data.IP
subnetID = addressData.Data.SubnetID
} else {
return nil, nil
}
addressSearchData, err := c.PhpIPAMClient.GetAddressSearch(addressInformation.Hostname)
if err != nil {
return nil, err
}
if addressSearchData.Code == 200 {
for _, address := range addressSearchData.Data {
if address.ID == addressID {
_, err = strconv.Atoi(address.Description)
if err == nil {
addressInformation.Index = address.Description
}
}
}
}
subnetData, err := c.PhpIPAMClient.GetSubnet(subnetID)
if err != nil {
return nil, err
}
if subnetData.Code == 200 {
addressInformation.Subnet = subnetData.Data.Description
addressInformation.Broadcast = subnetData.Data.Calculation.Broadcast
addressInformation.Gateway = subnetData.Data.Gateway.IPAddress
addressInformation.BitMask = subnetData.Data.Calculation.BitMask
sectionID = subnetData.Data.SectionID
} else {
return nil, errors.New("Address Subnet Not Found")
}
sectionData, err := c.PhpIPAMClient.GetSection(sectionID)
if err != nil {
return nil, err
}
if sectionData.Code == 200 {
addressInformation.Section = sectionData.Data.Name
} else {
return nil, errors.New("Subnet Section Not Found")
}
return addressInformation, nil
}
func checkAddressSubnet(existingSubnetID string, subnetID string) int {
var subnetMatchBool int
if existingSubnetID == subnetID {
subnetMatchBool = 1
} else {
subnetMatchBool = 0
}
return subnetMatchBool
}
func (c *Client) allocateNewAddress(subnetID string, hostname string, index string) (phpipam.AddressFirstFree, error) {
newAddress, err := c.PhpIPAMClient.CreateAddressFirstFree(subnetID, hostname, "terraform", index)
if err != nil {
return newAddress, err
}
return newAddress, nil
}
func (c *Client) deleteExistingAddress(addressID string) (phpipam.AddressDelete, error) {
deleteAddress, err := c.PhpIPAMClient.DeleteAddress(addressID)
if err != nil {
return deleteAddress, err
}
return deleteAddress, nil
}
func (c *Client) create(section string, subnet string, hostname string, update bool, index string) (string, error) {
addonLock.Lock()
defer addonLock.Unlock()
var addressID string
sectionID, err := c.findSectionID(section)
if err != nil {
return addressID, fmt.Errorf("Error Getting Section ID: %s", err)
}
subnetID, err := c.findSubnetID(sectionID, subnet)
if err != nil {
return addressID, fmt.Errorf("Error Getting Subnet ID: %s", err)
}
address, err := c.findExistingAddress(hostname, index)
if err != nil {
return addressID, fmt.Errorf("Error Finding Existing Addresses: %s", err)
}
if len(address) == 0 || update {
log.Printf("[DEBUG] New Address Section ID: %#v, Subnet ID: %#v", sectionID, subnetID)
newAddress, err := c.allocateNewAddress(subnetID, hostname, index)
if err != nil {
return addressID, fmt.Errorf("Error Allocating New Address: %s", err)
}
log.Printf("[DEBUG] New Address IP: %#v", newAddress)
addressID, err = c.getAddressID(newAddress.IP)
if err != nil {
return addressID, fmt.Errorf("Error Getting Created Address ID: %s", newAddress.IP)
}
log.Printf("[INFO] New Address Allocated: %s", newAddress.IP)
} else {
log.Printf("[DEBUG] Existing Address Section ID: %#v, Subnet ID: %#v", sectionID, subnetID)
log.Printf("[DEBUG] Existing Address IP: %#v", address)
addressID, err = c.getAddressID(address)
if err != nil {
return addressID, fmt.Errorf("Error Getting Created Address ID: %s", address)
}
log.Printf("[INFO] New Address Allocated: %s", address)
}
return addressID, nil
}
func (c *Client) delete(addressID string, update bool) error {
_, err := c.deleteExistingAddress(addressID)
if err != nil {
return fmt.Errorf("Delete Address Failed: %s", err)
}
log.Printf("[INFO] Address Removed: %s", addressID)
return nil
}
func resourcePhpIPAMAddressCreate(d *schema.ResourceData, m interface{}) error {
section := d.Get("section").(string)
subnet := d.Get("subnet").(string)
hostname := d.Get("hostname").(string)
index := d.Get("index").(string)
client := m.(*Client)
addressID, err := client.create(section, subnet, hostname, false, index)
if err != nil {
return err
}
d.SetId(addressID)
return resourcePhpIPAMAddressRead(d, m)
}
func resourcePhpIPAMAddressRead(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
log.Printf("[INFO] Address ID Created: %s", d.Id())
addressInformation, err := client.getAddressInformation(d.Id())
if err != nil {
return fmt.Errorf("Cannot Get Address Infomation: %s", err)
}
d.Set("hostname", addressInformation.Hostname)
d.Set("section", addressInformation.Section)
d.Set("subnet", addressInformation.Subnet)
d.Set("ip_address", addressInformation.IP)
d.Set("broadcast", addressInformation.Broadcast)
d.Set("gateway", addressInformation.Gateway)
d.Set("bitmask", addressInformation.BitMask)
d.Set("index", addressInformation.Index)
return nil
}
func resourcePhpIPAMAddressrUpdate(d *schema.ResourceData, m interface{}) error {
section := d.Get("section").(string)
subnet := d.Get("subnet").(string)
hostname := d.Get("hostname").(string)
index := d.Get("index").(string)
client := m.(*Client)
addressID := d.Id()
var err error
if d.HasChange("hostname") {
_, err = client.PhpIPAMClient.PatchUpdateAddress(hostname, addressID)
if err != nil {
return fmt.Errorf("Address Update Failed: %s", err)
}
log.Printf("[INFO] Address Updated: %s", hostname)
} else {
newAddressID, err := client.create(section, subnet, hostname, true, index)
if err != nil {
return err
}
err = client.delete(addressID, true)
if err != nil {
return err
}
d.SetId(newAddressID)
}
return resourcePhpIPAMAddressRead(d, m)
}
func resourcePhpIPAMAddressDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
err := client.delete(d.Id(), false)
return err
}