Blob Blame History Raw
From 75a74debba2205547d8eefae221221c2c71d99ce Mon Sep 17 00:00:00 2001
From: Jose Delarosa <jose.delarosa@dell.com>
Date: Mon, 15 Apr 2019 12:46:42 -0500
Subject: [PATCH] fence_redfish: add headers to HTTP methods

* Needed for full compliance with Redfish spec.
* May cause errors in some devices if not sent.
---
 agents/redfish/fence_redfish.py | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/agents/redfish/fence_redfish.py b/agents/redfish/fence_redfish.py
index f1424232..390a4827 100644
--- a/agents/redfish/fence_redfish.py
+++ b/agents/redfish/fence_redfish.py
@@ -16,6 +16,11 @@
 from fencing import *
 from fencing import fail_usage, run_delay
 
+GET_HEADERS = {'accept': 'application/json', 'OData-Version': '4.0'}
+POST_HEADERS = {'content-type': 'application/json', 'accept': 'application/json',
+                'OData-Version': '4.0'}
+
+
 def get_power_status(conn, options):
     response = send_get_request(options, options["--systems-uri"])
     if response['ret'] is False:
@@ -40,7 +45,6 @@ def set_power_status(conn, options):
     }[options["--action"]]
 
     payload = {'ResetType': action}
-    headers = {'content-type': 'application/json'}
 
     # Search for 'Actions' key and extract URI from it
     response = send_get_request(options, options["--systems-uri"])
@@ -49,7 +53,7 @@ def set_power_status(conn, options):
     data = response['data']
     action_uri = data["Actions"]["#ComputerSystem.Reset"]["target"]
 
-    response = send_post_request(options, action_uri, payload, headers)
+    response = send_post_request(options, action_uri, payload)
     if response['ret'] is False:
         fail_usage("Error sending power command")
     return
@@ -58,17 +62,18 @@ def send_get_request(options, uri):
     full_uri = "https://" + options["--ip"] + ":" + str(options["--ipport"]) + uri
     try:
         resp = requests.get(full_uri, verify=not "--ssl-insecure" in options,
+                            headers=GET_HEADERS,
                             auth=(options["--username"], options["--password"]))
         data = resp.json()
     except Exception as e:
         fail_usage("Failed: send_get_request: " + str(e))
     return {'ret': True, 'data': data}
 
-def send_post_request(options, uri, payload, headers):
+def send_post_request(options, uri, payload):
     full_uri = "https://" + options["--ip"] + ":" + str(options["--ipport"]) + uri
     try:
         requests.post(full_uri, data=json.dumps(payload),
-                      headers=headers, verify=not "--ssl-insecure" in options,
+                      headers=POST_HEADERS, verify=not "--ssl-insecure" in options,
                       auth=(options["--username"], options["--password"]))
     except Exception as e:
         fail_usage("Failed: send_post_request: " + str(e))