VMware {code}

 View Only
  • 1.  list attached objcts by tagging ID, Python Requests

    Posted Sep 26, 2021 10:07 AM

    Hello All,

    I am having issues querying list objects attached to a tag ids using python requests POST. I am always getting 404 error. I am very new to vmware and not even sure where exactly to see the error on vmware server.

    I can see the output from vsphere client development center, But when I try to query it I am getting the error 404.

    I can query the API for session-d and can query the tag categories and tag IDs, But to be able to see what objects are tagged to the TAG IDs , I am interested, it is not allowing me to.

    ~~~

    def request_post1(vcip, cmd, s, tags):
    payload = {"tag_ids":["urn:vmomi:InventoryServiceTag:b4055c62-a0ed-44d9-babb-3e05a5416bcd:GLOBAL"]}
    payload = json.dumps(payload)
    session = requests.Session()
    session.verify = False
    session1 = session.post('https://'+vcip+'/rest/com/vmware/cis/tagging/tag-association?action=list-attached-objects-on-tags',headers={'vmware-api-session-id':s, 'content-type': 'application/json'}, verify=False, data=payload)
    return session1.json()

    ~~~

    can anyone kindly help.

     

    Also, Is there a filter we can apply to query a cluster with more than 1000 vms. is there a filter we can apply to the query to limit number of vms queries, 

     

    Any help is really appreciated.



  • 2.  Re: list attached objcts by tagging ID, Python Requests

    Posted Oct 20, 2021 07:13 AM

    Missed "~" in the endpoint, ie, 

    POST - /rest/com/vmware/cis/tagging/tag-association?~action=list-attached-objects-on-tags
    POST (vSphere7.0U2) - /api/cis/tagging/tag-association?action=list-attached-objects-on-tags 

    Use the below sample to list the attached objects on the tag.

    import json
    import requests
    from requests.packages.urllib3.exceptions import InsecureRequestWarning

    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

    session = requests.Session()
    session.verify = False


    def get_vc_session(vc_ip, username, password):
    session.post('https://' + vc_ip + '/rest/com/vmware/cis/session', auth=(username, password))
    return session


    def listAttachedObjectsOnTags(vc_ip, tag_id):
    payload = {"tag_ids": [tag_id]}
    payload = json.dumps(payload)
    headers = {'Content-type': 'application/json'}
    session1 = session.post(
    'https://' + vc_ip + '/rest/com/vmware/cis/tagging/tag-association?~action=list-attached-objects-on-tags',
    data=payload, headers=headers)
    return session1.content


    if __name__ == '__main__':
    vc_ip = "10.xx.xx.xx"
    tag_id = "urn:vmomi:InventoryServiceTag:b4055c62-a0ed-44d9-babb-3e05a5416bcd:GLOBAL"
    get_vc_session(vc_ip, "<Username>", "<Password>")
    print(listAttachedObjectsOnTags(vc_ip, tag_id))