-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetUserProjects.py
120 lines (114 loc) · 4.22 KB
/
GetUserProjects.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
# region headers
# escript-template v20190611 / [email protected]
# * author: [email protected],
# * version: 2019/09/18
# task_name: GetUserProjects
# description: Counts how many projects a user owns.
# Returns an error if the count is too high.
# endregion
#region capture Calm variables
username = '@@{pc.username}@@'
username_secret = "@@{pc.secret}@@"
api_server = "@@{pc_ip}@@"
nutanix_calm_user_uuid = "@@{nutanix_calm_user_uuid}@@"
nutanix_calm_user_name = "@@{calm_username}@@"
# endregion
#region define variables
max_project_count = 3
user_project_count = 0
#endregion
# region prepare api call
api_server_port = "9440"
api_server_endpoint = "/api/nutanix/v3/projects/list"
length = 100
url = "https://{}:{}{}".format(
api_server,
api_server_port,
api_server_endpoint
)
method = "POST"
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
# Compose the json payload
payload = {
"kind":"project",
"length":length,
"offset":0
}
# endregion
#region make the api call
print("Making a {} API call to {}".format(method, url))
resp = urlreq(
url,
verb=method,
auth='BASIC',
user=username,
passwd=username_secret,
params=json.dumps(payload),
headers=headers,
verify=False
)
#endregion
#region process the results
if resp.ok:
json_resp = json.loads(resp.content)
print("Processing results from {} to {}".format(json_resp['metadata']['offset'], json_resp['metadata']['length']))
for project in json_resp['entities']:
if project['metadata'].get("owner_reference"):
#print("Comparing {} with {}".format(nutanix_calm_user_uuid,project['metadata']['owner_reference']['uuid']))
if nutanix_calm_user_uuid == project['metadata']['owner_reference']['uuid']:
user_project_count = user_project_count + 1
else:
print("Project {} has no owner".format(project['status']['name']))
while json_resp['metadata']['length'] is length:
payload = {
"kind": "project",
"length":length,
"offset": json_resp['metadata']['length'] + json_resp['metadata']['offset'] + 1
}
resp = urlreq(
url,
verb=method,
auth='BASIC',
user=username,
passwd=username_secret,
params=json.dumps(payload),
headers=headers,
verify=False
)
if resp.ok:
json_resp = json.loads(resp.content)
print("Processing results from {} to {}".format(json_resp['metadata']['offset'], json_resp['metadata']['offset'] + json_resp['metadata']['length']))
for project in json_resp['entities']:
if project['metadata'].get("owner_reference"):
#print("Comparing {} with {}".format(nutanix_calm_user_uuid,project['metadata']['owner_reference']['uuid']))
if nutanix_calm_user_uuid == project['metadata']['owner_reference']['uuid']:
user_project_count = user_project_count + 1
else:
print("Project {} has no owner".format(project['status']['name']))
else:
print("Request failed")
print("Headers: {}".format(headers))
print("Payload: {}".format(json.dumps(payload)))
print('Status code: {}'.format(resp.status_code))
print('Response: {}'.format(json.dumps(json.loads(resp.content), indent=4)))
exit(1)
if user_project_count >= max_project_count:
print("User {0} already owns {1} projects which is greater than the maximum allowed ({2})".format(nutanix_calm_user_name,user_project_count,max_project_count))
exit(1)
else:
print("User {0} owns {1} projects which is lower than the maximum allowed ({2})".format(nutanix_calm_user_name,user_project_count,max_project_count))
exit(0)
else:
# print the content of the response (which should have the error message)
print("Request failed", json.dumps(
json.loads(resp.content),
indent=4
))
print("Headers: {}".format(headers))
print("Payload: {}".format(payload))
exit(1)
# endregion