vmware / vsphere-automation-sdk-python

Python samples, language bindings, and API reference documentation for vSphere, VMC, and NSX-T using the VMware REST API
MIT License
746 stars 313 forks source link

CIS api tag calls added to the vapi.vcenter.tagging api calls #361

Closed alan-cugler closed 1 year ago

alan-cugler commented 1 year ago

Is your feature request related to a problem? Please describe.

A vmware client is asking for their vCenter Tags associated to VMs to be imported to the grains system of Aria Automation Config (open-salt). This is a good fit for integration between the two pieces of technology. I attempted to achieve this by installing the vsphere-automation-sdk in the open-salt python environment. This did work and I was able to authenticate a session to vCenter!

However, I found I was able to extract the information of VM names and VM IDs, and I was able to get Tag URNs assigned to VM IDs. But there was no way to associate the Tag URNs to the human readable Tag names.

image

Demonstrated rest api solution

echo -n "{username}:{password}" | base64
curl --insecure -X POST -H "Authorization: Basic {insert encoding}" https://{api_host}/api/session
curl --insecure -H "vmware-api-session-id: {session_token}" https://{api_host}/api/cis/tagging/tag
curl --insecure -H "vmware-api-session-id: {session_token}" https://{api_host}/api/cis/tagging/tag/{tag_id}

Describe the solution you'd like

I noticed in the REST api documentation there is a CIS api with tagging calls that have what is needed. As far as I could tell the python SDK doesnt have this wrapped. Can we add this please?

https://developer.vmware.com/apis/vsphere-automation/latest/cis/api/cis/tagging/tag/tag_id/get/

Describe alternatives you've considered

https://github.com/vmware/vsphere-automation-sdk-python/issues/360

Additional context

kunal-pmj commented 1 year ago

Kindly refer to the sample on how to get the tag from tag_id https://github.com/vmware/vsphere-automation-sdk-python/blob/master/samples/vsphere/tagging/tagging_workflow.py#L173 From tag association get the tag_id and pass it to the client.tagging.Tag.get(tag_id)

Here is a snippet which lists associations and tag names for each association

    tag_model_ass = self.client.vcenter.tagging.Associations.list()
    for association in tag_model_ass.associations:
        tag_id = association.tag # tag_id
        tag_obj = self.client.tagging.Tag.get(tag_id) # tag by tag_id
        print(tag_obj.name)

Thanks Kunal