-
Notifications
You must be signed in to change notification settings - Fork 0
/
vlans.go
195 lines (157 loc) · 4.62 KB
/
vlans.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
package goarubacloud
const vLANsListPath = "GetPurchasedVLans"
const vLANPurchasePath = "SetPurchaseVLan"
const vLANRemovePath = "SetRemoveVLan"
const vLANAttachPath = "SetEnqueueAssociateVLan"
const vLANDetachPath = "SetEnqueueDeassociateVLan"
// VLANsService is an interface for interfacing with the purchased VLANs
// endpoint of the Arubacloud API
type VLANsService interface {
List() ([]PurchasedVLAN, *Response, error)
Purchase(name string) (*PurchasedVLAN, *Response, error)
Delete(vlan_resource_id int) (*Response, error)
Attach(attachRequest *purchasedVLANAttachRequest) (*PurchasedVLAN, *Response, error)
Detach(network_adapter_id int, vlan_resource_id int) (*Response, error)
}
// VLANsServiceOp handles communication with the purchased VLANs related methods of the
// Arubacloud API.
type VLANsServiceOp struct {
client *Client
}
var _ VLANsService = &VLANsServiceOp{}
type PurchasedVLAN struct {
Name string
VlanCode string
ServerIds []int
ResourceId int
ResourceType int
ProductId int
CompanyId int
UserId int
}
type purchasedVLANAttachRequest struct {
NetworkAdapterId int
VLanResourceId int
Gateway string
IP string
SubnetMask string
}
type privateIP struct {
Gateway string `json:"GateWay"`
IP string
SubNetMask string
}
type purchasedVLANRoot struct {
PurchasedVLAN *PurchasedVLAN `json:"Value"`
}
type purchasedVLANsRoot struct {
PurchasedVLANs []PurchasedVLAN `json:"Value"`
}
type vLANRequestRoot struct {
NetworkAdapterId int
VLanResourceId int
SetOnVirtualMachine bool
PrivateIps []privateIP `json:"PrivateIps,omitempty"`
}
func NewPurchasedVLanAttachRequest(network_adapter_id int, vlan_resource_id int) *purchasedVLANAttachRequest {
return &purchasedVLANAttachRequest{
NetworkAdapterId: network_adapter_id,
VLanResourceId: vlan_resource_id,
}
}
// List all purchased VLANs.
func (s *VLANsServiceOp) List() ([]PurchasedVLAN, *Response, error) {
req, err := s.client.NewRequest(vLANsListPath, nil)
if err != nil {
return nil, nil, err
}
root := new(purchasedVLANsRoot)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
}
return root.PurchasedVLANs, resp, nil
}
func (s *VLANsServiceOp) Purchase(name string) (*PurchasedVLAN, *Response, error) {
body := struct{ VLanName string }{VLanName: name}
req, err := s.client.NewRequest(vLANPurchasePath, body)
if err != nil {
return nil, nil, err
}
root := new(purchasedVLANRoot)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
}
return root.PurchasedVLAN, resp, nil
}
func (s *VLANsServiceOp) Delete(vlan_resource_id int) (*Response, error) {
body := struct{ VLanResourceId int }{VLanResourceId: vlan_resource_id}
req, err := s.client.NewRequest(vLANRemovePath, body)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
func (s *VLANsServiceOp) Attach(attachRequest *purchasedVLANAttachRequest) (*PurchasedVLAN, *Response, error) {
if attachRequest == nil {
return nil, nil, NewArgError("attachRequest", "cannot be nil")
}
vLANRequestRoot := vLANRequestRoot{
NetworkAdapterId: attachRequest.NetworkAdapterId,
VLanResourceId: attachRequest.VLanResourceId,
SetOnVirtualMachine: false,
PrivateIps: []privateIP{},
}
if attachRequest.Gateway != "" {
vLANRequestRoot.SetOnVirtualMachine = true
vLANRequestRoot.PrivateIps = append(vLANRequestRoot.PrivateIps, privateIP{
Gateway: attachRequest.Gateway,
IP: attachRequest.IP,
SubNetMask: attachRequest.SubnetMask,
})
}
body := struct {
VLanRequest interface{}
}{vLANRequestRoot}
req, err := s.client.NewRequest(vLANAttachPath, body)
if err != nil {
return nil, nil, err
}
root := new(PurchasedVLAN)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
}
return root, resp, err
}
func (s *VLANsServiceOp) Detach(network_adapter_id int, vlan_resource_id int) (*Response, error) {
if network_adapter_id == 0 {
return nil, NewArgError("network_adapter_id", "cannot be nil")
}
if vlan_resource_id == 0 {
return nil, NewArgError("vlan_resource_id", "cannot be nil")
}
vLANRequestRoot := vLANRequestRoot{
NetworkAdapterId: network_adapter_id,
VLanResourceId: vlan_resource_id,
SetOnVirtualMachine: false,
}
body := struct {
VLanRequest interface{}
}{vLANRequestRoot}
req, err := s.client.NewRequest(vLANDetachPath, body)
if err != nil {
return nil, err
}
root := new(PurchasedVLAN)
resp, err := s.client.Do(req, root)
if err != nil {
return resp, err
}
return resp, err
}