-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathmanage-users.py
138 lines (100 loc) · 5.17 KB
/
manage-users.py
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
# Example of calling REST API from Python to manage APIC-EM users/roles using APIC-EM APIs.
# * THIS SAMPLE APPLICATION AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
# * OF ANY KIND BY CISCO, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
# * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY FITNESS FOR A PARTICULAR
# * PURPOSE, NONINFRINGEMENT, SATISFACTORY QUALITY OR ARISING FROM A COURSE OF
# * DEALING, LAW, USAGE, OR TRADE PRACTICE. CISCO TAKES NO RESPONSIBILITY
# * REGARDING ITS USAGE IN AN APPLICATION, AND IT IS PRESENTED ONLY AS AN
# * EXAMPLE. THE SAMPLE CODE HAS NOT BEEN THOROUGHLY TESTED AND IS PROVIDED AS AN
# * EXAMPLE ONLY, THEREFORE CISCO DOES NOT GUARANTEE OR MAKE ANY REPRESENTATIONS
# * REGARDING ITS RELIABILITY, SERVICEABILITY, OR FUNCTION. IN NO EVENT DOES
# * CISCO WARRANT THAT THE SOFTWARE IS ERROR FREE OR THAT CUSTOMER WILL BE ABLE
# * TO OPERATE THE SOFTWARE WITHOUT PROBLEMS OR INTERRUPTIONS. NOR DOES CISCO
# * WARRANT THAT THE SOFTWARE OR ANY EQUIPMENT ON WHICH THE SOFTWARE IS USED WILL
# * BE FREE OF VULNERABILITY TO INTRUSION OR ATTACK. THIS SAMPLE APPLICATION IS
# * NOT SUPPORTED BY CISCO IN ANY MANNER. CISCO DOES NOT ASSUME ANY LIABILITY
# * ARISING FROM THE USE OF THE APPLICATION. FURTHERMORE, IN NO EVENT SHALL CISCO
# * OR ITS SUPPLIERS BE LIABLE FOR ANY INCIDENTAL OR CONSEQUENTIAL DAMAGES, LOST
# * PROFITS, OR LOST DATA, OR ANY OTHER INDIRECT DAMAGES EVEN IF CISCO OR ITS
# * SUPPLIERS HAVE BEEN INFORMED OF THE POSSIBILITY THEREOF.-->
# import requests library
import requests
#import json library
import json
# Disable warnings
requests.packages.urllib3.disable_warnings()
controller='198.18.129.100'
#creates and returns a service ticket.
def getTicket():
print("\nCreating ticket")
# put the ip address or dns of your apic-em controller in this url
url = "https://" + controller + "/api/v1/ticket"
#the username and password to access the APIC-EM Controller
payload = {"username":"admin","password":"C1sco12345"}
#Content type must be included in the header
header = {"content-type": "application/json"}
#Performs a POST on the specified url to get the service ticket
response= requests.post(url,data=json.dumps(payload), headers=header, verify=False)
print(response.text)
#convert response to json format
r_json=response.json()
#parse the json to get the service ticket
ticket = r_json["response"]["serviceTicket"]
return ticket
#Get and display the APIC-EM Users
def getUsers(ticket):
print("\nGetting list of existing users")
# URL for user REST API call to get list of APIC-EM users.
url = "https://" + controller + "/api/v1/user"
#Content type as well as the ticket must be included in the header
header = {"content-type": "application/json", "X-Auth-Token":ticket}
# this statement performs a GET on the specified host url
response = requests.get(url, headers=header, verify=False)
# json.dumps serializes the json into a string and allows us to
# print the response in a 'pretty' format with indentation etc.
print ("Users = ")
print (json.dumps(response.json(), indent=4, separators=(',', ': ')))
#Adds a APIC-EM User
def addUser(ticket):
print("\nAdding new user")
# URL for user REST API call to get list of existing users in the network.
url = "https://" + controller + "/api/v1/user"
#Content type as well as the ticket must be included in the header
header = {"content-type": "application/json", "X-Auth-Token":ticket}
username="brett"
#Data for new user
payload={"password":"Brett123!","username":username,"authorization":[{"scope":"ALL","role":"ROLE_OBSERVER"}]}
# this statement performs a Post on the specified user url
response = requests.post(url, data=json.dumps(payload), headers=header, verify=False)
print ("Response after post: " + response.text)
return (username)
#Delete the user that corresponds to the passed in username parameter
def deleteUser(username, ticket):
print("\nRemoving user: " + username)
# URL for a specified user REST API call.
url = "https://" + controller + "/api/v1/user/" + username
#Content type as well as the ticket must be included in the header
header = {"content-type": "application/json", "X-Auth-Token":ticket}
# this statement performs a Delete on the specified user url
response = requests.delete(url, headers=header, verify=False)
print (response.text)
#Show the User that corresponds to the passed in username parameter
def showUser(username, ticket):
print("\nDisplaying user: " + username)
# URL for user REST API call to get APIC-EM user with corresponding name.
url = "https://" + controller + "/api/v1/user/" + username
#Content type as well as the ticket must be included in the header
header = {"content-type": "application/json", "X-Auth-Token":ticket}
# this statement performs a GET on the specified user url
response = requests.get(url, headers=header, verify=False)
# json.dumps serializes the json into a string and allows us to
# print the response in a 'pretty' format with indentation etc.
print ("User found = ")
print (json.dumps(response.json(), indent=4, separators=(',', ': ')))
theTicket=getTicket()
getUsers(theTicket)
name=addUser(theTicket)
showUser(name,theTicket)
getUsers(theTicket)
deleteUser(name,theTicket)
getUsers(theTicket)