← Back to list

Advanced applications of Pyvcloud

The pyvcloud package from VMware provides a host of different powerful funtions and methods to perform various kinds of automations in…

Shridhara Hegde · 2023-08-01 07:22 · 12 claps · 2.8 min read
#vcloud-director #vmware #python
Open on Medium ↗
Wiki topics: 🎬 · Film & Television

Advanced applications of Pyvcloud

The pyvcloud package from VMware provides a host of different powerful funtions and methods to perform various kinds of automations in vCloud Director, however, there are some areas in which the pyvcloud package does not suffice.

In this blog, we take a look at one such example and see how such a problem can be addressed.

Let us consider the case of Org Catalogs. An org catalog is a feature that allows cloud administrators to create and manage catalogs of pre-defined virtual machine templates, media files, and other cloud resources that can be made available to the users within a specific organization. An org catalog, can be configured to use a specific vDC’s storage profile. This would affect the deletion of that particular vDC as the Catalog needs to first be deleted in order to delete the vDC.

So here’s the problem statement, “If we wanted to automate the process of a vDC deletion, how would we delete the catalog that is backing the vDC’s storage profile ?“.

We cannot use any of the pyvcloud functions for this since there is no provision of a function to list catalogs that are backed by a particular vDC’s storage profile. In such a scenario, we can make use of the the pyvcloud.vcd.client which is a low-level interface to the vCloud Director REST API and issue typed queries to get the information we want.

First, we need to get the details of all the catalogs in the Org as shown below.

def list_all_org_catalogs(client: Client, org_name: str) -> List[Element]:
    """
       Get a list of all the Catalogs of a particular Org.
       Args:
            client: pyvcloud.vcd.client object
            org_name: name of the org in which the vDC is present
  """

    query = client.get_typed_query(
        ResourceType.ADMIN_CATALOG.value,
        query_result_format=QueryResultFormat.REFERENCES,
        equality_filter=('orgName', org_name)
    )

    catalogs = []
    for reference in query.execute():
        catalogs.append(
            client.get_resource(get_admin_href(reference.get('href')))
        )

    return catalogs

The above code uses the typed query

This may arise the question “Why isn’t the built in pyvcloud org.list_catalogs() function used?”. This is because, the built-in list_catalogs() function does not return an object that contains vDC storage profile information in it.

In order to get that information, we need to use client.get_typed_query function which queries for an ADMIN_CATALOG resource type. The query.execute() method executes the query that we just defined and returns a list of references to the catalogs. We iterate through these references and do a client.get_resource to get the objects that these references refer to, and store them in a list.

Now we have all the catalogs in the org. Each of these catalogs are in the XML format and upon decoding them look something like this:

<AdminCatalog
  name="catalog-name"
  type="application/vnd.vmware.admin.catalog+xml">
  <CatalogStorageProfiles>
    <VdcStorageProfile
      href=""
      id="urn:vcloud:vdcstorageProfile:1234-1234-1234-1234"
      name=""
      type="com.vmware.vcloud.entity.vdcstorageProfile"/>
  </CatalogStorageProfiles>
</AdminCatalog>

As we can see, this XML contains, the <VdcStorageProfiles> attribute which suggests that the catalog is backed by a vDC’s storage profile and we can use the “id” field to identify weather the catalog is backed by the vDC that we are trying to delete or not. This can be done as follows.

def get_backed_catalogs(client: Client, org: Org, org_name: str, vdc: VDC):
  """
    Get a list of all the catalogs that use a particular vDC's storage profile
    Args:
      client: pyvcloud.vcd.client object
      org: pyvcloud.vcd.org object
      org_name: name of the org in which the vDC is present
      vdc: pyvcloud.vcd.vdc object
  """
  backed_catalogs = []
  org_catalogs = list_all_org_catalogs(client, org_name) # The function that we previously wrote
  vdc_storage_profiles = vdc.get_storage_profiles()

  for catalog in org_catalogs:
      if hasattr(catalog, 'CatalogStorageProfiles') and hasattr(catalog.CatalogStorageProfiles, 'VdcStorageProfile'):
          catalog_storage_profile_id = getattr(catalog.CatalogStorageProfiles, 'VdcStorageProfile').get('id')
          catalog_name = catalog.get('name')

          for vdc_storage_profile in vdc_storage_profiles:
              if vdc_storage_profile.get('id') == catalog_storage_profile_id:
                  backed_catalogs.append(catalog_name)

  return backed_catalogs

The above code snippet gets the storage profiles of the vDC in question, using the vdc.get_storage_profiles() function and later compares the id of the catalogs which have the *<VdcStorageProfile>* *attribute. If the id* **matches, the names of the catalogs are stored in the list and returned.

Conclusion

In this blog, we tried to solve a problem of identifying if the Catalog is backed by a particular vDC’s storage profile, even though there is no such built-in function in pyvcloud to do so.


메타데이터
post_id
bf6c89e89bfa
slug
advanced-applications-of-pyvcloud-bf6c89e89bfa
url
https://medium.com/@cs.shridhara/advanced-applications-of-pyvcloud-bf6c89e89bfa
canonical_url
https://medium.com/@cs.shridhara/advanced-applications-of-pyvcloud-bf6c89e89bfa
author_url
https://medium.com/@cs.shridhara
status
ok
fetched_at
2026-06-24 13:29:15