Debian Codegraph @

ansible_collections/community/aws/plugins/modules/lightsail_static_ip.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: Ansible Project
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 DOCUMENTATION = r"""
8 ---
9 module: lightsail_static_ip
10 version_added: 4.1.0
11 short_description: Manage static IP addresses in AWS Lightsail
12 description:
13 - Manage static IP addresses in AWS Lightsail.
14 author:
15 - "Daniel Cotton (@danielcotton)"
16 options:
17 state:
18 description:
19 - Describes the desired state.
20 default: present
21 choices: ['present', 'absent']
22 type: str
23 name:
24 description: Name of the static IP.
25 required: true
26 type: str
27 extends_documentation_fragment:
28 - amazon.aws.common.modules
29 - amazon.aws.region.modules
30 - amazon.aws.boto3
31 """
32
33
34 EXAMPLES = r"""
35 - name: Provision a Lightsail static IP
36 community.aws.lightsail_static_ip:
37 state: present
38 name: my_static_ip
39 register: my_ip
40
41 - name: Remove a static IP
42 community.aws.lightsail_static_ip:
43 state: absent
44 name: my_static_ip
45 """
46
47 RETURN = r"""
48 static_ip:
49 description: static_ipinstance data
50 returned: always
51 type: dict
52 sample:
53 arn: "arn:aws:lightsail:ap-southeast-2:123456789012:StaticIp/d8f47672-c261-4443-a484-4a2ec983db9a"
54 created_at: "2021-02-28T00:04:05.202000+10:30"
55 ip_address: "192.0.2.5"
56 is_attached: false
57 location:
58 availability_zone: all
59 region_name: ap-southeast-2
60 name: "static_ip"
61 resource_type: StaticIp
62 support_code: "123456789012/192.0.2.5"
63 """
64
65 try:
66 import botocore
67 except ImportError:
68 # will be caught by AnsibleAWSModule
69 pass
70
71 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
72
73 from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code
74
75 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
76
77
78 def find_static_ip_info(module, client, static_ip_name, fail_if_not_found=False):
79 try:
80 res = client.get_static_ip(staticIpName=static_ip_name)
81 except is_boto3_error_code("NotFoundException") as e:
82 if fail_if_not_found:
83 module.fail_json_aws(e)
84 return None
85 except botocore.exceptions.ClientError as e: # pylint: disable=duplicate-except
86 module.fail_json_aws(e)
87 return res["staticIp"]
88
89
90 def create_static_ip(module, client, static_ip_name):
91 inst = find_static_ip_info(module, client, static_ip_name)
92 if inst:
93 module.exit_json(changed=False, static_ip=camel_dict_to_snake_dict(inst))
94 else:
95 create_params = {"staticIpName": static_ip_name}
96
97 try:
98 client.allocate_static_ip(**create_params)
99 except botocore.exceptions.ClientError as e:
100 module.fail_json_aws(e)
101
102 inst = find_static_ip_info(module, client, static_ip_name, fail_if_not_found=True)
103
104 module.exit_json(changed=True, static_ip=camel_dict_to_snake_dict(inst))
105
106
107 def delete_static_ip(module, client, static_ip_name):
108 inst = find_static_ip_info(module, client, static_ip_name)
109 if inst is None:
110 module.exit_json(changed=False, static_ip={})
111
112 changed = False
113 try:
114 client.release_static_ip(staticIpName=static_ip_name)
115 changed = True
116 except botocore.exceptions.ClientError as e:
117 module.fail_json_aws(e)
118
119 module.exit_json(changed=changed, static_ip=camel_dict_to_snake_dict(inst))
120
121
122 def main():
123 argument_spec = dict(
124 name=dict(type="str", required=True),
125 state=dict(type="str", default="present", choices=["present", "absent"]),
126 )
127
128 module = AnsibleAWSModule(argument_spec=argument_spec)
129
130 client = module.client("lightsail")
131
132 name = module.params.get("name")
133 state = module.params.get("state")
134
135 if state == "present":
136 create_static_ip(module, client, name)
137 elif state == "absent":
138 delete_static_ip(module, client, name)
139
140
141 if __name__ == "__main__":
142 main()