-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresource_ipaddress.go
130 lines (119 loc) · 3.4 KB
/
resource_ipaddress.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
package main
import (
"fmt"
"net/http"
"strings"
"sync"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/go-resty/resty/v2"
)
var resourceIPAddressCreateAvailableMutex = &sync.Mutex{}
func resourceIPAddress() *schema.Resource {
return &schema.Resource{
Create: resourceIPAddressCreate,
Read: resourceIPAddressRead,
Update: resourceIPAddressUpdate,
Delete: resourceIPAddressDelete,
Schema: map[string]*schema.Schema{
"prefix_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"address_cidr": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"dns_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
type IPAddress struct {
Id int `json:"id"`
Address string `json:"address"`
DNSName string `json:"dns_name"`
}
func resourceIPAddressCreate(d *schema.ResourceData, m interface{}) error {
var body IPAddress
var url string
client := m.(*resty.Client)
if ip, ok := d.GetOk("address_cidr"); ok {
body = IPAddress{Address: ip.(string), DNSName: d.Get("dns_name").(string)}
url = "/ipam/ip-addresses/"
} else {
if id, ok := d.GetOk("prefix_id"); !ok {
return fmt.Errorf("prefix_id is required when no address is set")
} else {
url = fmt.Sprintf("/ipam/prefixes/%s/available-ips/", id.(string))
}
resourceIPAddressCreateAvailableMutex.Lock()
defer resourceIPAddressCreateAvailableMutex.Unlock()
body = IPAddress{DNSName: d.Get("dns_name").(string)}
}
resp, err := client.R().
SetBody(body).
SetResult(IPAddress{}).
Post(url)
if err != nil {
return err
}
if resp.StatusCode() != http.StatusCreated {
return fmt.Errorf("POST: Unexpected HTTP status: %s", resp.Status())
}
ipaddress := resp.Result().(*IPAddress)
d.SetId(fmt.Sprintf("%d", ipaddress.Id))
return resourceIPAddressRead(d, m)
}
func resourceIPAddressRead(d *schema.ResourceData, m interface{}) error {
client := m.(*resty.Client)
resp, err := client.R().
SetResult(IPAddress{}).
Get(fmt.Sprintf("/ipam/ip-addresses/%s/", d.Id()))
if err != nil {
return err
}
if resp.StatusCode() != http.StatusOK {
return fmt.Errorf("GET: Unexpected HTTP status: %s", resp.Status())
}
ipaddress := resp.Result().(*IPAddress)
d.Set("address_cidr", ipaddress.Address)
address, _ := SplitAddressMask(ipaddress.Address)
d.Set("address", address)
return nil
}
func resourceIPAddressUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*resty.Client)
resp, err := client.R().
SetBody(IPAddress{Address: d.Get("address_cidr").(string), DNSName: d.Get("dns_name").(string)}).
Put(fmt.Sprintf("/ipam/ip-addresses/%s/", d.Id()))
if err != nil {
return err
}
if resp.StatusCode() != http.StatusOK {
return fmt.Errorf("PUT: Unexpected HTTP status: %s", resp.Status())
}
return resourceIPAddressRead(d, m)
}
func resourceIPAddressDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*resty.Client)
resp, err := client.R().
Delete(fmt.Sprintf("/ipam/ip-addresses/%s/", d.Id()))
if err != nil {
return err
}
if resp.StatusCode() != http.StatusNoContent {
return fmt.Errorf("DELETE: Unexpected HTTP status: %s", resp.Status())
}
return nil
}
func SplitAddressMask(address string) (string, string) {
x := strings.SplitN(address, "/", 2)
return x[0], x[1]
}