summaryrefslogtreecommitdiffstats
path: root/ansible_collections/amazon/aws/plugins/lookup/aws_collection_constants.py
blob: c03f144504e45125bd72e2b0dab0b1afe8457d4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-

# (c) 2023 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

DOCUMENTATION = r"""
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 = r"""
"""

RETURN = r"""
_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):  # pylint: disable=too-many-return-statements
        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)]