diff --git a/changelogs/fragments/aws_collection_constants.yml b/changelogs/fragments/aws_collection_constants.yml new file mode 100644 index 00000000000..d7cc85cbdf1 --- /dev/null +++ b/changelogs/fragments/aws_collection_constants.yml @@ -0,0 +1,2 @@ +trivial: +- Update integration tests to use lookup plugin when pulling the default AWS SDK versions diff --git a/plugins/lookup/aws_collection_constants.py b/plugins/lookup/aws_collection_constants.py new file mode 100644 index 00000000000..7e907f7bc14 --- /dev/null +++ b/plugins/lookup/aws_collection_constants.py @@ -0,0 +1,81 @@ +# (c) 2023 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +DOCUMENTATION = ''' +name: aws_collection_constants +author: + - Mark Chappell (@tremble) +short_description: expose various collection related constants +version_added: 6.0.0 +description: + - Exposes various collection related constants for use in integration tests. +options: + _terms: + description: Name of the constant. + choices: + - MINIMUM_BOTOCORE_VERSION + - MINIMUM_BOTO3_VERSION + - HAS_BOTO3 + - AMAZON_AWS_COLLECTION_VERSION + - AMAZON_AWS_COLLECTION_NAME + - COMMUNITY_AWS_COLLECTION_VERSION + - COMMUNITY_AWS_COLLECTION_NAME + required: True +''' + +EXAMPLES = """ +""" + +RETURN = """ +_raw: + description: value + type: str +""" + +from ansible.errors import AnsibleLookupError +from ansible.plugins.lookup import LookupBase + +import ansible_collections.amazon.aws.plugins.module_utils.botocore as botocore_utils +import ansible_collections.amazon.aws.plugins.module_utils.common as common_utils + +try: + import ansible_collections.community.aws.plugins.module_utils.common as community_utils + HAS_COMMUNITY = True +except ImportError: + HAS_COMMUNITY = False + + +class LookupModule(LookupBase): + def lookup_constant(self, name): + if name == 'MINIMUM_BOTOCORE_VERSION': + return botocore_utils.MINIMUM_BOTOCORE_VERSION + if name == 'MINIMUM_BOTO3_VERSION': + return botocore_utils.MINIMUM_BOTO3_VERSION + if name == 'HAS_BOTO3': + return botocore_utils.HAS_BOTO3 + + if name == 'AMAZON_AWS_COLLECTION_VERSION': + return common_utils.AMAZON_AWS_COLLECTION_VERSION + if name == 'AMAZON_AWS_COLLECTION_NAME': + return common_utils.AMAZON_AWS_COLLECTION_NAME + + if name == 'COMMUNITY_AWS_COLLECTION_VERSION': + if not HAS_COMMUNITY: + raise AnsibleLookupError( + 'Unable to load ansible_collections.community.aws.plugins.module_utils.common') + return community_utils.COMMUNITY_AWS_COLLECTION_VERSION + if name == 'COMMUNITY_AWS_COLLECTION_NAME': + if not HAS_COMMUNITY: + raise AnsibleLookupError( + 'Unable to load ansible_collections.community.aws.plugins.module_utils.common') + return community_utils.COMMUNITY_AWS_COLLECTION_NAME + + def run(self, terms, variables, **kwargs): + self.set_options(var_options=variables, direct=kwargs) + if not terms: + raise AnsibleLookupError("Constant name not provided") + if len(terms) > 1: + raise AnsibleLookupError("Multiple constant names provided") + name = terms[0].upper() + + return [self.lookup_constant(name)] diff --git a/tests/integration/targets/ec2_key/meta/main.yml b/tests/integration/targets/ec2_key/meta/main.yml index d9abc111089..a45fa19ef5c 100644 --- a/tests/integration/targets/ec2_key/meta/main.yml +++ b/tests/integration/targets/ec2_key/meta/main.yml @@ -2,4 +2,4 @@ dependencies: - setup_sshkey - role: setup_botocore_pip vars: - botocore_version: '1.21.23' + botocore_version: "1.21.23" diff --git a/tests/integration/targets/lookup_aws_collection_constants/aliases b/tests/integration/targets/lookup_aws_collection_constants/aliases new file mode 100644 index 00000000000..4ef4b2067d0 --- /dev/null +++ b/tests/integration/targets/lookup_aws_collection_constants/aliases @@ -0,0 +1 @@ +cloud/aws diff --git a/tests/integration/targets/lookup_aws_collection_constants/meta/main.yml b/tests/integration/targets/lookup_aws_collection_constants/meta/main.yml new file mode 100644 index 00000000000..32cf5dda7ed --- /dev/null +++ b/tests/integration/targets/lookup_aws_collection_constants/meta/main.yml @@ -0,0 +1 @@ +dependencies: [] diff --git a/tests/integration/targets/lookup_aws_collection_constants/tasks/main.yaml b/tests/integration/targets/lookup_aws_collection_constants/tasks/main.yaml new file mode 100644 index 00000000000..cfeb4a99e9c --- /dev/null +++ b/tests/integration/targets/lookup_aws_collection_constants/tasks/main.yaml @@ -0,0 +1,47 @@ +- name: 'MINIMUM_BOTOCORE_VERSION' + set_fact: + MINIMUM_BOTOCORE_VERSION: "{{ lookup('amazon.aws.aws_collection_constants', 'MINIMUM_BOTOCORE_VERSION') }}" + +- assert: + that: + - MINIMUM_BOTOCORE_VERSION.startswith("1.") + +- name: 'MINIMUM_BOTO3_VERSION' + set_fact: + MINIMUM_BOTO3_VERSION: "{{ lookup('amazon.aws.aws_collection_constants', 'MINIMUM_BOTO3_VERSION') }}" + +- assert: + that: + - MINIMUM_BOTO3_VERSION.startswith("1.") + +- name: 'HAS_BOTO3' + set_fact: + HAS_BOTO3: "{{ lookup('amazon.aws.aws_collection_constants', 'HAS_BOTO3') }}" + +- assert: + that: + - HAS_BOTO3 | bool + +- name: 'AMAZON_AWS_COLLECTION_VERSION' + set_fact: + AMAZON_AWS_COLLECTION_VERSION: "{{ lookup('amazon.aws.aws_collection_constants', 'AMAZON_AWS_COLLECTION_VERSION') }}" + +- name: 'AMAZON_AWS_COLLECTION_NAME' + set_fact: + AMAZON_AWS_COLLECTION_NAME: "{{ lookup('amazon.aws.aws_collection_constants', 'AMAZON_AWS_COLLECTION_NAME') }}" + +- assert: + that: + - AMAZON_AWS_COLLECTION_NAME == "amazon.aws" + +- name: 'COMMUNITY_AWS_COLLECTION_VERSION' + set_fact: + COMMUNITY_AWS_COLLECTION_VERSION: "{{ lookup('amazon.aws.aws_collection_constants', 'COMMUNITY_AWS_COLLECTION_VERSION') }}" + +- name: 'COMMUNITY_AWS_COLLECTION_NAME' + set_fact: + COMMUNITY_AWS_COLLECTION_NAME: "{{ lookup('amazon.aws.aws_collection_constants', 'COMMUNITY_AWS_COLLECTION_NAME') }}" + +- assert: + that: + - COMMUNITY_AWS_COLLECTION_NAME == "community.aws" diff --git a/tests/integration/targets/s3_bucket/meta/main.yml b/tests/integration/targets/s3_bucket/meta/main.yml index 67c81ac7f79..32cf5dda7ed 100644 --- a/tests/integration/targets/s3_bucket/meta/main.yml +++ b/tests/integration/targets/s3_bucket/meta/main.yml @@ -1,2 +1 @@ -dependencies: - - role: setup_botocore_pip +dependencies: [] diff --git a/tests/integration/targets/setup_botocore_pip/defaults/main.yml b/tests/integration/targets/setup_botocore_pip/defaults/main.yml index 16ad00270cc..9745064c991 100644 --- a/tests/integration/targets/setup_botocore_pip/defaults/main.yml +++ b/tests/integration/targets/setup_botocore_pip/defaults/main.yml @@ -1,2 +1,2 @@ -default_botocore_version: '1.21.0' -default_boto3_version: '1.18.0' +default_botocore_version: "{{ lookup('amazon.aws.aws_collection_constants', 'MINIMUM_BOTOCORE_VERSION') }}" +default_boto3_version: "{{ lookup('amazon.aws.aws_collection_constants', 'MINIMUM_BOTO3_VERSION') }}"