Home Page Technical/How-To VCF Compute (vSphere) VMware Cloud Foundation

Automating vSphere Configuration Profile APIs – Part 2 – PowerCLI and Python Sample Code

Welcome back to our series on Automating vSphere Configuration Profile (VCP). In Part 1, we broke down the core VCP APIs and how they maintain the “desired state” of vSphere clusters. Now, it’s time to get hands-on with the code.

This blog post explains how to consume these APIs using the tools most familiar to VI admins and developers: PowerCLI and Python.

Consuming VCP APIs Using PowerCLI SDK

If you are already living in a PowerShell world, the PowerCLI SDK is your best friend. It provides direct, low-level access to the VMware Cloud Foundation (VCF) and vSphere Automation APIs, making it a natural fit for existing automation pipelines.

Pro Tip: If you’re new to the latest SDK architecture, check out my recent blog post, Demystifying VCF PowerCLI 9.0 SDK, to understand how the new bindings work.

Getting Started

To begin, ensure you have VCF PowerCLI 9.0 (or later) installed. The magic of this SDK lies in its discoverability; you don’t have to guess the command names if you already know the REST URI.

Finding Your Command

The Get-vSphereOperation cmdlet is the “Rosetta Stone” for mapping REST APIs to PowerCLI. Let’s look at a common VCP task: checking a cluster’s enablement status.

The Discovery Command:

PowerShell

Running this will return a CommandInfo object. Once you have that, you can invoke the corresponding operation. 

The below PowerCLI code sample demonstrates the process of enabling vSphere Configuration Profiles (VCP) on a vSphere Cluster. Furthermore, it illustrates how to manage, update, and configure the desired state using the VCP draft APIs. 

Consuming VCP APIs Using Unified VCF SDK for Python​​

The Unified VCF SDK 9.0 for Python, introduced in VCF 9.0, represents a significant evolution in automating VMware environments. This new SDK simplifies the consumption of VCF APIs by unifying what were previously separate libraries such as pyvmomi for SOAP, the vSphere Automation SDK for REST, and a dedicated vSAN SDK for vSAN into a single package.

Developers can now use this single SDK to access all VCF APIs. Refer to the blog post Introducing a Unified VCF SDK 9.0 for Python and Java to learn more about the API coverage. 

Core Enhancements in VCF SDK 9.0

  • Consolidated Packaging: You can now install the entire suite via a single command: pip install vcf-sdk.
  • Unified Authentication: The biggest pain point in previous versions was managing different sessions for SOAP (/vim25) and REST (/api) endpoints. Now, you can log in once and share the session ID across both.
  • Expanded Coverage: Includes bindings for the new VCF Installer, SDDC Manager, and the latest vSphere Configuration Profile (VCP) APIs.

How are VCP APIs Structured in VCF SDK?

To automate vSphere Configuration Profiles (VCP), you will primarily work within the com.vmware.esx.settings.clusters.* namespace.

In Python, the SDK is organized into Modules (which contain the client logic) and Service Classes (which contain the methods like get, list, or enable_task). To perform an action, you must import the specific client module and then instantiate its service class.

Example: Transitioning a Cluster to VCP

One of the most common VCP tasks is moving a cluster from Host Profiles to VCP.

The Python Implementation: To invoke this, you use the Transition service class found within the enablement.configuration_client module.

1. Import the Service Class

First, import the Transition class from the appropriate client module:

Python

2. Initialize the Service Endpoint

Once imported, you create a service endpoint by passing your authenticated vsphere_client stub configuration to the class constructor. This “connects” the class to your active session.

Python

Key Takeaway

While the REST API path gives you the “address” of the operation, the Python SDK maps these paths into a nested hierarchy. Always look for the _client suffix in the module name to find the classes that contain the actual execution methods (the “verbs”) for your automation. 

Check out the complete python sample code to enable VCP on a vSphere Cluster. 

The vSphere Configuration Profile (VCP) APIs in the Unified VCF SDK and PowerCLI 9.0 provide a direct, programmatic way to manage cluster configurations. By using these unified tools, you can automate setup tasks and ensure consistency across your environment. Whether you use PowerCLI for quick scripts or the Python SDK for larger applications, managing VCP is now more integrated and accessible.

Resources

You can find the complete Python sample for the VCP enablement and configuration workflow here:

Next Steps

In the next post, we will move beyond manual scripts to explore GitOps workflows. You’ll learn how to manage these cluster profiles as code, using Git to trigger automated updates.


Discover more from VMware Cloud Foundation (VCF) Blog

Subscribe to get the latest posts sent to your email.