vpc2.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 vpc2Path = "/v2/vpc2"
12
13 // VPC2Service is the interface to interact with the VPC 2.0 endpoints on the Vultr API
14 // Link : https://www.vultr.com/api/#tag/vpc2
15 //
16 // Deprecated: VPC2 is no longer supported and functionality will cease in a
17 // future release
18 type VPC2Service interface {
19 // Deprecated: VPC2 is no longer supported
20 Create(ctx context.Context, createReq *VPC2Req) (*VPC2, *http.Response, error)
21 // Deprecated: VPC2 is no longer supported
22 Get(ctx context.Context, vpcID string) (*VPC2, *http.Response, error)
23 // Deprecated: VPC2 is no longer supported
24 Update(ctx context.Context, vpcID string, description string) error
25 // Deprecated: VPC2 is no longer supported
26 Delete(ctx context.Context, vpcID string) error
27 // Deprecated: VPC2 is no longer supported
28 List(ctx context.Context, options *ListOptions) ([]VPC2, *Meta, *http.Response, error)
29 // Deprecated: VPC2 is no longer supported
30 ListNodes(ctx context.Context, vpc2ID string, options *ListOptions) ([]VPC2Node, *Meta, *http.Response, error)
31 // Deprecated: VPC2 is no longer supported
32 Attach(ctx context.Context, vpcID string, attachReq *VPC2AttachDetachReq) error
33 // Deprecated: VPC2 is no longer supported
34 Detach(ctx context.Context, vpcID string, detachReq *VPC2AttachDetachReq) error
35 }
36
37 // VPC2ServiceHandler handles interaction with the VPC 2.0 methods for the Vultr API
38 type VPC2ServiceHandler struct {
39 client *Client
40 }
41
42 // VPC2 represents a Vultr VPC 2.0
43 //
44 // Deprecated: VPC2 is no longer supported and functionality will cease in a
45 // future release
46 type VPC2 struct {
47 ID string `json:"id"`
48 Region string `json:"region"`
49 Description string `json:"description"`
50 IPBlock string `json:"ip_block"`
51 PrefixLength int `json:"prefix_length"`
52 DateCreated string `json:"date_created"`
53 }
54
55 // VPC2Node represents a node attached to a VPC 2.0 network
56 type VPC2Node struct {
57 ID string `json:"id"`
58 IPAddress string `json:"ip_address"`
59 MACAddress int `json:"mac_address"`
60 Description string `json:"description"`
61 Type string `json:"type"`
62 NodeStatus string `json:"node_status"`
63 }
64
65 // VPC2Req represents parameters to create or update a VPC 2.0 resource
66 type VPC2Req struct {
67 Region string `json:"region"`
68 Description string `json:"description"`
69 IPType string `json:"ip_type"`
70 IPBlock string `json:"ip_block"`
71 PrefixLength int `json:"prefix_length"`
72 }
73
74 // VPC2AttachDetachReq represents parameters to mass attach or detach nodes from VPC 2.0 networks
75 type VPC2AttachDetachReq struct {
76 Nodes []string `json:"nodes"`
77 }
78
79 type vpcs2Base struct {
80 VPCs []VPC2 `json:"vpcs"`
81 Meta *Meta `json:"meta"`
82 }
83
84 type vpc2Base struct {
85 VPC *VPC2 `json:"vpc"`
86 }
87
88 type vpc2NodesBase struct {
89 Nodes []VPC2Node `json:"nodes"`
90 Meta *Meta `json:"meta"`
91 }
92
93 // Create creates a new VPC 2.0. A VPC 2.0 can only be used at the location for which it was created.
94 //
95 // Deprecated: VPC2 is no longer supported and functionality will cease in a
96 // future release
97 func (n *VPC2ServiceHandler) Create(ctx context.Context, createReq *VPC2Req) (*VPC2, *http.Response, error) {
98 req, err := n.client.NewRequest(ctx, http.MethodPost, vpc2Path, createReq)
99 if err != nil {
100 return nil, nil, err
101 }
102
103 vpc := new(vpc2Base)
104 resp, err := n.client.DoWithContext(ctx, req, vpc)
105 if err != nil {
106 return nil, resp, err
107 }
108
109 return vpc.VPC, resp, nil
110 }
111
112 // Get gets the VPC 2.0 of the requested ID
113 //
114 // Deprecated: VPC2 is no longer supported and functionality will cease in a
115 // future release
116 func (n *VPC2ServiceHandler) Get(ctx context.Context, vpcID string) (*VPC2, *http.Response, error) {
117 uri := fmt.Sprintf("%s/%s", vpc2Path, vpcID)
118 req, err := n.client.NewRequest(ctx, http.MethodGet, uri, nil)
119 if err != nil {
120 return nil, nil, err
121 }
122
123 vpc := new(vpc2Base)
124 resp, err := n.client.DoWithContext(ctx, req, vpc)
125 if err != nil {
126 return nil, resp, err
127 }
128
129 return vpc.VPC, resp, nil
130 }
131
132 // Update updates a VPC 2.0
133 //
134 // Deprecated: VPC2 is no longer supported and functionality will cease in a
135 // future release
136 func (n *VPC2ServiceHandler) Update(ctx context.Context, vpcID, description string) error {
137 uri := fmt.Sprintf("%s/%s", vpc2Path, vpcID)
138
139 vpcReq := RequestBody{"description": description}
140 req, err := n.client.NewRequest(ctx, http.MethodPut, uri, vpcReq)
141 if err != nil {
142 return err
143 }
144
145 _, err = n.client.DoWithContext(ctx, req, nil)
146 return err
147 }
148
149 // Delete deletes a VPC 2.0. Before deleting, a VPC 2.0 must be disabled from all instances
150 //
151 // Deprecated: VPC2 is no longer supported and functionality will cease in a
152 // future release
153 func (n *VPC2ServiceHandler) Delete(ctx context.Context, vpcID string) error {
154 uri := fmt.Sprintf("%s/%s", vpc2Path, vpcID)
155 req, err := n.client.NewRequest(ctx, http.MethodDelete, uri, nil)
156 if err != nil {
157 return err
158 }
159 _, err = n.client.DoWithContext(ctx, req, nil)
160 return err
161 }
162
163 // List lists all VPCs 2.0 on the current account
164 //
165 // Deprecated: VPC2 is no longer supported and functionality will cease in a
166 // future release
167 func (n *VPC2ServiceHandler) List(ctx context.Context, options *ListOptions) ([]VPC2, *Meta, *http.Response, error) { //nolint:dupl
168 req, err := n.client.NewRequest(ctx, http.MethodGet, vpc2Path, nil)
169 if err != nil {
170 return nil, nil, nil, err
171 }
172
173 newValues, err := query.Values(options)
174 if err != nil {
175 return nil, nil, nil, err
176 }
177
178 req.URL.RawQuery = newValues.Encode()
179
180 vpcs := new(vpcs2Base)
181 resp, err := n.client.DoWithContext(ctx, req, vpcs)
182 if err != nil {
183 return nil, nil, resp, err
184 }
185
186 return vpcs.VPCs, vpcs.Meta, resp, nil
187 }
188
189 // ListNodes lists all nodes attached to a VPC 2.0 network
190 //
191 // Deprecated: VPC2 is no longer supported and functionality will cease in a
192 // future release
193 func (n *VPC2ServiceHandler) ListNodes(ctx context.Context, vpc2ID string, options *ListOptions) ([]VPC2Node, *Meta, *http.Response, error) { //nolint:dupl,lll
194 uri := fmt.Sprintf("%s/%s/nodes", vpc2Path, vpc2ID)
195
196 req, err := n.client.NewRequest(ctx, http.MethodGet, uri, nil)
197 if err != nil {
198 return nil, nil, nil, err
199 }
200
201 newValues, err := query.Values(options)
202 if err != nil {
203 return nil, nil, nil, err
204 }
205
206 req.URL.RawQuery = newValues.Encode()
207
208 nodes := new(vpc2NodesBase)
209 resp, err := n.client.DoWithContext(ctx, req, nodes)
210 if err != nil {
211 return nil, nil, resp, err
212 }
213
214 return nodes.Nodes, nodes.Meta, resp, nil
215 }
216
217 // Attach attaches nodes to a VPC 2.0 network
218 //
219 // Deprecated: VPC2 is no longer supported and functionality will cease in a
220 // future release
221 func (n *VPC2ServiceHandler) Attach(ctx context.Context, vpcID string, attachReq *VPC2AttachDetachReq) error {
222 uri := fmt.Sprintf("%s/%s/nodes/attach", vpc2Path, vpcID)
223
224 req, err := n.client.NewRequest(ctx, http.MethodPost, uri, attachReq)
225 if err != nil {
226 return err
227 }
228
229 _, err = n.client.DoWithContext(ctx, req, nil)
230 return err
231 }
232
233 // Detach detaches nodes from a VPC 2.0 network
234 //
235 // Deprecated: VPC2 is no longer supported and functionality will cease in a
236 // future release
237 func (n *VPC2ServiceHandler) Detach(ctx context.Context, vpcID string, detachReq *VPC2AttachDetachReq) error {
238 uri := fmt.Sprintf("%s/%s/nodes/detach", vpc2Path, vpcID)
239
240 req, err := n.client.NewRequest(ctx, http.MethodPost, uri, detachReq)
241 if err != nil {
242 return err
243 }
244
245 _, err = n.client.DoWithContext(ctx, req, nil)
246 return err
247 }
248