Getting Started With Pyvcloud — A Python Library | by Yash Salvi | Apr, 2022

Picture by Christina Morillo from Pexels

Do you utilize VMWare’s vCloud Director? Are you uninterested in repeating the mundane steps of making vApps, VMs, and many others? Are you looking for

“Methods to use Pyvcloud”, “Python package deal for vCloud Director”

In case your reply is sure, then Meet VMWare’s very personal Pyvcloud.

In VMWare’s personal phrases, “Pyvcloud is the Python SDK for VMWare vCloud Director”.

In best phrases, Pyvcloud is a Python library for VCloud Director administration and all duties which you’ll carry out on vCloud Director will be accomplished with this easy-to-use Pyvcloud! There are exceptions for some duties which aren’t out there at current, we’ll cowl them in upcoming discussions.

Earlier than you Start:

  1. Obtain and set up python in your machine, utilizing Download Python
  2. Arrange an IDE, there are various IDEs on the market however I’d suggest sticking with VS Code for this tutorial.
  3. Intellisense to the rescue: Add Microsoft’s Extension for Python to VS Code

Putting in pyvcloud

You could set up pyvcloud in your system earlier than utilizing it in your Python packages. Run the command given beneath

pip set up pyvcloud
Picture by Client Patterson on unsplash

For this tutorial, we’ll create a category known as as VCD_Utils during which we’ll outline all of the strategies.

The constructor units up a connection of your python along with your VCD. On this code snippet, the Shopper is a low-level interface to the VCloud Director’s REST API. It wants your VCD’s hostname and different parameters are optionally available; we will both set them to True or False relying upon your log necessities. The BasicLoginCredentials does the primary wiring job and units up the connection for you.

The Org and VDC are constructors, in Pyvcloud bear in mind whereas utilizing constructors the one two issues which you want are the consumer object and respective useful resource. For instance for developing a VDC object, we provided the consumer and VDC’s useful resource. We’ll retailer the org and VDC objects as occasion variables as they’re wanted for additional operations.

Word: For our tutorial, we’ve a one-to-one mapping i.e. a single vApp incorporates just one VM.

Helper Strategies:

These are the helper strategies that can make our life simpler as we proceed.

The get_vapp() takes within the vapp_name, grabs its assets from our datacenter, after which we create an XML Object of our vAPP utilizing a constructor. The identical goes with the get_vm() technique which creates a VM object and returns it! The final technique does_vapp_exist() merely checks if the given vApp exists in our VCD or not, if yes->returns True else returns False.

Performing CRUD operations on VCD:

Picture by courtesy of Yash Salvi

Making a vApp:

This technique wants the title of the brand new vApp, the template for making the vApp, and different optionally available parameters like community, storage coverage, and many others. Underneath the hood, we’re utilizing VCD’s instantiate_vapp() technique which creates vApp and returns the vApp XML object. Now, after creating we will test if the vApp is created or not by our helper does_vapp_exist() technique.

Word: When the vApp is created it states is unresolved i.e. Energy state = 0 until the app’s state adjustments from unresolved to Powered off(energy state = 8), we will’t carry out any operations on the vApp.

Now, we will play with our vApp, we’ll want the vApp’s object which is constructed by pyvcloud’s VApp class. The vApp object incorporates a ton of strategies which you’ll simply see by including a dot after it, because of our Intellisense. For studying vApp’s information, let’s get the IP tackle of our VM.

Word: Usually, the Pyvcloud returns XML information which must be transformed to a dictionary. Pyvcloud has a built-in to_dict() technique that may assist us on this transformation and we will see the dictionary contents on the console!

Updating vApp

Now, we’ll replace our VM’s CPU. For this, we want our VM Object residing inside our vApp. We’ll use our helper technique get_vm() for fetching the VM object. The VM object can then be used to replace the CPU.

Deleting the vApp

Now, we’ll delete the vApp. The deletion can be fairly easy. We merely want vapp_name, provide it to Pyvcloud, and growth it will get deleted from VCD. The pressure true right here merely means to energy off the VM in it if it’s on. If VM is powered on and pressure true isn’t handed, then we’d get an error!

Alas! We’ve written our class.

Let’s rip the fruits and make our work pay by creating an object for our class.

Final however not the least, let’s log off of our VCD.

Until now, you must have efficiently carried out the CRUD operations on the VCD. For getting deep into the pyvcloud, you may undergo their documentation.

More Posts