forked from aiven/aiven-go-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
static_ips.go
138 lines (112 loc) · 3.63 KB
/
static_ips.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
package aiven
import "fmt"
type (
// StaticIPsHandler aiven go-client handler for static ips
StaticIPsHandler struct {
client *Client
}
// CreateStaticIPRequest Aiven API request
// POST https://api.aiven.io/v1/project/<project>/static-ips
CreateStaticIPRequest struct {
CloudName string `json:"cloud_name"`
}
// CreateStaticIPResponse Aiven API response
// POST https://api.aiven.io/v1/project/<project>/static-ips
CreateStaticIPResponse struct {
APIResponse
StaticIP
}
// DeleteStaticIPRequest Aiven API request
// DELETE https://api.aiven.io/v1/project/<project>/static-ips/<static_ip_address_id>
DeleteStaticIPRequest struct {
StaticIPAddressID string `json:"static_ip_address_id"`
}
// ListStaticIPResponse Aiven API response
// GET https://api.aiven.io/v1/project/<project>/static-ips
ListStaticIPResponse struct {
APIResponse
StaticIPs []StaticIP `json:"static_ips"`
}
// AssociateStaticIPRequest Aiven API request
// POST https://api.aiven.io/v1/project/<project>/static-ips/<static-ip>/association
AssociateStaticIPRequest struct {
ServiceName string `json:"service_name"`
}
// StaticIP shared fields by API responses
StaticIP struct {
CloudName string `json:"cloud_name"`
IPAddress string `json:"ip_address"`
ServiceName string `json:"service_name"`
State string `json:"state"`
StaticIPAddressID string `json:"static_ip_address_id"`
}
)
// Create creates a static ip
func (h *StaticIPsHandler) Create(project string, req CreateStaticIPRequest) (*CreateStaticIPResponse, error) {
path := buildPath("project", project, "static-ips")
bts, err := h.client.doPostRequest(path, req)
if err != nil {
return nil, err
}
var r CreateStaticIPResponse
errR := checkAPIResponse(bts, &r)
return &r, errR
}
// Delete deletes a static ip
func (h *StaticIPsHandler) Delete(project string, req DeleteStaticIPRequest) error {
path := buildPath("project", project, "static-ips", req.StaticIPAddressID)
bts, err := h.client.doDeleteRequest(path, nil)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}
// Get retrieves a Static IP
// NOTE: API does not support GET /v1/project/{project}/static-ips/{static-ip-id},
// need to fetch all, filter by ID and fake a 404 if nothing is found
func (h *StaticIPsHandler) Get(project, staticIPID string) (*StaticIP, error) {
staticIPs, err := h.List(project)
if err != nil {
return nil, err
}
var targetStaticIP StaticIP
for _, staticIP := range staticIPs.StaticIPs {
if staticIP.StaticIPAddressID == staticIPID {
targetStaticIP = staticIP
}
}
if targetStaticIP.StaticIPAddressID == "" {
return nil, Error{
Message: fmt.Sprintf("static ip not found by id:%s", staticIPID),
Status: 404,
}
}
return &targetStaticIP, nil
}
// List lists all static ips
func (h *StaticIPsHandler) List(project string) (*ListStaticIPResponse, error) {
path := buildPath("project", project, "static-ips")
bts, err := h.client.doGetRequest(path, nil)
if err != nil {
return nil, err
}
var r ListStaticIPResponse
errR := checkAPIResponse(bts, &r)
return &r, errR
}
func (h *StaticIPsHandler) Associate(project, staticIPID string, req AssociateStaticIPRequest) error {
path := buildPath("project", project, "static-ips", staticIPID, "association")
rsp, err := h.client.doPostRequest(path, req)
if err != nil {
return err
}
return checkAPIResponse(rsp, nil)
}
func (h *StaticIPsHandler) Dissociate(project, staticIPID string) error {
path := buildPath("project", project, "static-ips", staticIPID, "association")
rsp, err := h.client.doDeleteRequest(path, nil)
if err != nil {
return err
}
return checkAPIResponse(rsp, nil)
}