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