Skip to content

Commit

Permalink
Merge pull request #121 from PeterKchen2/if_match_option_support_2
Browse files Browse the repository at this point in the history
rf_raw_request.py PATCH/PUT method should set if-match header if Etag exist
  • Loading branch information
mraineri authored Aug 2, 2023
2 parents 6e181de + 6ea6f05 commit d655261
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions scripts/rf_raw_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import json
import os
import redfish
import re

# Get the input arguments
argget = argparse.ArgumentParser( description = "A tool perform a raw request to a Redfish service" )
Expand All @@ -27,6 +28,17 @@
argget.add_argument( "--verbose", "-v", action = "store_true", help = "Indicates if HTTP response codes and headers are displayed", default = False )
args = argget.parse_args()

def ifmatch_header(redfish_obj, path, headers=None):
if headers is None:
headers = {}
try:
response = redfish_obj.get(path)
etag = response.getheader( "ETag" )
if etag is not None:
headers[ "If-Match"] = etag
except Exception:
pass
return headers
# Connect to the service
with redfish.redfish_client( base_url = args.rhost, username = args.user, password = args.password ) as redfish_obj:
# Encode the body
Expand All @@ -50,9 +62,11 @@
elif args.method == "POST":
resp = redfish_obj.post( args.request, body = body )
elif args.method == "PATCH":
resp = redfish_obj.patch( args.request, body = body )
headers = ifmatch_header(redfish_obj, args.request, headers = headers)
resp = redfish_obj.patch( args.request, body = body, headers = headers)
elif args.method == "PUT":
resp = redfish_obj.put( args.request, body = body )
headers = ifmatch_header(redfish_obj, args.request, headers = headers)
resp = redfish_obj.put( args.request, body = body, headers = headers)
elif args.method == "DELETE":
resp = redfish_obj.delete( args.request )
else:
Expand Down

0 comments on commit d655261

Please sign in to comment.