Last active
December 9, 2019 14:28
-
-
Save gowatana/0e154fe613964d3e82e1a1dccbab1827 to your computer and use it in GitHub Desktop.
Nutanix Flow で REST API での Quarantine サンプル。
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
#!/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import json | |
import requests | |
import urllib3 | |
args = sys.argv | |
conf_file = args[1] | |
vm_uuid = args[2] | |
vm_quarantine = args[3] | |
with open(conf_file, "r") as file: | |
conf = file.read() | |
conf = json.loads(conf) | |
prism_addr = conf["prism_address"] | |
prism_user = conf["user_name"] | |
prism_pass = conf["password"] | |
def get_ntnx_v3_vm_spec(vm_uuid): | |
api_url = 'https://' + prism_addr + ':9440/api/nutanix/v3/vms/' + vm_uuid | |
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) | |
s = requests.Session() | |
s.auth = (prism_user, prism_pass) | |
s.headers.update({'Content-Type': 'application/json; charset=utf-8'}) | |
vm_spec = s.get(api_url, verify=False).json() | |
del vm_spec['status'] | |
return vm_spec | |
def update_ntnx_v3_vm(vm_uuid, vm_spec_json): | |
api_url = 'https://' + prism_addr + ':9440/api/nutanix/v3/vms/' + vm_uuid | |
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) | |
s = requests.Session() | |
s.auth = (prism_user, prism_pass) | |
s.headers.update({'Content-Type': 'application/json; charset=utf-8'}) | |
task = s.put(api_url, data=json.dumps(vm_spec_json), verify=False).json() | |
return task | |
def main(): | |
vm = get_ntnx_v3_vm_spec(vm_uuid) | |
if (vm_quarantine == "on"): | |
vm['metadata']['categories']['Quarantine'] = 'Default' | |
else: | |
del vm['metadata']['categories']['Quarantine'] | |
del vm['metadata']['last_update_time'] | |
print('Config ----------') | |
print(json.dumps(vm, indent=2)) | |
task = update_ntnx_v3_vm(vm_uuid, vm) | |
print('Return ----------') | |
print(json.dumps(task, indent=2)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
下記の投稿むけ。
Nutanix Flow で vNIC 遮断自動化くん。(Nutanix CE / REST API 編)
https://blog.ntnx.jp/entry/2019/12/09/232406