Reformat everything.

This commit is contained in:
Felix Fontein
2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View File

@@ -155,20 +155,18 @@ msg:
"""
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.validation import (
check_mutually_exclusive,
check_required_arguments
from ansible.module_utils.common.validation import check_mutually_exclusive, check_required_arguments
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
from ansible.module_utils.common.text.converters import to_native
class IdracRedfishUtils(RedfishUtils):
def set_manager_attributes(self, command):
result = {}
required_arg_spec = {'manager_attributes': {'required': True}}
required_arg_spec = {"manager_attributes": {"required": True}}
try:
check_required_arguments(required_arg_spec, self.module.params)
@@ -181,11 +179,11 @@ class IdracRedfishUtils(RedfishUtils):
command_manager_attributes_uri_map = {
"SetManagerAttributes": self.manager_uri,
"SetLifecycleControllerAttributes": "/redfish/v1/Managers/LifecycleController.Embedded.1",
"SetSystemAttributes": "/redfish/v1/Managers/System.Embedded.1"
"SetSystemAttributes": "/redfish/v1/Managers/System.Embedded.1",
}
manager_uri = command_manager_attributes_uri_map.get(command, self.manager_uri)
attributes = self.module.params['manager_attributes']
attributes = self.module.params["manager_attributes"]
attrs_to_patch = {}
attrs_skipped = {}
@@ -193,26 +191,24 @@ class IdracRedfishUtils(RedfishUtils):
# Search for key entry and extract URI from it
response = self.get_request(f"{self.root_uri}{manager_uri}/{key}")
if response['ret'] is False:
if response["ret"] is False:
return response
result['ret'] = True
data = response['data']
result["ret"] = True
data = response["data"]
if key not in data:
return {'ret': False,
'msg': f"{command}: Key {key} not found",
'warning': ""}
return {"ret": False, "msg": f"{command}: Key {key} not found", "warning": ""}
for attr_name, attr_value in attributes.items():
# Check if attribute exists
if attr_name not in data['Attributes']:
if attr_name not in data["Attributes"]:
# Skip and proceed to next attribute if this isn't valid
attrs_bad.update({attr_name: attr_value})
continue
# Find out if value is already set to what we want. If yes, exclude
# those attributes
if data['Attributes'][attr_name] == attr_value:
if data["Attributes"][attr_name] == attr_value:
attrs_skipped.update({attr_name: attr_value})
else:
attrs_to_patch.update({attr_name: attr_value})
@@ -222,30 +218,32 @@ class IdracRedfishUtils(RedfishUtils):
warning = f"Incorrect attributes {attrs_bad}"
if not attrs_to_patch:
return {'ret': True, 'changed': False,
'msg': "No changes made. Manager attributes already set.",
'warning': warning}
return {
"ret": True,
"changed": False,
"msg": "No changes made. Manager attributes already set.",
"warning": warning,
}
payload = {"Attributes": attrs_to_patch}
response = self.patch_request(f"{self.root_uri}{manager_uri}/{key}", payload)
if response['ret'] is False:
if response["ret"] is False:
return response
return {'ret': True, 'changed': True,
'msg': f"{command}: Modified Manager attributes {attrs_to_patch}",
'warning': warning}
return {
"ret": True,
"changed": True,
"msg": f"{command}: Modified Manager attributes {attrs_to_patch}",
"warning": warning,
}
CATEGORY_COMMANDS_ALL = {
"Manager": ["SetManagerAttributes", "SetLifecycleControllerAttributes",
"SetSystemAttributes"]
}
CATEGORY_COMMANDS_ALL = {"Manager": ["SetManagerAttributes", "SetLifecycleControllerAttributes", "SetSystemAttributes"]}
# list of mutually exclusive commands for a category
CATEGORY_COMMANDS_MUTUALLY_EXCLUSIVE = {
"Manager": [["SetManagerAttributes", "SetLifecycleControllerAttributes",
"SetSystemAttributes"]]
"Manager": [["SetManagerAttributes", "SetLifecycleControllerAttributes", "SetSystemAttributes"]]
}
@@ -253,66 +251,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),
manager_attributes=dict(type='dict', default={}),
timeout=dict(type='int', default=10),
resource_id=dict()
manager_attributes=dict(type="dict", default={}),
timeout=dict(type="int", default=10),
resource_id=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"]
# Build root URI
root_uri = f"https://{module.params['baseuri']}"
rf_utils = IdracRedfishUtils(creds, root_uri, timeout, module,
resource_id=resource_id, data_modification=True)
rf_utils = IdracRedfishUtils(creds, root_uri, timeout, module, resource_id=resource_id, data_modification=True)
# Check that Category is valid
if category not in CATEGORY_COMMANDS_ALL:
module.fail_json(msg=to_native(f"Invalid Category '{category}'. Valid Categories = {list(CATEGORY_COMMANDS_ALL.keys())}"))
module.fail_json(
msg=to_native(f"Invalid Category '{category}'. Valid Categories = {list(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]}")
)
# check for mutually exclusive commands
try:
# check_mutually_exclusive accepts a single list or list of lists that
# are groups of terms that should be mutually exclusive with one another
# and checks that against a dictionary
check_mutually_exclusive(CATEGORY_COMMANDS_MUTUALLY_EXCLUSIVE[category],
dict.fromkeys(command_list, True))
check_mutually_exclusive(CATEGORY_COMMANDS_MUTUALLY_EXCLUSIVE[category], dict.fromkeys(command_list, True))
except TypeError as e:
module.fail_json(msg=to_native(e))
@@ -322,22 +320,22 @@ def main():
if category == "Manager":
# execute only if we find a Manager resource
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 in ["SetManagerAttributes", "SetLifecycleControllerAttributes", "SetSystemAttributes"]:
result = rf_utils.set_manager_attributes(command)
# Return data back or fail with proper message
if result['ret'] is True:
if result.get('warning'):
module.warn(to_native(result['warning']))
if result["ret"] is True:
if result.get("warning"):
module.warn(to_native(result["warning"]))
module.exit_json(changed=result['changed'], msg=to_native(result['msg']))
module.exit_json(changed=result["changed"], msg=to_native(result["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()