-
Notifications
You must be signed in to change notification settings - Fork 56
/
vpc.go
134 lines (110 loc) · 3.6 KB
/
vpc.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
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
const vpcPath = "/v2/vpcs"
// VPCService is the interface to interact with the VPC endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/vpcs
type VPCService interface {
Create(ctx context.Context, createReq *VPCReq) (*VPC, *http.Response, error)
Get(ctx context.Context, vpcID string) (*VPC, *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) ([]VPC, *Meta, *http.Response, error)
}
// VPCServiceHandler handles interaction with the VPC methods for the Vultr API
type VPCServiceHandler struct {
client *Client
}
// VPC represents a Vultr VPC
type VPC struct {
ID string `json:"id"`
Region string `json:"region"`
Description string `json:"description"`
V4Subnet string `json:"v4_subnet"`
V4SubnetMask int `json:"v4_subnet_mask"`
DateCreated string `json:"date_created"`
}
// VPCReq represents parameters to create or update a VPC resource
type VPCReq struct {
Region string `json:"region"`
Description string `json:"description"`
V4Subnet string `json:"v4_subnet"`
V4SubnetMask int `json:"v4_subnet_mask"`
}
type vpcsBase struct {
VPCs []VPC `json:"vpcs"`
Meta *Meta `json:"meta"`
}
type vpcBase struct {
VPC *VPC `json:"vpc"`
}
// Create creates a new VPC. A VPC can only be used at the location for which it was created.
func (n *VPCServiceHandler) Create(ctx context.Context, createReq *VPCReq) (*VPC, *http.Response, error) {
req, err := n.client.NewRequest(ctx, http.MethodPost, vpcPath, createReq)
if err != nil {
return nil, nil, err
}
vpc := new(vpcBase)
resp, err := n.client.DoWithContext(ctx, req, vpc)
if err != nil {
return nil, resp, err
}
return vpc.VPC, resp, nil
}
// Get gets the VPC of the requested ID
func (n *VPCServiceHandler) Get(ctx context.Context, vpcID string) (*VPC, *http.Response, error) {
uri := fmt.Sprintf("%s/%s", vpcPath, vpcID)
req, err := n.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
vpc := new(vpcBase)
resp, err := n.client.DoWithContext(ctx, req, vpc)
if err != nil {
return nil, resp, err
}
return vpc.VPC, resp, nil
}
// Update updates a VPC
func (n *VPCServiceHandler) Update(ctx context.Context, vpcID, description string) error {
uri := fmt.Sprintf("%s/%s", vpcPath, 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. Before deleting, a VPC must be disabled from all instances
func (n *VPCServiceHandler) Delete(ctx context.Context, vpcID string) error {
uri := fmt.Sprintf("%s/%s", vpcPath, 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 on the current account
func (n *VPCServiceHandler) List(ctx context.Context, options *ListOptions) ([]VPC, *Meta, *http.Response, error) { //nolint:dupl
req, err := n.client.NewRequest(ctx, http.MethodGet, vpcPath, 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(vpcsBase)
resp, err := n.client.DoWithContext(ctx, req, vpcs)
if err != nil {
return nil, nil, resp, err
}
return vpcs.VPCs, vpcs.Meta, resp, nil
}