forked from f5devcentral/go-bigip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
363 lines (314 loc) · 11.1 KB
/
device.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
/*
Original work Copyright © 2015 Scott Ware
Modifications Copyright 2019 F5 Networks Inc
Licensed under the Apache License, Version 2.0 (the "License");
You may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
*/
package bigip
import (
"encoding/json"
)
// LIC contains device license for BIG-IP system.
type LICs struct {
LIC []LIC `json:"items"`
}
// VirtualAddress contains information about each individual virtual address.
type LIC struct {
DeviceAddress string
Username string
Password string
}
type LicensePools struct {
LicensePool []LicensePool `json:"items"`
}
type LicensePool struct {
Items []struct {
Uuid string `json:"Uuid,omitempty"`
}
}
type LICDTO struct {
DeviceAddress string `json:"deviceAddress,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
type Devicenames struct {
Devicenames []Devicename `json:"items"`
}
type Devicename struct {
Command string `json:"command,omitempty"`
Name string `json:"name,omitempty"`
Target string `json:"target,omitempty"`
}
type Devices struct {
Devices []Device `json:"items"`
}
// UnicastAddress is an abstraction and used by Device
type UnicastAddress struct {
EffectiveIP string `json:"effectiveIp"`
EffectivePort int `json:"effectivePort"`
IP string `json:"ip"`
Port int `json:"port"`
}
// Device represents an individual bigip as viewed from the cluster
// see: https://devcentral.f5.com/Wiki/iControlREST.APIRef_tm_cm_device.ashx
type Device struct {
Name string `json:"name,omitempty"`
MirrorIp string `json:"mirrorIp,omitempty"`
MirrorSecondaryIp string `json:"mirrorSecondaryIp,omitempty"`
ActiveModules []string `json:"activeModules,omitempty"`
AppService string `json:"appService,omitempty"`
BaseMac string `json:"baseMac,omitempty"`
Build string `json:"build,omitempty"`
Cert string `json:"cert,omitempty"`
ChassisID string `json:"chassisId,omitempty"`
ChassisType string `json:"chassisType,omitempty"`
ConfigsyncIp string `json:"configsyncIp,omitempty"`
Comment string `json:"comment,omitempty"`
Contact string `json:"contact,omitempty"`
Description string `json:"description,omitempty"`
Edition string `json:"edition,omitempty"`
FailoverState string `json:"failoverState,omitempty"`
HaCapacity int `json:"haCapacity,omitempty"`
Hostname string `json:"hostname,omitempty"`
InactiveModules string `json:"inactiveModules,omitempty"`
Key string `json:"key,omitempty"`
Location string `json:"location,omitempty"`
ManagementIP string `json:"managementIp,omitempty"`
MarketingName string `json:"marketingName,omitempty"`
MulticastInterface string `json:"multicastInterface,omitempty"`
MulticastIP string `json:"multicastIp,omitempty"`
MulticastPort int `json:"multicastPort,omitempty"`
OptionalModules []string `json:"optionalModules,omitempty"`
Partition string `json:"partition,omitempty"`
PlatformID string `json:"platformId,omitempty"`
Product string `json:"product,omitempty"`
SelfDevice string `json:"selfDevice,omitempty"`
TimeLimitedModules []string `json:"timeLimitedModules,omitempty"`
TimeZone string `json:"timeZone,omitempty"`
Version string `json:"version,omitempty"`
UnicastAddress []UnicastAddress
}
type Devicegroups struct {
Devicegroups []Devicegroup `json:"items"`
}
type Devicegroup struct {
AutoSync string
Name string
Partition string
Description string
Type string
FullLoadOnSync string
SaveOnAutoSync string
NetworkFailover string
IncrementalConfigSyncSizeMax int
Deviceb []Devicerecord
}
type devicegroupDTO struct {
AutoSync string `json:"autoSync,omitempty"`
Name string `json:"name,omitempty"`
Partition string `json:"partition,omitempty"`
Description string `json:"description,omitempty"`
Type string `json:"type,omitempty"`
FullLoadOnSync string `json:"fullLoadOnSync,omitempty"`
SaveOnAutoSync string `json:"saveOnAutoSync,omitempty"`
NetworkFailover string `json:"networkFailover,omitempty"`
IncrementalConfigSyncSizeMax int `json:"incrementalConfigSyncSizeMax,omitempty"`
Deviceb struct {
Items []Devicerecord `json:"items,omitempty"`
} `json:"devicesReference,omitempty"`
}
type Devicerecords struct {
Items []Devicerecord `json:"items,omitempty"`
}
type Devicerecord struct {
SetSyncLeader bool `json:"setSyncLeader"`
Name string `json:"name"`
}
func (p *Devicegroup) MarshalJSON() ([]byte, error) {
return json.Marshal(devicegroupDTO{
Name: p.Name,
Partition: p.Partition,
AutoSync: p.AutoSync,
Description: p.Description,
Type: p.Type,
FullLoadOnSync: p.FullLoadOnSync,
SaveOnAutoSync: p.SaveOnAutoSync,
NetworkFailover: p.NetworkFailover,
IncrementalConfigSyncSizeMax: p.IncrementalConfigSyncSizeMax,
Deviceb: struct {
Items []Devicerecord `json:"items,omitempty"`
}{Items: p.Deviceb},
})
}
func (p *Devicegroup) UnmarshalJSON(b []byte) error {
var dto devicegroupDTO
err := json.Unmarshal(b, &dto)
if err != nil {
return err
}
p.Name = dto.Name
p.Partition = dto.Partition
p.AutoSync = dto.AutoSync
p.Description = dto.Description
p.Type = dto.Type
p.FullLoadOnSync = dto.FullLoadOnSync
p.SaveOnAutoSync = dto.SaveOnAutoSync
p.NetworkFailover = dto.NetworkFailover
p.IncrementalConfigSyncSizeMax = dto.IncrementalConfigSyncSizeMax
p.Deviceb = dto.Deviceb.Items
return nil
}
// https://10.192.74.80/mgmt/cm/device/licensing/pool/purchased-pool/licenses
// The above command will spit out license uuid and which should be mapped uriUuid
const (
uriMgmt = "mgmt"
uriCm = "cm"
uriDiv = "device"
uriDevices = "devices"
uriDG = "device-group"
uriLins = "licensing"
uriPoo = "pool"
uriPur = "purchased-pool"
uriLicn = "licenses"
uriMemb = "members"
uriUtility = "utility"
uriOfferings = "offerings"
uriF5BIGMSPBT10G = "f37c66e0-a80d-43e8-924b-3bbe9fe96bbe"
)
func (p *LIC) MarshalJSON() ([]byte, error) {
var dto LICDTO
marshal(&dto, p)
return json.Marshal(dto)
}
func (p *LIC) UnmarshalJSON(b []byte) error {
var dto LICDTO
err := json.Unmarshal(b, &dto)
if err != nil {
return err
}
return marshal(p, &dto)
}
func (b *BigIP) getLicensePool() (*LicensePool, error) {
var licensePool LicensePool
err, _ := b.getForEntity(&licensePool, uriMgmt, uriCm, uriDiv, uriLins, uriPoo, uriPur, uriLicn)
if err != nil {
return nil, err
}
// for loop over all returned license pools to check which one has available licenses
// getAvailablePool(member[index_of_array].Uuid)
// At the end change return statement to return only the UUID string of the one where license
// is available
return &licensePool, nil
}
// VirtualAddresses returns a list of virtual addresses.
func (b *BigIP) LIC() (*LIC, error) {
var va LIC
licensePool, licensePoolErr := b.getLicensePool()
if licensePoolErr != nil {
return nil, licensePoolErr
}
err, _ := b.getForEntity(&va, uriMgmt, uriCm, uriDiv, uriLins, uriPoo, uriPur, uriLicn, licensePool.Items[0].Uuid, uriMemb)
if err != nil {
return nil, err
}
return &va, nil
}
func (b *BigIP) CreateLIC(deviceAddress string, username string, password string) error {
config := &LIC{
DeviceAddress: deviceAddress,
Username: username,
Password: password,
}
licensePool, licensePoolErr := b.getLicensePool()
if licensePoolErr != nil {
return licensePoolErr
}
return b.post(config, uriMgmt, uriCm, uriDiv, uriLins, uriPoo, uriPur, uriLicn, licensePool.Items[0].Uuid, uriMemb)
}
func (b *BigIP) ModifyLIC(config *LIC) error {
licensePool, licensePoolErr := b.getLicensePool()
if licensePoolErr != nil {
return licensePoolErr
}
return b.post(config, uriMgmt, uriCm, uriDiv, uriLins, uriPoo, uriPur, uriLicn, licensePool.Items[0].Uuid, uriMemb)
}
func (b *BigIP) LICs() (*LIC, error) {
var members LIC
licensePool, licensePoolErr := b.getLicensePool()
if licensePoolErr != nil {
return nil, licensePoolErr
}
err, _ := b.getForEntity(&members, uriMgmt, uriCm, uriDiv, uriLins, uriPoo, uriPur, uriLicn, licensePool.Items[0].Uuid, uriMemb)
if err != nil {
return nil, err
}
return &members, nil
}
func (b *BigIP) CreateDevice(name, configsyncIp, mirrorIp, mirrorSecondaryIp string) error {
config := &Device{
Name: name,
ConfigsyncIp: configsyncIp,
MirrorIp: mirrorIp,
MirrorSecondaryIp: mirrorSecondaryIp,
}
return b.post(config, uriCm, uriDiv)
}
// API does not work, you cannot modify API issue
func (b *BigIP) ModifyDevice(config *Device) error {
return b.put(config, uriCm, uriDiv)
}
func (b *BigIP) DeleteDevice(name string) error {
return b.delete(uriCm, uriDiv, name)
}
func (b *BigIP) Devices(name string) (*Device, error) {
var device Device
err, _ := b.getForEntity(&device, uriCm, uriDiv, name)
if err != nil {
return nil, err
}
return &device, nil
}
// GetDevices returns a list of the bigip's in the cluster.
func (b *BigIP) GetDevices() ([]Device, error) {
var devices Devices
err, _ := b.getForEntity(&devices, uriCm, uriDiv)
if err != nil {
return nil, err
}
return devices.Devices, nil
}
func (b *BigIP) CreateDevicegroup(p *Devicegroup) error {
return b.post(p, uriCm, uriDG)
}
func (b *BigIP) UpdateDevicegroup(name string, p *Devicegroup) error {
return b.put(p, uriCm, uriDG, name)
}
func (b *BigIP) ModifyDevicegroup(config *Devicegroup) error {
return b.put(config, uriCm, uriDG)
}
func (b *BigIP) Devicegroups(name string) (*Devicegroup, error) {
var devicegroup Devicegroup
err, _ := b.getForEntity(&devicegroup, uriCm, uriDG, name)
if err != nil {
return nil, err
}
return &devicegroup, nil
}
func (b *BigIP) DeleteDevicegroup(name string) error {
return b.delete(uriCm, uriDG, name)
}
func (b *BigIP) DeleteDevicegroupDevices(name, rname string) error {
return b.delete(uriCm, uriDG, name, uriDevices, rname)
}
func (b *BigIP) DevicegroupsDevices(name, rname string) (*Devicegroup, error) {
var devicegroup Devicegroup
err, _ := b.getForEntity(&devicegroup, uriCm, uriDG, name, uriDevices, rname)
if err != nil {
return nil, err
}
return &devicegroup, nil
}