Blame SOURCES/bz1402862-fence_rhevm-RHEV-v4-API-support.patch

d307bf
From a4e8b77ac51a0e4a6de489823ee1be47cbc7eb18 Mon Sep 17 00:00:00 2001
d307bf
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
d307bf
Date: Thu, 9 May 2019 12:09:48 +0200
d307bf
Subject: [PATCH] fence_rhevm: add RHEV v4 API support and auto-detection
d307bf
d307bf
---
d307bf
 agents/rhevm/fence_rhevm.py         | 44 +++++++++++++++++++++++------
d307bf
 tests/data/metadata/fence_rhevm.xml |  7 ++++-
d307bf
 2 files changed, 41 insertions(+), 10 deletions(-)
d307bf
d307bf
diff --git a/agents/rhevm/fence_rhevm.py b/agents/rhevm/fence_rhevm.py
d307bf
index a1cdaf605..6012c4239 100644
d307bf
--- a/agents/rhevm/fence_rhevm.py
d307bf
+++ b/agents/rhevm/fence_rhevm.py
d307bf
@@ -9,7 +9,8 @@
d307bf
 from fencing import fail, EC_FETCH_VM_UUID, run_delay
d307bf
 
d307bf
 RE_GET_ID = re.compile("
d307bf
-RE_STATUS = re.compile("<state>(.*?)</state>", re.IGNORECASE)
d307bf
+RE_STATUS = re.compile("<status>(.*?)</status>", re.IGNORECASE)
d307bf
+RE_STATE = re.compile("<state>(.*?)</state>", re.IGNORECASE)
d307bf
 RE_GET_NAME = re.compile("<name>(.*?)</name>", re.IGNORECASE)
d307bf
 
d307bf
 def get_power_status(conn, options):
d307bf
@@ -25,7 +26,10 @@ def get_power_status(conn, options):
d307bf
 
d307bf
 	options["id"] = result.group(2)
d307bf
 
d307bf
-	result = RE_STATUS.search(res)
d307bf
+	if tuple(map(int, options["--api-version"].split(".")))[0] > 3:
d307bf
+		result = RE_STATUS.search(res)
d307bf
+	else:
d307bf
+		result = RE_STATE.search(res)
d307bf
 	if result == None:
d307bf
 		# We were able to parse ID so output is correct
d307bf
 		# in some cases it is possible that RHEV-M output does not
d307bf
@@ -59,7 +63,10 @@ def get_list(conn, options):
d307bf
 		lines = res.split("
d307bf
 		for i in range(1, len(lines)):
d307bf
 			name = RE_GET_NAME.search(lines[i]).group(1)
d307bf
-			status = RE_STATUS.search(lines[i]).group(1)
d307bf
+			if tuple(map(int, options["--api-version"].split(".")))[0] > 3:
d307bf
+				status = RE_STATUS.search(lines[i]).group(1)
d307bf
+			else:
d307bf
+				status = RE_STATE.search(lines[i]).group(1)
d307bf
 			outlets[name] = ("", status)
d307bf
 	except AttributeError:
d307bf
 		return {}
d307bf
@@ -69,6 +76,13 @@ def get_list(conn, options):
d307bf
 	return outlets
d307bf
 
d307bf
 def send_command(opt, command, method="GET"):
d307bf
+	if opt["--api-version"] == "auto":
d307bf
+		opt["--api-version"] = "4"
d307bf
+		res = send_command(opt, "")
d307bf
+		if re.search("<title>Error</title>", res):
d307bf
+			opt["--api-version"] = "3"
d307bf
+		logging.debug("auto-detected API version: " + opt["--api-version"])
d307bf
+
d307bf
 	## setup correct URL
d307bf
 	if "--ssl" in opt or "--ssl-secure" in opt or "--ssl-insecure" in opt:
d307bf
 		url = "https:"
d307bf
@@ -90,7 +104,7 @@ def send_command(opt, command, method="GET"):
d307bf
 	web_buffer = io.BytesIO()
d307bf
 	conn.setopt(pycurl.URL, url.encode("UTF-8"))
d307bf
 	conn.setopt(pycurl.HTTPHEADER, [
d307bf
-		"Version: 3",
d307bf
+		"Version: {}".format(opt["--api-version"]),
d307bf
 		"Content-type: application/xml",
d307bf
 		"Accept: application/xml",
d307bf
 		"Prefer: persistent-auth",
d307bf
@@ -130,8 +144,9 @@ def send_command(opt, command, method="GET"):
d307bf
 
d307bf
 	result = web_buffer.getvalue().decode("UTF-8")
d307bf
 
d307bf
-	logging.debug("%s\n", command)
d307bf
-	logging.debug("%s\n", result)
d307bf
+	logging.debug("url: %s\n", url)
d307bf
+	logging.debug("command: %s\n", command)
d307bf
+	logging.debug("result: %s\n", result)
d307bf
 
d307bf
 	return result
d307bf
 
d307bf
@@ -151,6 +166,15 @@ def define_new_opts():
d307bf
 		"required" : "0",
d307bf
 		"shortdesc" : "Reuse cookies for authentication",
d307bf
 		"order" : 1}
d307bf
+	all_opt["api_version"] = {
d307bf
+		"getopt" : ":",
d307bf
+		"longopt" : "api-version",
d307bf
+		"help" : "--api-version                  "
d307bf
+			"Version of RHEV API (default: auto)",
d307bf
+		"required" : "0",
d307bf
+		"order" : 2,
d307bf
+		"default" : "auto",
d307bf
+	}
d307bf
 	all_opt["api_path"] = {
d307bf
 		"getopt" : ":",
d307bf
 		"longopt" : "api-path",
d307bf
@@ -158,20 +182,19 @@ def define_new_opts():
d307bf
 		"default" : "/ovirt-engine/api",
d307bf
 		"required" : "0",
d307bf
 		"shortdesc" : "The path part of the API URL",
d307bf
-		"order" : 2}
d307bf
+		"order" : 3}
d307bf
 	all_opt["disable_http_filter"] = {
d307bf
 		"getopt" : "",
d307bf
 		"longopt" : "disable-http-filter",
d307bf
 		"help" : "--disable-http-filter          Set HTTP Filter header to false",
d307bf
 		"required" : "0",
d307bf
 		"shortdesc" : "Set HTTP Filter header to false",
d307bf
-		"order" : 3}
d307bf
+		"order" : 4}
d307bf
 
d307bf
 
d307bf
 def main():
d307bf
 	device_opt = [
d307bf
 		"ipaddr",
d307bf
-		"api_path",
d307bf
 		"login",
d307bf
 		"passwd",
d307bf
 		"ssl",
d307bf
@@ -179,6 +202,8 @@ def main():
d307bf
 		"web",
d307bf
 		"port",
d307bf
 		"use_cookies",
d307bf
+		"api_version",
d307bf
+		"api_path",
d307bf
 		"disable_http_filter",
d307bf
 	]
d307bf
 
d307bf
@@ -186,6 +211,7 @@ def main():
d307bf
 	define_new_opts()
d307bf
 
d307bf
 	all_opt["power_wait"]["default"] = "1"
d307bf
+	all_opt["shell_timeout"]["default"] = "5"
d307bf
 
d307bf
 	options = check_input(device_opt, process_input(device_opt))
d307bf
 
d307bf
diff --git a/tests/data/metadata/fence_rhevm.xml b/tests/data/metadata/fence_rhevm.xml
d307bf
index 6344db79f..c56cf64b6 100644
d307bf
--- a/tests/data/metadata/fence_rhevm.xml
d307bf
+++ b/tests/data/metadata/fence_rhevm.xml
d307bf
@@ -98,6 +98,11 @@
d307bf
 		<content type="string"  />
d307bf
 		<shortdesc lang="en">Login name</shortdesc>
d307bf
 	</parameter>
d307bf
+	<parameter name="api_version" unique="0" required="0">
d307bf
+		<getopt mixed="--api-version" />
d307bf
+		<content type="string" default="auto"  />
d307bf
+		<shortdesc lang="en">Version of RHEV API (default: auto)</shortdesc>
d307bf
+	</parameter>
d307bf
 	<parameter name="api_path" unique="0" required="0">
d307bf
 		<getopt mixed="--api-path=[path]" />
d307bf
 		<shortdesc lang="en">The path part of the API URL</shortdesc>
d307bf
@@ -164,7 +169,7 @@
d307bf
 	</parameter>
d307bf
 	<parameter name="shell_timeout" unique="0" required="0">
d307bf
 		<getopt mixed="--shell-timeout=[seconds]" />
d307bf
-		<content type="second" default="3"  />
d307bf
+		<content type="second" default="5"  />
d307bf
 		<shortdesc lang="en">Wait X seconds for cmd prompt after issuing command</shortdesc>
d307bf
 	</parameter>
d307bf
 	<parameter name="retry_on" unique="0" required="0">