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

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