From e0a32a41de3588031c57b67bd7ce3788ad244cba Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Tue, 11 Oct 2022 20:12:24 +0200 Subject: [PATCH] Use missing_required_lib more consistently (#1152) (#1153) Use missing_required_lib more consistently SUMMARY The developer docs recomment using missing_required_lib rather than a custom error message: https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_best_practices.html#importing-and-using-shared-code ISSUE TYPE Feature Pull Request COMPONENT NAME plugins/inventory/aws_ec2.py plugins/inventory/aws_rds.py plugins/lookup/aws_account_attribute.py plugins/lookup/aws_secret.py plugins/lookup/aws_ssm.py ADDITIONAL INFORMATION Reviewed-by: Alina Buzachis (cherry picked from commit 63cad4c395b043fd4e4dace3c1f0d05536b0f2b4) --- changelogs/fragments/1152-missing-botocore.yml | 6 ++++++ plugins/inventory/aws_ec2.py | 3 ++- plugins/inventory/aws_rds.py | 3 ++- plugins/lookup/aws_account_attribute.py | 3 ++- plugins/lookup/aws_secret.py | 3 ++- plugins/lookup/aws_ssm.py | 3 ++- plugins/module_utils/modules.py | 2 +- 7 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/1152-missing-botocore.yml diff --git a/changelogs/fragments/1152-missing-botocore.yml b/changelogs/fragments/1152-missing-botocore.yml new file mode 100644 index 00000000000..268805bde0c --- /dev/null +++ b/changelogs/fragments/1152-missing-botocore.yml @@ -0,0 +1,6 @@ +minor_changes: +- aws_ec2 inventory plugin - use ``missing_required_lib`` for more consistent error message when boto3/botocore is not available (https://github.com/ansible-collections/amazon.aws/pull/1152). +- aws_rds inventory plugin - use ``missing_required_lib`` for more consistent error message when boto3/botocore is not available (https://github.com/ansible-collections/amazon.aws/pull/1152). +- aws_account_attribute lookup plugin - use ``missing_required_lib`` for more consistent error message when boto3/botocore is not available (https://github.com/ansible-collections/amazon.aws/pull/1152). +- aws_secret lookup plugin - use ``missing_required_lib`` for more consistent error message when boto3/botocore is not available (https://github.com/ansible-collections/amazon.aws/pull/1152). +- aws_ssm lookup plugin - use ``missing_required_lib`` for more consistent error message when boto3/botocore is not available (https://github.com/ansible-collections/amazon.aws/pull/1152). diff --git a/plugins/inventory/aws_ec2.py b/plugins/inventory/aws_ec2.py index af2603e3de5..c7adcb873da 100644 --- a/plugins/inventory/aws_ec2.py +++ b/plugins/inventory/aws_ec2.py @@ -273,6 +273,7 @@ from ansible.errors import AnsibleError from ansible.module_utils._text import to_native from ansible.module_utils._text import to_text +from ansible.module_utils.basic import missing_required_lib from ansible.plugins.inventory import BaseInventoryPlugin from ansible.plugins.inventory import Cacheable from ansible.plugins.inventory import Constructable @@ -858,7 +859,7 @@ def parse(self, inventory, loader, path, cache=True): super(InventoryModule, self).parse(inventory, loader, path) if not HAS_BOTO3: - raise AnsibleError('The ec2 dynamic inventory plugin requires boto3 and botocore.') + raise AnsibleError(missing_required_lib('botocore and boto3')) self._read_config_data(path) diff --git a/plugins/inventory/aws_rds.py b/plugins/inventory/aws_rds.py index 016cdee4ba6..e03464168f7 100644 --- a/plugins/inventory/aws_rds.py +++ b/plugins/inventory/aws_rds.py @@ -88,6 +88,7 @@ from ansible.errors import AnsibleError from ansible.module_utils._text import to_native +from ansible.module_utils.basic import missing_required_lib from ansible.plugins.inventory import BaseInventoryPlugin from ansible.plugins.inventory import Cacheable from ansible.plugins.inventory import Constructable @@ -357,7 +358,7 @@ def parse(self, inventory, loader, path, cache=True): super(InventoryModule, self).parse(inventory, loader, path) if not HAS_BOTO3: - raise AnsibleError('The RDS dynamic inventory plugin requires boto3 and botocore.') + raise AnsibleError(missing_required_lib('botocore and boto3')) self._set_credentials() diff --git a/plugins/lookup/aws_account_attribute.py b/plugins/lookup/aws_account_attribute.py index 84973b9daa0..b04731f1096 100644 --- a/plugins/lookup/aws_account_attribute.py +++ b/plugins/lookup/aws_account_attribute.py @@ -57,6 +57,7 @@ from ansible.errors import AnsibleError from ansible.module_utils._text import to_native +from ansible.module_utils.basic import missing_required_lib from ansible.plugins.lookup import LookupBase from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry @@ -99,7 +100,7 @@ class LookupModule(LookupBase): def run(self, terms, variables, **kwargs): if not HAS_BOTO3: - raise AnsibleError("The lookup aws_account_attribute requires boto3 and botocore.") + raise AnsibleError(missing_required_lib('botocore and boto3')) self.set_options(var_options=variables, direct=kwargs) boto_credentials = _get_credentials(self._options) diff --git a/plugins/lookup/aws_secret.py b/plugins/lookup/aws_secret.py index e4a38039bb6..7cfd5b51432 100644 --- a/plugins/lookup/aws_secret.py +++ b/plugins/lookup/aws_secret.py @@ -129,6 +129,7 @@ from ansible.errors import AnsibleError from ansible.module_utils.six import string_types from ansible.module_utils._text import to_native +from ansible.module_utils.basic import missing_required_lib from ansible.plugins.lookup import LookupBase from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code @@ -177,7 +178,7 @@ def run(self, terms, variables=None, boto_profile=None, aws_profile=None, :returns: A list of parameter values or a list of dictionaries if bypath=True. ''' if not HAS_BOTO3: - raise AnsibleError('botocore and boto3 are required for aws_ssm lookup.') + raise AnsibleError(missing_required_lib('botocore and boto3')) deleted = on_deleted.lower() if not isinstance(deleted, string_types) or deleted not in ['error', 'warn', 'skip']: diff --git a/plugins/lookup/aws_ssm.py b/plugins/lookup/aws_ssm.py index 62e3e4f32a5..81e4cbb9b83 100644 --- a/plugins/lookup/aws_ssm.py +++ b/plugins/lookup/aws_ssm.py @@ -137,6 +137,7 @@ from ansible.plugins.lookup import LookupBase from ansible.utils.display import Display from ansible.module_utils.six import string_types +from ansible.module_utils.basic import missing_required_lib from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_conn from ansible_collections.amazon.aws.plugins.module_utils.ec2 import get_aws_connection_info @@ -170,7 +171,7 @@ def run(self, terms, variables=None, boto_profile=None, aws_profile=None, ''' if not HAS_BOTO3: - raise AnsibleError('botocore and boto3 are required for aws_ssm lookup.') + raise AnsibleError(missing_required_lib('botocore and boto3')) # validate arguments 'on_missing' and 'on_denied' if on_missing is not None and (not isinstance(on_missing, string_types) or on_missing.lower() not in ['error', 'warn', 'skip']): diff --git a/plugins/module_utils/modules.py b/plugins/module_utils/modules.py index 431e6e4186d..b2689291a72 100644 --- a/plugins/module_utils/modules.py +++ b/plugins/module_utils/modules.py @@ -123,7 +123,7 @@ def __init__(self, **kwargs): if local_settings["check_boto3"]: if not HAS_BOTO3: self._module.fail_json( - msg=missing_required_lib('botocore or boto3')) + msg=missing_required_lib('botocore and boto3')) if not self.botocore_at_least('1.21.0'): self.warn('botocore < 1.21.0 is not supported or tested.' ' Some features may not work.')