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 53
/
vlan_assignments.go
215 lines (178 loc) · 7.59 KB
/
vlan_assignments.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
package packngo
import (
"path"
)
const (
portVLANAssignmentsPath = "vlan-assignments"
portVLANAssignmentsBatchPath = "batches"
)
type vlanAssignmentsRoot struct {
VLANAssignments []VLANAssignment `json:"vlan_assignments"`
Meta meta `json:"meta"`
}
type vlanAssignmentBatchesRoot struct {
VLANAssignmentBatches []VLANAssignmentBatch `json:"batches"`
Meta meta `json:"meta"`
}
// VLANAssignmentService handles operations on a VLANAssignment
type VLANAssignmentService interface {
Get(string, string, *GetOptions) (*VLANAssignment, *Response, error)
List(string, *ListOptions) ([]VLANAssignment, *Response, error)
GetBatch(string, string, *GetOptions) (*VLANAssignmentBatch, *Response, error)
ListBatch(string, *ListOptions) ([]VLANAssignmentBatch, *Response, error)
CreateBatch(string, *VLANAssignmentBatchCreateRequest, *GetOptions) (*VLANAssignmentBatch, *Response, error)
}
type VLANAssignmentServiceOp struct {
client requestDoer
}
var _ VLANAssignmentService = (*VLANAssignmentServiceOp)(nil)
type VLANAssignmentBatchState string
const (
VLANAssignmentBatchQueued VLANAssignmentBatchState = "queued"
VLANAssignmentBatchInProgress VLANAssignmentBatchState = "in_progress"
VLANAssignmentBatchCompleted VLANAssignmentBatchState = "completed"
VLANAssignmentBatchFailed VLANAssignmentBatchState = "failed"
)
type VLANAssignmentState string
const (
VLANAssignmentAssigned VLANAssignmentState = "assigned"
VLANAssignmentUnassigned VLANAssignmentState = "unassigned"
)
// VLANAssignment struct for VLANAssignmentService.Get represents a port VLAN assignment that has been enacted
type VLANAssignment struct {
// ID of the assignment
ID string `json:"id,omitempty"`
CreatedAt Timestamp `json:"created_at,omitempty"`
UpdatedAt Timestamp `json:"updated_at,omitempty"`
// Native indicates the VLAN is the native VLAN on the port and packets for this vlan will be untagged
Native bool `json:"native,omitempty"`
// State of the assignment
State VLANAssignmentState `json:"state,omitempty"`
// VLAN is the VirtualNetwork.VXLAN of the VLAN the assignment was made to
VLAN int `json:"vlan,omitempty"`
// Port is a reference to the Port the assignment was made on
Port *Port `json:"port,omitempty"`
// VirtualNetwork is a reference to the VLAN the assignment was made to
VirtualNetwork *VirtualNetwork `json:"virtual_network,omitempty"`
}
// BatchedVLANAssignment represents the data requested in the batch before being processed. ID represents the VLAN ID, not the VLAN Assignment ID.
type BatchedVLANAssignment struct {
// VirtualNetworkID is the VirtualNetwork.ID of the VLAN the assignment was made to
VirtualNetworkID string `json:"id,omitempty"`
// Native indicates the VLAN is the native VLAN on the port and packets for this vlan will be untagged
Native bool `json:"native,omitempty"`
// State of the assignment
State VLANAssignmentState `json:"state,omitempty"`
// VLAN is the VirtualNetwork.VXLAN of the VLAN the assignment was made to
VLAN int `json:"vlan,omitempty"`
}
// VLANAssignmentBatch struct for VLANAssignmentBatch
type VLANAssignmentBatch struct {
ID string `json:"id,omitempty"`
ErrorMessages []string `json:"error_messages,omitempty"`
Quantity int `json:"quantity,omitempty"`
State VLANAssignmentBatchState `json:"state,omitempty"`
CreatedAt Timestamp `json:"created_at,omitempty"`
UpdatedAt Timestamp `json:"updated_at,omitempty"`
Port *Port `json:"port,omitempty"`
Project *Project `json:"project,omitempty"`
VLANAssignments []BatchedVLANAssignment `json:"vlan_assignments,omitempty"`
}
// VLANAssignmentBatchCreateRequest struct for VLANAssignmentBatch Create
type VLANAssignmentBatchCreateRequest struct {
VLANAssignments []VLANAssignmentCreateRequest `json:"vlan_assignments"`
}
// VLANAssignmentCreateRequest struct for VLANAssignmentBatchCreateRequest
type VLANAssignmentCreateRequest struct {
VLAN string `json:"vlan,omitempty"`
State VLANAssignmentState `json:"state,omitempty"`
Native *bool `json:"native,omitempty"`
}
// List returns VLANAssignmentBatches
func (s *VLANAssignmentServiceOp) ListBatch(portID string, opts *ListOptions) (results []VLANAssignmentBatch, resp *Response, err error) {
if validateErr := ValidateUUID(portID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(portBasePath, portID, portVLANAssignmentsPath, portVLANAssignmentsBatchPath)
apiPathQuery := opts.WithQuery(endpointPath)
for {
subset := new(vlanAssignmentBatchesRoot)
resp, err = s.client.DoRequest("GET", apiPathQuery, nil, subset)
if err != nil {
return nil, resp, err
}
results = append(results, subset.VLANAssignmentBatches...)
if apiPathQuery = nextPage(subset.Meta, opts); apiPathQuery != "" {
continue
}
return
}
}
// Get returns a VLANAssignmentBatch by id
func (s *VLANAssignmentServiceOp) GetBatch(portID, batchID string, opts *GetOptions) (*VLANAssignmentBatch, *Response, error) {
if validateErr := ValidateUUID(portID); validateErr != nil {
return nil, nil, validateErr
}
if validateErr := ValidateUUID(batchID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(portBasePath, portID, portVLANAssignmentsPath, portVLANAssignmentsBatchPath, batchID)
apiPathQuery := opts.WithQuery(endpointPath)
batch := new(VLANAssignmentBatch)
resp, err := s.client.DoRequest("GET", apiPathQuery, nil, batch)
if err != nil {
return nil, resp, err
}
return batch, resp, err
}
// Create creates VLANAssignmentBatch objects
func (s *VLANAssignmentServiceOp) CreateBatch(portID string, request *VLANAssignmentBatchCreateRequest, opts *GetOptions) (*VLANAssignmentBatch, *Response, error) {
if validateErr := ValidateUUID(portID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(portBasePath, portID, portVLANAssignmentsPath, portVLANAssignmentsBatchPath)
apiPathQuery := opts.WithQuery(endpointPath)
batch := new(VLANAssignmentBatch)
resp, err := s.client.DoRequest("POST", apiPathQuery, request, batch)
if err != nil {
return nil, resp, err
}
return batch, resp, err
}
// List returns VLANAssignment
func (s *VLANAssignmentServiceOp) List(portID string, opts *ListOptions) (results []VLANAssignment, resp *Response, err error) {
if validateErr := ValidateUUID(portID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(portBasePath, portID, portVLANAssignmentsPath)
apiPathQuery := opts.WithQuery(endpointPath)
for {
subset := new(vlanAssignmentsRoot)
resp, err = s.client.DoRequest("GET", apiPathQuery, nil, subset)
if err != nil {
return nil, resp, err
}
results = append(results, subset.VLANAssignments...)
if apiPathQuery = nextPage(subset.Meta, opts); apiPathQuery != "" {
continue
}
return
}
}
// Get returns a VLANAssignment by id
func (s *VLANAssignmentServiceOp) Get(portID, assignmentID string, opts *GetOptions) (*VLANAssignment, *Response, error) {
if validateErr := ValidateUUID(portID); validateErr != nil {
return nil, nil, validateErr
}
if validateErr := ValidateUUID(assignmentID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(portBasePath, portID, portVLANAssignmentsPath, assignmentID)
apiPathQuery := opts.WithQuery(endpointPath)
VLANAssignment := new(VLANAssignment)
resp, err := s.client.DoRequest("GET", apiPathQuery, nil, VLANAssignment)
if err != nil {
return nil, resp, err
}
return VLANAssignment, resp, err
}