Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_collection_constants #1344

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/aws_collection_constants.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
trivial:
- Update integration tests to use lookup plugin when pulling the default AWS SDK versions
81 changes: 81 additions & 0 deletions plugins/lookup/aws_collection_constants.py
Original file line number Diff line number Diff line change
@@ -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")
Comment on lines +75 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of raising an error, why not return all constant values?
this needs to change the return format as dictionnary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't think of a use case where we'd want to dump out all of them, so was trying to follow the KISS Principle

if len(terms) > 1:
raise AnsibleLookupError("Multiple constant names provided")
name = terms[0].upper()

return [self.lookup_constant(name)]
2 changes: 1 addition & 1 deletion tests/integration/targets/ec2_key/meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ dependencies:
- setup_sshkey
- role: setup_botocore_pip
vars:
botocore_version: '1.21.23'
botocore_version: "1.21.23"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cloud/aws
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dependencies: []
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 1 addition & 2 deletions tests/integration/targets/s3_bucket/meta/main.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
dependencies:
- role: setup_botocore_pip
dependencies: []
Original file line number Diff line number Diff line change
@@ -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') }}"