-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathreserved_ip.go
196 lines (162 loc) · 5.93 KB
/
reserved_ip.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
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
const ripPath = "/v2/reserved-ips"
// ReservedIPService is the interface to interact with the reserved IP endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/reserved-ip
type ReservedIPService interface {
Create(ctx context.Context, ripCreate *ReservedIPReq) (*ReservedIP, *http.Response, error)
Update(ctx context.Context, id string, ripUpdate *ReservedIPUpdateReq) (*ReservedIP, *http.Response, error)
Get(ctx context.Context, id string) (*ReservedIP, *http.Response, error)
Delete(ctx context.Context, id string) error
List(ctx context.Context, options *ListOptions) ([]ReservedIP, *Meta, *http.Response, error)
Convert(ctx context.Context, ripConvert *ReservedIPConvertReq) (*ReservedIP, *http.Response, error)
Attach(ctx context.Context, id, instance string) error
Detach(ctx context.Context, id string) error
}
// ReservedIPServiceHandler handles interaction with the reserved IP methods for the Vultr API
type ReservedIPServiceHandler struct {
client *Client
}
// ReservedIP represents an reserved IP on Vultr
type ReservedIP struct {
ID string `json:"id"`
Region string `json:"region"`
IPType string `json:"ip_type"`
Subnet string `json:"subnet"`
SubnetSize int `json:"subnet_size"`
Label string `json:"label"`
InstanceID string `json:"instance_id"`
}
// ReservedIPReq represents the parameters for creating a new Reserved IP on Vultr
type ReservedIPReq struct {
Region string `json:"region,omitempty"`
IPType string `json:"ip_type,omitempty"`
IPAddress string `json:"ip_address,omitempty"`
Label string `json:"label,omitempty"`
InstanceID string `json:"instance_id,omitempty"`
}
// ReservedIPUpdateReq represents the parameters for updating a Reserved IP on Vultr
type ReservedIPUpdateReq struct {
Label *string `json:"label"`
}
type reservedIPsBase struct {
ReservedIPs []ReservedIP `json:"reserved_ips"`
Meta *Meta `json:"meta"`
}
type reservedIPBase struct {
ReservedIP *ReservedIP `json:"reserved_ip"`
}
// ReservedIPConvertReq is the struct used for create and update calls.
type ReservedIPConvertReq struct {
IPAddress string `json:"ip_address,omitempty"`
Label string `json:"label,omitempty"`
}
// Create adds the specified reserved IP to your Vultr account
func (r *ReservedIPServiceHandler) Create(ctx context.Context, ripCreate *ReservedIPReq) (*ReservedIP, *http.Response, error) {
req, err := r.client.NewRequest(ctx, http.MethodPost, ripPath, ripCreate)
if err != nil {
return nil, nil, err
}
rip := new(reservedIPBase)
resp, err := r.client.DoWithContext(ctx, req, rip)
if err != nil {
return nil, resp, err
}
return rip.ReservedIP, resp, nil
}
// Update updates label on the Reserved IP
func (r *ReservedIPServiceHandler) Update(ctx context.Context, id string, ripUpdate *ReservedIPUpdateReq) (*ReservedIP, *http.Response, error) { //nolint:lll
uri := fmt.Sprintf("%s/%s", ripPath, id)
req, err := r.client.NewRequest(ctx, http.MethodPatch, uri, ripUpdate)
if err != nil {
return nil, nil, err
}
rip := new(reservedIPBase)
resp, err := r.client.DoWithContext(ctx, req, rip)
if err != nil {
return nil, resp, err
}
return rip.ReservedIP, resp, nil
}
// Get gets the reserved IP associated with provided ID
func (r *ReservedIPServiceHandler) Get(ctx context.Context, id string) (*ReservedIP, *http.Response, error) {
uri := fmt.Sprintf("%s/%s", ripPath, id)
req, err := r.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
rip := new(reservedIPBase)
resp, err := r.client.DoWithContext(ctx, req, rip)
if err != nil {
return nil, resp, err
}
return rip.ReservedIP, resp, nil
}
// Delete removes the specified reserved IP from your Vultr account
func (r *ReservedIPServiceHandler) Delete(ctx context.Context, id string) error {
uri := fmt.Sprintf("%s/%s", ripPath, id)
req, err := r.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
_, err = r.client.DoWithContext(ctx, req, nil)
return err
}
// List lists all the reserved IPs associated with your Vultr account
func (r *ReservedIPServiceHandler) List(ctx context.Context, options *ListOptions) ([]ReservedIP, *Meta, *http.Response, error) { //nolint:dupl,lll
req, err := r.client.NewRequest(ctx, http.MethodGet, ripPath, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
ips := new(reservedIPsBase)
resp, err := r.client.DoWithContext(ctx, req, ips)
if err != nil {
return nil, nil, resp, err
}
return ips.ReservedIPs, ips.Meta, resp, nil
}
// Convert an existing IP on a subscription to a reserved IP.
func (r *ReservedIPServiceHandler) Convert(ctx context.Context, ripConvert *ReservedIPConvertReq) (*ReservedIP, *http.Response, error) {
uri := fmt.Sprintf("%s/convert", ripPath)
req, err := r.client.NewRequest(ctx, http.MethodPost, uri, ripConvert)
if err != nil {
return nil, nil, err
}
rip := new(reservedIPBase)
resp, err := r.client.DoWithContext(ctx, req, rip)
if err != nil {
return nil, resp, err
}
return rip.ReservedIP, resp, nil
}
// Attach a reserved IP to an existing subscription
func (r *ReservedIPServiceHandler) Attach(ctx context.Context, id, instance string) error {
uri := fmt.Sprintf("%s/%s/attach", ripPath, id)
reqBody := RequestBody{"instance_id": instance}
req, err := r.client.NewRequest(ctx, http.MethodPost, uri, reqBody)
if err != nil {
return err
}
_, err = r.client.DoWithContext(ctx, req, nil)
return err
}
// Detach a reserved IP from an existing subscription.
func (r *ReservedIPServiceHandler) Detach(ctx context.Context, id string) error {
uri := fmt.Sprintf("%s/%s/detach", ripPath, id)
req, err := r.client.NewRequest(ctx, http.MethodPost, uri, nil)
if err != nil {
return err
}
_, err = r.client.DoWithContext(ctx, req, nil)
return err
}