-
Notifications
You must be signed in to change notification settings - Fork 56
/
vpc2.go
209 lines (170 loc) · 5.98 KB
/
vpc2.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
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
const vpc2Path = "/v2/vpc2"
// VPC2Service is the interface to interact with the VPC 2.0 endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/vpc2
type VPC2Service interface {
Create(ctx context.Context, createReq *VPC2Req) (*VPC2, *http.Response, error)
Get(ctx context.Context, vpcID string) (*VPC2, *http.Response, error)
Update(ctx context.Context, vpcID string, description string) error
Delete(ctx context.Context, vpcID string) error
List(ctx context.Context, options *ListOptions) ([]VPC2, *Meta, *http.Response, error)
ListNodes(ctx context.Context, vpc2ID string, options *ListOptions) ([]VPC2Node, *Meta, *http.Response, error)
Attach(ctx context.Context, vpcID string, attachReq *VPC2AttachDetachReq) error
Detach(ctx context.Context, vpcID string, detachReq *VPC2AttachDetachReq) error
}
// VPC2ServiceHandler handles interaction with the VPC 2.0 methods for the Vultr API
type VPC2ServiceHandler struct {
client *Client
}
// VPC2 represents a Vultr VPC 2.0
type VPC2 struct {
ID string `json:"id"`
Region string `json:"region"`
Description string `json:"description"`
IPBlock string `json:"ip_block"`
PrefixLength int `json:"prefix_length"`
DateCreated string `json:"date_created"`
}
// VPC2Node represents a node attached to a VPC 2.0 network
type VPC2Node struct {
ID string `json:"id"`
IPAddress string `json:"ip_address"`
MACAddress int `json:"mac_address"`
Description string `json:"description"`
Type string `json:"type"`
NodeStatus string `json:"node_status"`
}
// VPC2Req represents parameters to create or update a VPC 2.0 resource
type VPC2Req struct {
Region string `json:"region"`
Description string `json:"description"`
IPType string `json:"ip_type"`
IPBlock string `json:"ip_block"`
PrefixLength int `json:"prefix_length"`
}
// VPC2AttachDetachReq represents parameters to mass attach or detach nodes from VPC 2.0 networks
type VPC2AttachDetachReq struct {
Nodes []string `json:"nodes"`
}
type vpcs2Base struct {
VPCs []VPC2 `json:"vpcs"`
Meta *Meta `json:"meta"`
}
type vpc2Base struct {
VPC *VPC2 `json:"vpc"`
}
type vpc2NodesBase struct {
Nodes []VPC2Node `json:"nodes"`
Meta *Meta `json:"meta"`
}
// Create creates a new VPC 2.0. A VPC 2.0 can only be used at the location for which it was created.
func (n *VPC2ServiceHandler) Create(ctx context.Context, createReq *VPC2Req) (*VPC2, *http.Response, error) {
req, err := n.client.NewRequest(ctx, http.MethodPost, vpc2Path, createReq)
if err != nil {
return nil, nil, err
}
vpc := new(vpc2Base)
resp, err := n.client.DoWithContext(ctx, req, vpc)
if err != nil {
return nil, resp, err
}
return vpc.VPC, resp, nil
}
// Get gets the VPC 2.0 of the requested ID
func (n *VPC2ServiceHandler) Get(ctx context.Context, vpcID string) (*VPC2, *http.Response, error) {
uri := fmt.Sprintf("%s/%s", vpc2Path, vpcID)
req, err := n.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
vpc := new(vpc2Base)
resp, err := n.client.DoWithContext(ctx, req, vpc)
if err != nil {
return nil, resp, err
}
return vpc.VPC, resp, nil
}
// Update updates a VPC 2.0
func (n *VPC2ServiceHandler) Update(ctx context.Context, vpcID, description string) error {
uri := fmt.Sprintf("%s/%s", vpc2Path, vpcID)
vpcReq := RequestBody{"description": description}
req, err := n.client.NewRequest(ctx, http.MethodPut, uri, vpcReq)
if err != nil {
return err
}
_, err = n.client.DoWithContext(ctx, req, nil)
return err
}
// Delete deletes a VPC 2.0. Before deleting, a VPC 2.0 must be disabled from all instances
func (n *VPC2ServiceHandler) Delete(ctx context.Context, vpcID string) error {
uri := fmt.Sprintf("%s/%s", vpc2Path, vpcID)
req, err := n.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
_, err = n.client.DoWithContext(ctx, req, nil)
return err
}
// List lists all VPCs 2.0 on the current account
func (n *VPC2ServiceHandler) List(ctx context.Context, options *ListOptions) ([]VPC2, *Meta, *http.Response, error) { //nolint:dupl
req, err := n.client.NewRequest(ctx, http.MethodGet, vpc2Path, 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()
vpcs := new(vpcs2Base)
resp, err := n.client.DoWithContext(ctx, req, vpcs)
if err != nil {
return nil, nil, resp, err
}
return vpcs.VPCs, vpcs.Meta, resp, nil
}
// ListNodes lists all nodes attached to a VPC 2.0 network
func (n *VPC2ServiceHandler) ListNodes(ctx context.Context, vpc2ID string, options *ListOptions) ([]VPC2Node, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := fmt.Sprintf("%s/%s/nodes", vpc2Path, vpc2ID)
req, err := n.client.NewRequest(ctx, http.MethodGet, uri, 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()
nodes := new(vpc2NodesBase)
resp, err := n.client.DoWithContext(ctx, req, nodes)
if err != nil {
return nil, nil, resp, err
}
return nodes.Nodes, nodes.Meta, resp, nil
}
// Attach attaches nodes to a VPC 2.0 network
func (n *VPC2ServiceHandler) Attach(ctx context.Context, vpcID string, attachReq *VPC2AttachDetachReq) error {
uri := fmt.Sprintf("%s/%s/nodes/attach", vpc2Path, vpcID)
req, err := n.client.NewRequest(ctx, http.MethodPost, uri, attachReq)
if err != nil {
return err
}
_, err = n.client.DoWithContext(ctx, req, nil)
return err
}
// Detach detaches nodes from a VPC 2.0 network
func (n *VPC2ServiceHandler) Detach(ctx context.Context, vpcID string, detachReq *VPC2AttachDetachReq) error {
uri := fmt.Sprintf("%s/%s/nodes/detach", vpc2Path, vpcID)
req, err := n.client.NewRequest(ctx, http.MethodPost, uri, detachReq)
if err != nil {
return err
}
_, err = n.client.DoWithContext(ctx, req, nil)
return err
}