Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2017 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: elasticbeanstalk_app
10 version_added: 1.0.0
11
12 short_description: Create, update, and delete an Elastic Beanstalk application
13
14 description:
15 - Creates, updates, deletes Elastic Beanstalk applications if I(app_name) is provided.
16 - Prior to release 5.0.0 this module was called C(community.aws.aws_elasticbeanstalk_app).
17 The usage did not change.
18
19 options:
20 app_name:
21 description:
22 - Name of the Beanstalk application you wish to manage.
23 aliases: [ 'name' ]
24 type: str
25 description:
26 description:
27 - The description of the application.
28 type: str
29 state:
30 description:
31 - Whether to ensure the application is present or absent.
32 default: present
33 choices: ['absent','present']
34 type: str
35 terminate_by_force:
36 description:
37 - When I(terminate_by_force=true), running environments will be terminated before deleting the application.
38 default: false
39 type: bool
40 author:
41 - Harpreet Singh (@hsingh)
42 - Stephen Granger (@viper233)
43 extends_documentation_fragment:
44 - amazon.aws.common.modules
45 - amazon.aws.region.modules
46 - amazon.aws.boto3
47 """
48
49 EXAMPLES = r"""
50 # Create or update an application
51 - community.aws.elasticbeanstalk_app:
52 app_name: Sample_App
53 description: "Hello World App"
54 state: present
55
56 # Delete application
57 - community.aws.elasticbeanstalk_app:
58 app_name: Sample_App
59 state: absent
60 """
61
62 RETURN = r"""
63 app:
64 description: Beanstalk application.
65 returned: always
66 type: dict
67 sample: {
68 "ApplicationName": "app-name",
69 "ConfigurationTemplates": [],
70 "DateCreated": "2016-12-28T14:50:03.185000+00:00",
71 "DateUpdated": "2016-12-28T14:50:03.185000+00:00",
72 "Description": "description",
73 "Versions": [
74 "1.0.0",
75 "1.0.1"
76 ]
77 }
78 output:
79 description: Message indicating what change will occur.
80 returned: in check mode
81 type: str
82 sample: App is up-to-date
83 """
84
85 try:
86 import botocore
87 except ImportError:
88 pass # handled by AnsibleAWSModule
89
90 from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_message
91
92 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
93
94
95 def describe_app(ebs, app_name, module):
96 apps = list_apps(ebs, app_name, module)
97
98 return None if len(apps) != 1 else apps[0]
99
100
101 def list_apps(ebs, app_name, module):
102 try:
103 if app_name is not None:
104 apps = ebs.describe_applications(ApplicationNames=[app_name])
105 else:
106 apps = ebs.describe_applications()
107 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
108 module.fail_json_aws(e, msg="Could not describe application")
109
110 return apps.get("Applications", [])
111
112
113 def check_app(ebs, app, module):
114 description = module.params["description"]
115 state = module.params["state"]
116 terminate_by_force = module.params["terminate_by_force"]
117
118 result = {}
119
120 if state == "present" and app is None:
121 result = dict(changed=True, output="App would be created")
122 elif state == "present" and app.get("Description", None) != description:
123 result = dict(changed=True, output="App would be updated", app=app)
124 elif state == "present" and app.get("Description", None) == description:
125 result = dict(changed=False, output="App is up-to-date", app=app)
126 elif state == "absent" and app is None:
127 result = dict(changed=False, output="App does not exist", app={})
128 elif state == "absent" and app is not None:
129 result = dict(changed=True, output="App will be deleted", app=app)
130 elif state == "absent" and app is not None and terminate_by_force is True:
131 result = dict(changed=True, output="Running environments terminated before the App will be deleted", app=app)
132
133 module.exit_json(**result)
134
135
136 def filter_empty(**kwargs):
137 retval = {}
138 for k, v in kwargs.items():
139 if v:
140 retval[k] = v
141 return retval
142
143
144 def main():
145 argument_spec = dict(
146 app_name=dict(aliases=["name"], type="str", required=False),
147 description=dict(),
148 state=dict(choices=["present", "absent"], default="present"),
149 terminate_by_force=dict(type="bool", default=False, required=False),
150 )
151
152 module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)
153
154 app_name = module.params["app_name"]
155 description = module.params["description"]
156 state = module.params["state"]
157 terminate_by_force = module.params["terminate_by_force"]
158
159 if app_name is None:
160 module.fail_json(msg='Module parameter "app_name" is required')
161
162 result = {}
163
164 ebs = module.client("elasticbeanstalk")
165
166 app = describe_app(ebs, app_name, module)
167
168 if module.check_mode:
169 check_app(ebs, app, module)
170 module.fail_json(msg="ASSERTION FAILURE: check_app() should not return control.")
171
172 if state == "present":
173 if app is None:
174 try:
175 ebs.create_application(**filter_empty(ApplicationName=app_name, Description=description))
176 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
177 module.fail_json_aws(e, msg="Could not create application")
178
179 app = describe_app(ebs, app_name, module)
180
181 result = dict(changed=True, app=app)
182 else:
183 if app.get("Description", None) != description:
184 try:
185 if not description:
186 ebs.update_application(ApplicationName=app_name)
187 else:
188 ebs.update_application(ApplicationName=app_name, Description=description)
189 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
190 module.fail_json_aws(e, msg="Could not update application")
191
192 app = describe_app(ebs, app_name, module)
193
194 result = dict(changed=True, app=app)
195 else:
196 result = dict(changed=False, app=app)
197
198 else:
199 if app is None:
200 result = dict(changed=False, output="Application not found", app={})
201 else:
202 try:
203 if terminate_by_force:
204 # Running environments will be terminated before deleting the application
205 ebs.delete_application(ApplicationName=app_name, TerminateEnvByForce=terminate_by_force)
206 else:
207 ebs.delete_application(ApplicationName=app_name)
208 changed = True
209 except is_boto3_error_message("It is currently pending deletion"):
210 changed = False
211 except (
212 botocore.exceptions.ClientError,
213 botocore.exceptions.BotoCoreError,
214 ) as e: # pylint: disable=duplicate-except
215 module.fail_json_aws(e, msg="Cannot terminate app")
216
217 result = dict(changed=changed, app=app)
218
219 module.exit_json(**result)
220
221
222 if __name__ == "__main__":
223 main()