def createReservation(self, payload): """ Creating a reservation by type http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-11510887-0F55-4EA4-858C-9881F94C718B.html Parameters: payload = JSON payload containing the reservation information """ host = self.host token = self.token url = 'https://{host}/reservation-service/api/reservations'.format( host=host) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.post(url=url, headers=headers, data=json.dumps(payload), verify=False) checkResponse(r) reservationId = r.headers['location'].split('/')[6] return reservationId
def getResourceByName(self, name, show='json'): """ Function that will get a vRA resource by id. Parameters: show = return data as a table or json object name = name of the vRA resource. """ host = self.host token = self.token url = "https://{host}/catalog-service/api/consumer/resources?$filter=name%20eq%20'{name}'".format( host=host, name=name) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resource = r.json() if show == 'table': table = PrettyTable(['Id', 'Name', 'Status', 'Catalog Item']) table.add_row([ resource['content'][0]['id'], resource['content'][0]['name'], resource['content'][0]['status'], resource['content'][0]['catalogItem']['label'] ]) print table elif show == 'json': return resource['content'][0]
def getReservationByName(self, name, show='table'): """ Get a reservation by name Parameters: name = name of a new or existing reservation show = return data as a table or json object """ host = self.host token = self.token url = "https://{host}/reservation-service/api/reservations?$filter=name%20eq%20'{name}'".format( host=host, name=name) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) reservation = r.json() if show == 'table': table = PrettyTable(['Id', 'Name']) table.add_row([ reservation['content'][0]['id'], reservation['content'][0]['name'] ]) print table elif show == 'json': return reservation['content'][0]
def getComputeResourceForReservation(self, schemaclassid): """ Get a compute resource for the reservation http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-AF6F177D-13C2-47AD-842D-1D341591D5F4.html Parameters: schemaclassid = schemaClassId of supported reservation Type. E.g Infrastructure.Reservation.Virtual.vSphere """ host = self.host token = self.token url = 'https://{host}/reservation-service/api/data-service/schema/{schemaclassid}/default/computeResource/values'.format( host=host, schemaclassid=schemaclassid) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } payload = {} r = requests.post(url=url, headers=headers, data=json.dumps(payload), verify=False) checkResponse(r) computeResource = r.json() return computeResource
def getBusinessGroupId(self, tenant=None, buisenessGroupName=None): """ Get the business group ID for a reservation http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-588865AE-0134-4087-B090-C725790C052C.html Parameters: tenant = vRA tenant. if null then it will default to vsphere.local businessGroupName = For future use. Need to be able to fetch ID from business group name """ host = self.host token = self.token if tenant is None: tenant = "vsphere.local" url = 'https://{host}/identity/api/tenants/{tenant}/subtenants'.format( host=host, tenant=tenant) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) businessGroupId = r.json() return businessGroupId
def getBusinessGroupId(self, tenant=None, buisenessGroupName=None): """ Get the business group ID for a reservation http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-588865AE-0134-4087-B090-C725790C052C.html Parameters: tenant = vRA tenant. if null then it will default to vsphere.local businessGroupName = For future use. Need to be able to fetch ID from business group name """ host = self.host token = self.token if tenant is None: tenant = "vsphere.local" url = 'https://{host}/identity/api/tenants/{tenant}/subtenants'.format(host=host, tenant=tenant) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) businessGroupId = r.json() return businessGroupId
def getEntitledCatalogItems(self, show='table', limit=20): """ Function that will return all entitled catalog items for the current user. Parameters: show = return data as a table or json object limit = The number of entries per page. """ host = self.host token = self.token url = 'https://{host}/catalog-service/api/consumer/entitledCatalogItems?limit={limit}&$orderby=name%20asc'.format( host=host, limit=limit) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) items = r.json() if show == 'table': table = PrettyTable(['Id', 'Name']) for i in items['content']: table.add_row([i['catalogItem']['id'], i['catalogItem']['name']]) print table elif show == 'json': return items['content']
def getReservationByName(self, name, show='table'): """ Get a reservation by name Parameters: name = name of a new or existing reservation show = return data as a table or json object """ host = self.host token = self.token url = "https://{host}/reservation-service/api/reservations?$filter=name%20eq%20'{name}'".format(host=host, name=name) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) reservation = r.json() if show == 'table': table = PrettyTable(['Id', 'Name']) table.add_row([ reservation['content'][0]['id'], reservation['content'][0]['name']]) print table elif show == 'json': return reservation['content'][0]
def getAllResources(self, show='table', limit=20): """ Function that will return all resources that are available to the current user. Parameters: show = return data as a table or json object limit = The number of entries per page. Api call to /api/consumer/resources returns only basic data about Virtual Machines. """ host = self.host headers = self.headers url = 'https://{host}/catalog-service/api/consumer/resources?limit={limit}&$orderby=name%20asc'.format( host=host, limit=limit) r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resources = r.json() if not resources['content']: print 'You do not currently have any provisioned items to display.' elif show == 'table': table = PrettyTable(['Id', 'Name', 'Description', 'Lease end', 'Catalog Item']) for i in resources['content']: table.add_row([i['id'], i['name'], i['description'], i['lease']['end'], i['catalogItem']['label']]) print table elif show == 'json': return resources['content']
def getAllResourcesDetailed(self, show='table', limit=20): """ Function that will return all resources that are available to the current user. Parameters: show = return data as a table or json object limit = The number of entries per page. Api call to api/consumer/resources/types/Infrastructure.Machine returns detailed data about Virtual Machines. """ host = self.host headers = self.headers url = 'https://{host}/catalog-service/api/consumer/resources/types/Infrastructure.Machine?limit={limit}&$orderby=name%20asc'.format( host=host, limit=limit) r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resources = r.json() if show == 'table': table = PrettyTable(['Id', 'Name', 'Description', 'Status', 'Lease end', 'Catalog Item']) vm_status = "unknown" for i in resources['content']: for entry in i['resourceData']['entries']: if entry['key'] == 'MachineStatus': vm_status = entry['value']['value'] table.add_row([i['id'], i['name'], i['description'], vm_status, i['lease']['end'], i['catalogItem']['label']]) print table elif show == 'json': return resources['content']
def requestResource(self, payload): """ Function that will submit a request based on payload. payload = json body (example in request.json) Parameters: payload = JSON request body. """ host = self.host token = self.token url = 'https://{host}/catalog-service/api/consumer/requests'.format( host=host) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.post(url=url, data=json.dumps(payload), headers=headers, verify=False) checkResponse(r) id = r.headers['location'].split('/')[7] return id
def getAllReservations(self, show='table', limit=20): """ Get all reservations Parameters: show = Output either table format or raw json limit = The number of entries per page. """ host = self.host token = self.token url = 'https://{host}/reservation-service/api/reservations?limit={limit}'.format( host=host, limit=limit) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) reservations = r.json() if show == 'table': table = PrettyTable(['Id', 'Name']) for i in reservations['content']: table.add_row([i['id'], i['name']]) print table elif show == 'json': return reservations['content']
def getResourceByBusinessGroup(self, name, limit=100, show='json'): """ Function that will get all vRA resources running for a specific Business group Parameters: show = return data as a table or json object name = name of the vRA resource. """ host = self.host token = self.token url = "https://{host}/catalog-service/api/consumer/resources?$filter=organization/subTenant/name%20eq%20'{name}'&limit={limit}".format(host=host, name=name, limit=limit) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resource = r.json() if show == 'table': table = PrettyTable(['Id', 'Name', 'Description','Label' 'Status']) for item in resource['content']: table.add_row([ item['id'], item['name'], item['description'], item['resourceTypeRef']['label'], item['status'], ]) print table elif show == 'json': return resource
def requestResource(self, payload): """ Function that will submit a request based on payload. payload = json body (example in request.json) Parameters: payload = JSON request body. """ host = self.host token = self.token url = 'https://{host}/catalog-service/api/consumer/requests'.format(host=host) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.post(url=url, data=json.dumps(payload), headers=headers, verify=False) checkResponse(r) id = r.headers['location'].split('/')[7] return id
def getComputeResourceForReservation(self, schemaclassid): """ Get a compute resource for the reservation http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-AF6F177D-13C2-47AD-842D-1D341591D5F4.html Parameters: schemaclassid = schemaClassId of supported reservation Type. E.g Infrastructure.Reservation.Virtual.vSphere """ host = self.host token = self.token url = 'https://{host}/reservation-service/api/data-service/schema/{schemaclassid}/default/computeResource/values'.format(host=host, schemaclassid=schemaclassid) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } payload = {} r = requests.post(url=url, headers=headers, data=json.dumps(payload), verify=False) checkResponse(r) computeResource = r.json() return computeResource
def getAllRequests(self, show='table', limit=20): """ Function that will return the resource that were provisioned as a result of a given request. Parameters: show = return data as a table or json object limit = The number of entries per page. """ host = self.host headers = self.headers url = 'https://{host}/catalog-service/api/consumer/requests?limit={limit}&$orderby=requestNumber%20desc'.format( host=host, limit=limit) r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) items = r.json() if show == 'table': table = PrettyTable(['Id', 'Request Number', 'Item', 'State']) for i in items['content']: table.add_row([ i['id'], i['requestNumber'], i['requestedItemName'], i['state'] ]) print table elif show == 'json': return items['content']
def getEntitledCatalogItemViews(self, show='table', limit=20): """ Function that will return all entitled catalog items for the current user. Parameters: show = return data as a table or json object limit = The number of entries per page. """ host = self.host token = self.token url = 'https://{host}/catalog-service/api/consumer/entitledCatalogItemViews?limit={limit}&$orderby=name%20asc'.format( host=host, limit=limit) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) items = r.json() if show == 'table': table = PrettyTable(['Id', 'Name']) for i in items['content']: table.add_row([i['catalogItemId'], i['name']]) print table elif show == 'json': return items['content']
def getReservation(self, reservationid, show='table'): """ Verify a reservation and get reservation details http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-2A2D96DE-9BBE-414B-82AB-DD70B82D3E0C.html Parameters: reservationid = Id of a new or existing reservation show = return data as a table or json object """ host = self.host token = self.token url = 'https://{host}/reservation-service/api/reservations/{reservationid}'.format(host=host, reservationid=reservationid) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) reservation = r.json() if show == 'table': table = PrettyTable(['Id', 'Name']) table.add_row([ reservation['id'], reservation['name']]) print table elif show == 'json': return reservation
def getAllRequests(self, show='table', limit=20): """ Function that will return the resource that were provisioned as a result of a given request. Parameters: show = return data as a table or json object limit = The number of entries per page. """ host = self.host token = self.token url = 'https://{host}/catalog-service/api/consumer/requests?limit={limit}&$orderby=requestNumber%20desc'.format(host=host, limit=limit) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) items = r.json() if show == 'table': table = PrettyTable(['Id', 'Request Number', 'Item', 'State']) for i in items['content']: table.add_row([i['id'], i['requestNumber'], i['requestedItemName'], i['state']]) print table elif show == 'json': return items['content']
def getRequest(self, id, show='table'): """ Function that will return request information for a given request. Parameters: id = the id of the vRA request. show = return data as a table or json object """ host = self.host token = self.token url = 'https://{host}/catalog-service/api/consumer/requests/{id}'.format( host=host, id=id) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) request = r.json() if show == 'table': table = PrettyTable(['Id', 'Request Number', 'Item', 'State']) table.add_row([ request['id'], request['requestNumber'], request['requestedItemName'], request['state'] ]) print table elif show == 'json': return request
def getReservation(self, reservationid, show='table'): """ Verify a reservation and get reservation details http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-2A2D96DE-9BBE-414B-82AB-DD70B82D3E0C.html Parameters: reservationid = Id of a new or existing reservation show = return data as a table or json object """ host = self.host token = self.token url = 'https://{host}/reservation-service/api/reservations/{reservationid}'.format( host=host, reservationid=reservationid) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) reservation = r.json() if show == 'table': table = PrettyTable(['Id', 'Name']) table.add_row([reservation['id'], reservation['name']]) print table elif show == 'json': return reservation
def getResourceByName(self, name, show='json'): """ Function that will get a vRA resource by id. Parameters: show = return data as a table or json object name = name of the vRA resource. """ host = self.host token = self.token url = "https://{host}/catalog-service/api/consumer/resources?$filter=name%20eq%20'{name}'".format(host=host, name=name) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resource = r.json() if show == 'table': table = PrettyTable(['Id', 'Name', 'Status', 'Catalog Item']) table.add_row([ resource['content'][0]['id'], resource['content'][0]['name'], resource['content'][0]['status'], resource['content'][0]['catalogItem']['label'] ]) print table elif show == 'json': return resource['content'][0]
def getRequest(self, id, show='table'): """ Function that will return request information for a given request. Parameters: id = the id of the vRA request. show = return data as a table or json object """ host = self.host token = self.token url = 'https://{host}/catalog-service/api/consumer/requests/{id}'.format(host=host, id=id) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) request = r.json() if show == 'table': table = PrettyTable(['Id', 'Request Number', 'Item', 'State']) table.add_row([request['id'], request['requestNumber'], request['requestedItemName'], request['state']]) print table elif show == 'json': return request
def getResource(self, id, show='json'): """ Function that will get a vRA resource by id. Parameters: show = return data as a table or json object id = id of the vRA resource. """ host = self.host headers = self.headers url = 'https://{host}/catalog-service/api/consumer/resources/{id}'.format(host=host, id=id) r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resource = r.json() if show == 'table': table = PrettyTable(['Id', 'Name', 'Description', 'Lease end', 'Catalog Item']) table.add_row([ resource['id'], resource['name'], resource['description'], resource['lease']['end'], resource['catalogItem']['label'] ]) print table elif show == 'json': return resource
def createReservation(self, payload): """ Creating a reservation by type http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-11510887-0F55-4EA4-858C-9881F94C718B.html Parameters: payload = JSON payload containing the reservation information """ host = self.host token = self.token url = 'https://{host}/reservation-service/api/reservations'.format(host=host) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.post(url=url, headers=headers, data=json.dumps(payload), verify=False) checkResponse(r) reservationId = r.headers['location'].split('/')[6] return reservationId
def getResource(self, id, show='json'): """ Function that will get a vRA resource by id. Parameters: show = return data as a table or json object id = id of the vRA resource. """ host = self.host headers = self.headers url = 'https://{host}/catalog-service/api/consumer/resources/{id}'.format( host=host, id=id) r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resource = r.json() if show == 'table': table = PrettyTable( ['Id', 'Name', 'Description', 'Lease end', 'Catalog Item']) table.add_row([ resource['id'], resource['name'], resource['description'], resource['lease']['end'], resource['catalogItem']['label'] ]) print table elif show == 'json': return resource
def getCatalogItemFormDetails(self, catalogItem): host = self.host token = self.token url = 'https://{host}/catalog-service/api/consumer/catalogItems/{id}/forms/details'.format(host=host, id=catalogItem["id"]) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) form = r.json() return form
def getResourceSchemaForReservation(self, schemaclassid, fieldid, computeresourceid): """ Getting a resource schema by reservation type #http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-C19E4316-603F-4497-86DB-C241ECE4EEB4.html vSphere reservation syntax: http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-CAB141B4-E25F-42EC-B2C4-8516366CB43F.html Parameters: schemaclassid = schemaClassId of supported reservation Type. E.g Infrastructure.Reservation.Virtual.vSphere fieldid = Extension field supported in the reservation... E.g resourcePool computeresourceId = Id of the compute resource to query """ host = self.host token = self.token url = 'https://{host}/reservation-service/api/data-service/schema/{schemaclassid}/default/{fieldid}/values'.format( host=host, schemaclassid=schemaclassid, fieldid=fieldid) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } payload = { "text": "", "dependencyValues": { "entries": [{ "key": "computeResource", "value": { "type": "entityRef", "componentId": 'null', "classId": "ComputeResource", "id": "{computeResourceId}".format( computeResourceId=computeresourceid) } }] } } r = requests.post(url=url, headers=headers, data=json.dumps(payload), verify=False) checkResponse(r) resourceSchema = r.json() return resourceSchema
def getCatalogItemFormDetails(self, catalogItem): host = self.host token = self.token url = 'https://{host}/catalog-service/api/consumer/catalogItems/{id}/forms/details'.format( host=host, id=catalogItem["id"]) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) form = r.json() return form
def getResourceActions(self, id, raw=False): host = self.host token = self.token url = "https://{host}/catalog-service/api/consumer/resources/{id}/actions".format(host=host, id=id) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) actions = r.json() if raw: return actions actionsContent = actions["content"] return {action["name"]: action for action in actionsContent}
def getResourceIdByRequestId(self, id): """ Function that will search for a resource with a matching requestId. Parameters: id = request id of the vRA resource. """ host = self.host headers = self.headers url = "https://{host}/catalog-service/api/consumer/resources?$filter=request%20eq%20'{id}'".format(host=host, id=id) r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resource = r.json() resource_id = resource['content'][0]['id'] return resource_id
def getResourceActions(self, id, raw=False): host = self.host token = self.token url = "https://{host}/catalog-service/api/consumer/resources/{id}/actions".format( host=host, id=id) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) actions = r.json() if raw: return actions actionsContent = actions["content"] return {action["name"]: action for action in actionsContent}
def getRequestResource(self, id): """ Function that will return the resource that were provisioned as a result of a given request. Parameters: id = the id of the vRA request. """ host = self.host headers = self.headers url = 'https://{host}/catalog-service/api/consumer/requests/{id}/resources'.format(host=host, id=id) r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resource = r.json() return resource['content']
def getResourceIdByRequestId(self, id): """ Function that will search for a resource with a matching requestId. Parameters: id = request id of the vRA resource. """ host = self.host headers = self.headers url = "https://{host}/catalog-service/api/consumer/resources?$filter=request%20eq%20'{id}'".format( host=host, id=id) r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resource = r.json() resource_id = resource['content'][0]['id'] return resource_id
def getRequestResource(self, id): """ Function that will return the resource that were provisioned as a result of a given request. Parameters: id = the id of the vRA request. """ host = self.host headers = self.headers url = 'https://{host}/catalog-service/api/consumer/requests/{id}/resources'.format( host=host, id=id) r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resource = r.json() return resource['content']
def getResourceSchemaForReservation(self, schemaclassid, fieldid, computeresourceid): """ Getting a resource schema by reservation type #http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-C19E4316-603F-4497-86DB-C241ECE4EEB4.html vSphere reservation syntax: http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-CAB141B4-E25F-42EC-B2C4-8516366CB43F.html Parameters: schemaclassid = schemaClassId of supported reservation Type. E.g Infrastructure.Reservation.Virtual.vSphere fieldid = Extension field supported in the reservation... E.g resourcePool computeresourceId = Id of the compute resource to query """ host = self.host token = self.token url = 'https://{host}/reservation-service/api/data-service/schema/{schemaclassid}/default/{fieldid}/values'.format(host=host, schemaclassid=schemaclassid, fieldid=fieldid) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } payload = { "text": "", "dependencyValues": { "entries": [{ "key": "computeResource", "value": { "type": "entityRef", "componentId": 'null', "classId": "ComputeResource", "id": "{computeResourceId}".format( computeResourceId = computeresourceid) } }] } } r = requests.post(url=url, headers=headers, data=json.dumps(payload), verify=False) checkResponse(r) resourceSchema = r.json() return resourceSchema
def getReservationTypes(self): """ Display a list of supported reservation types: http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-57F7623F-6740-49EC-A572-0525D56862F1.html """ host = self.host token = self.token url = 'https://{host}/reservation-service/api/reservations/types'.format(host=host) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) reservationTypes = r.json() return reservationTypes[u'content']
def getEntitledCatalogItemsRequestsTemplate(self, id): """ Function that will return json object of request form for the catalog item blueprint Parameters: id = id of vRA Catalog Item """ host = self.host token = self.token url = 'https://{host}/catalog-service/api/consumer/entitledCatalogItems/{id}/requests/template'.format( host=host, id=id) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) return r.json()
def getResourceByBusinessGroup(self, name, limit=100, show='json'): """ Function that will get all vRA resources running for a specific Business group Parameters: show = return data as a table or json object name = name of the vRA resource. """ host = self.host token = self.token url = "https://{host}/catalog-service/api/consumer/resources?$filter=organization/subTenant/name%20eq%20'{name}'&limit={limit}".format( host=host, name=name, limit=limit) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resource = r.json() if show == 'table': table = PrettyTable( ['Id', 'Name', 'Description', 'Label' 'Status']) for item in resource['content']: table.add_row([ item['id'], item['name'], item['description'], item['resourceTypeRef']['label'], item['status'], ]) print table elif show == 'json': return resource
def getReservationTypes(self): """ Display a list of supported reservation types: http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-57F7623F-6740-49EC-A572-0525D56862F1.html """ host = self.host token = self.token url = 'https://{host}/reservation-service/api/reservations/types'.format( host=host) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) reservationTypes = r.json() return reservationTypes[u'content']
def getResourceIdByRequestId(self, id): """ Function that will search for a resource with a matching requestId. Parameters: id = request id of the vRA resource. """ host = self.host token = self.token url = "https://{host}/catalog-service/api/consumer/resources?$filter=request%20eq%20'{id}'".format(host=host, id=id) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resource = r.json() resourceId = resource['content'][0]['id'] return resourceId
def getRequestResource(self, id): """ Function that will return the resource that were provisioned as a result of a given request. Parameters: id = the id of the vRA request. """ host = self.host token = self.token url = 'https://{host}/catalog-service/api/consumer/requests/{id}/resources'.format(host=host, id=id) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resource = r.json() return resource['content']
def getReservationSchema(self, schemaclassid): """ Displaying a schema definition for a reservation http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-E957942A-1CCC-4C16-8147-0F5D382CDCB5.html Parameters: schemaclassid = schemaClassId of supported reservation Type. E.g Infrastructure.Reservation.Virtual.vSphere """ host = self.host token = self.token url = 'https://{host}/reservation-service/api/data-service/schema/{schemaclassid}/default'.format(host=host, schemaclassid=schemaclassid) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) reservationSchema = r.json() return reservationSchema[u'fields']
def requestResource(self, payload): """ Function that will submit a request based on payload. payload = json body (example in request.json) Parameters: payload = JSON request body. """ host = self.host headers = self.headers url = 'https://{host}/catalog-service/api/consumer/requests'.format(host=host) r = requests.post(url=url, data=json.dumps(payload), headers=headers, verify=False) checkResponse(r) request_id = r.headers['location'].split('/')[7] return request_id
def getAllResourcesDetailed(self, show='table', limit=20): """ Function that will return all resources that are available to the current user. Parameters: show = return data as a table or json object limit = The number of entries per page. Api call to api/consumer/resources/types/Infrastructure.Machine returns detailed data about Virtual Machines. """ host = self.host headers = self.headers url = 'https://{host}/catalog-service/api/consumer/resources/types/Infrastructure.Machine?limit={limit}&$orderby=name%20asc'.format( host=host, limit=limit) r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resources = r.json() if show == 'table': table = PrettyTable([ 'Id', 'Name', 'Description', 'Status', 'Lease end', 'Catalog Item' ]) vm_status = "unknown" for i in resources['content']: for entry in i['resourceData']['entries']: if entry['key'] == 'MachineStatus': vm_status = entry['value']['value'] table.add_row([ i['id'], i['name'], i['description'], vm_status, i['lease']['end'], i['catalogItem']['label'] ]) print table elif show == 'json': return resources['content']
def getReservationSchema(self, schemaclassid): """ Displaying a schema definition for a reservation http://pubs.vmware.com/vra-62/index.jsp#com.vmware.vra.programming.doc/GUID-E957942A-1CCC-4C16-8147-0F5D382CDCB5.html Parameters: schemaclassid = schemaClassId of supported reservation Type. E.g Infrastructure.Reservation.Virtual.vSphere """ host = self.host token = self.token url = 'https://{host}/reservation-service/api/data-service/schema/{schemaclassid}/default'.format( host=host, schemaclassid=schemaclassid) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) reservationSchema = r.json() return reservationSchema[u'fields']
def requestResource(self, payload): """ Function that will submit a request based on payload. payload = json body (example in request.json) Parameters: payload = JSON request body. """ host = self.host headers = self.headers url = 'https://{host}/catalog-service/api/consumer/requests'.format( host=host) r = requests.post(url=url, data=json.dumps(payload), headers=headers, verify=False) checkResponse(r) request_id = r.headers['location'].split('/')[7] return request_id
def getAllBusinessGroups(self, tenant=None, show='table', limit=20): """ Get All business groups List ID and name for each business group Parameters: tenant = vRA tenant. if null then it will default to vsphere.local show = return data as a table or json object limit = The number of entries per page. """ host = self.host token = self.token if tenant is None: tenant = "vsphere.local" url = 'https://{host}/identity/api/tenants/{tenant}/subtenants?limit={limit}&$orderby=name'.format( host=host, tenant=tenant, limit=limit) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': token } r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) businessGroups = r.json() if show == 'table': table = PrettyTable(['Id', 'Name']) for i in businessGroups['content']: table.add_row([i['id'], i['name']]) print table elif show == 'json': return businessGroups['content']
def getAllResources(self, show='table', limit=20): """ Function that will return all resources that are available to the current user. Parameters: show = return data as a table or json object limit = The number of entries per page. Api call to /api/consumer/resources returns only basic data about Virtual Machines. """ host = self.host headers = self.headers url = 'https://{host}/catalog-service/api/consumer/resources?limit={limit}&$orderby=name%20asc'.format( host=host, limit=limit) r = requests.get(url=url, headers=headers, verify=False) checkResponse(r) resources = r.json() if not resources['content']: print 'You do not currently have any provisioned items to display.' elif show == 'table': table = PrettyTable( ['Id', 'Name', 'Description', 'Lease end', 'Catalog Item']) for i in resources['content']: table.add_row([ i['id'], i['name'], i['description'], i['lease']['end'], i['catalogItem']['label'] ]) print table elif show == 'json': return resources['content']