VMware Cloud on AWS APIs
Using PowerShell
- Click on Start, and scroll down until you see the Windows PowerShell menu
Right click on the PowerShell CLI shortcut icon and select Run as Administrator
Install the VMware PowerCLI module
Install-Module VMware.PowerCLI
NOTE: You will be asked to install the NuGet provider, take the default or press Y and press enter, you will then be asked to trusted an untrusted repository, DO NOT take the default but type Y and press Enter.
We now need to set the execution policy to Remote Signed.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force
You now will need to set the PowerCLI Configuration to Ignore Invalid Certificates.
IMPORTANT STEP:
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false -WarningAction:SilentlyContinue
NOTE: Be sure the “i” in “Ignore” is capitalized if you are not using copy/paste
We now need to install the VMware CLI commands
Install-Module -name VMware.VMC -scope AllUsers -Force
Let’s take a quick look at the VMware CLI commands
Get-VMCCommand -WarningAction:SilentlyContinue
We now need to get your Refresh Token from the VMC console. Switch back to or open the web browser and log into vmc.vmware.com
If you are not already logged in
- Open a new tab
- Click on the VMware Cloud on AWS shortcut
- Fill in your email address
Click on Next
Click on the drop down next to your Name/Org ID
Click on My Account
We will now create a new Refresh Token for the ID linked to this Org
Click on API Tokens tab.
Click Generate a New API Token
Give the token a name.
Select checkbox by Organization Owner.
Select checkbox by VMware Cloud on AWS.
Click Generate button.
Click on Copy button to save refresh token to clipboard.
Note: Make sure to save this refresh token in a safe place to be used in the next section when using API’s in Postman.
Now let’s attach to the VMC server, input the command below and append the refresh token after the -refreshtoken parameter
connect-vmc -refreshtoken
Now that we are connected to our VMC organization through PowerShell, we can see what Orgs we have access to using the following command
Get-VMCorg
Note the Org Display_Name and ID
Now that we know the Org Display_Name we can find out information about the SDDC’s inside our org.
NOTE: replace # with your workstation number
Get-VMCSDDC -Org VMC-WS#
Another cool thing you can do is see the Default Credentials for your SDDC
Get-VMCSDDCDefaultCredential -org VMC-WS#
NOTE: replace # with your workstation number
REST APIs through Developer Center
In this module we will be using the VMware Cloud on AWS REST API to get some basic information about your VMware Cloud on AWS Organization and SDDC deployment. To do this we will be using the new Developer Center feature in VMware Cloud on AWS. This was built specifically to focus on using APIs and scripts to create SDDCs, add and remove hosts, plus connect to and use the full vCenter API set. To get started, let go back to your VMC environment.
Launch the Chrome browser on your Student View Desktop
If you are not already logged in, log into your VMware Cloud on AWS organisation.
From within the VMware Cloud on AWS tab, click on the Developer Center menu
In the Developer Center there are a lot of great resources for you to explore. For example, let’s check out a code sample that was uploaded by one of our API developers. If you scroll through this screen you will see there are code samples for Postman (a REST API Development Tool)
You will also find samples for Python, PowerCLI, and many others. Anyone can contribute code samples to the community, if that interests you go to http://code.vmware.com or click on the link VMware{code} Sample Exchange.
Click on Code Samples in the menu
Click on Download in the “PowerCLI - VMC Example Script” box
After the script downloads
Click on the dropdown arrow
Click on Show in Folder
Unzip the PowerCLI-Example-Scripts-master.zip file
Open the PowerCLI-Example-Scripts-master folder
Open Scripts folder
Open VMware_Cloud_on_AWS folder
Right click on the VMC Example Script.ps1 script
Click on edit
This will open the PowerShell ISE environment. Now you can see the PowerShell commands you used in the previous module as well as other commands you can use with your SDDC. Close the PowerShell ISE windows
Let’s now run some simple REST API commands built into Developer Center, go back to your browser
Click on the API Explorer menu
Make sure you select your SDDC
Click on the drop down arrow next to Organization
Click on the drop down arrow next to the first “GET” API
Click on Execute
What did we not do?? We did not put in any authentication to pull this data. The reason is we are using the session authentication to execute these commands. To run these commands in other application, like PowerShell or Postman, you will need to get your resource and session tokens before you can run these commands.
Let’s look through the response.
Here you see the Organization’s alphanumeric name. Which you can also find in #3
The organization ID. NOTE: Copy the ID number, without the quotes, for possible use in the next step.
The organization Display_Name
The organization Version
In this step, we will GET some information about our organization
Click on the drop down arrow by SDDCs
Click on GET
The Org ID should already be filled in for you, another great feature the developers built in based on customer feedback. NOTE: If this Org ID did not automatically fill in, paste it in.
Click on Execute
Now let’s look at the response body
The creation date of the SDDC
The SDDC ID
the SDDC state
Postman
In this module, we will be exploring how to use Postman to execute REST API requests and build automation through collections. Postman is an API Explorer tool. As an example, you can create variables for use within the APIs, test the response, and use webhooks to integrate with collaboration platforms.
Postman is very easy to install, so let’s get started.
- Open a new browser tab and go to https://www.getpostman.com
Click on Download the App
Select Postman for Windows (64-bit). Click Download. Double-click on the downloaded file, the install will execute without interaction.
NOTE: For cleanup you can close all postman tabs in Chrome
Click on the text: Skip Signing in and Take me straight to the app
Uncheck Show this window on launch
Close this window
Go back to your browser window, if you do not have a tab opened for VMware Cloud on AWS, follow the below instructions
Navigate to https://github.com/vmware/vsphere-automation-sdk-rest/archive/master.zip to download the vSphere Automation REST SDK.
Our internal API development team has done a great job pre-creating SDKs for many of the popular languages in use today. For this module, we will be using the SDK for REST to show you how you can easily import and reuse some pre-built collections to create your own.
This step intentionally left blank
This step intentionally left blank
Click on the download menu
Click on Open
Click on Extract
Click on Extract all
We will keep the default file path.
Uncheck the box
Click on Extract
Close the file explorer window
Now that we have Postman installed and our REST samples on our local system, lets import the VMC collection and use some the requests to build our own collection.
Click on Import
Click on Choose Files
To import the VMC collection json file we downloaded earlier.
Browse to the directory we extracted the zip file to earlier. That directory should be C:\downloads\vsphere-automation-sdk-rest-master\vsphere-automation-sdk-rest-master\samples\postman
Click VMware Cloud on AWS APIs.postman_collection.json
Click Open
We now need to get our refresh token for our Org in VMC. Go back to your VMware Cloud on AWS tab in your browser
Click on the drop down next to your Name/Org ID
Click on My Account
We will now create a new Refresh Token for the ID linked to this Org.
NOTE: If you have already generated a token, use the same token that was generated. You can also regenerate a new token if needed.
Click on API Tokens tab.
Click Generate a New API Token
Give the token a name.
Select checkbox by Organization Owner.
Select checkbox by VMware Cloud on AWS.
Click Generate button.
Click on Copy button to save refresh token to clipboard.
Return to the Postman app. We now need to setup a Postman environment for use with VMC. An environment is where we will be creating and storing our variables. These variables can be local or global, depending on your use within Postman. In this module, we will only be using local variables.
Click on New
Click on Environment
Name the environment VMC
In the Key column type in refresh_token
In the Value column use CTRL-V to paste your actual refresh token you copied in a previous step.
Click on Add
Close the window
Now set this as our default environment.
NOTE: If you don’t set the default environment to VMC, then the variables that get created will not be accessible.
Click on the drop down arrow
Select VMC
Now we will start to build our own collection by using some request that came in the SDK we imported earlier.
Click on Collections
Click on - Authentication and Login
See how this request is our refresh token variable we defined in an earlier step.
NOTE: If the environment is not set to VMC, this will request will fail because the refresh_token variable is not defined.
Click on Send
You will now see the access token that was generated with the refresh token. This is the body or payload of the response to our request.
Click on the Eye icon
You will see that we have stored your access token into a variable so we can use it for futurecalls. How did we do that? We ran a “test” on the response body. You will see how in the next step.
Click on Tests
The access_token variable was set by running some java script code against the response. We are also using the Postman setEnvironmentVariable function to create it.
Lets save this request to our own collection so we can use it later.
Click on the drop down arrow
Click on Save As
Change the Request name to Authorize
Change the Request description to Get Access Token
Click on Create Collection
Type Workshop and click the check box
Select the Workshop folder
Click on Save to Workshop
A new window will pop open indicating that you created a new collection. We will not do anything here at this time.
Close this window
Let’s request some details from our Org so we can send them to Slack.
Click on Orgs and List Orgs
Click on Headers
Click Send
You see here how we are using the access_token variable for the csp-auth-token. This will authorize our request. NOTE: This access token is only good for 30 minutes. If you run this request and get a response of 400 unauthorized, go back and run the authorize request.
Look through the response body for your Org’s display_name
Let’s save this request to our own collection so we can use it later.
Click on the drop down arrow
Click on Save As
Change the Request name to Org list
Change the Request description to Get a list of your Orgs
Be sure Workshop is selected under Select a collection or folder to save to:
Click on Save to Workshop
We need to replace the Test code that came with the SDK so we can create variable we want to use when send our message to Slack.
Click on Tests Copy and paste the below code into the Tests section. NOTE: You may have to press CTRL-V to paste into the text box.
Click Send
var jsonData = JSON.parse(responseBody); if (responseCode.code === 200) { for (i = 0; i < jsonData.length; i++) { pm.environment.set("name", jsonData[i].display_name); pm.environment.set("ID", jsonData[i].id); pm.environment.set("version", jsonData[i].version); pm.environment.set("state", jsonData[i].project_state); } }
We can verify if the variables have been created and assigned values.
Click on the eye icon
Scroll down to see if the new variables were created. Once verified click on the “eye” icon again to close the window
Lets save the changes we made to this request.
Click on Save
Now that we have details of our Org lets send them to slack inn a message.
To post to slack a link needs to be generated for the slack channel that we want to post to. This has already been done for you and is listed below. One of the instructors will have this slack channel displayed on the screens. So you can see the results.
Slack channel URL:
https://hooks.slack.com/services/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
Now we need to setup the request:
Click on the + sign for a new request
Change the request type to POST
Cut and paste the above slack channel URL to the address box
Select Body
Change the format type to raw
Type the below code, or cut and paste it into the Body section. NOTE: You may have to press CTRL-V to past into the text box.
{ "text" : "{% raw %}Your Org ID is: {{ID}}\nYour Org version is: {{version}}\nAnd your Org state is: {{state}}{% endraw %}", "username" : "{% raw %}{{name}}{% endraw %}" }
Click Send
Lets save this request to our own collection so we can use it later.
Click on the drop down arrow
Click on Save As
Change the Request name to Post to Slack
Change the Request description to Post some Org details to slack Be sure Workshop is selected under Select a collection or folder to save to:
Click on Save to Workshop
Check and see if your request posted the Name, ID, Version, and Status of your Org.
The last thing to show you with Postman is the way that you can run a collection to automate a series of tasks. What we have been doing in this module is building a collection. As you see in the screen shot there are 3 tasks in the Workshop collection.
Click on the Arrow in the Workshop window
Click on Run
Click on Run Workshop
Be sure the Environment is set to VMC
If all your work was saved and ran individually, they should run here as well.
Check out the status of each request.
If you have all “200 OK” then you will see another post in slack for your workshop Org.
Please add comments below if you would like to give feedback on this lab.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.