Debian Codegraph @

ansible_collections/amazon/aws/plugins/lookup/aws_account_attribute.py

raw
1 # -*- coding: utf-8 -*-
2
3 # (c) 2017 Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 DOCUMENTATION = r"""
7 name: aws_account_attribute
8 author:
9 - Sloane Hertel (@s-hertel) <[email protected]>
10 short_description: Look up AWS account attributes
11 description:
12 - Describes attributes of your AWS account. You can specify one of the listed
13 attribute choices or omit it to see all attributes.
14 options:
15 attribute:
16 description: The attribute for which to get the value(s).
17 choices:
18 - supported-platforms
19 - default-vpc
20 - max-instances
21 - vpc-max-security-groups-per-interface
22 - max-elastic-ips
23 - vpc-max-elastic-ips
24 - has-ec2-classic
25 extends_documentation_fragment:
26 - amazon.aws.boto3
27 - amazon.aws.common.plugins
28 - amazon.aws.region.plugins
29 """
30
31 EXAMPLES = r"""
32 vars:
33 has_ec2_classic: "{{ lookup('aws_account_attribute', attribute='has-ec2-classic') }}"
34 # true | false
35
36 default_vpc_id: "{{ lookup('aws_account_attribute', attribute='default-vpc') }}"
37 # vpc-xxxxxxxx | none
38
39 account_details: "{{ lookup('aws_account_attribute', wantlist='true') }}"
40 # {'default-vpc': ['vpc-xxxxxxxx'], 'max-elastic-ips': ['5'], 'max-instances': ['20'],
41 # 'supported-platforms': ['VPC', 'EC2'], 'vpc-max-elastic-ips': ['5'], 'vpc-max-security-groups-per-interface': ['5']}
42 """
43
44 RETURN = r"""
45 _raw:
46 description:
47 - Returns a boolean when O(attribute=check_ec2_classic). Otherwise returns the value(s) of the attribute
48 (or all attributes if one is not specified).
49 """
50
51 try:
52 import botocore
53 except ImportError:
54 pass # Handled by AWSLookupBase
55
56 from ansible.errors import AnsibleLookupError
57 from ansible.module_utils.common.text.converters import to_native
58
59 from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
60 from ansible_collections.amazon.aws.plugins.plugin_utils.lookup import AWSLookupBase
61
62
63 def _describe_account_attributes(client, **params):
64 return client.describe_account_attributes(aws_retry=True, **params)
65
66
67 class LookupModule(AWSLookupBase):
68 def run(self, terms, variables, **kwargs):
69 super().run(terms, variables, **kwargs)
70
71 client = self.client("ec2", AWSRetry.jittered_backoff())
72
73 attribute = kwargs.get("attribute")
74 params = {"AttributeNames": []}
75 check_ec2_classic = False
76 if "has-ec2-classic" == attribute:
77 check_ec2_classic = True
78 params["AttributeNames"] = ["supported-platforms"]
79 elif attribute:
80 params["AttributeNames"] = [attribute]
81
82 try:
83 response = _describe_account_attributes(client, **params)["AccountAttributes"]
84 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
85 raise AnsibleLookupError(f"Failed to describe account attributes: {to_native(e)}")
86
87 if check_ec2_classic:
88 attr = response[0]
89 return [any(value["AttributeValue"] == "EC2" for value in attr["AttributeValues"])]
90
91 if attribute:
92 attr = response[0]
93 return [value["AttributeValue"] for value in attr["AttributeValues"]]
94
95 flattened = {}
96 for k_v_dict in response:
97 flattened[k_v_dict["AttributeName"]] = [value["AttributeValue"] for value in k_v_dict["AttributeValues"]]
98 return [flattened]