/
api_client.go
428 lines (400 loc) · 21.5 KB
/
api_client.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package isegosdk
import (
"crypto/tls"
"fmt"
"os"
"io/ioutil"
"path/filepath"
"github.com/go-resty/resty/v2"
)
// RestyClient is the REST Client
var host = ""
var UseAPIGateway = false
var useCSRFToken = false
var CSRFToken string
const ISE_BASE_URL = "ISE_BASE_URL"
const ISE_USERNAME = "ISE_USERNAME"
const ISE_PASSWORD = "ISE_PASSWORD"
const ISE_DEBUG = "ISE_DEBUG"
const ISE_SSL_VERIFY = "ISE_SSL_VERIFY"
const ISE_USE_API_GATEWAY = "ISE_USE_API_GATEWAY"
const ISE_USE_CSRF_TOKEN = "ISE_USE_CSRF_TOKEN"
const emptyStringToJSONError = "unexpected end of JSON input"
type FileDownload struct {
FileName string
FileData []byte
}
func (f *FileDownload) SaveDownload(path string) error {
fpath := filepath.Join(path, f.FileName)
return ioutil.WriteFile(fpath, f.FileData, 0664)
}
type Client struct {
common service // Reuse a single struct instead of allocating one for each service on the heap.
// API Services
AciBindings *AciBindingsService
AciSettings *AciSettingsService
AncEndpoint *AncEndpointService
ActiveDirectory *ActiveDirectoryService
AdminUser *AdminUserService
AllowedProtocols *AllowedProtocolsService
AncPolicy *AncPolicyService
AuthorizationProfile *AuthorizationProfileService
ByodPortal *ByodPortalService
BackupAndRestore *BackupAndRestoreService
CertificateProfile *CertificateProfileService
CertificateTemplate *CertificateTemplateService
Certificates *CertificatesService
ClearThreatsAndVulnerabilities *ClearThreatsAndVulnerabilitiesService
Consumer *ConsumerService
DeviceAdministrationAuthenticationRules *DeviceAdministrationAuthenticationRulesService
DeviceAdministrationAuthorizationExceptionRules *DeviceAdministrationAuthorizationExceptionRulesService
DeviceAdministrationAuthorizationGlobalExceptionRules *DeviceAdministrationAuthorizationGlobalExceptionRulesService
DeviceAdministrationAuthorizationRules *DeviceAdministrationAuthorizationRulesService
DeviceAdministrationCommandSet *DeviceAdministrationCommandSetService
DeviceAdministrationConditions *DeviceAdministrationConditionsService
DeviceAdministrationDictionaryAttributesList *DeviceAdministrationDictionaryAttributesListService
DeviceAdministrationIDentityStores *DeviceAdministrationIDentityStoresService
DeviceAdministrationNetworkConditions *DeviceAdministrationNetworkConditionsService
DeviceAdministrationPolicySet *DeviceAdministrationPolicySetService
DeviceAdministrationProfiles *DeviceAdministrationProfilesService
DeviceAdministrationServiceNames *DeviceAdministrationServiceNamesService
DeviceAdministrationTimeDateConditions *DeviceAdministrationTimeDateConditionsService
DownloadableACL *DownloadableACLService
EgressMatrixCell *EgressMatrixCellService
EndpointCertificate *EndpointCertificateService
EndpointIDentityGroup *EndpointIDentityGroupService
ExternalRadiusServer *ExternalRadiusServerService
FilterPolicy *FilterPolicyService
GuestLocation *GuestLocationService
GuestSmtpNotificationConfiguration *GuestSmtpNotificationConfigurationService
GuestSSID *GuestSSIDService
GuestType *GuestTypeService
GuestUser *GuestUserService
HotspotPortal *HotspotPortalService
IPToSgtMapping *IPToSgtMappingService
IPToSgtMappingGroup *IPToSgtMappingGroupService
IDentityGroups *IDentityGroupsService
IDentitySequence *IDentitySequenceService
InternalUser *InternalUserService
Licensing *LicensingService
Mdm *MdmService
Misc *MiscService
MyDevicePortal *MyDevicePortalService
NativeSupplicantProfile *NativeSupplicantProfileService
NetworkAccessAuthenticationRules *NetworkAccessAuthenticationRulesService
NetworkAccessAuthorizationExceptionRules *NetworkAccessAuthorizationExceptionRulesService
NetworkAccessAuthorizationGlobalExceptionRules *NetworkAccessAuthorizationGlobalExceptionRulesService
NetworkAccessAuthorizationRules *NetworkAccessAuthorizationRulesService
NetworkAccessConditions *NetworkAccessConditionsService
NetworkAccessDictionary *NetworkAccessDictionaryService
NetworkAccessDictionaryAttribute *NetworkAccessDictionaryAttributeService
NetworkAccessDictionaryAttributesList *NetworkAccessDictionaryAttributesListService
NetworkAccessIDentityStores *NetworkAccessIDentityStoresService
NetworkAccessNetworkConditions *NetworkAccessNetworkConditionsService
NetworkAccessPolicySet *NetworkAccessPolicySetService
NetworkAccessProfiles *NetworkAccessProfilesService
NetworkAccessSecurityGroups *NetworkAccessSecurityGroupsService
NetworkAccessServiceNames *NetworkAccessServiceNamesService
NetworkAccessTimeDateConditions *NetworkAccessTimeDateConditionsService
NetworkDevice *NetworkDeviceService
NetworkDeviceGroup *NetworkDeviceGroupService
NodeDeployment *NodeDeploymentService
NodeGroup *NodeGroupService
NodeServices *NodeServicesService
NodeDetails *NodeDetailsService
PanHa *PanHaService
Patching *PatchingService
PortalGlobalSetting *PortalGlobalSettingService
PortalTheme *PortalThemeService
Profiler *ProfilerService
ProfilerProfile *ProfilerProfileService
Provider *ProviderService
PsnNodeDetailsWithRadiusService *PsnNodeDetailsWithRadiusServiceService
PullDeploymentInfo *PullDeploymentInfoService
PxGridSettings *PxGridSettingsService
RadiusFailure *RadiusFailureService
RadiusServerSequence *RadiusServerSequenceService
RestidStore *RestidStoreService
Repository *RepositoryService
SmsProvider *SmsProviderService
SxpConnections *SxpConnectionsService
SxpLocalBindings *SxpLocalBindingsService
SxpVpns *SxpVpnsService
SecurityGroupToVirtualNetwork *SecurityGroupToVirtualNetworkService
SecurityGroups *SecurityGroupsService
SecurityGroupsACLs *SecurityGroupsACLsService
SelfRegisteredPortal *SelfRegisteredPortalService
SessionDirectory *SessionDirectoryService
SponsorGroup *SponsorGroupService
SponsorGroupMember *SponsorGroupMemberService
SponsorPortal *SponsorPortalService
SponsoredGuestPortal *SponsoredGuestPortalService
SupportBundleDownload *SupportBundleDownloadService
SupportBundleStatus *SupportBundleStatusService
SupportBundleTriggerConfiguration *SupportBundleTriggerConfigurationService
SystemHealth *SystemHealthService
SystemCertificate *SystemCertificateService
TacacsCommandSets *TacacsCommandSetsService
TacacsExternalServers *TacacsExternalServersService
TacacsProfile *TacacsProfileService
TacacsServerSequence *TacacsServerSequenceService
TelemetryInformation *TelemetryInformationService
TrustSecConfiguration *TrustSecConfigurationService
TrustSecSxp *TrustSecSxpService
VersionAndPatch *VersionAndPatchService
VersionInfo *VersionInfoService
Endpoint *EndpointService
NbarApp *NbarAppService
Portal *PortalService
Proxy *ProxyService
PxGridNode *PxGridNodeService
SgVnMapping *SgVnMappingService
Tasks *TasksService
Telemetry *TelemetryService
VirtualNetwork *VirtualNetworkService
VnVLANMapping *VnVLANMappingService
}
type service struct {
client *resty.Client
}
func setHost(client *resty.Client, module string) {
port := getPort(module)
client.SetBaseURL(host + port)
}
func getCSFRToken(headers map[string][]string) {
if useCSRFToken {
if val, ok := headers["X-Csrf-Token"]; ok {
CSRFToken = val[0]
}
}
}
func setCSRFToken(client *resty.Client) {
if useCSRFToken {
tval := "fetch"
if CSRFToken != "" {
tval = CSRFToken
}
client.SetHeader("X-CSRF-Token", tval)
}
}
func getPort(group string) string {
if UseAPIGateway {
return ""
}
if group == "_ui" {
return ":443"
}
if group == "_ers" {
return ":9060"
}
if group == "_mnt" {
return ":443"
}
if group == "_px_grid" {
return ":8910"
}
return ""
}
// Error indicates an error from the invocation of a Cisco ISE.
var Error map[string]interface{}
// NewClient creates a new API client. Requires a userAgent string describing your application.
// optionally a custom http.Client to allow for advanced features such as caching.
func NewClient() (*Client, error) {
var err error
client := resty.New()
c := &Client{}
c.common.client = client
username := ""
password := ""
if os.Getenv(ISE_DEBUG) == "true" {
client.SetDebug(true)
}
if os.Getenv(ISE_SSL_VERIFY) == "false" {
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
}
if os.Getenv(ISE_BASE_URL) != "" {
host = os.Getenv(ISE_BASE_URL)
} else {
err = fmt.Errorf("enviroment variable %s was not defined", ISE_BASE_URL)
}
if os.Getenv(ISE_USERNAME) != "" {
username = os.Getenv(ISE_USERNAME)
} else {
err = fmt.Errorf("enviroment variable %s was not defined", ISE_USERNAME)
}
if os.Getenv(ISE_PASSWORD) != "" {
password = os.Getenv(ISE_PASSWORD)
} else {
err = fmt.Errorf("enviroment variable %s was not defined", ISE_PASSWORD)
}
if os.Getenv(ISE_USE_API_GATEWAY) == "true" {
UseAPIGateway = true
}
if os.Getenv(ISE_USE_CSRF_TOKEN) == "true" {
useCSRFToken = true
}
if err != nil {
return nil, err
}
c.AciBindings = (*AciBindingsService)(&c.common)
c.AciSettings = (*AciSettingsService)(&c.common)
c.AncEndpoint = (*AncEndpointService)(&c.common)
c.ActiveDirectory = (*ActiveDirectoryService)(&c.common)
c.AdminUser = (*AdminUserService)(&c.common)
c.AllowedProtocols = (*AllowedProtocolsService)(&c.common)
c.AncPolicy = (*AncPolicyService)(&c.common)
c.AuthorizationProfile = (*AuthorizationProfileService)(&c.common)
c.ByodPortal = (*ByodPortalService)(&c.common)
c.BackupAndRestore = (*BackupAndRestoreService)(&c.common)
c.CertificateProfile = (*CertificateProfileService)(&c.common)
c.CertificateTemplate = (*CertificateTemplateService)(&c.common)
c.Certificates = (*CertificatesService)(&c.common)
c.ClearThreatsAndVulnerabilities = (*ClearThreatsAndVulnerabilitiesService)(&c.common)
c.Consumer = (*ConsumerService)(&c.common)
c.DeviceAdministrationAuthenticationRules = (*DeviceAdministrationAuthenticationRulesService)(&c.common)
c.DeviceAdministrationAuthorizationExceptionRules = (*DeviceAdministrationAuthorizationExceptionRulesService)(&c.common)
c.DeviceAdministrationAuthorizationGlobalExceptionRules = (*DeviceAdministrationAuthorizationGlobalExceptionRulesService)(&c.common)
c.DeviceAdministrationAuthorizationRules = (*DeviceAdministrationAuthorizationRulesService)(&c.common)
c.DeviceAdministrationCommandSet = (*DeviceAdministrationCommandSetService)(&c.common)
c.DeviceAdministrationConditions = (*DeviceAdministrationConditionsService)(&c.common)
c.DeviceAdministrationDictionaryAttributesList = (*DeviceAdministrationDictionaryAttributesListService)(&c.common)
c.DeviceAdministrationIDentityStores = (*DeviceAdministrationIDentityStoresService)(&c.common)
c.DeviceAdministrationNetworkConditions = (*DeviceAdministrationNetworkConditionsService)(&c.common)
c.DeviceAdministrationPolicySet = (*DeviceAdministrationPolicySetService)(&c.common)
c.DeviceAdministrationProfiles = (*DeviceAdministrationProfilesService)(&c.common)
c.DeviceAdministrationServiceNames = (*DeviceAdministrationServiceNamesService)(&c.common)
c.DeviceAdministrationTimeDateConditions = (*DeviceAdministrationTimeDateConditionsService)(&c.common)
c.DownloadableACL = (*DownloadableACLService)(&c.common)
c.EgressMatrixCell = (*EgressMatrixCellService)(&c.common)
c.EndpointCertificate = (*EndpointCertificateService)(&c.common)
c.EndpointIDentityGroup = (*EndpointIDentityGroupService)(&c.common)
c.ExternalRadiusServer = (*ExternalRadiusServerService)(&c.common)
c.FilterPolicy = (*FilterPolicyService)(&c.common)
c.GuestLocation = (*GuestLocationService)(&c.common)
c.GuestSmtpNotificationConfiguration = (*GuestSmtpNotificationConfigurationService)(&c.common)
c.GuestSSID = (*GuestSSIDService)(&c.common)
c.GuestType = (*GuestTypeService)(&c.common)
c.GuestUser = (*GuestUserService)(&c.common)
c.HotspotPortal = (*HotspotPortalService)(&c.common)
c.IPToSgtMapping = (*IPToSgtMappingService)(&c.common)
c.IPToSgtMappingGroup = (*IPToSgtMappingGroupService)(&c.common)
c.IDentityGroups = (*IDentityGroupsService)(&c.common)
c.IDentitySequence = (*IDentitySequenceService)(&c.common)
c.InternalUser = (*InternalUserService)(&c.common)
c.Licensing = (*LicensingService)(&c.common)
c.Mdm = (*MdmService)(&c.common)
c.Misc = (*MiscService)(&c.common)
c.MyDevicePortal = (*MyDevicePortalService)(&c.common)
c.NativeSupplicantProfile = (*NativeSupplicantProfileService)(&c.common)
c.NetworkAccessAuthenticationRules = (*NetworkAccessAuthenticationRulesService)(&c.common)
c.NetworkAccessAuthorizationExceptionRules = (*NetworkAccessAuthorizationExceptionRulesService)(&c.common)
c.NetworkAccessAuthorizationGlobalExceptionRules = (*NetworkAccessAuthorizationGlobalExceptionRulesService)(&c.common)
c.NetworkAccessAuthorizationRules = (*NetworkAccessAuthorizationRulesService)(&c.common)
c.NetworkAccessConditions = (*NetworkAccessConditionsService)(&c.common)
c.NetworkAccessDictionary = (*NetworkAccessDictionaryService)(&c.common)
c.NetworkAccessDictionaryAttribute = (*NetworkAccessDictionaryAttributeService)(&c.common)
c.NetworkAccessDictionaryAttributesList = (*NetworkAccessDictionaryAttributesListService)(&c.common)
c.NetworkAccessIDentityStores = (*NetworkAccessIDentityStoresService)(&c.common)
c.NetworkAccessNetworkConditions = (*NetworkAccessNetworkConditionsService)(&c.common)
c.NetworkAccessPolicySet = (*NetworkAccessPolicySetService)(&c.common)
c.NetworkAccessProfiles = (*NetworkAccessProfilesService)(&c.common)
c.NetworkAccessSecurityGroups = (*NetworkAccessSecurityGroupsService)(&c.common)
c.NetworkAccessServiceNames = (*NetworkAccessServiceNamesService)(&c.common)
c.NetworkAccessTimeDateConditions = (*NetworkAccessTimeDateConditionsService)(&c.common)
c.NetworkDevice = (*NetworkDeviceService)(&c.common)
c.NetworkDeviceGroup = (*NetworkDeviceGroupService)(&c.common)
c.NodeDeployment = (*NodeDeploymentService)(&c.common)
c.NodeGroup = (*NodeGroupService)(&c.common)
c.NodeServices = (*NodeServicesService)(&c.common)
c.NodeDetails = (*NodeDetailsService)(&c.common)
c.PanHa = (*PanHaService)(&c.common)
c.Patching = (*PatchingService)(&c.common)
c.PortalGlobalSetting = (*PortalGlobalSettingService)(&c.common)
c.PortalTheme = (*PortalThemeService)(&c.common)
c.Profiler = (*ProfilerService)(&c.common)
c.ProfilerProfile = (*ProfilerProfileService)(&c.common)
c.Provider = (*ProviderService)(&c.common)
c.PsnNodeDetailsWithRadiusService = (*PsnNodeDetailsWithRadiusServiceService)(&c.common)
c.PullDeploymentInfo = (*PullDeploymentInfoService)(&c.common)
c.PxGridSettings = (*PxGridSettingsService)(&c.common)
c.RadiusFailure = (*RadiusFailureService)(&c.common)
c.RadiusServerSequence = (*RadiusServerSequenceService)(&c.common)
c.RestidStore = (*RestidStoreService)(&c.common)
c.Repository = (*RepositoryService)(&c.common)
c.SmsProvider = (*SmsProviderService)(&c.common)
c.SxpConnections = (*SxpConnectionsService)(&c.common)
c.SxpLocalBindings = (*SxpLocalBindingsService)(&c.common)
c.SxpVpns = (*SxpVpnsService)(&c.common)
c.SecurityGroupToVirtualNetwork = (*SecurityGroupToVirtualNetworkService)(&c.common)
c.SecurityGroups = (*SecurityGroupsService)(&c.common)
c.SecurityGroupsACLs = (*SecurityGroupsACLsService)(&c.common)
c.SelfRegisteredPortal = (*SelfRegisteredPortalService)(&c.common)
c.SessionDirectory = (*SessionDirectoryService)(&c.common)
c.SponsorGroup = (*SponsorGroupService)(&c.common)
c.SponsorGroupMember = (*SponsorGroupMemberService)(&c.common)
c.SponsorPortal = (*SponsorPortalService)(&c.common)
c.SponsoredGuestPortal = (*SponsoredGuestPortalService)(&c.common)
c.SupportBundleDownload = (*SupportBundleDownloadService)(&c.common)
c.SupportBundleStatus = (*SupportBundleStatusService)(&c.common)
c.SupportBundleTriggerConfiguration = (*SupportBundleTriggerConfigurationService)(&c.common)
c.SystemHealth = (*SystemHealthService)(&c.common)
c.SystemCertificate = (*SystemCertificateService)(&c.common)
c.TacacsCommandSets = (*TacacsCommandSetsService)(&c.common)
c.TacacsExternalServers = (*TacacsExternalServersService)(&c.common)
c.TacacsProfile = (*TacacsProfileService)(&c.common)
c.TacacsServerSequence = (*TacacsServerSequenceService)(&c.common)
c.TelemetryInformation = (*TelemetryInformationService)(&c.common)
c.TrustSecConfiguration = (*TrustSecConfigurationService)(&c.common)
c.TrustSecSxp = (*TrustSecSxpService)(&c.common)
c.VersionAndPatch = (*VersionAndPatchService)(&c.common)
c.VersionInfo = (*VersionInfoService)(&c.common)
c.Endpoint = (*EndpointService)(&c.common)
c.NbarApp = (*NbarAppService)(&c.common)
c.Portal = (*PortalService)(&c.common)
c.Proxy = (*ProxyService)(&c.common)
c.PxGridNode = (*PxGridNodeService)(&c.common)
c.SgVnMapping = (*SgVnMappingService)(&c.common)
c.Tasks = (*TasksService)(&c.common)
c.Telemetry = (*TelemetryService)(&c.common)
c.VirtualNetwork = (*VirtualNetworkService)(&c.common)
c.VnVLANMapping = (*VnVLANMappingService)(&c.common)
client.SetBasicAuth(username, password)
return c, nil
}
//NewClientWithOptions is the client with options passed with parameters
func NewClientWithOptions(baseURL string, username string, password string, debug string, sslVerify string, useAPIGateway string, useCSRFToken string) (*Client, error) {
var err error
err = os.Setenv(ISE_BASE_URL, baseURL)
if err != nil {
return nil, err
}
err = os.Setenv(ISE_USERNAME, username)
if err != nil {
return nil, err
}
err = os.Setenv(ISE_PASSWORD, password)
if err != nil {
return nil, err
}
err = os.Setenv(ISE_DEBUG, debug)
if err != nil {
return nil, err
}
err = os.Setenv(ISE_SSL_VERIFY, sslVerify)
if err != nil {
return nil, err
}
err = os.Setenv(ISE_USE_API_GATEWAY, useAPIGateway)
if err != nil {
return nil, err
}
err = os.Setenv(ISE_USE_CSRF_TOKEN, useCSRFToken)
if err != nil {
return nil, err
}
return NewClient()
}
// RestyClient returns the resty.Client used by the sdk
func (s *Client) RestyClient() *resty.Client {
return s.common.client
}