-
Notifications
You must be signed in to change notification settings - Fork 301
/
vpcs.go
273 lines (228 loc) · 7.84 KB
/
vpcs.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
package godo
import (
"context"
"net/http"
"time"
)
const vpcsBasePath = "/v2/vpcs"
// VPCsService is an interface for managing Virtual Private Cloud configurations with the
// DigitalOcean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/VPCs
type VPCsService interface {
Create(context.Context, *VPCCreateRequest) (*VPC, *Response, error)
Get(context.Context, string) (*VPC, *Response, error)
List(context.Context, *ListOptions) ([]*VPC, *Response, error)
ListMembers(context.Context, string, *VPCListMembersRequest, *ListOptions) ([]*VPCMember, *Response, error)
Update(context.Context, string, *VPCUpdateRequest) (*VPC, *Response, error)
Set(context.Context, string, ...VPCSetField) (*VPC, *Response, error)
Delete(context.Context, string) (*Response, error)
CreateVPCPeering(context.Context, *VPCPeeringCreateRequest) (*VPCPeering, *Response, error)
GetVPCPeering(context.Context, string) (*VPCPeering, *Response, error)
ListVPCPeerings(context.Context, *ListOptions) ([]*VPCPeering, *Response, error)
UpdateVPCPeering(context.Context, string, *VPCPeeringUpdateRequest) (*VPCPeering, *Response, error)
DeleteVPCPeering(context.Context, string) (*Response, error)
CreateVPCPeeringByVPCID(context.Context, string, *VPCPeeringCreateRequestByVPCID) (*VPCPeering, *Response, error)
ListVPCPeeringsByVPCID(context.Context, string, *ListOptions) ([]*VPCPeering, *Response, error)
UpdateVPCPeeringByVPCID(context.Context, string, string, *VPCPeeringUpdateRequest) (*VPCPeering, *Response, error)
}
var _ VPCsService = &VPCsServiceOp{}
// VPCsServiceOp interfaces with VPC endpoints in the DigitalOcean API.
type VPCsServiceOp struct {
client *Client
}
// VPCCreateRequest represents a request to create a Virtual Private Cloud.
type VPCCreateRequest struct {
Name string `json:"name,omitempty"`
RegionSlug string `json:"region,omitempty"`
Description string `json:"description,omitempty"`
IPRange string `json:"ip_range,omitempty"`
}
// VPCUpdateRequest represents a request to update a Virtual Private Cloud.
type VPCUpdateRequest struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Default *bool `json:"default,omitempty"`
}
// VPCSetField allows one to set individual fields within a VPC configuration.
type VPCSetField interface {
vpcSetField(map[string]interface{})
}
// VPCSetName is used when one want to set the `name` field of a VPC.
// Ex.: VPCs.Set(..., VPCSetName("new-name"))
type VPCSetName string
// VPCSetDescription is used when one want to set the `description` field of a VPC.
// Ex.: VPCs.Set(..., VPCSetDescription("vpc description"))
type VPCSetDescription string
// VPCSetDefault is used when one wants to enable the `default` field of a VPC, to
// set a VPC as the default one in the region
// Ex.: VPCs.Set(..., VPCSetDefault())
func VPCSetDefault() VPCSetField {
return &vpcSetDefault{}
}
// vpcSetDefault satisfies the VPCSetField interface
type vpcSetDefault struct{}
// VPC represents a DigitalOcean Virtual Private Cloud configuration.
type VPC struct {
ID string `json:"id,omitempty"`
URN string `json:"urn"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
IPRange string `json:"ip_range,omitempty"`
RegionSlug string `json:"region,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Default bool `json:"default,omitempty"`
}
type VPCListMembersRequest struct {
ResourceType string `url:"resource_type,omitempty"`
}
type VPCMember struct {
URN string `json:"urn,omitempty"`
Name string `json:"name,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}
type vpcRoot struct {
VPC *VPC `json:"vpc"`
}
type vpcsRoot struct {
VPCs []*VPC `json:"vpcs"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
type vpcMembersRoot struct {
Members []*VPCMember `json:"members"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
// Get returns the details of a Virtual Private Cloud.
func (v *VPCsServiceOp) Get(ctx context.Context, id string) (*VPC, *Response, error) {
path := vpcsBasePath + "/" + id
req, err := v.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(vpcRoot)
resp, err := v.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.VPC, resp, nil
}
// Create creates a new Virtual Private Cloud.
func (v *VPCsServiceOp) Create(ctx context.Context, create *VPCCreateRequest) (*VPC, *Response, error) {
path := vpcsBasePath
req, err := v.client.NewRequest(ctx, http.MethodPost, path, create)
if err != nil {
return nil, nil, err
}
root := new(vpcRoot)
resp, err := v.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.VPC, resp, nil
}
// List returns a list of the caller's VPCs, with optional pagination.
func (v *VPCsServiceOp) List(ctx context.Context, opt *ListOptions) ([]*VPC, *Response, error) {
path, err := addOptions(vpcsBasePath, opt)
if err != nil {
return nil, nil, err
}
req, err := v.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(vpcsRoot)
resp, err := v.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.VPCs, resp, nil
}
// Update updates a Virtual Private Cloud's properties.
func (v *VPCsServiceOp) Update(ctx context.Context, id string, update *VPCUpdateRequest) (*VPC, *Response, error) {
path := vpcsBasePath + "/" + id
req, err := v.client.NewRequest(ctx, http.MethodPut, path, update)
if err != nil {
return nil, nil, err
}
root := new(vpcRoot)
resp, err := v.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.VPC, resp, nil
}
func (n VPCSetName) vpcSetField(in map[string]interface{}) {
in["name"] = n
}
func (n VPCSetDescription) vpcSetField(in map[string]interface{}) {
in["description"] = n
}
func (*vpcSetDefault) vpcSetField(in map[string]interface{}) {
in["default"] = true
}
// Set updates specific properties of a Virtual Private Cloud.
func (v *VPCsServiceOp) Set(ctx context.Context, id string, fields ...VPCSetField) (*VPC, *Response, error) {
path := vpcsBasePath + "/" + id
update := make(map[string]interface{}, len(fields))
for _, field := range fields {
field.vpcSetField(update)
}
req, err := v.client.NewRequest(ctx, http.MethodPatch, path, update)
if err != nil {
return nil, nil, err
}
root := new(vpcRoot)
resp, err := v.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.VPC, resp, nil
}
// Delete deletes a Virtual Private Cloud. There is no way to recover a VPC once it has been
// destroyed.
func (v *VPCsServiceOp) Delete(ctx context.Context, id string) (*Response, error) {
path := vpcsBasePath + "/" + id
req, err := v.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := v.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
func (v *VPCsServiceOp) ListMembers(ctx context.Context, id string, request *VPCListMembersRequest, opt *ListOptions) ([]*VPCMember, *Response, error) {
path := vpcsBasePath + "/" + id + "/members"
pathWithResourceType, err := addOptions(path, request)
if err != nil {
return nil, nil, err
}
pathWithOpts, err := addOptions(pathWithResourceType, opt)
if err != nil {
return nil, nil, err
}
req, err := v.client.NewRequest(ctx, http.MethodGet, pathWithOpts, nil)
if err != nil {
return nil, nil, err
}
root := new(vpcMembersRoot)
resp, err := v.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Members, resp, nil
}