From f35c8b2c18db9a42b80a63f635ea32ea1147b53a Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Fri, 24 Feb 2023 10:57:43 +0100 Subject: [PATCH] lint / black --- plugins/modules/mq_broker.py | 295 ++++++++++++---------------- plugins/modules/mq_broker_config.py | 139 ++++++------- plugins/modules/mq_broker_info.py | 64 +++--- plugins/modules/mq_user.py | 157 +++++++-------- plugins/modules/mq_user_info.py | 70 +++---- 5 files changed, 322 insertions(+), 403 deletions(-) diff --git a/plugins/modules/mq_broker.py b/plugins/modules/mq_broker.py index cdb713f6e36..7bbdbdb1ff2 100644 --- a/plugins/modules/mq_broker.py +++ b/plugins/modules/mq_broker.py @@ -2,11 +2,7 @@ # # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - - -DOCUMENTATION = ''' +DOCUMENTATION = r""" --- module: mq_broker version_added: 6.0.0 @@ -131,10 +127,10 @@ - amazon.aws.boto3 - amazon.aws.common.modules - amazon.aws.region.modules -''' +""" -EXAMPLES = ''' +EXAMPLES = r""" - name: create broker (if missing) with minimal required parameters amazon.aws.mq_broker: broker_name: "{{ broker_name }}" @@ -193,9 +189,9 @@ amazon.aws.mq_broker: broker_name: "my_broker_2" state: absent -''' +""" -RETURN = ''' +RETURN = r""" broker: description: - "All API responses are converted to snake yaml except 'Tags'" @@ -204,71 +200,71 @@ - "'state=restarted': result of describe_broker() after reboot has been triggered" type: dict returned: success -''' +""" try: import botocore -except ImportError as ex: +except ImportError: # handled by AnsibleAWSModule pass -from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict +from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule PARAMS_MAP = { - 'authentication_strategy': 'AuthenticationStrategy', - 'auto_minor_version_upgrade': 'AutoMinorVersionUpgrade', - 'broker_name': 'BrokerName', - 'deployment_mode': 'DeploymentMode', - 'use_aws_owned_key': 'EncryptionOptions/UseAwsOwnedKey', - 'kms_key_id': 'EncryptionOptions/KmsKeyId', - 'engine_type': 'EngineType', - 'engine_version': 'EngineVersion', - 'host_instance_type': 'HostInstanceType', - 'enable_audit_log': 'Logs/Audit', - 'enable_general_log': 'Logs/General', - 'maintenance_window_start_time': 'MaintenanceWindowStartTime', - 'publicly_accessible': 'PubliclyAccessible', - 'security_groups': 'SecurityGroups', - 'storage_type': 'StorageType', - 'subnet_ids': 'SubnetIds', - 'users': 'Users' + "authentication_strategy": "AuthenticationStrategy", + "auto_minor_version_upgrade": "AutoMinorVersionUpgrade", + "broker_name": "BrokerName", + "deployment_mode": "DeploymentMode", + "use_aws_owned_key": "EncryptionOptions/UseAwsOwnedKey", + "kms_key_id": "EncryptionOptions/KmsKeyId", + "engine_type": "EngineType", + "engine_version": "EngineVersion", + "host_instance_type": "HostInstanceType", + "enable_audit_log": "Logs/Audit", + "enable_general_log": "Logs/General", + "maintenance_window_start_time": "MaintenanceWindowStartTime", + "publicly_accessible": "PubliclyAccessible", + "security_groups": "SecurityGroups", + "storage_type": "StorageType", + "subnet_ids": "SubnetIds", + "users": "Users", } DEFAULTS = { - 'authentication_strategy': 'SIMPLE', - 'auto_minor_version_upgrade': False, - 'deployment_mode': 'SINGLE_INSTANCE', - 'use_aws_owned_key': True, - 'engine_type': 'ACTIVEMQ', - 'engine_version': 'latest', - 'host_instance_type': 'mq.t3.micro', - 'enable_audit_log': False, - 'enable_general_log': False, - 'publicly_accessible': False, - 'storage_type': 'EFS' + "authentication_strategy": "SIMPLE", + "auto_minor_version_upgrade": False, + "deployment_mode": "SINGLE_INSTANCE", + "use_aws_owned_key": True, + "engine_type": "ACTIVEMQ", + "engine_version": "latest", + "host_instance_type": "mq.t3.micro", + "enable_audit_log": False, + "enable_general_log": False, + "publicly_accessible": False, + "storage_type": "EFS", } CREATE_ONLY_PARAMS = [ - 'deployment_mode', - 'use_aws_owned_key', - 'kms_key_id', - 'engine_type', - 'maintenance_window_start_time', - 'publicly_accessible', - 'storage_type', - 'subnet_ids', - 'users', - 'tags' + "deployment_mode", + "use_aws_owned_key", + "kms_key_id", + "engine_type", + "maintenance_window_start_time", + "publicly_accessible", + "storage_type", + "subnet_ids", + "users", + "tags", ] def _set_kwarg(kwargs, key, value): mapped_key = PARAMS_MAP[key] - if '/' in mapped_key: - key_list = mapped_key.split('/') + if "/" in mapped_key: + key_list = mapped_key.split("/") key_list.reverse() else: key_list = [mapped_key] @@ -285,8 +281,8 @@ def _set_kwarg(kwargs, key, value): def _fill_kwargs(module, apply_defaults=True, ignore_create_params=False): kwargs = {} if apply_defaults: - for p_name in DEFAULTS: - _set_kwarg(kwargs, p_name, DEFAULTS[p_name]) + for p_name, p_value in DEFAULTS.items(): + _set_kwarg(kwargs, p_name, p_value) for p_name in module.params: if ignore_create_params and p_name in CREATE_ONLY_PARAMS: # silently ignore CREATE_ONLY_PARAMS on update to @@ -353,22 +349,20 @@ def _needs_change(current, desired): def get_latest_engine_version(conn, module, engine_type): try: - response = conn.describe_broker_engine_types( - EngineType=engine_type - ) - return response['BrokerEngineTypes'][0]['EngineVersions'][0]['Name'] + response = conn.describe_broker_engine_types(EngineType=engine_type) + return response["BrokerEngineTypes"][0]["EngineVersions"][0]["Name"] except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Couldn't list engine versions") def get_broker_id(conn, module): try: - broker_name = module.params['broker_name'] + broker_name = module.params["broker_name"] broker_id = None response = conn.list_brokers(MaxResults=100) - for broker in response['BrokerSummaries']: - if broker['BrokerName'] == broker_name: - broker_id = broker['BrokerId'] + for broker in response["BrokerSummaries"]: + if broker["BrokerName"] == broker_name: + broker_id = broker["BrokerId"] break return broker_id except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: @@ -384,74 +378,56 @@ def get_broker_info(conn, module, broker_id): def reboot_broker(conn, module, broker_id): try: - return conn.reboot_broker( - BrokerId=broker_id - ) + return conn.reboot_broker(BrokerId=broker_id) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Couldn't reboot broker.") def delete_broker(conn, module, broker_id): try: - return conn.delete_broker( - BrokerId=broker_id - ) + return conn.delete_broker(BrokerId=broker_id) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Couldn't delete broker.") def create_broker(conn, module): kwargs = _fill_kwargs(module) - if 'EngineVersion' in kwargs and kwargs['EngineVersion'] == 'latest': - kwargs['EngineVersion'] = get_latest_engine_version( - conn, module, kwargs['EngineType'] - ) - if kwargs['AuthenticationStrategy'] == 'LDAP': - module.fail_json_aws(RuntimeError, msg="'AuthenticationStrategy=LDAP' not supported, yet") - if 'Users' not in kwargs: + if "EngineVersion" in kwargs and kwargs["EngineVersion"] == "latest": + kwargs["EngineVersion"] = get_latest_engine_version(conn, module, kwargs["EngineType"]) + if kwargs["AuthenticationStrategy"] == "LDAP": + module.fail_json(msg="'AuthenticationStrategy=LDAP' not supported, yet") + if "Users" not in kwargs: # add some stupid default (cannot create broker without any users) - kwargs['Users'] = [ - { - 'Username': 'admin', - 'Password': 'adminPassword', - 'ConsoleAccess': True, - 'Groups': [] - } - ] - if 'EncryptionOptions' in kwargs and 'UseAwsOwnedKey' in kwargs['EncryptionOptions']: - kwargs['EncryptionOptions']['UseAwsOwnedKey'] = False + kwargs["Users"] = [{"Username": "admin", "Password": "adminPassword", "ConsoleAccess": True, "Groups": []}] + if "EncryptionOptions" in kwargs and "UseAwsOwnedKey" in kwargs["EncryptionOptions"]: + kwargs["EncryptionOptions"]["UseAwsOwnedKey"] = False # - if 'SecurityGroups' not in kwargs or len(kwargs['SecurityGroups']) == 0: - module.fail_json_aws(RuntimeError, msg="At least one security group must be specified on broker creation") + if "SecurityGroups" not in kwargs or len(kwargs["SecurityGroups"]) == 0: + module.fail_json(msg="At least one security group must be specified on broker creation") # changed = True result = conn.create_broker(**kwargs) # - return {'broker': camel_dict_to_snake_dict(result, ignore_list=['Tags']), - 'changed': changed} + return {"broker": camel_dict_to_snake_dict(result, ignore_list=["Tags"]), "changed": changed} def update_broker(conn, module, broker_id): kwargs = _fill_kwargs(module, apply_defaults=False, ignore_create_params=True) # replace name with id - broker_name = kwargs['BrokerName'] - del kwargs['BrokerName'] - kwargs['BrokerId'] = broker_id + broker_name = kwargs["BrokerName"] + del kwargs["BrokerName"] + kwargs["BrokerId"] = broker_id # get current state for comparison: api_result = get_broker_info(conn, module, broker_id) - if api_result['BrokerState'] != 'RUNNING': - module.fail_json_aws(RuntimeError, - msg="Cannot trigger update while broker ({0}) is in state {1}".format( - broker_id, api_result['BrokerState'] - )) + if api_result["BrokerState"] != "RUNNING": + module.fail_json( + msg=f"Cannot trigger update while broker ({broker_id}) is in state {api_result['BrokerState']}", + ) # engine version of 'latest' is taken as "keep current one" # i.e. do not request upgrade on playbook rerun - if 'EngineVersion' in kwargs and kwargs['EngineVersion'] == 'latest': - kwargs['EngineVersion'] = api_result['EngineVersion'] - result = { - 'broker_id': broker_id, - 'broker_name': broker_name - } + if "EngineVersion" in kwargs and kwargs["EngineVersion"] == "latest": + kwargs["EngineVersion"] = api_result["EngineVersion"] + result = {"broker_id": broker_id, "broker_name": broker_name} changed = False if _needs_change(api_result, kwargs): changed = True @@ -459,102 +435,96 @@ def update_broker(conn, module, broker_id): api_result = conn.update_broker(**kwargs) # # - return {'broker': result, 'changed': changed} + return {"broker": result, "changed": changed} def ensure_absent(conn, module): - result = { - 'broker_name': module.params['broker_name'], - 'broker_id': None - } + result = {"broker_name": module.params["broker_name"], "broker_id": None} if module.check_mode: - return { - 'broker': camel_dict_to_snake_dict(result, ignore_list=['Tags']), - 'changed': True} + return {"broker": camel_dict_to_snake_dict(result, ignore_list=["Tags"]), "changed": True} broker_id = get_broker_id(conn, module) - result['broker_id'] = broker_id - if broker_id: - try: - # check for pending delete (small race condition possible here - api_result = get_broker_info(conn, module, broker_id) - if api_result['BrokerState'] == 'DELETION_IN_PROGRESS': - return {'broker': result, 'changed': False} - delete_broker(conn, module, broker_id) - except botocore.exceptions.ClientError as e: - module.fail_json_aws(e) - # - return {'broker': result, 'changed': True} - else: + result["broker_id"] = broker_id + + if not broker_id: # silently ignore delete of unknown broker (to make it idempotent) - return {'broker': result, 'changed': False} + return {"broker": result, "changed": False} + + try: + # check for pending delete (small race condition possible here + api_result = get_broker_info(conn, module, broker_id) + if api_result["BrokerState"] == "DELETION_IN_PROGRESS": + return {"broker": result, "changed": False} + delete_broker(conn, module, broker_id) + except botocore.exceptions.ClientError as e: + module.fail_json_aws(e) + + return {"broker": result, "changed": True} def ensure_present(conn, module): if module.check_mode: - return {'broker': { - 'broker_arn': 'fakeArn', - 'broker_id': 'fakeId' - }, 'changed': True} - # + return {"broker": {"broker_arn": "fakeArn", "broker_id": "fakeId"}, "changed": True} + broker_id = get_broker_id(conn, module) if broker_id: return update_broker(conn, module, broker_id) - else: - return create_broker(conn, module) + + return create_broker(conn, module) def main(): argument_spec = dict( - broker_name=dict(required=True, type='str'), - state=dict(default='present', choices=['present', 'absent', 'restarted']), + broker_name=dict(required=True, type="str"), + state=dict(default="present", choices=["present", "absent", "restarted"]), # parameters only allowed on create - deployment_mode=dict(choices=['SINGLE_INSTANCE', 'ACTIVE_STANDBY_MULTI_AZ', 'CLUSTER_MULTI_AZ']), - use_aws_owned_key=dict(type='bool'), - kms_key_id=dict(type='str'), - engine_type=dict(choices=['ACTIVEMQ', 'RABBITMQ'], type='str'), - maintenance_window_start_time=dict(type='dict'), - publicly_accessible=dict(type='bool'), - storage_type=dict(choices=['EBS', 'EFS']), - subnet_ids=dict(type='list', elements='str'), - users=dict(type='list', elements='dict'), - tags=dict(type='dict'), + deployment_mode=dict(choices=["SINGLE_INSTANCE", "ACTIVE_STANDBY_MULTI_AZ", "CLUSTER_MULTI_AZ"]), + use_aws_owned_key=dict(type="bool"), + kms_key_id=dict(type="str"), + engine_type=dict(choices=["ACTIVEMQ", "RABBITMQ"], type="str"), + maintenance_window_start_time=dict(type="dict"), + publicly_accessible=dict(type="bool"), + storage_type=dict(choices=["EBS", "EFS"]), + subnet_ids=dict(type="list", elements="str"), + users=dict(type="list", elements="dict"), + tags=dict(type="dict"), # parameters allowed on update as well - authentication_strategy=dict(choices=['SIMPLE', 'LDAP']), - auto_minor_version_upgrade=dict(default=True, type='bool'), - engine_version=dict(type='str'), - host_instance_type=dict(type='str'), - enable_audit_log=dict(default=False, type='bool'), - enable_general_log=dict(default=False, type='bool'), - security_groups=dict(type='list', elements='str') + authentication_strategy=dict(choices=["SIMPLE", "LDAP"]), + auto_minor_version_upgrade=dict(default=True, type="bool"), + engine_version=dict(type="str"), + host_instance_type=dict(type="str"), + enable_audit_log=dict(default=False, type="bool"), + enable_general_log=dict(default=False, type="bool"), + security_groups=dict(type="list", elements="str"), ) module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) - connection = module.client('mq') + connection = module.client("mq") - if module.params['state'] == 'present': + if module.params["state"] == "present": try: compound_result = ensure_present(connection, module) except botocore.exceptions.ClientError as e: module.fail_json_aws(e) # module.exit_json(**compound_result) - elif module.params['state'] == 'absent': + + if module.params["state"] == "absent": try: compound_result = ensure_absent(connection, module) except botocore.exceptions.ClientError as e: module.fail_json_aws(e) # module.exit_json(**compound_result) - elif module.params['state'] == 'restarted': + + if module.params["state"] == "restarted": broker_id = get_broker_id(connection, module) if module.check_mode: - module.exit_json(broker={ - 'broker_id': broker_id if broker_id else 'fakeId' - }, changed=True) + module.exit_json(broker={"broker_id": broker_id if broker_id else "fakeId"}, changed=True) if not broker_id: - module.fail_json_aws(RuntimeError, - msg="Cannot find broker with name {0}.".format(module.params['broker_name'])) + module.fail_json( + msg="Cannot find broker with name {module.params['broker_name']}.", + ) try: changed = True if not module.check_mode: @@ -564,10 +534,7 @@ def main(): except botocore.exceptions.ClientError as e: module.fail_json_aws(e) module.exit_json(broker=result, changed=changed) - else: - module.fail_json_aws(RuntimeError, - msg="Invalid broker state requested ({0}). Valid are: 'present', 'absent', 'restarted'".format(module.params['state'])) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/plugins/modules/mq_broker_config.py b/plugins/modules/mq_broker_config.py index da1ab975925..b073798ae86 100644 --- a/plugins/modules/mq_broker_config.py +++ b/plugins/modules/mq_broker_config.py @@ -2,11 +2,7 @@ # # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - - -DOCUMENTATION = ''' +DOCUMENTATION = r""" --- module: mq_broker_config version_added: 6.0.0 @@ -42,9 +38,9 @@ - amazon.aws.boto3 - amazon.aws.common.modules - amazon.aws.region.modules -''' +""" -EXAMPLES = ''' +EXAMPLES = r""" - name: send new XML config to broker relying on credentials from environment amazon.aws.mq_broker_config: broker_id: "aws-mq-broker-id" @@ -61,9 +57,9 @@ config_xml: "{{ lookup('file', 'activemq3.xml')}}" config_description: "custom description for configuration object" register: result -''' +""" -RETURN = ''' +RETURN = r""" broker: description: API response of describe_broker() converted to snake yaml after changes have been applied. type: dict @@ -81,79 +77,68 @@ description: Revision of the configuration that will be active after next reboot. type: int example: 4 -''' +""" import base64 import re -import sys try: import botocore -except ImportError as ex: +except ImportError: # handled by AnsibleAWSModule pass -from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict +from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule -DEFAULTS = { - 'reboot': False -} +DEFAULTS = {"reboot": False} FULL_DEBUG = False def is_same_config(old, new): # we a simple comparision here: strip down spaces and compare the rest # TODO: use same XML normalizer on new as used by AWS before comparing strings - old_stripped = re.sub(r'\s+', ' ', old, flags=re.S).rstrip() - new_stripped = re.sub(r'\s+', ' ', new, flags=re.S).rstrip() + old_stripped = re.sub(r"\s+", " ", old, flags=re.S).rstrip() + new_stripped = re.sub(r"\s+", " ", new, flags=re.S).rstrip() return old_stripped == new_stripped def get_broker_info(conn, module): try: - return conn.describe_broker(BrokerId=module.params['broker_id']) + return conn.describe_broker(BrokerId=module.params["broker_id"]) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: if module.check_mode: return { - 'broker_id': module.params['broker_id'], + "broker_id": module.params["broker_id"], } - else: - module.fail_json_aws(e, msg="Couldn't get broker details.") + module.fail_json_aws(e, msg="Couldn't get broker details.") def get_current_configuration(conn, module, cfg_id, cfg_revision): try: - return conn.describe_configuration_revision( - ConfigurationId=cfg_id, - ConfigurationRevision=str(cfg_revision) - ) + return conn.describe_configuration_revision(ConfigurationId=cfg_id, ConfigurationRevision=str(cfg_revision)) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Couldn't get configuration revision.") def create_and_assign_config(conn, module, broker_id, cfg_id, cfg_xml_encoded): - kwargs = { - 'ConfigurationId': cfg_id, - 'Data': cfg_xml_encoded - } - if 'config_description' in module.params and module.params['config_description']: - kwargs['Description'] = module.params['config_description'] + kwargs = {"ConfigurationId": cfg_id, "Data": cfg_xml_encoded} + if "config_description" in module.params and module.params["config_description"]: + kwargs["Description"] = module.params["config_description"] else: - kwargs['Description'] = 'Updated through amazon.aws.mq_broker_config ansible module' + kwargs["Description"] = "Updated through amazon.aws.mq_broker_config ansible module" # try: c_response = conn.update_configuration(**kwargs) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Couldn't create new configuration revision.") # - new_config_revision = c_response['LatestRevision']['Revision'] + new_config_revision = c_response["LatestRevision"]["Revision"] try: - b_response = conn.update_broker(BrokerId=broker_id, Configuration={ - 'Id': cfg_id, - 'Revision': new_config_revision - }) + b_response = conn.update_broker( + BrokerId=broker_id, Configuration={"Id": cfg_id, "Revision": new_config_revision} + ) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Couldn't assign new configuration revision to broker.") # @@ -162,74 +147,66 @@ def create_and_assign_config(conn, module, broker_id, cfg_id, cfg_xml_encoded): def reboot_broker(conn, module, broker_id): try: - return conn.reboot_broker( - BrokerId=broker_id - ) + return conn.reboot_broker(BrokerId=broker_id) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Couldn't reboot broker.") def ensure_config(conn, module): - broker_id = module.params['broker_id'] + broker_id = module.params["broker_id"] broker_info = get_broker_info(conn, module) changed = False - if module.check_mode and 'Configurations' not in broker_info: + if module.check_mode and "Configurations" not in broker_info: # not result from get_broker_info(). use requeste config - current_cfg_decoded = module.params['config_xml'] + current_cfg_decoded = module.params["config_xml"] else: - current_cfg = broker_info['Configurations']['Current'] - if 'Pending' in broker_info['Configurations']: - current_cfg = broker_info['Configurations']['Pending'] - current_cfg_encoded = get_current_configuration(conn, module, - current_cfg['Id'], - current_cfg['Revision'])['Data'] + current_cfg = broker_info["Configurations"]["Current"] + if "Pending" in broker_info["Configurations"]: + current_cfg = broker_info["Configurations"]["Pending"] + current_cfg_encoded = get_current_configuration(conn, module, current_cfg["Id"], current_cfg["Revision"])[ + "Data" + ] current_cfg_decoded = base64.b64decode(current_cfg_encoded.encode()).decode() - if is_same_config(current_cfg_decoded, module.params['config_xml']): - return { - 'changed': changed, - 'broker': camel_dict_to_snake_dict(broker_info, ignore_list=['Tags']) - } - else: - (c_response, b_response) = (None, None) - if not module.check_mode: - new_cfg_encoded = base64.b64encode(module.params['config_xml'].encode()).decode() - (c_response, b_response) = create_and_assign_config(conn, module, - broker_id, - current_cfg['Id'], - new_cfg_encoded) - # - changed = True + + if is_same_config(current_cfg_decoded, module.params["config_xml"]): + return {"changed": changed, "broker": camel_dict_to_snake_dict(broker_info, ignore_list=["Tags"])} + + (c_response, _b_response) = (None, None) + if not module.check_mode: + new_cfg_encoded = base64.b64encode(module.params["config_xml"].encode()).decode() + (c_response, _b_response) = create_and_assign_config( + conn, module, broker_id, current_cfg["Id"], new_cfg_encoded + ) # - if changed and module.params['reboot'] and not module.check_mode: + changed = True + + if changed and module.params["reboot"] and not module.check_mode: reboot_broker(conn, module, broker_id) # broker_info = get_broker_info(conn, module) return_struct = { - 'changed': changed, - 'broker': camel_dict_to_snake_dict(broker_info, ignore_list=['Tags']), - 'configuration': { - 'id': c_response['Id'], - 'revision': c_response['LatestRevision']['Revision'] - } + "changed": changed, + "broker": camel_dict_to_snake_dict(broker_info, ignore_list=["Tags"]), + "configuration": {"id": c_response["Id"], "revision": c_response["LatestRevision"]["Revision"]}, } if FULL_DEBUG: - return_struct['old_config_xml'] = base64.b64decode(current_cfg_encoded) - return_struct['new_config_xml'] = module.params['config_xml'] - return_struct['old_config_revision'] = current_cfg['Revision'] + return_struct["old_config_xml"] = base64.b64decode(current_cfg_encoded) + return_struct["new_config_xml"] = module.params["config_xml"] + return_struct["old_config_revision"] = current_cfg["Revision"] return return_struct def main(): argument_spec = dict( - broker_id=dict(required=True, type='str'), - config_xml=dict(required=True, type='str'), - config_description=dict(required=False, type='str'), - reboot=dict(required=False, type='bool', default=DEFAULTS['reboot']), + broker_id=dict(required=True, type="str"), + config_xml=dict(required=True, type="str"), + config_description=dict(required=False, type="str"), + reboot=dict(required=False, type="bool", default=DEFAULTS["reboot"]), ) module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) - connection = module.client('mq') + connection = module.client("mq") try: result = ensure_config(connection, module) @@ -239,5 +216,5 @@ def main(): module.exit_json(**result) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/plugins/modules/mq_broker_info.py b/plugins/modules/mq_broker_info.py index e06728ee889..386c2efc041 100644 --- a/plugins/modules/mq_broker_info.py +++ b/plugins/modules/mq_broker_info.py @@ -2,11 +2,7 @@ # # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - - -DOCUMENTATION = ''' +DOCUMENTATION = r""" --- module: mq_broker_info version_added: 6.0.0 @@ -28,10 +24,10 @@ - amazon.aws.boto3 - amazon.aws.common.modules - amazon.aws.region.modules -''' +""" -EXAMPLES = ''' +EXAMPLES = r""" - name: get current broker settings by id amazon.aws.mq_broker_info: broker_id: "aws-mq-broker-id" @@ -40,33 +36,33 @@ amazon.aws.mq_broker_info: broker_name: "aws-mq-broker-name" register: broker_info -''' +""" -RETURN = ''' +RETURN = r""" broker: description: API response of describe_broker() converted to snake yaml. type: dict returned: success -''' +""" try: import botocore -except ImportError as ex: +except ImportError: # handled by AnsibleAWSModule pass -from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict +from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule def get_broker_id(conn, module): try: - broker_name = module.params['broker_name'] + broker_name = module.params["broker_name"] broker_id = None response = conn.list_brokers(MaxResults=100) - for broker in response['BrokerSummaries']: - if broker['BrokerName'] == broker_name: - broker_id = broker['BrokerId'] + for broker in response["BrokerSummaries"]: + if broker["BrokerName"] == broker_name: + broker_id = broker["BrokerId"] break return broker_id except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: @@ -78,43 +74,41 @@ def get_broker_info(conn, module, broker_id): return conn.describe_broker(BrokerId=broker_id) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: if module.check_mode: - module.exit_json(broker={ - 'broker_id': broker_id, - 'broker_name': 'fakeName' - }) + module.exit_json(broker={"broker_id": broker_id, "broker_name": "fakeName"}) else: module.fail_json_aws(e, msg="Couldn't get broker details.") def main(): - argument_spec = dict( - broker_id=dict(type='str'), - broker_name=dict(type='str') + argument_spec = dict(broker_id=dict(type="str"), broker_name=dict(type="str")) + required_one_of = ( + ("broker_name", "broker_id",), ) - module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) - broker_id = module.params['broker_id'] - broker_name = module.params['broker_name'] - if not broker_id and not broker_name: - module.fail_json_aws(RuntimeError, msg="Either 'broker_id' or 'broker_name' must be specified") + module = AnsibleAWSModule( + argument_spec=argument_spec, + required_one_of=required_one_of, + supports_check_mode=True, + ) + broker_id = module.params["broker_id"] + broker_name = module.params["broker_name"] - connection = module.client('mq') + connection = module.client("mq") try: if not broker_id: broker_id = get_broker_id(connection, module) if not broker_id: if module.check_mode: - module.exit_json(broker={ - 'broker_id': 'fakeId', - 'broker_name': broker_name if broker_name else 'fakeName' - }) + module.exit_json( + broker={"broker_id": "fakeId", "broker_name": broker_name if broker_name else "fakeName"} + ) result = get_broker_info(connection, module, broker_id) except botocore.exceptions.ClientError as e: module.fail_json_aws(e) # - module.exit_json(broker=camel_dict_to_snake_dict(result, ignore_list=['Tags'])) + module.exit_json(broker=camel_dict_to_snake_dict(result, ignore_list=["Tags"])) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/plugins/modules/mq_user.py b/plugins/modules/mq_user.py index d346bc12846..eab8932d3f9 100644 --- a/plugins/modules/mq_user.py +++ b/plugins/modules/mq_user.py @@ -2,11 +2,7 @@ # Copyright: Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -from __future__ import absolute_import, division, print_function -__metaclass__ = type - - -DOCUMENTATION = ''' +DOCUMENTATION = r""" --- module: mq_user version_added: 6.0.0 @@ -60,9 +56,9 @@ - amazon.aws.boto3 - amazon.aws.common.modules - amazon.aws.region.modules -''' +""" -EXAMPLES = ''' +EXAMPLES = r""" - name: create/update user - set provided password if user doesn't exist, yet amazon.aws.mq_user: state: present @@ -83,19 +79,18 @@ state: absent broker_id: "aws-mq-broker-id" username: "other_user" -''' +""" -RETURN = ''' +RETURN = r""" user: description: - just echos the username - "only present when state=present" type: str returned: success -''' +""" import secrets -import sys try: import botocore @@ -103,23 +98,24 @@ # handled by AnsibleAWSModule pass -from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict -CREATE_DEFAULTS = { - 'console_access': False, - 'groups': [], +from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule +from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code +CREATE_DEFAULTS = { + "console_access": False, + "groups": [], } def _group_change_required(user_response, requested_groups): current_groups = [] - if 'Groups' in user_response: - current_groups = user_response['Groups'] - elif 'Pending' in user_response: + if "Groups" in user_response: + current_groups = user_response["Groups"] + elif "Pending" in user_response: # to support automatic testing without broker reboot - current_groups = user_response['Pending']['Groups'] + current_groups = user_response["Pending"]["Groups"] if len(current_groups) != len(requested_groups): return True if len(current_groups) != len(set(current_groups) & set(requested_groups)): @@ -129,12 +125,12 @@ def _group_change_required(user_response, requested_groups): def _console_access_change_required(user_response, requested_boolean): - current_boolean = CREATE_DEFAULTS['console_access'] - if 'ConsoleAccess' in user_response: - current_boolean = user_response['ConsoleAccess'] - elif 'Pending' in user_response: + current_boolean = CREATE_DEFAULTS["console_access"] + if "ConsoleAccess" in user_response: + current_boolean = user_response["ConsoleAccess"] + elif "Pending" in user_response: # to support automatic testing without broker reboot - current_boolean = user_response['Pending']['ConsoleAccess'] + current_boolean = user_response["Pending"]["ConsoleAccess"] # return current_boolean != requested_boolean @@ -145,22 +141,19 @@ def generate_password(): # returns API response object def _create_user(conn, module): - kwargs = { - 'BrokerId': module.params['broker_id'], - 'Username': module.params['username'] - } - if 'groups' in module.params and module.params['groups'] is not None: - kwargs['Groups'] = module.params['groups'] + kwargs = {"BrokerId": module.params["broker_id"], "Username": module.params["username"]} + if "groups" in module.params and module.params["groups"] is not None: + kwargs["Groups"] = module.params["groups"] else: - kwargs['Groups'] = CREATE_DEFAULTS['groups'] - if 'password' in module.params and module.params['password']: - kwargs['Password'] = module.params['password'] + kwargs["Groups"] = CREATE_DEFAULTS["groups"] + if "password" in module.params and module.params["password"]: + kwargs["Password"] = module.params["password"] else: - kwargs['Password'] = generate_password() - if 'console_access' in module.params and module.params['console_access'] is not None: - kwargs['ConsoleAccess'] = module.params['console_access'] + kwargs["Password"] = generate_password() + if "console_access" in module.params and module.params["console_access"] is not None: + kwargs["ConsoleAccess"] = module.params["console_access"] else: - kwargs['ConsoleAccess'] = CREATE_DEFAULTS['console_access'] + kwargs["ConsoleAccess"] = CREATE_DEFAULTS["console_access"] try: response = conn.create_user(**kwargs) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: @@ -179,90 +172,90 @@ def _update_user(conn, module, kwargs): def get_matching_user(conn, module, broker_id, username): try: - return conn.describe_user(BrokerId=broker_id, Username=username) - except botocore.exceptions.ClientError as e: - if e.response['Error']['Code'] == 'NotFoundException': - return None - else: - module.fail_json_aws(e, msg="Couldn't get user details") - except botocore.exceptions.BotoCoreError as e: + response = conn.describe_user(BrokerId=broker_id, Username=username) + except is_boto3_error_code("NotFoundException"): + return None + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Couldn't get user details") + return response def ensure_user_present(conn, module): - user = get_matching_user(conn, module, module.params['broker_id'], module.params['username']) + user = get_matching_user(conn, module, module.params["broker_id"], module.params["username"]) changed = False if user is None: if not module.check_mode: - response = _create_user(conn, module) + _response = _create_user(conn, module) changed = True else: kwargs = {} - if 'groups' in module.params and module.params['groups'] is not None: - if _group_change_required(user, module.params['groups']): - kwargs['Groups'] = module.params['groups'] - if 'console_access' in module.params and module.params['console_access'] is not None: - if _console_access_change_required(user, module.params['console_access']): - kwargs['ConsoleAccess'] = module.params['console_access'] - if 'password' in module.params and module.params['password']: - if 'allow_pw_update' in module.params and module.params['allow_pw_update']: - kwargs['Password'] = module.params['password'] + if "groups" in module.params and module.params["groups"] is not None: + if _group_change_required(user, module.params["groups"]): + kwargs["Groups"] = module.params["groups"] + if "console_access" in module.params and module.params["console_access"] is not None: + if _console_access_change_required(user, module.params["console_access"]): + kwargs["ConsoleAccess"] = module.params["console_access"] + if "password" in module.params and module.params["password"]: + if "allow_pw_update" in module.params and module.params["allow_pw_update"]: + kwargs["Password"] = module.params["password"] if len(kwargs) == 0: changed = False else: if not module.check_mode: - kwargs['BrokerId'] = module.params['broker_id'] - kwargs['Username'] = module.params['username'] + kwargs["BrokerId"] = module.params["broker_id"] + kwargs["Username"] = module.params["username"] response = _update_user(conn, module, kwargs) # changed = True # - user = get_matching_user(conn, module, module.params['broker_id'], module.params['username']) + user = get_matching_user(conn, module, module.params["broker_id"], module.params["username"]) - return { - 'changed': changed, - 'user': camel_dict_to_snake_dict(user, ignore_list=['Tags']) - } + return {"changed": changed, "user": camel_dict_to_snake_dict(user, ignore_list=["Tags"])} def ensure_user_absent(conn, module): - user = get_matching_user(conn, module, module.params['broker_id'], module.params['username']) + user = get_matching_user(conn, module, module.params["broker_id"], module.params["username"]) + result = {"changed": False} if user is None: - return {'changed': False} + return result # better support for testing - if 'Pending' in user and 'PendingChange' in user['Pending'] \ - and user['Pending']['PendingChange'] == 'DELETE': - return {'changed': False} + if "Pending" in user and "PendingChange" in user["Pending"] and user["Pending"]["PendingChange"] == "DELETE": + return result + + result = {"changed": True} + if module.check_mode: + return result + try: - if not module.check_mode: - conn.delete_user(BrokerId=user['BrokerId'], Username=user['Username']) - return {'changed': True} + conn.delete_user(BrokerId=user["BrokerId"], Username=user["Username"]) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Couldn't delete user") + return result + def main(): argument_spec = dict( - broker_id=dict(required=True, type='str'), - username=dict(required=True, type='str'), - console_access=dict(required=False, type='bool'), - groups=dict(required=False, type='list', elements='str'), - password=dict(required=False, type='str', no_log=True), - allow_pw_update=dict(default=False, required=False, type='bool'), - state=dict(default='present', choices=['present', 'absent']) + broker_id=dict(required=True, type="str"), + username=dict(required=True, type="str"), + console_access=dict(required=False, type="bool"), + groups=dict(required=False, type="list", elements="str"), + password=dict(required=False, type="str", no_log=True), + allow_pw_update=dict(default=False, required=False, type="bool"), + state=dict(default="present", choices=["present", "absent"]), ) module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) - connection = module.client('mq') + connection = module.client("mq") - state = module.params.get('state') + state = module.params.get("state") try: - if state == 'present': + if state == "present": result = ensure_user_present(connection, module) - elif state == 'absent': + elif state == "absent": result = ensure_user_absent(connection, module) except botocore.exceptions.ClientError as e: module.fail_json_aws(e) @@ -270,5 +263,5 @@ def main(): module.exit_json(**result) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/plugins/modules/mq_user_info.py b/plugins/modules/mq_user_info.py index 44c0c61eaaa..096db713082 100644 --- a/plugins/modules/mq_user_info.py +++ b/plugins/modules/mq_user_info.py @@ -2,11 +2,7 @@ # # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - - -DOCUMENTATION = ''' +DOCUMENTATION = r""" --- module: mq_user_info version_added: 6.0.0 @@ -47,10 +43,10 @@ - amazon.aws.boto3 - amazon.aws.common.modules - amazon.aws.region.modules -''' +""" -EXAMPLES = ''' +EXAMPLES = r""" - name: get all users as list - relying on environment for API credentials amazon.aws.mq_user_info: broker_id: "aws-mq-broker-id" @@ -68,16 +64,16 @@ amazon.aws.mq_user_info: broker_id: "aws-mq-broker-id" skip_pending_create: true -''' +""" -RETURN = ''' +RETURN = r""" users: type: dict returned: success description: - dict key is username - each entry is the record for a user as returned by API but converted to snake yaml -''' +""" try: import botocore @@ -89,64 +85,56 @@ from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict -DEFAULTS = { - 'max_results': 100, - 'skip_pending_create': False, - 'skip_pending_delete': False, - 'as_dict': True -} +DEFAULTS = {"max_results": 100, "skip_pending_create": False, "skip_pending_delete": False, "as_dict": True} def get_user_info(conn, module): try: - response = conn.list_users(BrokerId=module.params['broker_id'], - MaxResults=module.params['max_results']) + response = conn.list_users(BrokerId=module.params["broker_id"], MaxResults=module.params["max_results"]) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: if module.check_mode: # return empty set for unknown broker in check mode - if DEFAULTS['as_dict']: + if DEFAULTS["as_dict"]: return {} - else: - return [] - else: - module.fail_json_aws(e, msg='Failed to describe users') + return [] + module.fail_json_aws(e, msg="Failed to describe users") # - if not module.params['skip_pending_create'] and not module.params['skip_pending_delete']: + if not module.params["skip_pending_create"] and not module.params["skip_pending_delete"]: # we can simply return the sub-object from the response - records = response['Users'] + records = response["Users"] else: records = [] - for record in response['Users']: - if 'PendingChange' in record: - if record['PendingChange'] == 'CREATE' and module.params['skip_pending_create']: + for record in response["Users"]: + if "PendingChange" in record: + if record["PendingChange"] == "CREATE" and module.params["skip_pending_create"]: continue - if record['PendingChange'] == 'DELETE' and module.params['skip_pending_delete']: + if record["PendingChange"] == "DELETE" and module.params["skip_pending_delete"]: continue # records.append(record) # - if DEFAULTS['as_dict']: + if DEFAULTS["as_dict"]: user_records = {} for record in records: - user_records[record['Username']] = record + user_records[record["Username"]] = record # - return camel_dict_to_snake_dict(user_records, ignore_list=['Tags']) - else: - return camel_dict_to_snake_dict(records, ignore_list=['Tags']) + return camel_dict_to_snake_dict(user_records, ignore_list=["Tags"]) + + return camel_dict_to_snake_dict(records, ignore_list=["Tags"]) def main(): argument_spec = dict( - broker_id=dict(required=True, type='str'), - max_results=dict(required=False, type='int', default=DEFAULTS['max_results']), - skip_pending_create=dict(required=False, type='bool', default=DEFAULTS['skip_pending_create']), - skip_pending_delete=dict(required=False, type='bool', default=DEFAULTS['skip_pending_delete']), - as_dict=dict(required=False, type='bool', default=False), + broker_id=dict(required=True, type="str"), + max_results=dict(required=False, type="int", default=DEFAULTS["max_results"]), + skip_pending_create=dict(required=False, type="bool", default=DEFAULTS["skip_pending_create"]), + skip_pending_delete=dict(required=False, type="bool", default=DEFAULTS["skip_pending_delete"]), + as_dict=dict(required=False, type="bool", default=False), ) module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) - connection = module.client('mq') + connection = module.client("mq") try: user_records = get_user_info(connection, module) @@ -156,5 +144,5 @@ def main(): module.exit_json(users=user_records) -if __name__ == '__main__': +if __name__ == "__main__": main()