Created
March 16, 2018 06:34
-
-
Save codyde/00db19e3b70e22bb5a5ab1dcbdbcfb32 to your computer and use it in GitHub Desktop.
Example of VMC API Calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import os | |
def login(): | |
key = os.environ['oauthkey'] | |
baseurl = 'https://console.cloud.vmware.com/csp/gateway' | |
uri = '/am/api/auth/api-tokens/authorize' | |
headers = {'Content-Type':'application/json'} | |
payload = {'refresh_token': key} | |
r = requests.post(f'{baseurl}{uri}', headers = headers, params = payload) | |
if r.status_code != 200: | |
print(f'Unsuccessful Login Attmept. Error code {r.status_code}') | |
else: | |
print('Login successful. ') | |
auth_json = r.json()['access_token'] | |
auth_Header = {'Content-Type':'application/json','csp-auth-token':auth_json} | |
return auth_Header | |
auth_header = login() | |
orgList = requests.get('https://vmc.vmware.com/vmc/api/orgs', headers = auth_header) | |
orgID = orgList.json()[1]['id'] | |
sddcList = requests.get(f'https://vmc.vmware.com/vmc/api/orgs/{orgID}/sddcs', headers = auth_header) | |
if sddcList.status_code != 200: | |
print('API Error') | |
else: | |
for sddc in sddcList.json(): | |
print("SDDC Name: " + sddc['name']) | |
print("SDDC Create Date: " + sddc['created']) | |
print("SDDC Provider: " + sddc['provider']) | |
print("SDDC Region: " + sddc['resource_config']['region']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment