Skip to content

Commit

Permalink
aws_collection_constants (#1344)
Browse files Browse the repository at this point in the history
aws_collection_constants

SUMMARY
Adds a simple lookup plugin to retrieve some of the constants in the collection
ISSUE TYPE

New Module Pull Request

COMPONENT NAME
plugins/lookup/aws_collection_constants.py
ADDITIONAL INFORMATION
This means we've one less place we need to update when bumping the supported botocore version.

Reviewed-by: Bikouo Aubin <None>
Reviewed-by: Mark Chappell <None>
Reviewed-by: Alina Buzachis <None>
  • Loading branch information
tremble authored Jan 24, 2023
1 parent 4a066e5 commit e929ca4
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 5 deletions.
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")
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') }}"

0 comments on commit e929ca4

Please sign in to comment.