-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouters.go
299 lines (238 loc) · 8.35 KB
/
routers.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
package edgecloud
import (
"context"
"fmt"
"net/http"
)
const (
routersBasePathV1 = "/v1/routers"
)
const (
routersAttach = "attach"
routersDetach = "detach"
)
// RoutersService is an interface for creating and managing Routers with the EdgecenterCloud API.
// See: https://apidocs.edgecenter.ru/cloud#tag/routers
type RoutersService interface {
List(context.Context) ([]Router, *Response, error)
Create(context.Context, *RouterCreateRequest) (*TaskResponse, *Response, error)
Delete(context.Context, string) (*TaskResponse, *Response, error)
Get(context.Context, string) (*Router, *Response, error)
Update(context.Context, string, *RouterUpdateRequest) (*Router, *Response, error)
Attach(context.Context, string, *RouterAttachRequest) (*Router, *Response, error)
Detach(context.Context, string, *RouterDetachRequest) (*Router, *Response, error)
}
// RoutersServiceOp handles communication with Routers methods of the EdgecenterCloud API.
type RoutersServiceOp struct {
client *Client
}
var _ RoutersService = &RoutersServiceOp{}
// Router represents an EdgecenterCloud Router.
type Router struct {
Region string `json:"region"`
UpdatedAt string `json:"updated_at"`
CreatedAt string `json:"created_at"`
Name string `json:"name"`
ID string `json:"id"`
RegionID int `json:"region_id"`
ProjectID int `json:"project_id"`
TaskID string `json:"task_id"`
Status string `json:"status"`
CreatorTaskID string `json:"creator_task_id"`
ExternalGatewayInfo ExternalGatewayInfo `json:"external_gateway_info"`
Interfaces []RouterInterface `json:"interfaces"`
Routes []HostRoute `json:"routes"`
}
// RouterInterface represents a router instance interface.
type RouterInterface struct {
PortID string `json:"port_id"`
IPAssignments []PortIP `json:"ip_assignments"`
MacAddress string `json:"mac_address"`
NetworkID string `json:"network_id"`
}
type ExternalGatewayInfo struct {
ExternalFixedIPs []ExternalFixedIP `json:"external_fixed_ips"`
NetworkID string `json:"network_id"`
EnableSnat bool `json:"enable_snat"`
}
type ExternalFixedIP struct {
IPAddress string `json:"ip_address"`
SubnetID string `json:"subnet_id"`
}
type RouterCreateRequest struct {
Interfaces []RouterInterfaceCreate `json:"interfaces"`
ExternalGatewayInfo ExternalGatewayInfoCreate `json:"external_gateway_info"`
Name string `json:"name" required:"true" validate:"required"`
Routes []HostRoute `json:"routes"`
}
type RouterUpdateRequest struct {
ExternalGatewayInfo ExternalGatewayInfoCreate `json:"external_gateway_info"`
Name string `json:"name" required:"true" validate:"required"`
Routes []HostRoute `json:"routes"`
}
type RouterInterfaceCreate struct {
SubnetID string `json:"subnet_id" required:"true" validate:"required"`
Type string `json:"type"`
}
type ExternalGatewayInfoCreate struct {
EnableSnat *bool `json:"enable_snat"`
Type string `json:"type,omitempty" validate:"omitempty,enum"`
NetworkID string `json:"network_id,omitempty" validate:"rfe=Type:manual,omitempty,uuid4"`
}
type RouterAttachRequest struct {
SubnetID string `json:"subnet_id" required:"true" validate:"required"`
}
type RouterDetachRequest struct {
SubnetID string `json:"subnet_id" required:"true" validate:"required"`
}
// routersRoot represents a Routers root.
type routersRoot struct {
Count int
Routers []Router `json:"results"`
}
// List get routers.
func (s *RoutersServiceOp) List(ctx context.Context) ([]Router, *Response, error) {
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := s.client.addProjectRegionPath(routersBasePathV1)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(routersRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Routers, resp, err
}
// Create a Router.
func (s *RoutersServiceOp) Create(ctx context.Context, reqBody *RouterCreateRequest) (*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(routersBasePathV1)
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 a Router.
func (s *RoutersServiceOp) Delete(ctx context.Context, routerID string) (*TaskResponse, *Response, error) {
if resp, err := isValidUUID(routerID, "routerID"); 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(routersBasePathV1), routerID)
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
}
// Get a Router.
func (s *RoutersServiceOp) Get(ctx context.Context, routerID string) (*Router, *Response, error) {
if resp, err := isValidUUID(routerID, "routerID"); 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(routersBasePathV1), routerID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
router := new(Router)
resp, err := s.client.Do(ctx, req, router)
if err != nil {
return nil, resp, err
}
return router, resp, err
}
// Update a Router.
func (s *RoutersServiceOp) Update(ctx context.Context, routerID string, reqBody *RouterUpdateRequest) (*Router, *Response, error) {
if resp, err := isValidUUID(routerID, "routerID"); 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(routersBasePathV1), routerID)
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, reqBody)
if err != nil {
return nil, nil, err
}
router := new(Router)
resp, err := s.client.Do(ctx, req, router)
if err != nil {
return nil, resp, err
}
return router, resp, err
}
// Attach a subnet to a Router.
func (s *RoutersServiceOp) Attach(ctx context.Context, routerID string, reqBody *RouterAttachRequest) (*Router, *Response, error) {
if resp, err := isValidUUID(routerID, "routerID"); 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", s.client.addProjectRegionPath(routersBasePathV1), routerID, routersAttach)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, reqBody)
if err != nil {
return nil, nil, err
}
router := new(Router)
resp, err := s.client.Do(ctx, req, router)
if err != nil {
return nil, resp, err
}
return router, resp, err
}
// Detach a subnet from a Router.
func (s *RoutersServiceOp) Detach(ctx context.Context, routerID string, reqBody *RouterDetachRequest) (*Router, *Response, error) {
if resp, err := isValidUUID(routerID, "routerID"); 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", s.client.addProjectRegionPath(routersBasePathV1), routerID, routersDetach)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, reqBody)
if err != nil {
return nil, nil, err
}
router := new(Router)
resp, err := s.client.Do(ctx, req, router)
if err != nil {
return nil, resp, err
}
return router, resp, err
}