From ab193580dcdd810b7bef69cc04cebef315f4781d Mon Sep 17 00:00:00 2001 From: Oyvind Albrigtsen Date: Thu, 23 Apr 2020 15:55:11 +0200 Subject: [PATCH] fence_vmware_rest: add filter parameter --- agents/vmware_rest/fence_vmware_rest.py | 24 ++++++++++++++++++++--- tests/data/metadata/fence_vmware_rest.xml | 9 ++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) --- a/agents/vmware_rest/fence_vmware_rest.py 2020-04-24 14:28:04.674694199 +0200 +++ b/agents/vmware_rest/fence_vmware_rest.py 2020-04-24 13:02:25.001890003 +0200 @@ -8,10 +8,14 @@ from fencing import * from fencing import fail, run_delay, EC_LOGIN_DENIED, EC_STATUS -state = {"POWERED_ON": "on", 'POWERED_OFF': "off"} +state = {"POWERED_ON": "on", 'POWERED_OFF': "off", 'SUSPENDED': "off"} def get_power_status(conn, options): - res = send_command(conn, "vcenter/vm?filter.names={}".format(options["--plug"]))["value"] + try: + res = send_command(conn, "vcenter/vm?filter.names={}".format(options["--plug"]))["value"] + except Exception as e: + logging.debug("Failed: {}".format(e)) + fail(EC_STATUS) if len(res) == 0: fail(EC_STATUS) @@ -28,12 +32,23 @@ "off" : "stop" }[options["--action"]] - send_command(conn, "vcenter/vm/{}/power/{}".format(options["id"], action), "POST") + try: + send_command(conn, "vcenter/vm/{}/power/{}".format(options["id"], action), "POST") + except Exception as e: + logging.debug("Failed: {}".format(e)) + fail(EC_STATUS) def get_list(conn, options): outlets = {} - res = send_command(conn, "vcenter/vm") + try: + command = "vcenter/vm" + if "--filter" in options: + command = command + "?" + options["--filter"] + res = send_command(conn, command) + except: + logging.debug("Failed: {}".format(e)) + fail(EC_STATUS) for r in res["value"]: outlets[r["name"]] = ("", state[r["power_state"]]) @@ -87,7 +102,10 @@ return conn def disconnect(conn): - send_command(conn, "com/vmware/cis/session", "DELETE") + try: + send_command(conn, "com/vmware/cis/session", "DELETE") + except Exception as e: + logging.debug("Failed: {}".format(e)) conn.close() def send_command(conn, command, method="GET"): @@ -142,6 +160,16 @@ "required" : "0", "shortdesc" : "The path part of the API URL", "order" : 2} + all_opt["filter"] = { + "getopt" : ":", + "longopt" : "filter", + "help" : "--filter=[filter] Filter to only return relevant VMs" + " (e.g. \"filter.names=node1&filter.names=node2\").", + "default" : "", + "required" : "0", + "shortdesc" : "Filter to only return relevant VMs. It can be used to avoid " + "the agent failing when more than 1000 VMs should be returned.", + "order" : 2} def main(): @@ -154,6 +182,7 @@ "notls", "web", "port", + "filter", ] atexit.register(atexit_handler) @@ -166,8 +195,12 @@ docs = {} docs["shortdesc"] = "Fence agent for VMware REST API" - docs["longdesc"] = "fence_vmware_rest is an I/O Fencing agent which can be \ -used with VMware API to fence virtual machines." + docs["longdesc"] = """fence_vmware_rest is an I/O Fencing agent which can be \ +used with VMware API to fence virtual machines. + +NOTE: If there's more than 1000 VMs there is a filter parameter to work around \ +the API limit. See https://code.vmware.com/apis/62/vcenter-management#/VM%20/get_vcenter_vm \ +for full list of filters.""" docs["vendorurl"] = "https://www.vmware.com" show_docs(options, docs) diff --git a/tests/data/metadata/fence_vmware_rest.xml b/tests/data/metadata/fence_vmware_rest.xml index 5b497a6a..d60c8775 100644 --- a/tests/data/metadata/fence_vmware_rest.xml +++ b/tests/data/metadata/fence_vmware_rest.xml @@ -1,6 +1,8 @@ -fence_vmware_rest is an I/O Fencing agent which can be used with VMware API to fence virtual machines. +fence_vmware_rest is an I/O Fencing agent which can be used with VMware API to fence virtual machines. + +NOTE: If there's more than 1000 VMs there is a filter parameter to work around the API limit. See https://code.vmware.com/apis/62/vcenter-management#/VM%20/get_vcenter_vm for full list of filters. https://www.vmware.com @@ -87,6 +89,11 @@ The path part of the API URL + + + + Filter to only return relevant VMs. It can be used to avoid the agent failing when more than 1000 VMs should be returned. +