-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubnets.go
367 lines (297 loc) · 11.2 KB
/
subnets.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package edgecloud
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
)
const (
subnetsBasePathV1 = "/v1/subnets"
gatewayIPTag = "gateway_ip"
)
// SubnetworksService is an interface for creating and managing Subnetworks with the EdgecenterCloud API.
// See: https://apidocs.edgecenter.ru/cloud#tag/subnets
type SubnetworksService interface {
List(context.Context, *SubnetworkListOptions) ([]Subnetwork, *Response, error)
Get(context.Context, string) (*Subnetwork, *Response, error)
Create(context.Context, *SubnetworkCreateRequest) (*TaskResponse, *Response, error)
Delete(context.Context, string) (*TaskResponse, *Response, error)
Update(context.Context, string, *SubnetworkUpdateRequest) (*Subnetwork, *Response, error)
SubnetworksMetadata
}
type SubnetworksMetadata interface {
MetadataList(context.Context, string) ([]MetadataDetailed, *Response, error)
MetadataCreate(context.Context, string, *Metadata) (*Response, error)
MetadataUpdate(context.Context, string, *Metadata) (*Response, error)
MetadataDeleteItem(context.Context, string, *MetadataItemOptions) (*Response, error)
MetadataGetItem(context.Context, string, *MetadataItemOptions) (*MetadataDetailed, *Response, error)
}
// SubnetworksServiceOp handles communication with Subnetworks methods of the EdgecenterCloud API.
type SubnetworksServiceOp struct {
client *Client
}
var _ SubnetworksService = &SubnetworksServiceOp{}
// Subnetwork represents an EdgecenterCloud Subnetwork.
type Subnetwork struct {
ID string `json:"id"`
Name string `json:"name"`
NetworkID string `json:"network_id"`
IPVersion int `json:"ip_version"`
EnableDHCP bool `json:"enable_dhcp"`
ConnectToNetworkRouter bool `json:"connect_to_network_router"`
CIDR string `json:"cidr"` // TODO add cidr parsing.
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
CreatorTaskID string `json:"creator_task_id"`
TaskID string `json:"task_id"`
AvailableIps int `json:"available_ips"`
TotalIps int `json:"total_ips"`
HasRouter bool `json:"has_router"`
DNSNameservers []net.IP `json:"dns_nameservers"`
HostRoutes []HostRoute `json:"host_routes"`
GatewayIP net.IP `json:"gateway_ip"`
Metadata []MetadataDetailed `json:"metadata,omitempty"`
Region string `json:"region"`
ProjectID int `json:"project_id"`
RegionID int `json:"region_id"`
}
// SubnetworkCreateRequest represents a request to create a Subnetwork.
type SubnetworkCreateRequest struct {
Name string `json:"name" required:"true"`
NetworkID string `json:"network_id" required:"true"`
EnableDHCP bool `json:"enable_dhcp,omitempty"`
CIDR string `json:"cidr" required:"true"`
ConnectToNetworkRouter bool `json:"connect_to_network_router"`
DNSNameservers []net.IP `json:"dns_nameservers,omitempty"`
GatewayIP *net.IP `json:"gateway_ip"`
Metadata Metadata `json:"metadata,omitempty"`
HostRoutes []HostRoute `json:"host_routes,omitempty"`
}
func (scr *SubnetworkCreateRequest) MarshalJSON() ([]byte, error) {
type alias SubnetworkCreateRequest
scrJSON, err := json.Marshal((*alias)(scr))
if err != nil {
return nil, fmt.Errorf("json.Marshal error: %w", err)
}
if !(scr.GatewayIP == nil && scr.ConnectToNetworkRouter) {
return scrJSON, nil
}
scrMap := make(map[string]any)
err = json.Unmarshal(scrJSON, &scrMap)
if err != nil {
return nil, fmt.Errorf("json.Unmarshal error: %w", err)
}
delete(scrMap, gatewayIPTag)
return json.Marshal(scrMap)
}
// SubnetworkUpdateRequest represents a request to update a Subnetwork properties.
type SubnetworkUpdateRequest struct {
Name string `json:"name,omitempty"`
DNSNameservers []net.IP `json:"dns_nameservers"`
EnableDHCP bool `json:"enable_dhcp"`
HostRoutes []HostRoute `json:"host_routes"`
GatewayIP *net.IP `json:"gateway_ip"`
}
type CIDR struct {
net.IPNet
}
// UnmarshalJSON - implements Unmarshaler interface for CIDR.
func (c *CIDR) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
cd, err := ParseCIDRString(s)
if err != nil {
return err
}
*c = *cd
return nil
}
// MarshalJSON - implements Marshaler interface for CIDR.
func (c CIDR) MarshalJSON() ([]byte, error) {
return json.Marshal(c.String())
}
// String - implements Stringer.
func (c CIDR) String() string {
return c.IPNet.String()
}
func ParseCIDRString(s string) (*CIDR, error) {
_, nt, err := net.ParseCIDR(s)
if err != nil {
return nil, err
}
return &CIDR{IPNet: *nt}, nil
}
// HostRoute represents a route that should be used by devices with IPs from
// a subnet (not including local subnet route).
type HostRoute struct {
Destination CIDR `json:"destination"`
NextHop net.IP `json:"nexthop"`
}
// SubnetworkListOptions specifies the optional query parameters to List method.
type SubnetworkListOptions struct {
NetworkID string `url:"network_id,omitempty" validate:"omitempty"`
MetadataKV string `url:"metadata_kv,omitempty" validate:"omitempty"`
MetadataK string `url:"metadata_k,omitempty" validate:"omitempty"`
}
// subnetworkRoot represents a Subnetworks root.
type subnetworkRoot struct {
Count int
Subnetworks []Subnetwork `json:"results"`
}
// List get subnetworks.
func (s *SubnetworksServiceOp) List(ctx context.Context, opts *SubnetworkListOptions) ([]Subnetwork, *Response, error) {
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := s.client.addProjectRegionPath(subnetsBasePathV1)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(subnetworkRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Subnetworks, resp, err
}
// Get individual Subnetwork.
func (s *SubnetworksServiceOp) Get(ctx context.Context, subnetworkID string) (*Subnetwork, *Response, error) {
if resp, err := isValidUUID(subnetworkID, "subnetworkID"); err != nil {
return nil, resp, err
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := fmt.Sprintf("%s/%s", s.client.addProjectRegionPath(subnetsBasePathV1), subnetworkID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
subnetwork := new(Subnetwork)
resp, err := s.client.Do(ctx, req, subnetwork)
if err != nil {
return nil, resp, err
}
return subnetwork, resp, err
}
// Create a Subnetwork.
func (s *SubnetworksServiceOp) Create(ctx context.Context, reqBody *SubnetworkCreateRequest) (*TaskResponse, *Response, error) {
if reqBody == nil {
return nil, nil, NewArgError("reqBody", "cannot be nil")
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := s.client.addProjectRegionPath(subnetsBasePathV1)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, reqBody)
if err != nil {
return nil, nil, err
}
tasks := new(TaskResponse)
resp, err := s.client.Do(ctx, req, tasks)
if err != nil {
return nil, resp, err
}
return tasks, resp, err
}
// Delete the Subnetwork.
func (s *SubnetworksServiceOp) Delete(ctx context.Context, subnetworkID string) (*TaskResponse, *Response, error) {
if resp, err := isValidUUID(subnetworkID, "subnetworkID"); err != nil {
return nil, resp, err
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := fmt.Sprintf("%s/%s", s.client.addProjectRegionPath(subnetsBasePathV1), subnetworkID)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, nil, err
}
tasks := new(TaskResponse)
resp, err := s.client.Do(ctx, req, tasks)
if err != nil {
return nil, resp, err
}
return tasks, resp, err
}
// Update the Subnetwork properties.
func (s *SubnetworksServiceOp) Update(ctx context.Context, subnetworkID string, reqBody *SubnetworkUpdateRequest) (*Subnetwork, *Response, error) {
if resp, err := isValidUUID(subnetworkID, "subnetworkID"); err != nil {
return nil, resp, err
}
if reqBody == nil {
return nil, nil, NewArgError("reqBody", "cannot be nil")
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := fmt.Sprintf("%s/%s", s.client.addProjectRegionPath(subnetsBasePathV1), subnetworkID)
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, reqBody)
if err != nil {
return nil, nil, err
}
subnetwork := new(Subnetwork)
resp, err := s.client.Do(ctx, req, subnetwork)
if err != nil {
return nil, resp, err
}
return subnetwork, resp, err
}
// MetadataList subnetwork detailed metadata items.
func (s *SubnetworksServiceOp) MetadataList(ctx context.Context, subnetworkID string) ([]MetadataDetailed, *Response, error) {
if resp, err := isValidUUID(subnetworkID, "subnetworkID"); err != nil {
return nil, resp, err
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
return metadataList(ctx, s.client, subnetworkID, subnetsBasePathV1)
}
// MetadataCreate or update subnetwork metadata.
func (s *SubnetworksServiceOp) MetadataCreate(ctx context.Context, subnetworkID string, reqBody *Metadata) (*Response, error) {
if resp, err := isValidUUID(subnetworkID, "subnetworkID"); err != nil {
return resp, err
}
if resp, err := s.client.Validate(); err != nil {
return resp, err
}
return metadataCreate(ctx, s.client, subnetworkID, subnetsBasePathV1, reqBody)
}
// MetadataUpdate subnetwork metadata.
func (s *SubnetworksServiceOp) MetadataUpdate(ctx context.Context, subnetworkID string, reqBody *Metadata) (*Response, error) {
if resp, err := isValidUUID(subnetworkID, "subnetworkID"); err != nil {
return resp, err
}
if resp, err := s.client.Validate(); err != nil {
return resp, err
}
return metadataUpdate(ctx, s.client, subnetworkID, subnetsBasePathV1, reqBody)
}
// MetadataDeleteItem a subnetwork metadata item by key.
func (s *SubnetworksServiceOp) MetadataDeleteItem(ctx context.Context, subnetworkID string, opts *MetadataItemOptions) (*Response, error) {
if resp, err := isValidUUID(subnetworkID, "subnetworkID"); err != nil {
return resp, err
}
if resp, err := s.client.Validate(); err != nil {
return resp, err
}
return metadataDeleteItem(ctx, s.client, subnetworkID, subnetsBasePathV1, opts)
}
// MetadataGetItem subnetwork detailed metadata.
func (s *SubnetworksServiceOp) MetadataGetItem(ctx context.Context, subnetworkID string, opts *MetadataItemOptions) (*MetadataDetailed, *Response, error) {
if resp, err := isValidUUID(subnetworkID, "subnetworkID"); err != nil {
return nil, resp, err
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
return metadataGetItem(ctx, s.client, subnetworkID, subnetsBasePathV1, opts)
}