Created
August 3, 2020 03:25
-
-
Save waddles/d3fc808c3d50b62cd7e81e1e65905666 to your computer and use it in GitHub Desktop.
Update Nutanix VM's categories (tags)
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
#!/usr/bin/python3 | |
import requests, os, subprocess, platform, getpass, time | |
from requests.auth import HTTPBasicAuth | |
from requests.packages.urllib3.exceptions import InsecureRequestWarning | |
cluster_address = 'prism-central' | |
user = '' | |
passwd = '' | |
headers = {'content-type': 'application/json'} | |
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) | |
def restget(uri, params=None): | |
response = requests.get(uri, auth=HTTPBasicAuth(user, passwd), headers=headers, params=params, verify=False) | |
return(response) | |
def restpost(uri, payload={}, params=None): | |
response = requests.post(uri, auth=HTTPBasicAuth(user, passwd), headers=headers, params=params, json=payload, verify=False) | |
return(response) | |
def restput(uri, payload={}, params=None): | |
response = requests.put(uri, auth=HTTPBasicAuth(user, passwd), headers=headers, params=params, json=payload, verify=False) | |
return(response) | |
if __name__ == '__main__': | |
prism_api = "https://" + cluster_address + ":9440/api/nutanix/v3" | |
payload = {'kind': 'vm', 'sort_order': 'ASCENDING', 'sort_attribute': 'name', 'length': 2} | |
status = restpost(prism_api + '/vms/list', payload) | |
# print(status.json()) | |
if status.ok: | |
vm_list = status.json()['entities'] | |
for vm in vm_list: | |
print('name: {}, uuid: {}, categories: {}'.format(vm['status']['name'], vm['metadata']['uuid'], vm['metadata'].get('categories'))) | |
# update categories | |
categories = {'Environment':'Production'} | |
vm['metadata']['categories'] = categories | |
# delete the existing status from vm object to avoid 422 error | |
del vm['status'] | |
update_status = restput(prism_api + '/vms/' + vm['metadata']['uuid'], payload=vm) | |
if update_status.ok: | |
print(update_status.json()['status']['execution_context']['task_uuid']) | |
else: | |
print("Unable to get vm list from PC:\t ", status.json()) | |
raise SystemExit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment