mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-09 21:47:17 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -303,376 +303,368 @@ redfish_facts:
|
|||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible_collections.community.general.plugins.module_utils.redfish_utils import RedfishUtils, REDFISH_COMMON_ARGUMENT_SPEC
|
||||
from ansible_collections.community.general.plugins.module_utils.redfish_utils import (
|
||||
RedfishUtils,
|
||||
REDFISH_COMMON_ARGUMENT_SPEC,
|
||||
)
|
||||
|
||||
|
||||
class XCCRedfishUtils(RedfishUtils):
|
||||
@staticmethod
|
||||
def _find_empty_virt_media_slot(resources, media_types,
|
||||
media_match_strict=True):
|
||||
def _find_empty_virt_media_slot(resources, media_types, media_match_strict=True):
|
||||
for uri, data in resources.items():
|
||||
# check MediaTypes
|
||||
if 'MediaTypes' in data and media_types:
|
||||
if not set(media_types).intersection(set(data['MediaTypes'])):
|
||||
if "MediaTypes" in data and media_types:
|
||||
if not set(media_types).intersection(set(data["MediaTypes"])):
|
||||
continue
|
||||
else:
|
||||
if media_match_strict:
|
||||
continue
|
||||
if 'RDOC' in uri:
|
||||
if "RDOC" in uri:
|
||||
continue
|
||||
if 'Remote' in uri:
|
||||
if "Remote" in uri:
|
||||
continue
|
||||
# if ejected, 'Inserted' should be False and 'ImageName' cleared
|
||||
if (not data.get('Inserted', False) and
|
||||
not data.get('ImageName')):
|
||||
if not data.get("Inserted", False) and not data.get("ImageName"):
|
||||
return uri, data
|
||||
return None, None
|
||||
|
||||
def virtual_media_eject_one(self, image_url):
|
||||
# read the VirtualMedia resources from systems
|
||||
response = self.get_request(self.root_uri + self.systems_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
data = response['data']
|
||||
if 'VirtualMedia' not in data:
|
||||
data = response["data"]
|
||||
if "VirtualMedia" not in data:
|
||||
# read the VirtualMedia resources from manager
|
||||
response = self.get_request(self.root_uri + self.manager_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
data = response['data']
|
||||
if 'VirtualMedia' not in data:
|
||||
return {'ret': False, 'msg': "VirtualMedia resource not found"}
|
||||
data = response["data"]
|
||||
if "VirtualMedia" not in data:
|
||||
return {"ret": False, "msg": "VirtualMedia resource not found"}
|
||||
virt_media_uri = data["VirtualMedia"]["@odata.id"]
|
||||
response = self.get_request(self.root_uri + virt_media_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
data = response['data']
|
||||
data = response["data"]
|
||||
virt_media_list = []
|
||||
for member in data['Members']:
|
||||
virt_media_list.append(member['@odata.id'])
|
||||
for member in data["Members"]:
|
||||
virt_media_list.append(member["@odata.id"])
|
||||
resources, headers = self._read_virt_media_resources(virt_media_list)
|
||||
|
||||
# find the VirtualMedia resource to eject
|
||||
uri, data, eject = self._find_virt_media_to_eject(resources, image_url)
|
||||
if uri and eject:
|
||||
if ('Actions' not in data or
|
||||
'#VirtualMedia.EjectMedia' not in data['Actions']):
|
||||
if "Actions" not in data or "#VirtualMedia.EjectMedia" not in data["Actions"]:
|
||||
# try to eject via PATCH if no EjectMedia action found
|
||||
h = headers[uri]
|
||||
if 'allow' in h:
|
||||
methods = [m.strip() for m in h.get('allow').split(',')]
|
||||
if 'PATCH' not in methods:
|
||||
if "allow" in h:
|
||||
methods = [m.strip() for m in h.get("allow").split(",")]
|
||||
if "PATCH" not in methods:
|
||||
# if Allow header present and PATCH missing, return error
|
||||
return {'ret': False,
|
||||
'msg': "#VirtualMedia.EjectMedia action not found and PATCH not allowed"}
|
||||
return {"ret": False, "msg": "#VirtualMedia.EjectMedia action not found and PATCH not allowed"}
|
||||
return self.virtual_media_eject_via_patch(uri)
|
||||
else:
|
||||
# POST to the EjectMedia Action
|
||||
action = data['Actions']['#VirtualMedia.EjectMedia']
|
||||
if 'target' not in action:
|
||||
return {'ret': False,
|
||||
'msg': "target URI property missing from Action "
|
||||
"#VirtualMedia.EjectMedia"}
|
||||
action_uri = action['target']
|
||||
action = data["Actions"]["#VirtualMedia.EjectMedia"]
|
||||
if "target" not in action:
|
||||
return {"ret": False, "msg": "target URI property missing from Action #VirtualMedia.EjectMedia"}
|
||||
action_uri = action["target"]
|
||||
# empty payload for Eject action
|
||||
payload = {}
|
||||
# POST to action
|
||||
response = self.post_request(self.root_uri + action_uri,
|
||||
payload)
|
||||
if response['ret'] is False:
|
||||
response = self.post_request(self.root_uri + action_uri, payload)
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
return {'ret': True, 'changed': True,
|
||||
'msg': "VirtualMedia ejected"}
|
||||
return {"ret": True, "changed": True, "msg": "VirtualMedia ejected"}
|
||||
elif uri and not eject:
|
||||
# already ejected: return success but changed=False
|
||||
return {'ret': True, 'changed': False,
|
||||
'msg': f"VirtualMedia image '{image_url}' already ejected"}
|
||||
return {"ret": True, "changed": False, "msg": f"VirtualMedia image '{image_url}' already ejected"}
|
||||
else:
|
||||
# return failure (no resources matching image_url found)
|
||||
return {'ret': False, 'changed': False,
|
||||
'msg': f"No VirtualMedia resource found with image '{image_url}' inserted"}
|
||||
return {
|
||||
"ret": False,
|
||||
"changed": False,
|
||||
"msg": f"No VirtualMedia resource found with image '{image_url}' inserted",
|
||||
}
|
||||
|
||||
def virtual_media_eject(self, options):
|
||||
if options:
|
||||
image_url = options.get('image_url')
|
||||
image_url = options.get("image_url")
|
||||
if image_url: # eject specified one media
|
||||
return self.virtual_media_eject_one(image_url)
|
||||
|
||||
# eject all inserted media when no image_url specified
|
||||
# read the VirtualMedia resources from systems
|
||||
response = self.get_request(self.root_uri + self.systems_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
data = response['data']
|
||||
if 'VirtualMedia' not in data:
|
||||
data = response["data"]
|
||||
if "VirtualMedia" not in data:
|
||||
# read the VirtualMedia resources from manager
|
||||
response = self.get_request(self.root_uri + self.manager_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
data = response['data']
|
||||
if 'VirtualMedia' not in data:
|
||||
return {'ret': False, 'msg': "VirtualMedia resource not found"}
|
||||
data = response["data"]
|
||||
if "VirtualMedia" not in data:
|
||||
return {"ret": False, "msg": "VirtualMedia resource not found"}
|
||||
# read all the VirtualMedia resources
|
||||
virt_media_uri = data["VirtualMedia"]["@odata.id"]
|
||||
response = self.get_request(self.root_uri + virt_media_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
data = response['data']
|
||||
data = response["data"]
|
||||
virt_media_list = []
|
||||
for member in data['Members']:
|
||||
virt_media_list.append(member['@odata.id'])
|
||||
for member in data["Members"]:
|
||||
virt_media_list.append(member["@odata.id"])
|
||||
resources, headers = self._read_virt_media_resources(virt_media_list)
|
||||
|
||||
# eject all inserted media one by one
|
||||
ejected_media_list = []
|
||||
for uri, data in resources.items():
|
||||
if data.get('Image') and data.get('Inserted', True):
|
||||
returndict = self.virtual_media_eject_one(data.get('Image'))
|
||||
if not returndict['ret']:
|
||||
if data.get("Image") and data.get("Inserted", True):
|
||||
returndict = self.virtual_media_eject_one(data.get("Image"))
|
||||
if not returndict["ret"]:
|
||||
return returndict
|
||||
ejected_media_list.append(data.get('Image'))
|
||||
ejected_media_list.append(data.get("Image"))
|
||||
|
||||
if len(ejected_media_list) == 0:
|
||||
# no media inserted: return success but changed=False
|
||||
return {'ret': True, 'changed': False,
|
||||
'msg': "No VirtualMedia image inserted"}
|
||||
return {"ret": True, "changed": False, "msg": "No VirtualMedia image inserted"}
|
||||
else:
|
||||
return {'ret': True, 'changed': True,
|
||||
'msg': f"VirtualMedia {ejected_media_list!s} ejected"}
|
||||
return {"ret": True, "changed": True, "msg": f"VirtualMedia {ejected_media_list!s} ejected"}
|
||||
|
||||
def virtual_media_insert(self, options):
|
||||
param_map = {
|
||||
'Inserted': 'inserted',
|
||||
'WriteProtected': 'write_protected',
|
||||
'UserName': 'username',
|
||||
'Password': 'password',
|
||||
'TransferProtocolType': 'transfer_protocol_type',
|
||||
'TransferMethod': 'transfer_method'
|
||||
"Inserted": "inserted",
|
||||
"WriteProtected": "write_protected",
|
||||
"UserName": "username",
|
||||
"Password": "password",
|
||||
"TransferProtocolType": "transfer_protocol_type",
|
||||
"TransferMethod": "transfer_method",
|
||||
}
|
||||
image_url = options.get('image_url')
|
||||
image_url = options.get("image_url")
|
||||
if not image_url:
|
||||
return {'ret': False,
|
||||
'msg': "image_url option required for VirtualMediaInsert"}
|
||||
media_types = options.get('media_types')
|
||||
return {"ret": False, "msg": "image_url option required for VirtualMediaInsert"}
|
||||
media_types = options.get("media_types")
|
||||
|
||||
# read the VirtualMedia resources from systems
|
||||
response = self.get_request(self.root_uri + self.systems_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
data = response['data']
|
||||
if 'VirtualMedia' not in data:
|
||||
data = response["data"]
|
||||
if "VirtualMedia" not in data:
|
||||
# read the VirtualMedia resources from manager
|
||||
response = self.get_request(self.root_uri + self.manager_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
data = response['data']
|
||||
if 'VirtualMedia' not in data:
|
||||
return {'ret': False, 'msg': "VirtualMedia resource not found"}
|
||||
data = response["data"]
|
||||
if "VirtualMedia" not in data:
|
||||
return {"ret": False, "msg": "VirtualMedia resource not found"}
|
||||
virt_media_uri = data["VirtualMedia"]["@odata.id"]
|
||||
response = self.get_request(self.root_uri + virt_media_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
data = response['data']
|
||||
data = response["data"]
|
||||
virt_media_list = []
|
||||
for member in data['Members']:
|
||||
virt_media_list.append(member['@odata.id'])
|
||||
for member in data["Members"]:
|
||||
virt_media_list.append(member["@odata.id"])
|
||||
resources, headers = self._read_virt_media_resources(virt_media_list)
|
||||
|
||||
# see if image already inserted; if so, nothing to do
|
||||
if self._virt_media_image_inserted(resources, image_url):
|
||||
return {'ret': True, 'changed': False,
|
||||
'msg': f"VirtualMedia '{image_url}' already inserted"}
|
||||
return {"ret": True, "changed": False, "msg": f"VirtualMedia '{image_url}' already inserted"}
|
||||
|
||||
# find an empty slot to insert the media
|
||||
# try first with strict media_type matching
|
||||
uri, data = self._find_empty_virt_media_slot(
|
||||
resources, media_types, media_match_strict=True)
|
||||
uri, data = self._find_empty_virt_media_slot(resources, media_types, media_match_strict=True)
|
||||
if not uri:
|
||||
# if not found, try without strict media_type matching
|
||||
uri, data = self._find_empty_virt_media_slot(
|
||||
resources, media_types, media_match_strict=False)
|
||||
uri, data = self._find_empty_virt_media_slot(resources, media_types, media_match_strict=False)
|
||||
if not uri:
|
||||
return {'ret': False,
|
||||
'msg': f'Unable to find an available VirtualMedia resource {f"supporting {media_types}" if media_types else ""}'}
|
||||
return {
|
||||
"ret": False,
|
||||
"msg": f"Unable to find an available VirtualMedia resource {f'supporting {media_types}' if media_types else ''}",
|
||||
}
|
||||
|
||||
# confirm InsertMedia action found
|
||||
if ('Actions' not in data or
|
||||
'#VirtualMedia.InsertMedia' not in data['Actions']):
|
||||
if "Actions" not in data or "#VirtualMedia.InsertMedia" not in data["Actions"]:
|
||||
# try to insert via PATCH if no InsertMedia action found
|
||||
h = headers[uri]
|
||||
if 'allow' in h:
|
||||
methods = [m.strip() for m in h.get('allow').split(',')]
|
||||
if 'PATCH' not in methods:
|
||||
if "allow" in h:
|
||||
methods = [m.strip() for m in h.get("allow").split(",")]
|
||||
if "PATCH" not in methods:
|
||||
# if Allow header present and PATCH missing, return error
|
||||
return {'ret': False,
|
||||
'msg': "#VirtualMedia.InsertMedia action not found and PATCH not allowed"}
|
||||
return self.virtual_media_insert_via_patch(options, param_map,
|
||||
uri, data)
|
||||
return {"ret": False, "msg": "#VirtualMedia.InsertMedia action not found and PATCH not allowed"}
|
||||
return self.virtual_media_insert_via_patch(options, param_map, uri, data)
|
||||
|
||||
# get the action property
|
||||
action = data['Actions']['#VirtualMedia.InsertMedia']
|
||||
if 'target' not in action:
|
||||
return {'ret': False,
|
||||
'msg': "target URI missing from Action "
|
||||
"#VirtualMedia.InsertMedia"}
|
||||
action_uri = action['target']
|
||||
action = data["Actions"]["#VirtualMedia.InsertMedia"]
|
||||
if "target" not in action:
|
||||
return {"ret": False, "msg": "target URI missing from Action #VirtualMedia.InsertMedia"}
|
||||
action_uri = action["target"]
|
||||
# get ActionInfo or AllowableValues
|
||||
ai = self._get_all_action_info_values(action)
|
||||
# construct payload
|
||||
payload = self._insert_virt_media_payload(options, param_map, data, ai)
|
||||
# POST to action
|
||||
response = self.post_request(self.root_uri + action_uri, payload)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
return {'ret': True, 'changed': True, 'msg': "VirtualMedia inserted"}
|
||||
return {"ret": True, "changed": True, "msg": "VirtualMedia inserted"}
|
||||
|
||||
def raw_get_resource(self, resource_uri):
|
||||
if resource_uri is None:
|
||||
return {'ret': False, 'msg': "resource_uri is missing"}
|
||||
return {"ret": False, "msg": "resource_uri is missing"}
|
||||
response = self.get_request(self.root_uri + resource_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
data = response['data']
|
||||
return {'ret': True, 'data': data}
|
||||
data = response["data"]
|
||||
return {"ret": True, "data": data}
|
||||
|
||||
def raw_get_collection_resource(self, resource_uri):
|
||||
if resource_uri is None:
|
||||
return {'ret': False, 'msg': "resource_uri is missing"}
|
||||
return {"ret": False, "msg": "resource_uri is missing"}
|
||||
response = self.get_request(self.root_uri + resource_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
if 'Members' not in response['data']:
|
||||
return {'ret': False, 'msg': "Specified resource_uri doesn't have Members property"}
|
||||
member_list = [i['@odata.id'] for i in response['data'].get('Members', [])]
|
||||
if "Members" not in response["data"]:
|
||||
return {"ret": False, "msg": "Specified resource_uri doesn't have Members property"}
|
||||
member_list = [i["@odata.id"] for i in response["data"].get("Members", [])]
|
||||
|
||||
# get member resource one by one
|
||||
data_list = []
|
||||
for member_uri in member_list:
|
||||
uri = self.root_uri + member_uri
|
||||
response = self.get_request(uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
data = response['data']
|
||||
data = response["data"]
|
||||
data_list.append(data)
|
||||
|
||||
return {'ret': True, 'data_list': data_list}
|
||||
return {"ret": True, "data_list": data_list}
|
||||
|
||||
def raw_patch_resource(self, resource_uri, request_body):
|
||||
if resource_uri is None:
|
||||
return {'ret': False, 'msg': "resource_uri is missing"}
|
||||
return {"ret": False, "msg": "resource_uri is missing"}
|
||||
if request_body is None:
|
||||
return {'ret': False, 'msg': "request_body is missing"}
|
||||
return {"ret": False, "msg": "request_body is missing"}
|
||||
# check whether resource_uri existing or not
|
||||
response = self.get_request(self.root_uri + resource_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
original_etag = response['data']['@odata.etag']
|
||||
original_etag = response["data"]["@odata.etag"]
|
||||
|
||||
# check validity of keys in request_body
|
||||
data = response['data']
|
||||
data = response["data"]
|
||||
for key in request_body.keys():
|
||||
if key not in data:
|
||||
return {'ret': False, 'msg': f"Key {key} not found. Supported key list: {data.keys()}"}
|
||||
return {"ret": False, "msg": f"Key {key} not found. Supported key list: {data.keys()}"}
|
||||
|
||||
# perform patch
|
||||
response = self.patch_request(self.root_uri + resource_uri, request_body)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
|
||||
# check whether changed or not
|
||||
current_etag = ''
|
||||
if 'data' in response and '@odata.etag' in response['data']:
|
||||
current_etag = response['data']['@odata.etag']
|
||||
current_etag = ""
|
||||
if "data" in response and "@odata.etag" in response["data"]:
|
||||
current_etag = response["data"]["@odata.etag"]
|
||||
if current_etag != original_etag:
|
||||
return {'ret': True, 'changed': True}
|
||||
return {"ret": True, "changed": True}
|
||||
else:
|
||||
return {'ret': True, 'changed': False}
|
||||
return {"ret": True, "changed": False}
|
||||
|
||||
def raw_post_resource(self, resource_uri, request_body):
|
||||
if resource_uri is None:
|
||||
return {'ret': False, 'msg': "resource_uri is missing"}
|
||||
return {"ret": False, "msg": "resource_uri is missing"}
|
||||
resource_uri_has_actions = True
|
||||
if '/Actions/' not in resource_uri:
|
||||
if "/Actions/" not in resource_uri:
|
||||
resource_uri_has_actions = False
|
||||
if request_body is None:
|
||||
return {'ret': False, 'msg': "request_body is missing"}
|
||||
return {"ret": False, "msg": "request_body is missing"}
|
||||
# get action base uri data for further checking
|
||||
action_base_uri = resource_uri.split('/Actions/')[0]
|
||||
action_base_uri = resource_uri.split("/Actions/")[0]
|
||||
response = self.get_request(self.root_uri + action_base_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
if 'Actions' not in response['data']:
|
||||
if "Actions" not in response["data"]:
|
||||
if resource_uri_has_actions:
|
||||
return {'ret': False, 'msg': f"Actions property not found in {action_base_uri}"}
|
||||
return {"ret": False, "msg": f"Actions property not found in {action_base_uri}"}
|
||||
else:
|
||||
response['data']['Actions'] = {}
|
||||
response["data"]["Actions"] = {}
|
||||
|
||||
# check resouce_uri with target uri found in action base uri data
|
||||
action_found = False
|
||||
action_info_uri = None
|
||||
action_target_uri_list = []
|
||||
for key in response['data']['Actions'].keys():
|
||||
for key in response["data"]["Actions"].keys():
|
||||
if action_found:
|
||||
break
|
||||
if not key.startswith('#'):
|
||||
if not key.startswith("#"):
|
||||
continue
|
||||
if 'target' in response['data']['Actions'][key]:
|
||||
if resource_uri == response['data']['Actions'][key]['target']:
|
||||
if "target" in response["data"]["Actions"][key]:
|
||||
if resource_uri == response["data"]["Actions"][key]["target"]:
|
||||
action_found = True
|
||||
if '@Redfish.ActionInfo' in response['data']['Actions'][key]:
|
||||
action_info_uri = response['data']['Actions'][key]['@Redfish.ActionInfo']
|
||||
if "@Redfish.ActionInfo" in response["data"]["Actions"][key]:
|
||||
action_info_uri = response["data"]["Actions"][key]["@Redfish.ActionInfo"]
|
||||
else:
|
||||
action_target_uri_list.append(response['data']['Actions'][key]['target'])
|
||||
if not action_found and 'Oem' in response['data']['Actions']:
|
||||
for key in response['data']['Actions']['Oem'].keys():
|
||||
action_target_uri_list.append(response["data"]["Actions"][key]["target"])
|
||||
if not action_found and "Oem" in response["data"]["Actions"]:
|
||||
for key in response["data"]["Actions"]["Oem"].keys():
|
||||
if action_found:
|
||||
break
|
||||
if not key.startswith('#'):
|
||||
if not key.startswith("#"):
|
||||
continue
|
||||
if 'target' in response['data']['Actions']['Oem'][key]:
|
||||
if resource_uri == response['data']['Actions']['Oem'][key]['target']:
|
||||
if "target" in response["data"]["Actions"]["Oem"][key]:
|
||||
if resource_uri == response["data"]["Actions"]["Oem"][key]["target"]:
|
||||
action_found = True
|
||||
if '@Redfish.ActionInfo' in response['data']['Actions']['Oem'][key]:
|
||||
action_info_uri = response['data']['Actions']['Oem'][key]['@Redfish.ActionInfo']
|
||||
if "@Redfish.ActionInfo" in response["data"]["Actions"]["Oem"][key]:
|
||||
action_info_uri = response["data"]["Actions"]["Oem"][key]["@Redfish.ActionInfo"]
|
||||
else:
|
||||
action_target_uri_list.append(response['data']['Actions']['Oem'][key]['target'])
|
||||
action_target_uri_list.append(response["data"]["Actions"]["Oem"][key]["target"])
|
||||
|
||||
if not action_found and resource_uri_has_actions:
|
||||
return {'ret': False,
|
||||
'msg': (f'Specified resource_uri is not a supported action target uri, please specify a supported target uri instead. '
|
||||
f'Supported uri: {action_target_uri_list}')}
|
||||
return {
|
||||
"ret": False,
|
||||
"msg": (
|
||||
f"Specified resource_uri is not a supported action target uri, please specify a supported target uri instead. "
|
||||
f"Supported uri: {action_target_uri_list}"
|
||||
),
|
||||
}
|
||||
|
||||
# check request_body with parameter name defined by @Redfish.ActionInfo
|
||||
if action_info_uri is not None:
|
||||
response = self.get_request(self.root_uri + action_info_uri)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
for key in request_body.keys():
|
||||
key_found = False
|
||||
for para in response['data']['Parameters']:
|
||||
if key == para['Name']:
|
||||
for para in response["data"]["Parameters"]:
|
||||
if key == para["Name"]:
|
||||
key_found = True
|
||||
break
|
||||
if not key_found:
|
||||
return {'ret': False,
|
||||
'msg': (f"Invalid property {key} found in request_body. "
|
||||
f"Please refer to @Redfish.ActionInfo Parameters: {response['data']['Parameters']}")}
|
||||
return {
|
||||
"ret": False,
|
||||
"msg": (
|
||||
f"Invalid property {key} found in request_body. "
|
||||
f"Please refer to @Redfish.ActionInfo Parameters: {response['data']['Parameters']}"
|
||||
),
|
||||
}
|
||||
|
||||
# perform post
|
||||
response = self.post_request(self.root_uri + resource_uri, request_body)
|
||||
if response['ret'] is False:
|
||||
if response["ret"] is False:
|
||||
return response
|
||||
return {'ret': True, 'changed': True}
|
||||
return {"ret": True, "changed": True}
|
||||
|
||||
|
||||
# More will be added as module features are expanded
|
||||
CATEGORY_COMMANDS_ALL = {
|
||||
"Manager": ["VirtualMediaInsert",
|
||||
"VirtualMediaEject"],
|
||||
"Raw": ["GetResource",
|
||||
"GetCollectionResource",
|
||||
"PatchResource",
|
||||
"PostResource"]
|
||||
"Manager": ["VirtualMediaInsert", "VirtualMediaEject"],
|
||||
"Raw": ["GetResource", "GetCollectionResource", "PatchResource", "PostResource"],
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -680,68 +672,66 @@ def main():
|
|||
result = {}
|
||||
argument_spec = dict(
|
||||
category=dict(required=True),
|
||||
command=dict(required=True, type='list', elements='str'),
|
||||
command=dict(required=True, type="list", elements="str"),
|
||||
baseuri=dict(required=True),
|
||||
username=dict(),
|
||||
password=dict(no_log=True),
|
||||
auth_token=dict(no_log=True),
|
||||
timeout=dict(type='int', default=10),
|
||||
timeout=dict(type="int", default=10),
|
||||
resource_id=dict(),
|
||||
virtual_media=dict(
|
||||
type='dict',
|
||||
type="dict",
|
||||
options=dict(
|
||||
media_types=dict(type='list', elements='str', default=[]),
|
||||
media_types=dict(type="list", elements="str", default=[]),
|
||||
image_url=dict(),
|
||||
inserted=dict(type='bool', default=True),
|
||||
write_protected=dict(type='bool', default=True),
|
||||
inserted=dict(type="bool", default=True),
|
||||
write_protected=dict(type="bool", default=True),
|
||||
username=dict(),
|
||||
password=dict(no_log=True),
|
||||
transfer_protocol_type=dict(),
|
||||
transfer_method=dict(),
|
||||
)
|
||||
),
|
||||
),
|
||||
resource_uri=dict(),
|
||||
request_body=dict(
|
||||
type='dict',
|
||||
type="dict",
|
||||
),
|
||||
)
|
||||
argument_spec.update(REDFISH_COMMON_ARGUMENT_SPEC)
|
||||
module = AnsibleModule(
|
||||
argument_spec,
|
||||
required_together=[
|
||||
('username', 'password'),
|
||||
("username", "password"),
|
||||
],
|
||||
required_one_of=[
|
||||
('username', 'auth_token'),
|
||||
("username", "auth_token"),
|
||||
],
|
||||
mutually_exclusive=[
|
||||
('username', 'auth_token'),
|
||||
("username", "auth_token"),
|
||||
],
|
||||
supports_check_mode=False
|
||||
supports_check_mode=False,
|
||||
)
|
||||
|
||||
category = module.params['category']
|
||||
command_list = module.params['command']
|
||||
category = module.params["category"]
|
||||
command_list = module.params["command"]
|
||||
|
||||
# admin credentials used for authentication
|
||||
creds = {'user': module.params['username'],
|
||||
'pswd': module.params['password'],
|
||||
'token': module.params['auth_token']}
|
||||
creds = {"user": module.params["username"], "pswd": module.params["password"], "token": module.params["auth_token"]}
|
||||
|
||||
# timeout
|
||||
timeout = module.params['timeout']
|
||||
timeout = module.params["timeout"]
|
||||
|
||||
# System, Manager or Chassis ID to modify
|
||||
resource_id = module.params['resource_id']
|
||||
resource_id = module.params["resource_id"]
|
||||
|
||||
# VirtualMedia options
|
||||
virtual_media = module.params['virtual_media']
|
||||
virtual_media = module.params["virtual_media"]
|
||||
|
||||
# resource_uri
|
||||
resource_uri = module.params['resource_uri']
|
||||
resource_uri = module.params["resource_uri"]
|
||||
|
||||
# request_body
|
||||
request_body = module.params['request_body']
|
||||
request_body = module.params["request_body"]
|
||||
|
||||
# Build root URI
|
||||
root_uri = f"https://{module.params['baseuri']}"
|
||||
|
|
@ -749,52 +739,56 @@ def main():
|
|||
|
||||
# Check that Category is valid
|
||||
if category not in CATEGORY_COMMANDS_ALL:
|
||||
module.fail_json(msg=to_native(f"Invalid Category '{category}'. Valid Categories = {CATEGORY_COMMANDS_ALL.keys()}"))
|
||||
module.fail_json(
|
||||
msg=to_native(f"Invalid Category '{category}'. Valid Categories = {CATEGORY_COMMANDS_ALL.keys()}")
|
||||
)
|
||||
|
||||
# Check that all commands are valid
|
||||
for cmd in command_list:
|
||||
# Fail if even one command given is invalid
|
||||
if cmd not in CATEGORY_COMMANDS_ALL[category]:
|
||||
module.fail_json(msg=to_native(f"Invalid Command '{cmd}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}"))
|
||||
module.fail_json(
|
||||
msg=to_native(f"Invalid Command '{cmd}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}")
|
||||
)
|
||||
|
||||
# Organize by Categories / Commands
|
||||
if category == "Manager":
|
||||
# For virtual media resource locates on Systems service
|
||||
result = rf_utils._find_systems_resource()
|
||||
if result['ret'] is False:
|
||||
module.fail_json(msg=to_native(result['msg']))
|
||||
if result["ret"] is False:
|
||||
module.fail_json(msg=to_native(result["msg"]))
|
||||
# For virtual media resource locates on Managers service
|
||||
result = rf_utils._find_managers_resource()
|
||||
if result['ret'] is False:
|
||||
module.fail_json(msg=to_native(result['msg']))
|
||||
if result["ret"] is False:
|
||||
module.fail_json(msg=to_native(result["msg"]))
|
||||
|
||||
for command in command_list:
|
||||
if command == 'VirtualMediaInsert':
|
||||
if command == "VirtualMediaInsert":
|
||||
result = rf_utils.virtual_media_insert(virtual_media)
|
||||
elif command == 'VirtualMediaEject':
|
||||
elif command == "VirtualMediaEject":
|
||||
result = rf_utils.virtual_media_eject(virtual_media)
|
||||
elif category == "Raw":
|
||||
for command in command_list:
|
||||
if command == 'GetResource':
|
||||
if command == "GetResource":
|
||||
result = rf_utils.raw_get_resource(resource_uri)
|
||||
elif command == 'GetCollectionResource':
|
||||
elif command == "GetCollectionResource":
|
||||
result = rf_utils.raw_get_collection_resource(resource_uri)
|
||||
elif command == 'PatchResource':
|
||||
elif command == "PatchResource":
|
||||
result = rf_utils.raw_patch_resource(resource_uri, request_body)
|
||||
elif command == 'PostResource':
|
||||
elif command == "PostResource":
|
||||
result = rf_utils.raw_post_resource(resource_uri, request_body)
|
||||
|
||||
# Return data back or fail with proper message
|
||||
if result['ret'] is True:
|
||||
if command == 'GetResource' or command == 'GetCollectionResource':
|
||||
if result["ret"] is True:
|
||||
if command == "GetResource" or command == "GetCollectionResource":
|
||||
module.exit_json(redfish_facts=result)
|
||||
else:
|
||||
changed = result.get('changed', True)
|
||||
msg = result.get('msg', 'Action was successful')
|
||||
changed = result.get("changed", True)
|
||||
msg = result.get("msg", "Action was successful")
|
||||
module.exit_json(changed=changed, msg=msg)
|
||||
else:
|
||||
module.fail_json(msg=to_native(result['msg']))
|
||||
module.fail_json(msg=to_native(result["msg"]))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue