- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvRA_Python_REST_script.py
More file actions
Latest commit
67 lines (62 loc) · 2.95 KB
/
Copy pathvRA_Python_REST_script.py
File metadata and controls
67 lines (62 loc) · 2.95 KB
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
importrequests
importjson
fromprettytableimportPrettyTable
fromrequests.packages.urllib3.exceptionsimportInsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) # Disable SSL warning
defvra_auth(vrafqdn, user, password, tenant):
"""
Builds an authentication token for the user. Takes input of the fqdn of vRA, username,
password, and tenant
"""
url="https://{}/identity/api/tokens".format(vrafqdn)
payload='{{"username":"{}","password":"{}","tenant":"{}"}}'.format(user, password, tenant)
headers= {
'accept': "application/json",
'content-type': "application/json",
}
response=requests.request("POST", url, data=payload, headers=headers, verify=False)
j=response.json()['id']
auth="Bearer "+j
returnauth
defreturn_vra_vms_asTable(vrafqdn, user, password, tenant):
"""
Builds a table using PrettyTable to return vRA provisioned VM Details
"""
auth=vra_auth(vrafqdn, user, password, tenant)
vraheaders= {
'accept': "application/json",
'authorization': auth
}
vraApiUrl="https://{}/catalog-service/api/consumer/resources".format(vrafqdn)
req=requests.request("GET", vraApiUrl, headers=vraheaders, verify=False).json()['content']
vratable=PrettyTable(['Name', 'ID', 'OS'])
foriinreq:
ifi['resourceTypeRef']['id'] =='Infrastructure.Virtual':
resid=i['id']
vraResUrl="https://{}/catalog-service/api/consumer/resources/{}".format(vrafqdn, resid)
resreq=requests.request("GET", vraResUrl, headers=vraheaders, verify=False).json()
vratable.add_row((i['name'], i['id'], resreq['resourceData']['entries'][0]['value']['value']))
returnprint(vratable)
defvra_build(vrafqdn, user, password, tenant, blueprint):
"""
Provisions a catalog item with the default template. Takes inputs same as above,
also requires blueprint name in string format.
"""
varray= {}
auth=vra_auth(vrafqdn, user, password, tenant)
vraApiUrl="https://{}/catalog-service/api/consumer/entitledCatalogItemViews".format(vrafqdn)
vraheaders= {
'accept': "application/json",
'authorization': auth
}
tempres=requests.request("GET", vraApiUrl, headers=vraheaders, verify=False)
foriintempres.json()['content']:
p=i['name']
q=i['catalogItemId']
varray[p] =q
template="https://{}/catalog-service/api/consumer/entitledCatalogItems/{}/requests/template".format(vrafqdn, varray[blueprint])
req="https://{}/catalog-service/api/consumer/entitledCatalogItems/{}/requests".format(vrafqdn, varray[blueprint])
templateJson=requests.request("GET", template, headers=vraheaders, verify=False)
vraCatDeploy=requests.request("Post", req, headers=vraheaders, data=templateJson, verify=False)
buildStatus="a "+blueprint+" Server build has been requested"
returnbuildStatus