This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_add-user.py
executable file
·56 lines (40 loc) · 2.12 KB
/
2_add-user.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
#!/usr/bin/env python3
import requests # need this for Get/Post/Delete
import json
# These are placed here for convenience
# in the real world, one would not embed these values in a script to be redistributed
# Be sure to clear these values before sharing with others.
my_token = ""
# Now we are just defining a function to perform the same thing we did in the last example...
# this will allow us to call it whenever we want.
def get_access_token(my_token):
params = {'refresh_token': my_token}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.post('https://console.cloud.vmware.com/csp/gateway/am/api/auth/api-tokens/authorize',
params=params, headers=headers)
json_response = response.json()
access_token = json_response['access_token']
return access_token
# Here we are calling the function to retrieve the access token and store it as a variable.
access_token = get_access_token(my_token)
# Based on the API URL, we need to supply a couple additional pieces of information - first, the ORG ID:
org_id = ""
# We must also provide the ID of the group to add users to
# We are using the python "input" method to request user input:
group_id = input("Please type in the group ID to add the user to: ")
# Finally, we have to request the email address of the user to add to the group:
users = input("Please type in the email of the user to add: ")
# Below we build a JSON payload to provide the necessary information to the API...
# See https://developer.vmware.com/apis/csp/csp-iam/latest/csp/gateway/am/api/orgs/orgId/groups/groupId/users/post/
# for more information
payload = {
'notifyUsers': 'false',
'usernamesToAdd': [users]
}
myHeader = {'csp-auth-token': access_token, 'Content-Type': 'application/json'}
myURL = f'https://console.cloud.vmware.com/csp/gateway/am/api/orgs/{org_id}/groups/{group_id}/users'
response = requests.post(myURL, data=json.dumps(payload), headers=myHeader)
json_response = response.json()
print(" ")
print(f"Added: {json_response['succeeded']}")
print(f"Failed: {json_response['failed']}")