In the functions, you'll see the use of apiVersion. The Api version is not needed and can be omitted if you prefer. The parameter is used to lock and API to a specific version. Without the API version, your code will use the latest version. The function for "ListAllProjects" in my code below is called without the apiVersion as an example. If you wish to modify other functions, you would remove the parameter as follows:
vraURL = baseURL + "/blueprint/api/blueprint-requests?apiVersion="+apiVersion
becomes:
vraURL = baseURL + "/blueprint/api/blueprint-requests"
For this write-up, I am performing the following functions:
- authToken=Authenticate()
- Acquire an access token
- OrgId = GetOrgId(authToken)
- Display and return Organization Ids
- GetOrgRole(authToken,OrgId)
- Display your Roles based on the authenticated account
- GetServiceRole(authToken,OrgId)
- Display Service Roles based on the authenticated account
- ListAllProjects(authToken)
- Display Projects
- ListAllBluePrints(authToken, apiVersion)
- List all of the blueprints
- ListAllCatalogSourceTypes(authToken, apiVersion)
- List available Catalog types available to you
- ListCatalogItems(authToken, apiVersion)
- List items published to the catalog
Running the script in my lab gives me the following:
I hope you have found this useful. Thanks for looking.
#!/usr/bin/python3
# vRA8.1 API Calls
import sys, os, time, configparser, json, requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
def Authenticate():
# Read Config parameters
config = configparser.ConfigParser()
config.read('config.ini')
#Your vROps environment parameters
usrName = config['vra user']['usrName']
usrPass = config['vra user']['usrPass']
usrTenant = config['vra user']['usrTenant']
srvName = config['vra server']['srvName']
global baseURL
baseURL = "https://" + srvName
vraURL = baseURL + "/csp/gateway/am/idp/auth/login"
apiJSON = {
"username": usrName,
"password": usrPass,
"domain":usrTenant,
"scope":""
}
apiHeaders = {"Content-Type":"application/json","Accept":"application/json"}
apiToken = requests.post(vraURL,data=json.dumps(apiJSON),headers=apiHeaders,verify=False)
refreshToken=apiToken.json()['cspAuthToken']
return(refreshToken)
def GetOrgId(token):
vraURL = baseURL + "/csp/gateway/am/api/loggedin/user/orgs?expand"
Headers = {"accept": "application/json", "authorization":token}
apiReq = requests.get(vraURL,headers=Headers,verify=False)
if apiReq.status_code == 200:
print("Your assigned Orgs:")
for x in range(len(apiReq.json()['items'])):
print(" -"+apiReq.json()['items'][x]['name'])
print(" -"+apiReq.json()['items'][x]['displayName'])
print(" -"+apiReq.json()['items'][x]['id'])
return(apiReq.json()['items'][0]['id'])
def GetOrgRole(token,OrgId):
vraURL = baseURL + "/csp/gateway/am/api/loggedin/user/orgs/"+OrgId+"/roles"
Headers = {"accept": "application/json", "authorization":token}
apiReq = requests.get(vraURL,headers=Headers,verify=False)
if apiReq.status_code == 200:
print("Your assigned Roles:")
for x in range(len(apiReq.json())):
print(" -"+apiReq.json()[x]['name'])
print(" -"+apiReq.json()[x]['displayName'])
return(apiReq.json()[0]['name'])
def GetServiceRole(token,OrgId):
vraURL = baseURL + "/csp/gateway/am/api/loggedin/user/orgs/"+OrgId+"/service-roles?serviceDefinitionLink"
Headers = {"accept": "application/json", "authorization":token}
apiReq = requests.get(vraURL,headers=Headers,verify=False)
if apiReq.status_code == 200:
print("Your assigned Service Roles:")
for x in range(len(apiReq.json()['serviceRoles'])):
for y in range(len(apiReq.json()['serviceRoles'][x]['serviceRoleNames'])):
print(" -"+apiReq.json()['serviceRoles'][x]['serviceRoleNames'][y])
def ListAllProjects(token):
vraURL = baseURL + "/iaas/api/projects"
authToken = "Bearer " + token
Headers = {"accept": "application/json", "authorization":authToken}
apiReq = requests.get(vraURL,headers=Headers,verify=False)
if apiReq.status_code == 200:
print("Your projects:")
for x in range(apiReq.json()['totalElements']):
print(" -Project Name: "+apiReq.json()['content'][x]['name'])
print(" -Project Id: "+apiReq.json()['content'][x]['id'])
print(" -Org Id: "+apiReq.json()['content'][x]['orgId'])
def ListAllBluePrints(token, apiVersion):
vraURL = baseURL + "/blueprint/api/blueprints"
authToken = "Bearer " + token
authHeaders = {"accept": "application/json", "authorization":authToken}
apiReq = requests.get(vraURL,headers=authHeaders,verify=False)
print("Blueprints:")
if apiReq.status_code == 200:
for x in range(apiReq.json()['numberOfElements']):
proj=" -"+apiReq.json()['content'][x]['projectName']
proj+=": "+apiReq.json()['content'][x]['name']
print(proj+": "+apiReq.json()['content'][x]['id'])
def DeployBlueprint(token, apiVersion, BlueprintId):
vraURL = baseURL + "/blueprint/api/blueprint-requests?apiVersion="+apiVersion
authToken = "Bearer " + token
authHeaders = {"content-type": "application/json", "authorization":authToken}
reqData = {
"simulate": True,
"blueprintId": BlueprintId,
}
apiReq = requests.post(vraURL,headers=authHeaders,data=json.dumps(reqData), verify=False)
if apiReq.status_code == 200:
print (apiReq.json())
return (apiReq.json()['id'])
def GetDeploymentStatus(token, apiVersion, reqId):
vraURL = baseURL + "/blueprint/api/blueprint-requests/"+reqId+"?apiVersion="+apiVersion
authToken = "Bearer " + token
authHeaders = {"accept": "application/json", "authorization":authToken}
while True:
apiReq = requests.get(vraURL,headers=authHeaders,verify=False)
if apiReq.status_code == 200:
print (apiReq.json())
time.sleep(15)
def ListAllCatalogSourceTypes(token, apiVersion):
vraURL = baseURL + "/catalog/api/types?apiVersion="+apiVersion
authToken = "Bearer " + token
authHeaders = {"accept": "application/json", "authorization":authToken}
apiReq = requests.get(vraURL,headers=authHeaders,verify=False)
print("Catalog Source Types:")
if apiReq.status_code == 200:
for x in range(apiReq.json()['numberOfElements']):
name=" -"+apiReq.json()['content'][x]['name']
print(name+": "+apiReq.json()['content'][x]['id'])
def ListCatalogItems(token, apiVersion):
vraURL = baseURL + "/catalog/api/admin/sources?apiVersion="+apiVersion
authToken = "Bearer " + token
authHeaders = {"accept": "application/json", "authorization":authToken}
apiReq = requests.get(vraURL,headers=authHeaders,verify=False)
print("Catalog Items and Id:")
if apiReq.status_code == 200:
for x in range(apiReq.json()['numberOfElements']):
name=" -"+apiReq.json()['content'][x]['name']
print(name+": "+apiReq.json()['content'][x]['id'])
def LogOut(token):
vraURL = baseURL + "/csp/gateway/am/api/auth/logout"
LogOutData = {"idToken":token}
authHeaders = {"accept": "*/*", "Content-Type": "application/json"}
apiReq = requests.post(vraURL,headers=authHeaders,data=json.dumps(LogOutData),verify=False)
# Script starts here
def main():
apiVersion="2019-09-12"
blueprintId = "892bde2f-160b-4dfe-84d0-09649c26d245"
authToken=Authenticate()
OrgId = GetOrgId(authToken)
GetOrgRole(authToken,OrgId)
GetServiceRole(authToken,OrgId)
ListAllProjects(authToken)
ListAllBluePrints(authToken, apiVersion)
ListAllCatalogSourceTypes(authToken, apiVersion)
ListCatalogItems(authToken, apiVersion)
print()
# reqId = DeployBlueprint(authToken, apiVersion, blueprintId)
# GetDeploymentStatus(authToken, apiVersion, reqId)
LogOut(authToken)
if __name__ == "__main__":
main()
No comments:
Post a Comment