vpc.go raw
1 package govultr
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7
8 "github.com/google/go-querystring/query"
9 )
10
11 const vpcPath = "/v2/vpcs"
12
13 // VPCService is the interface to interact with the VPC endpoints on the Vultr API
14 // Link : https://www.vultr.com/api/#tag/vpcs
15 type VPCService interface {
16 Create(ctx context.Context, createReq *VPCReq) (*VPC, *http.Response, error)
17 Get(ctx context.Context, vpcID string) (*VPC, *http.Response, error)
18 Update(ctx context.Context, vpcID string, description string) error
19 Delete(ctx context.Context, vpcID string) error
20 List(ctx context.Context, options *ListOptions) ([]VPC, *Meta, *http.Response, error)
21 }
22
23 // VPCServiceHandler handles interaction with the VPC methods for the Vultr API
24 type VPCServiceHandler struct {
25 client *Client
26 }
27
28 // VPC represents a Vultr VPC
29 type VPC struct {
30 ID string `json:"id"`
31 Region string `json:"region"`
32 Description string `json:"description"`
33 V4Subnet string `json:"v4_subnet"`
34 V4SubnetMask int `json:"v4_subnet_mask"`
35 DateCreated string `json:"date_created"`
36 }
37
38 // VPCReq represents parameters to create or update a VPC resource
39 type VPCReq struct {
40 Region string `json:"region"`
41 Description string `json:"description"`
42 V4Subnet string `json:"v4_subnet"`
43 V4SubnetMask int `json:"v4_subnet_mask"`
44 }
45
46 type vpcsBase struct {
47 VPCs []VPC `json:"vpcs"`
48 Meta *Meta `json:"meta"`
49 }
50
51 type vpcBase struct {
52 VPC *VPC `json:"vpc"`
53 }
54
55 // Create creates a new VPC. A VPC can only be used at the location for which it was created.
56 func (n *VPCServiceHandler) Create(ctx context.Context, createReq *VPCReq) (*VPC, *http.Response, error) {
57 req, err := n.client.NewRequest(ctx, http.MethodPost, vpcPath, createReq)
58 if err != nil {
59 return nil, nil, err
60 }
61
62 vpc := new(vpcBase)
63 resp, err := n.client.DoWithContext(ctx, req, vpc)
64 if err != nil {
65 return nil, resp, err
66 }
67
68 return vpc.VPC, resp, nil
69 }
70
71 // Get gets the VPC of the requested ID
72 func (n *VPCServiceHandler) Get(ctx context.Context, vpcID string) (*VPC, *http.Response, error) {
73 uri := fmt.Sprintf("%s/%s", vpcPath, vpcID)
74 req, err := n.client.NewRequest(ctx, http.MethodGet, uri, nil)
75 if err != nil {
76 return nil, nil, err
77 }
78
79 vpc := new(vpcBase)
80 resp, err := n.client.DoWithContext(ctx, req, vpc)
81 if err != nil {
82 return nil, resp, err
83 }
84
85 return vpc.VPC, resp, nil
86 }
87
88 // Update updates a VPC
89 func (n *VPCServiceHandler) Update(ctx context.Context, vpcID, description string) error {
90 uri := fmt.Sprintf("%s/%s", vpcPath, vpcID)
91
92 vpcReq := RequestBody{"description": description}
93 req, err := n.client.NewRequest(ctx, http.MethodPut, uri, vpcReq)
94 if err != nil {
95 return err
96 }
97
98 _, err = n.client.DoWithContext(ctx, req, nil)
99 return err
100 }
101
102 // Delete deletes a VPC. Before deleting, a VPC must be disabled from all instances
103 func (n *VPCServiceHandler) Delete(ctx context.Context, vpcID string) error {
104 uri := fmt.Sprintf("%s/%s", vpcPath, vpcID)
105 req, err := n.client.NewRequest(ctx, http.MethodDelete, uri, nil)
106 if err != nil {
107 return err
108 }
109 _, err = n.client.DoWithContext(ctx, req, nil)
110 return err
111 }
112
113 // List lists all VPCs on the current account
114 func (n *VPCServiceHandler) List(ctx context.Context, options *ListOptions) ([]VPC, *Meta, *http.Response, error) { //nolint:dupl
115 req, err := n.client.NewRequest(ctx, http.MethodGet, vpcPath, nil)
116 if err != nil {
117 return nil, nil, nil, err
118 }
119
120 newValues, err := query.Values(options)
121 if err != nil {
122 return nil, nil, nil, err
123 }
124
125 req.URL.RawQuery = newValues.Encode()
126
127 vpcs := new(vpcsBase)
128 resp, err := n.client.DoWithContext(ctx, req, vpcs)
129 if err != nil {
130 return nil, nil, resp, err
131 }
132
133 return vpcs.VPCs, vpcs.Meta, resp, nil
134 }
135