forked from scottdware/go-bigip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.go
280 lines (248 loc) · 8 KB
/
shared.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
package bigip
import (
"bytes"
"fmt"
"os"
"strings"
"time"
)
const (
uriClientSSL = "client-ssl"
uriDatagroup = "data-group"
uriHttp = "http"
uriHttpCompression = "http-compression"
uriIRule = "rule"
uriInternal = "internal"
uriLtm = "ltm"
uriMonitor = "monitor"
uriNode = "node"
uriOneConnect = "one-connect"
uriPolicy = "policy"
uriPool = "pool"
uriPoolMember = "members"
uriProfile = "profile"
uriRules = "rules"
uriServerSSL = "server-ssl"
uriSnatPool = "snatpool"
uriTcp = "tcp"
uriUdp = "udp"
uriVirtual = "virtual"
uriVirtualAddress = "virtual-address"
uriGtm = "gtm"
uriWideIp = "wideip"
uriARecord = "a"
uriAAAARecord = "aaaa"
uriCNameRecord = "cname"
uriMXRecord = "mx"
uriNaptrRecord = "naptr"
uriSrvRecord = "srv"
uriPoolMembers = "members"
ENABLED = "enable"
DISABLED = "disable"
CONTEXT_SERVER = "serverside"
CONTEXT_CLIENT = "clientside"
CONTEXT_ALL = "all"
// Newer policy APIs have a draft-publish workflow that this library does not support.
policyVersionSuffix = "?ver=11.5.1"
)
var cidr = map[string]string{
"0": "0.0.0.0",
"1": "128.0.0.0",
"2": "192.0.0.0",
"3": "224.0.0.0",
"4": "240.0.0.0",
"5": "248.0.0.0",
"6": "252.0.0.0",
"7": "254.0.0.0",
"8": "255.0.0.0",
"9": "255.128.0.0",
"10": "255.192.0.0",
"11": "255.224.0.0",
"12": "255.240.0.0",
"13": "255.248.0.0",
"14": "255.252.0.0",
"15": "255.254.0.0",
"16": "255.255.0.0",
"17": "255.255.128.0",
"18": "255.255.192.0",
"19": "255.255.224.0",
"20": "255.255.240.0",
"21": "255.255.248.0",
"22": "255.255.252.0",
"23": "255.255.254.0",
"24": "255.255.255.0",
"25": "255.255.255.128",
"26": "255.255.255.192",
"27": "255.255.255.224",
"28": "255.255.255.240",
"29": "255.255.255.248",
"30": "255.255.255.252",
"31": "255.255.255.254",
"32": "255.255.255.255",
}
// GTMType handles the record types possible in the GTM as strings
type GTMType string
// GTM Record Types
const (
ARecord GTMType = uriARecord
AAAARecord GTMType = uriAAAARecord
CNAMERecord GTMType = uriCNameRecord
MXRecord GTMType = uriMXRecord
NAPTRRecord GTMType = uriNaptrRecord
SRVRecord GTMType = uriSrvRecord
)
const (
uriShared = "shared"
uriLicensing = "licensing"
uriActivation = "activation"
uriRegistration = "registration"
uriFileTransfer = "file-transfer"
uriUploads = "uploads"
activationComplete = "LICENSING_COMPLETE"
activationInProgress = "LICENSING_ACTIVATION_IN_PROGRESS"
activationFailed = "LICENSING_FAILED"
activationNeedEula = "NEED_EULA_ACCEPT"
)
// https://devcentral.f5.com/wiki/iControl.Licensing_resource_API.ashx
type Activation struct {
BaseRegKey string `json:"baseRegKey,omitempty"`
AddOnKeys []string `json:"addOnKeys,omitempty"`
IsAutomaticActivation bool `json:"isAutomaticActivation"`
Status string `json:"status,omitempty"`
LicenseText *string `json:"licenseText,omitempty"`
ErrorText *string `json:"errorText,omitempty"`
EulaText *string `json:"eulaText,omitempty"`
}
// https://devcentral.f5.com/wiki/iControl.Licensing_resource_API.ashx
type LicenseState struct {
Vendor string `json:"vendor"`
LicensedDateTime string `json:"licensedDateTime"`
LicensedVersion string `json:"licensedVersion"`
LicenseEndDateTime string `json:"licenseEndDateTime"`
LicenseStartDateTime string `json:"licenseStartDateTime"`
RegistrationKey string `json:"registrationKey"`
Dossier string `json:"dossier"`
Authorization string `json:"authorization"`
Usage string `json:"usage"`
PlatformId string `json:"platformId"`
AuthVers string `json:"authVers"`
ServiceCheckDateTime string `json:"serviceCheckDateTime"`
MachineId string `json:"machineId"`
ExclusivePlatform []string `json:"exclusivePlatform"`
ActiveModules []string `json:"activeModules"`
OptionalModules []string `json:"optionalModules"`
FeatureFlags []LicenseFeatureFlag `json:"featureFlags"`
ExpiresInDays string `json:"expiresInDays"`
ExpiresInDaysMessage string `json:"expiresInDaysMessage"`
}
// Describes feature flags that are defined in licenses.
type LicenseFeatureFlag struct {
FeatureName string `json:"featureName"`
FeatureValue string `json:"featureValue"`
}
// Gets the current activation status. Use after calling Activate. See the docs for more:
// https://devcentral.f5.com/wiki/iControl.Licensing_activation_APIs.ashx
func (b *BigIP) GetActivationStatus() (*Activation, error) {
var a Activation
err, _ := b.getForEntity(&a, uriShared, uriLicensing, uriActivation)
if err != nil {
return nil, err
}
return &a, nil
}
// Sends the Activation to the activation endpoint. For documentation on how this works, see:
// https://devcentral.f5.com/wiki/iControl.Licensing_activation_APIs.ashx
func (b *BigIP) Activate(a Activation) error {
return b.post(a, uriShared, uriLicensing, uriActivation)
}
// Returns the current license state.
func (b *BigIP) GetLicenseState() (*LicenseState, error) {
var l LicenseState
err, _ := b.getForEntity(&l, uriShared, uriLicensing, uriRegistration)
if err != nil {
return nil, err
}
return &l, nil
}
// Installs the given license.
func (b *BigIP) InstallLicense(licenseText string) error {
r := map[string]string{"licenseText": licenseText}
return b.put(r, uriShared, uriLicensing, uriRegistration)
}
// Automatically activate this registration key and install the resulting license.
// The BIG-IP must have access to the activation server for this to work.
func (b *BigIP) AutoLicense(regKey string, addOnKeys []string, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
actreq := Activation{BaseRegKey: regKey, AddOnKeys: addOnKeys, IsAutomaticActivation: true}
if err := b.Activate(actreq); err != nil {
return err
}
loop:
for time.Now().Before(deadline) {
actresp, err := b.GetActivationStatus()
if err != nil {
return err
}
if actresp.Status == activationInProgress {
time.Sleep(1 * time.Second)
continue
}
switch actresp.Status {
case activationComplete:
return b.InstallLicense(*actresp.LicenseText)
case activationFailed:
return fmt.Errorf("Licensing failed: %s", *actresp.ErrorText)
case activationNeedEula:
eula := *actresp.EulaText
actreq.EulaText = &eula
break loop
default:
return fmt.Errorf("Unknown licensing status: %s", actresp.Status)
}
}
if actreq.EulaText == nil {
return fmt.Errorf("Timed out after %s", timeout)
}
// Proceed with EULA acceptance
if err := b.Activate(actreq); err != nil {
return err
}
for time.Now().Before(deadline) {
actresp, err := b.GetActivationStatus()
if err != nil {
return err
}
if actresp.Status == activationInProgress {
time.Sleep(1 * time.Second)
continue
}
switch actresp.Status {
case activationComplete:
return b.InstallLicense(*actresp.LicenseText)
case activationNeedEula:
return fmt.Errorf("Tried to accept EULA, but status is: %s", *actresp.ErrorText)
case activationFailed:
return fmt.Errorf("Licensing failed: %s", *actresp.ErrorText)
}
return fmt.Errorf("Unknown licensing status: %s", actresp.Status)
}
return fmt.Errorf("Timed out after %s", timeout)
}
// Upload a file
func (b *BigIP) UploadFile(f *os.File) (*Upload, error) {
if strings.HasSuffix(f.Name(), ".iso") {
err := fmt.Errorf("File must not have .iso extension")
return nil, err
}
info, err := f.Stat()
if err != nil {
return nil, err
}
return b.Upload(f, info.Size(), uriShared, uriFileTransfer, uriUploads, info.Name())
}
// Upload a file from a byte slice
func (b *BigIP) UploadBytes(data []byte, filename string) (*Upload, error) {
r := bytes.NewReader(data)
size := int64(len(data))
return b.Upload(r, size, uriShared, uriFileTransfer, uriUploads, filename)
}