This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
connections.go
331 lines (291 loc) · 11.7 KB
/
connections.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
package packngo
import (
"errors"
"fmt"
"path"
"time"
)
type ConnectionRedundancy string
type ConnectionType string
type ConnectionPortRole string
type ConnectionMode string
const (
connectionBasePath = "/connections"
ConnectionShared ConnectionType = "shared"
ConnectionDedicated ConnectionType = "dedicated"
ConnectionRedundant ConnectionRedundancy = "redundant"
ConnectionPrimary ConnectionRedundancy = "primary"
ConnectionPortPrimary ConnectionPortRole = "primary"
ConnectionPortSecondary ConnectionPortRole = "secondary"
ConnectionModeStandard ConnectionMode = "standard"
ConnectionModeTunnel ConnectionMode = "tunnel"
ConnectionDeleteTimeout = 60 * time.Second
ConnectionDeleteCheck = 2 * time.Second
)
type ConnectionService interface {
OrganizationCreate(string, *ConnectionCreateRequest) (*Connection, *Response, error)
ProjectCreate(string, *ConnectionCreateRequest) (*Connection, *Response, error)
Update(string, *ConnectionUpdateRequest, *GetOptions) (*Connection, *Response, error)
OrganizationList(string, *GetOptions) ([]Connection, *Response, error)
ProjectList(string, *GetOptions) ([]Connection, *Response, error)
Delete(string, bool) (*Response, error)
Get(string, *GetOptions) (*Connection, *Response, error)
Events(string, *GetOptions) ([]Event, *Response, error)
PortEvents(string, string, *GetOptions) ([]Event, *Response, error)
Ports(string, *GetOptions) ([]ConnectionPort, *Response, error)
Port(string, string, *GetOptions) (*ConnectionPort, *Response, error)
VirtualCircuits(string, string, *GetOptions) ([]VirtualCircuit, *Response, error)
}
type ConnectionServiceOp struct {
client *Client
}
type connectionPortsRoot struct {
Ports []ConnectionPort `json:"ports"`
}
type connectionsRoot struct {
Connections []Connection `json:"interconnections"`
Meta meta `json:"meta"`
}
type ConnectionPort struct {
*Href `json:",inline"`
ID string `json:"id"`
LinkStatus string `json:"link_status,omitempty"`
Name string `json:"name,omitempty"`
Organization *Organization `json:"organization,omitempty"`
Role ConnectionPortRole `json:"role,omitempty"`
// Speed is the maximum allowed throughput. This value inherits changes made in the Equinix Fabric API.
Speed uint64 `json:"speed,omitempty"`
Status string `json:"status,omitempty"`
Tokens []FabricServiceToken `json:"tokens,omitempty"`
VirtualCircuits []VirtualCircuit `json:"virtual_circuits,omitempty"`
}
type Connection struct {
ID string `json:"id"`
ContactEmail string `json:"contact_email,omitempty"`
Name string `json:"name,omitempty"`
Status string `json:"status,omitempty"`
Redundancy ConnectionRedundancy `json:"redundancy,omitempty"`
Facility *Facility `json:"facility,omitempty"`
Metro *Metro `json:"metro,omitempty"`
Type ConnectionType `json:"type,omitempty"`
Mode *ConnectionMode `json:"mode,omitempty"`
Description string `json:"description,omitempty"`
Project *Project `json:"project,omitempty"`
Organization *Organization `json:"organization,omitempty"`
Speed uint64 `json:"speed,omitempty"`
Token string `json:"token,omitempty"`
Tokens []FabricServiceToken `json:"service_tokens,omitempty"`
Tags []string `json:"tags,omitempty"`
Ports []ConnectionPort `json:"ports,omitempty"`
ServiceTokenType string `json:"service_token_type,omitempty"`
}
type ConnectionCreateRequest struct {
ContactEmail string `json:"contact_email,omitempty"`
Description *string `json:"description,omitempty"`
Facility string `json:"facility,omitempty"`
Metro string `json:"metro,omitempty"`
Mode ConnectionMode `json:"mode,omitempty"`
Name string `json:"name,omitempty"`
Project string `json:"project,omitempty"`
Redundancy ConnectionRedundancy `json:"redundancy,omitempty"`
ServiceTokenType FabricServiceTokenType `json:"service_token_type,omitempty"`
Speed uint64 `json:"speed,omitempty"`
Tags []string `json:"tags,omitempty"`
Type ConnectionType `json:"type,omitempty"`
VLANs []int `json:"vlans,omitempty"`
}
type ConnectionUpdateRequest struct {
Redundancy ConnectionRedundancy `json:"redundancy,omitempty"`
Mode *ConnectionMode `json:"mode,omitempty"`
Description *string `json:"description,omitempty"`
Tags []string `json:"tags,omitempty"`
}
func (c *Connection) PortByRole(r ConnectionPortRole) *ConnectionPort {
for _, p := range c.Ports {
if p.Role == r {
return &p
}
}
return nil
}
func (s *ConnectionServiceOp) create(apiUrl string, createRequest *ConnectionCreateRequest) (*Connection, *Response, error) {
connection := new(Connection)
resp, err := s.client.DoRequest("POST", apiUrl, createRequest, connection)
if err != nil {
return nil, resp, err
}
return connection, resp, err
}
func (s *ConnectionServiceOp) OrganizationCreate(id string, createRequest *ConnectionCreateRequest) (*Connection, *Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, nil, validateErr
}
apiUrl := path.Join(organizationBasePath, id, connectionBasePath)
return s.create(apiUrl, createRequest)
}
func (s *ConnectionServiceOp) ProjectCreate(id string, createRequest *ConnectionCreateRequest) (*Connection, *Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, nil, validateErr
}
apiUrl := path.Join(projectBasePath, id, connectionBasePath)
return s.create(apiUrl, createRequest)
}
func (s *ConnectionServiceOp) list(url string, opts *GetOptions) (connections []Connection, resp *Response, err error) {
apiPathQuery := opts.WithQuery(url)
for {
subset := new(connectionsRoot)
resp, err = s.client.DoRequest("GET", apiPathQuery, nil, subset)
if err != nil {
return nil, resp, err
}
connections = append(connections, subset.Connections...)
if apiPathQuery = nextPage(subset.Meta, opts); apiPathQuery != "" {
continue
}
return
}
}
func (s *ConnectionServiceOp) OrganizationList(id string, opts *GetOptions) ([]Connection, *Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, nil, validateErr
}
apiUrl := path.Join(organizationBasePath, id, connectionBasePath)
return s.list(apiUrl, opts)
}
func (s *ConnectionServiceOp) ProjectList(id string, opts *GetOptions) ([]Connection, *Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, nil, validateErr
}
apiUrl := path.Join(projectBasePath, id, connectionBasePath)
return s.list(apiUrl, opts)
}
func (s *ConnectionServiceOp) Delete(id string, wait bool) (*Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, validateErr
}
apiPath := path.Join(connectionBasePath, id)
connection := new(Connection)
resp, err := s.client.DoRequest("DELETE", apiPath, nil, connection)
if err != nil {
return resp, err
}
// We really miss context here.
if wait {
timeout := time.After(ConnectionDeleteTimeout)
//ticker := time.Tick(ConnectionDeleteCheck)
ticker := time.NewTicker(ConnectionDeleteCheck)
for {
select {
case <-ticker.C:
c, resp2, err := s.Get(id, nil)
if resp2.StatusCode == 404 {
// Connection has been deleted
return resp, nil
}
if err != nil {
return resp, err
}
if c.Status != "deleting" {
return resp, fmt.Errorf("Connection %s is in undexpected state %s", id, c.Status)
}
case <-timeout:
return resp, errors.New("Timeout waiting for connection to be deleted")
}
}
}
return resp, nil
}
func (s *ConnectionServiceOp) Port(connID, portID string, opts *GetOptions) (*ConnectionPort, *Response, error) {
if validateErr := ValidateUUID(connID); validateErr != nil {
return nil, nil, validateErr
}
if validateErr := ValidateUUID(portID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(connectionBasePath, connID, portBasePath, portID)
apiPathQuery := opts.WithQuery(endpointPath)
port := new(ConnectionPort)
resp, err := s.client.DoRequest("GET", apiPathQuery, nil, port)
if err != nil {
return nil, resp, err
}
return port, resp, err
}
func (s *ConnectionServiceOp) Get(id string, opts *GetOptions) (*Connection, *Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(connectionBasePath, id)
apiPathQuery := opts.WithQuery(endpointPath)
connection := new(Connection)
resp, err := s.client.DoRequest("GET", apiPathQuery, nil, connection)
if err != nil {
return nil, resp, err
}
return connection, resp, err
}
func (s *ConnectionServiceOp) Update(id string, updateRequest *ConnectionUpdateRequest, opts *GetOptions) (*Connection, *Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(connectionBasePath, id)
apiPathQuery := opts.WithQuery(endpointPath)
connection := new(Connection)
resp, err := s.client.DoRequest("PUT", apiPathQuery, updateRequest, connection)
if err != nil {
return nil, resp, err
}
return connection, resp, err
}
func (s *ConnectionServiceOp) Ports(connID string, opts *GetOptions) ([]ConnectionPort, *Response, error) {
if validateErr := ValidateUUID(connID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(connectionBasePath, connID, portBasePath)
apiPathQuery := opts.WithQuery(endpointPath)
ports := new(connectionPortsRoot)
resp, err := s.client.DoRequest("GET", apiPathQuery, nil, ports)
if err != nil {
return nil, resp, err
}
return ports.Ports, resp, nil
}
func (s *ConnectionServiceOp) Events(id string, opts *GetOptions) ([]Event, *Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, nil, validateErr
}
apiPath := path.Join(connectionBasePath, id, eventBasePath)
return listEvents(s.client, apiPath, opts)
}
func (s *ConnectionServiceOp) PortEvents(connID, portID string, opts *GetOptions) ([]Event, *Response, error) {
if validateErr := ValidateUUID(connID); validateErr != nil {
return nil, nil, validateErr
}
if validateErr := ValidateUUID(portID); validateErr != nil {
return nil, nil, validateErr
}
apiPath := path.Join(connectionBasePath, connID, portBasePath, portID, eventBasePath)
return listEvents(s.client, apiPath, opts)
}
func (s *ConnectionServiceOp) VirtualCircuits(connID, portID string, opts *GetOptions) (vcs []VirtualCircuit, resp *Response, err error) {
if validateErr := ValidateUUID(connID); validateErr != nil {
return nil, nil, validateErr
}
if validateErr := ValidateUUID(portID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(connectionBasePath, connID, portBasePath, portID, virtualCircuitBasePath)
apiPathQuery := opts.WithQuery(endpointPath)
for {
subset := new(virtualCircuitsRoot)
resp, err = s.client.DoRequest("GET", apiPathQuery, nil, subset)
if err != nil {
return nil, resp, err
}
vcs = append(vcs, subset.VirtualCircuits...)
if apiPathQuery = nextPage(subset.Meta, opts); apiPathQuery != "" {
continue
}
return
}
}