In today’s world of Dev Ops, automation is the key to working effectively and efficiently in the datacenter. Thinking more as a developer for operations engineers is crucial to staying both relevant in the ever-growing automation centric world and being able to stay up with the demands of the ever more complex and massive provisioning and configuration processes. Being able to query and use Restful APIs for operations engineers is a key component in being able to successfully use automation. What is Restful APIs and why are they so important for engineers thinking about automation? How do we interact with Restful APIs and use them to perform configuration tasks? Let’s take a closer look into Restful services.
Restful APIs are application programming interfaces that have the characteristics that are found to have the characteristics of a Restful API. What are those characteristics? Basically, Restful APIs have the following characteristics. They are client/server architecture, they are stateless, they have a uniform interface. The characteristic and requirement that they are stateless is key to understanding how Restful APIs work. They cannot rely on any requirement of state information being shared between the server and client. They each don’t care about the state information. Typically, today’s Restful API calls are made over HTTP requests and enacted with the verbs GET, PUT, POST DELETE, etc. Most of today’s infrastructure software is being developed with automation in mind and Restful APIs built in that allow for interacting with infrastructure systems in a programmatic way that allows for running infrastructure with code based mechanisms that interact with these APIs. VMware’s vSphere is a great example of having full APIs available to interact with in a way to carry out tasks in an automated way. Let’s take a look at some of the APIs with various vSphere products and how to interact with them.
VMware vSphere APIs and Interacting with them
Especially with the introduction of vSphere 6.5, there are numerous APIs that can be taken advantage of for automation. A great resource to check out is the VMware vSphere API Reference Documentation. When you access your vSphere vCenter server, you can check out the available APIs on a live system by navigating to https://< your vCenter server/apiexplorer. You can check out the available operations and expand those operations through the apiexplorer web interface. Below is an example of the cluster API value that can be interacted with. Notice when we view the expanded operations we see JSON response values.
Additionally, once you connect to your vCenter server via PowerCLI, you can use the command help Get-CisService -Examples to retrieve PSObject objects that represent a proxy to a vSphere Automation SDK API service and can be used to invoke operations via the vSphere Automation SDK. The help entry for the Get-CisService commandlet gives us a couple of examples that we can use for learning and to be querying the VMware APIs to do something useful.
In the second example, we are given the code to create a virtual machine using the VMware Automation SDK API. In this example we connect to the Automation SDK server, retrieve the service for the virtual machine management and create the virtual machine based on the details provided. We can use this example as a template if we want to be customized for the needs of our environment.
C:\PS># Connect to the vSphere Automation SDK API
Connect-CisServer -Server $serverAddress -User $user -Password $pass
# Get the service for VM management
$vmService = Get-CisService com.vmware.vcenter.VM
# Create a VM creation specification
$createSpec = $vmService.Help.create.spec.CreateExample()
# Fill in the creation details
$createSpec.name = “ExampleVM”
$createSpec.guest_OS = “WINDOWS_7_64”
$createSpec.placement.folder = (Get-Folder vm).ExtensionData.MoRef.Value
$createSpec.placement.host = (Get-VMHost).ExtensionData.MoRef.Value
$createSpec.placement.datastore = (Get-Datastore).ExtensionData.MoRef.Value
$createSpec.placement.cluster = $null
$createSpec.placement.resource_pool = $null
# Call the create method passing the specification
$vmService.create( $createSpec )
Connects to a vSphere Automation SDK server, retrieves the service for virtual machine management, and creates a virtual machine, based on the provided creation details, by passing the specification to the create method.
We are beginning to see the power of the VMware Automation SDK API interface and the powerful automation that can be accomplished via the Restful APIs.
VMware NSX and configuration using Rest
VMware NSX is another VMware product that has powerful API hooks to be able to get, alter, or delete configurations from our VMware NSX environment. We want to talk about another means to interact with these APIs as well. We can use third party utilities to be able to speak to the API interface. A great way to begin interacting with APIs with another utility is by using a utility such as Google’s Postman utility. Postman is available either via a full installation or via a browser plugin.
Below, we have queried our NSX controller via a GET method to return to us the SSO configuration of our vSphere environment.
Many operations in the web client GUI are very cumbersome to accomplish especially with the tedious configurations required with many aspects of VMware NSX. Configuring large scale NSX deployments by hand using the web client would be extremely labor intensive. However, by utilizing the power of NSX APIs, we can script out many aspects of the deployment, configuration, and “day 2” operations required with the NSX deployment.
In today’s world of infrastructure IT, managing infrastructure the way we have always managed it in the past is no longer possible. We simply can’t accomplish what we need to do using traditional tools in the fast paced manner that is required of infrastructure IT. Understanding and utilizing Restful APIs can open up a whole new world of thinking for infrastructure IT operations engineers. While this means that Ops engineers do need to understand and conceptualize code, we don’t have to necessarily feel that we have to become programmers. There are many tools and utilities out there such as Google’s Postman that can help to become more familiar with Restful APIs and how to interact with them in various settings such as VMware’s NSX platform. Additionally, using PowerShell to interact with APIs is a great way to do this also. A good example of this as shown is with the VMware. PowerCLI module interacting with the vSphere Automation SDK APIs.