Friday, May 1, 2020

vRealize Automation 8.1 and Python - Extract vRA environment information

In an earlier post, I showed the process to acquire a token.  For this post, I wanted to show some more basics around extracting environmental data from your vRA server.  Just like the other post, I also want to show how to use Python within the vRA API.  If you have any questions on the config.ini file used at the start of the Authenticate function, see the previous post.

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
At the bottom of this post, you will see the results of the script listed here:

#!/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/api/login?access_token"
    apiJSON = {"username": usrName,"password": usrPass,"tenant":usrTenant}
    apiHeaders = {"Content-Type":"application/json","Accept":"application/json"}
    apiToken = requests.post(vraURL,data=json.dumps(apiJSON),headers=apiHeaders,verify=False)
    refreshToken=apiToken.json()['refresh_token']

    vraURL = baseURL + "/iaas/api/login"
    accessJSON = {"refreshToken":apiToken.json()['refresh_token']}
    accessHeaders = {"Content-Type":"application/json","Accept":"application/json"}
    accessToken = requests.post(vraURL,data=json.dumps(accessJSON),headers=accessHeaders,verify=False)
    return(accessToken.json()['token'])

def GetOrgId(token):
    vraURL = baseURL + "/csp/gateway/am/api/loggedin/user/orgs"
    authToken = "Bearer " + token
    Headers = {"accept": "application/json", "authorization":authToken}
    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"
    authToken = "Bearer " + token
    Headers = {"accept": "application/json", "authorization":authToken}
    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"
    authToken = "Bearer " + token
    Headers = {"accept": "application/json", "authorization":authToken}
    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
    authHeaders = {"accept": "application/json", "authorization":authToken}
    apiReq = requests.get(vraURL,headers=authHeaders,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'])
    
# 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)

if __name__ == "__main__":
    main()


Running the script in my lab gives me the following:

Your assigned Orgs:
 -VM-IDM
 -VM-IDM
 -48191d87-5e9e-404a-9f1f-f19f91f32bfe
Your assigned Roles:
 -org_owner
 -Organization Owner
Your assigned Service Roles:
 -CodeStream:developer
 -CodeStream:executor
 -CodeStream:user
 -CodeStream:administrator
 -orchestration:admin
 -orchestration:designer
 -automationservice:cloud_admin
 -automationservice:user
 -automationservice:viewer
 -migration:admin
 -migration:viewer
 -catalog:user
 -catalog:admin
 -catalog:viewer
Your projects:
 -Project Name: TheWhitesHouse
 -Project Id: 5b9b2152-e8e4-470a-ba6b-d99f78098dfd
 -Org Id: 48191d87-5e9e-404a-9f1f-f19f91f32bfe
Blueprints:
 -TheWhitesHouse: deb-10-mini-1: 892bde2f-160b-4dfe-84d0-09649c26d245
 -TheWhitesHouse: Flask-Jinja2: 8d9beb7c-4266-4d3b-a8e4-db9f51be5881
 -TheWhitesHouse: testbp: 8127a4a5-395e-48a0-a214-6519e4a8ab56
 -TheWhitesHouse: Linux BP: c9ecfe03-7ea2-4c3f-9bfe-583e2211cde8
 -TheWhitesHouse: Hostname from Template: 1818e275-f9d0-42ab-9a50-2967013ae794
 -TheWhitesHouse: Hostname from Input: 59c30aca-2287-46f7-b987-5cccd96a3f17
Catalog Source Types:
 -AWS CloudFormation Template: com.aws.cft
 -Cloud Assembly Blueprint: com.vmw.blueprint
 -Extensibility actions: com.vmw.abx.actions
 -Marketplace VM Templates - OVA: com.vmw.mp.ova
 -Code Stream Pipeline: com.vmw.codestream
 -vRealize Orchestrator Workflow: com.vmw.vro.workflow
Catalog Items and Id:
 -Quickstart Content Source-1: 9de1fd08-bbee-4a3f-aa3e-ad9a5c30bf16

I hope you have found this useful.  Thanks for looking.

UPDATE - May 2022

I was looking at this blog after upgrading to vRA8.7.  Looks like some changes have occurred with the API calls since I wrote the scripts.  You'll see some differences between the scripts in the token process.  When I ran the older script in vRA8.7, most still works but I was getting an error when I ran the GetOrgId process.

#!/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