vmware-archive / pyvcloud

Python SDK for VMware vCloud Director
https://vmware.github.io/pyvcloud
Other
170 stars 190 forks source link

403/ACCESS_TO_RESOURCE_IS_FORBIDDEN when inserting media #672

Open bogdan-ds opened 4 years ago

bogdan-ds commented 4 years ago

Hello,

I'm trying to insert a media from a public catalog into a VM, when using the method insert_cd I'm getting a 403 error. However when I try to insert the same CD from the same catalog via vCloud director it works without any issues. Also the eject method, which is more or less the same works fine. The full error is:

AccessForbiddenException: Status code: 403/ACCESS_TO_RESOURCE_IS_FORBIDDEN, [ ae496a05-e91d-4fc9-a189-9ae70428746c ] Either you need some or all of the following rights [TEMPLATE_MEDIA_VIEW] to perform operations [MEDIA_VIEW] for a8f929c3-0e8d-4ab1-b509-a773dd16b1e3 or the target entity is invalid. (request id: ae496a05-e91d-4fc9-a189-9ae70428746c)

P.S When using the pyvcloud client I'm using the administrative user and the system org, the same as in vCD. I also checked if the organisation has the required rights and it does.

Thanks in advance.

sumitkapoor commented 4 years ago

Hi,

Believe you would be passing the catalog ID instead of media ID when inserting the CD for a VM. To get the media ID, you would need to access the Entity for the catalog Item.

Sharing a code snippet to help you out:


# Get VM object 
>>> vm = VM(client, resource=vapp.get_all_vms()[0])
# Get catalog item
>>> it = org.get_catalog_item(CATALOG_LIBRARY, ITEM_NAME)
# Media ID can be accessed via **Entity** attribute for the catalog object
>>> it.Entity.items()
[('href', 'https://fqdn/api/media/e91052bd-46bb-4a2f-bf11-4285511c52ef'), ('id', 'urn:vcloud:media:e91052bd-46bb-4a2f-bf11-4285511c52ef'), ('name', ITEM_NAME), ('type', 'application/vnd.vmware.vcloud.media+xml')]
# insert CD
>>> t = vm.insert_cd_from_catalog(dict(it.Entity.items())["id"].split(":")[-1])
# eject CD
>>> t = vm.eject_cd(dict(it.Entity.items())["id"].split(":")[-1])```
bogdan-ds commented 4 years ago

Thanks a lot @sumitkapoor, that worked!