mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Use different headers for each HTTP method (#55193)
This commit is contained in:
parent
4190985995
commit
f88b2cbe59
4 changed files with 31 additions and 27 deletions
|
@ -10,7 +10,12 @@ from ansible.module_utils._text import to_text
|
|||
from ansible.module_utils.six.moves import http_client
|
||||
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
|
||||
|
||||
HEADERS = {'content-type': 'application/json'}
|
||||
GET_HEADERS = {'accept': 'application/json', 'OData-Version': '4.0'}
|
||||
POST_HEADERS = {'content-type': 'application/json', 'accept': 'application/json',
|
||||
'OData-Version': '4.0'}
|
||||
PATCH_HEADERS = {'content-type': 'application/json', 'accept': 'application/json',
|
||||
'OData-Version': '4.0'}
|
||||
DELETE_HEADERS = {'accept': 'application/json', 'OData-Version': '4.0'}
|
||||
|
||||
|
||||
class RedfishUtils(object):
|
||||
|
@ -25,7 +30,7 @@ class RedfishUtils(object):
|
|||
# The following functions are to send GET/POST/PATCH/DELETE requests
|
||||
def get_request(self, uri):
|
||||
try:
|
||||
resp = open_url(uri, method="GET",
|
||||
resp = open_url(uri, method="GET", headers=GET_HEADERS,
|
||||
url_username=self.creds['user'],
|
||||
url_password=self.creds['pswd'],
|
||||
force_basic_auth=True, validate_certs=False,
|
||||
|
@ -46,10 +51,10 @@ class RedfishUtils(object):
|
|||
'msg': "Failed GET request to '%s': '%s'" % (uri, to_text(e))}
|
||||
return {'ret': True, 'data': data}
|
||||
|
||||
def post_request(self, uri, pyld, hdrs):
|
||||
def post_request(self, uri, pyld):
|
||||
try:
|
||||
resp = open_url(uri, data=json.dumps(pyld),
|
||||
headers=hdrs, method="POST",
|
||||
headers=POST_HEADERS, method="POST",
|
||||
url_username=self.creds['user'],
|
||||
url_password=self.creds['pswd'],
|
||||
force_basic_auth=True, validate_certs=False,
|
||||
|
@ -69,10 +74,10 @@ class RedfishUtils(object):
|
|||
'msg': "Failed POST request to '%s': '%s'" % (uri, to_text(e))}
|
||||
return {'ret': True, 'resp': resp}
|
||||
|
||||
def patch_request(self, uri, pyld, hdrs):
|
||||
def patch_request(self, uri, pyld):
|
||||
try:
|
||||
resp = open_url(uri, data=json.dumps(pyld),
|
||||
headers=hdrs, method="PATCH",
|
||||
headers=PATCH_HEADERS, method="PATCH",
|
||||
url_username=self.creds['user'],
|
||||
url_password=self.creds['pswd'],
|
||||
force_basic_auth=True, validate_certs=False,
|
||||
|
@ -92,10 +97,10 @@ class RedfishUtils(object):
|
|||
'msg': "Failed PATCH request to '%s': '%s'" % (uri, to_text(e))}
|
||||
return {'ret': True, 'resp': resp}
|
||||
|
||||
def delete_request(self, uri, pyld, hdrs):
|
||||
def delete_request(self, uri, pyld):
|
||||
try:
|
||||
resp = open_url(uri, data=json.dumps(pyld),
|
||||
headers=hdrs, method="DELETE",
|
||||
headers=DELETE_HEADERS, method="DELETE",
|
||||
url_username=self.creds['user'],
|
||||
url_password=self.creds['pswd'],
|
||||
force_basic_auth=True, validate_certs=False,
|
||||
|
@ -302,7 +307,7 @@ class RedfishUtils(object):
|
|||
# Check to make sure option is available, otherwise error is ugly
|
||||
if "Actions" in _data:
|
||||
if "#LogService.ClearLog" in _data[u"Actions"]:
|
||||
self.post_request(self.root_uri + _data[u"Actions"]["#LogService.ClearLog"]["target"], {}, HEADERS)
|
||||
self.post_request(self.root_uri + _data[u"Actions"]["#LogService.ClearLog"]["target"], {})
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
return {'ret': True}
|
||||
|
@ -468,7 +473,7 @@ class RedfishUtils(object):
|
|||
action_uri = data[key]["#Manager.Reset"]["target"]
|
||||
|
||||
payload = {'ResetType': 'GracefulRestart'}
|
||||
response = self.post_request(self.root_uri + action_uri, payload, HEADERS)
|
||||
response = self.post_request(self.root_uri + action_uri, payload)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
return {'ret': True}
|
||||
|
@ -491,7 +496,7 @@ class RedfishUtils(object):
|
|||
|
||||
if command in payloads.keys():
|
||||
payload = {'IndicatorLED': payloads[command]}
|
||||
response = self.patch_request(self.root_uri + chassis_uri, payload, HEADERS)
|
||||
response = self.patch_request(self.root_uri + chassis_uri, payload)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
else:
|
||||
|
@ -534,7 +539,7 @@ class RedfishUtils(object):
|
|||
else:
|
||||
return {'ret': False, 'msg': 'Invalid Command'}
|
||||
|
||||
response = self.post_request(self.root_uri + action_uri, payload, HEADERS)
|
||||
response = self.post_request(self.root_uri + action_uri, payload)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
result['ret'] = True
|
||||
|
@ -580,7 +585,7 @@ class RedfishUtils(object):
|
|||
roleid = {'RoleId': user['userrole']}
|
||||
enabled = {'Enabled': True}
|
||||
for payload in username, pswd, roleid, enabled:
|
||||
response = self.patch_request(uri, payload, HEADERS)
|
||||
response = self.patch_request(uri, payload)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
return {'ret': True}
|
||||
|
@ -588,7 +593,7 @@ class RedfishUtils(object):
|
|||
def enable_user(self, user):
|
||||
uri = self.root_uri + self.accounts_uri + "/" + user['userid']
|
||||
payload = {'Enabled': True}
|
||||
response = self.patch_request(uri, payload, HEADERS)
|
||||
response = self.patch_request(uri, payload)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
return {'ret': True}
|
||||
|
@ -596,7 +601,7 @@ class RedfishUtils(object):
|
|||
def delete_user(self, user):
|
||||
uri = self.root_uri + self.accounts_uri + "/" + user['userid']
|
||||
payload = {'UserName': ""}
|
||||
response = self.patch_request(uri, payload, HEADERS)
|
||||
response = self.patch_request(uri, payload)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
return {'ret': True}
|
||||
|
@ -604,7 +609,7 @@ class RedfishUtils(object):
|
|||
def disable_user(self, user):
|
||||
uri = self.root_uri + self.accounts_uri + "/" + user['userid']
|
||||
payload = {'Enabled': False}
|
||||
response = self.patch_request(uri, payload, HEADERS)
|
||||
response = self.patch_request(uri, payload)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
return {'ret': True}
|
||||
|
@ -612,7 +617,7 @@ class RedfishUtils(object):
|
|||
def update_user_role(self, user):
|
||||
uri = self.root_uri + self.accounts_uri + "/" + user['userid']
|
||||
payload = {'RoleId': user['userrole']}
|
||||
response = self.patch_request(uri, payload, HEADERS)
|
||||
response = self.patch_request(uri, payload)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
return {'ret': True}
|
||||
|
@ -620,7 +625,7 @@ class RedfishUtils(object):
|
|||
def update_user_password(self, user):
|
||||
uri = self.root_uri + self.accounts_uri + "/" + user['userid']
|
||||
payload = {'Password': user['userpswd']}
|
||||
response = self.patch_request(uri, payload, HEADERS)
|
||||
response = self.patch_request(uri, payload)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
return {'ret': True}
|
||||
|
@ -778,7 +783,7 @@ class RedfishUtils(object):
|
|||
data = response['data']
|
||||
reset_bios_settings_uri = data["Actions"]["#Bios.ResetBios"]["target"]
|
||||
|
||||
response = self.post_request(self.root_uri + reset_bios_settings_uri, {}, HEADERS)
|
||||
response = self.post_request(self.root_uri + reset_bios_settings_uri, {})
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
return {'ret': True, 'changed': True, 'msg': "Set BIOS to default settings"}
|
||||
|
@ -810,7 +815,7 @@ class RedfishUtils(object):
|
|||
else:
|
||||
payload = {"Boot": {"BootSourceOverrideTarget": bootdevice}}
|
||||
|
||||
response = self.patch_request(self.root_uri + self.systems_uris[0], payload, HEADERS)
|
||||
response = self.patch_request(self.root_uri + self.systems_uris[0], payload)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
return {'ret': True}
|
||||
|
@ -851,7 +856,7 @@ class RedfishUtils(object):
|
|||
# Example: bios_attr = {\"name\":\"value\"}
|
||||
bios_attr = "{\"" + attr['bios_attr_name'] + "\":\"" + attr['bios_attr_value'] + "\"}"
|
||||
payload = {"Attributes": json.loads(bios_attr)}
|
||||
response = self.patch_request(self.root_uri + set_bios_attr_uri, payload, HEADERS)
|
||||
response = self.patch_request(self.root_uri + set_bios_attr_uri, payload)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
return {'ret': True, 'changed': True, 'msg': "Modified BIOS attribute"}
|
||||
|
|
|
@ -71,7 +71,7 @@ msg:
|
|||
|
||||
import re
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.redfish_utils import RedfishUtils, HEADERS
|
||||
from ansible.module_utils.redfish_utils import RedfishUtils
|
||||
from ansible.module_utils._text import to_native
|
||||
|
||||
|
||||
|
@ -105,8 +105,7 @@ class IdracRedfishUtils(RedfishUtils):
|
|||
|
||||
payload = {"TargetSettingsURI": set_bios_attr_uri}
|
||||
response = self.post_request(
|
||||
self.root_uri + self.manager_uri + "/" + jobs,
|
||||
payload, HEADERS)
|
||||
self.root_uri + self.manager_uri + "/" + jobs, payload)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
|
||||
|
|
|
@ -102,7 +102,7 @@ msg:
|
|||
import json
|
||||
import re
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.redfish_utils import RedfishUtils, HEADERS
|
||||
from ansible.module_utils.redfish_utils import RedfishUtils
|
||||
from ansible.module_utils._text import to_native
|
||||
|
||||
|
||||
|
@ -141,7 +141,7 @@ class IdracRedfishUtils(RedfishUtils):
|
|||
return {'ret': True, 'changed': False, 'msg': "Manager attribute already set"}
|
||||
|
||||
payload = {"Attributes": json.loads(manager_attr)}
|
||||
response = self.patch_request(self.root_uri + self.manager_uri + "/" + key, payload, HEADERS)
|
||||
response = self.patch_request(self.root_uri + self.manager_uri + "/" + key, payload)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
return {'ret': True, 'changed': True, 'msg': "Modified Manager attribute %s" % attr['mgr_attr_name']}
|
||||
|
|
|
@ -71,7 +71,7 @@ msg:
|
|||
|
||||
import re
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.redfish_utils import RedfishUtils, HEADERS
|
||||
from ansible.module_utils.redfish_utils import RedfishUtils
|
||||
from ansible.module_utils._text import to_native
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue