Continuing from where we left off in part 1 of this series, we will explore how to use Swagger Codegen to generate API client SDKs for VMware products such as vCenter and vCloud Director. In this post, we will focus on using environment variables to set local settings and demonstrate how to authenticate using cookie-based authentication.
As a recap, in part 1, we created a new API SDK for a subset of vCenter REST APIs and imported our new vc_client module. We also setup the target hostname and authentication settings using environment variables. Our goal is to use this session to get data from the vCenter API without providing username/password for each request.
To start, we can import our new vc_client module and use the client.call_api instruction to make API calls. We will rely on the cookie update feature to authenticate using cookie-based authentication. Here’s an example of how to do this:
“`
client = vc_client.Client(
hostname=”
username=”
password=”
verify=False,
)
response = client.call_api(“GET”, “/api/session”)
s = response.headers[“Set-Cookie”]
client.cookie = s[2]
“`
In this example, we use the `call_api` method to make a GET request to the `/api/session` endpoint to retrieve the session cookie. We then store the cookie in the `client.cookie` attribute.
Now that we have a session established, we can use it to get data from the vCenter API. Here’s an example of how to list all VMs:
“`
response = client.call_api(“GET”, “/api/virtualMachines”)
for vm in response.json():
print(vm[“name”])
“`
In this example, we use the `call_api` method to make a GET request to the `/api/virtualMachines` endpoint to retrieve a list of all VMs. We then iterate over the list and print the name of each VM.
As a final example, we will demonstrate how to use our new session to list our rights in the current organization using vCloud Director. Here’s an example of how to do this:
“`
response = client.call_api(“GET”, “/api/organization/rights”)
for right in response.json():
print(right[“name”])
“`
In this example, we use the `call_api` method to make a GET request to the `/api/organization/rights` endpoint to retrieve a list of all rights in the current organization. We then iterate over the list and print the name of each right.
As you can see, generating a new API client SDK for VMware products using Swagger Codegen is straightforward and easy to use. Authentication can require some customization, but the most limiting thing will be linked to the limited available actions through the REST API on some products. However, for the available and documented REST API parts, you can now deliver/provide a lot of SDKs, even without knowing the bases of the used language.
In conclusion, using Swagger Codegen to generate API client SDKs for VMware products such as vCenter and vCloud Director is a powerful tool that can help you save time and effort when building APIs for these products. By leveraging environment variables to set local settings and authenticating using cookie-based authentication, you can easily create customized SDKs that meet your specific needs.