Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2017 Jon Meran <[email protected]>
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: batch_job_queue
10 version_added: 1.0.0
11 short_description: Manage AWS Batch Job Queues
12 description:
13 - This module allows the management of AWS Batch Job Queues.
14 - It is idempotent and supports "Check" mode.
15 - Use module M(community.aws.batch_compute_environment) to manage the compute
16 environment, M(community.aws.batch_job_queue) to manage job queues, M(community.aws.batch_job_definition) to manage job definitions.
17 - Prior to release 5.0.0 this module was called C(community.aws.aws_batch_job_queue).
18 The usage did not change.
19 author:
20 - Jon Meran (@jonmer85)
21 options:
22 job_queue_name:
23 description:
24 - The name for the job queue.
25 required: true
26 type: str
27 state:
28 description:
29 - Describes the desired state.
30 default: "present"
31 choices: ["present", "absent"]
32 type: str
33 job_queue_state:
34 description:
35 - The state of the job queue. If the job queue state is ENABLED, it is able to accept jobs.
36 default: "ENABLED"
37 choices: ["ENABLED", "DISABLED"]
38 type: str
39 priority:
40 description:
41 - The priority of the job queue. Job queues with a higher priority (or a lower integer value for the priority
42 parameter) are evaluated first when associated with same compute environment. Priority is determined in
43 ascending order, for example, a job queue with a priority value of 1 is given scheduling preference over a job
44 queue with a priority value of 10.
45 required: true
46 type: int
47 compute_environment_order:
48 description:
49 - The set of compute environments mapped to a job queue and their order relative to each other. The job
50 scheduler uses this parameter to determine which compute environment should execute a given job. Compute
51 environments must be in the VALID state before you can associate them with a job queue. You can associate up to
52 3 compute environments with a job queue.
53 required: true
54 type: list
55 elements: dict
56 suboptions:
57 order:
58 type: int
59 description: The relative priority of the environment.
60 compute_environment:
61 type: str
62 description: The name of the compute environment.
63 extends_documentation_fragment:
64 - amazon.aws.common.modules
65 - amazon.aws.region.modules
66 - amazon.aws.boto3
67 """
68
69 EXAMPLES = r"""
70 - name: My Batch Job Queue
71 community.aws.batch_job_queue:
72 job_queue_name: jobQueueName
73 state: present
74 region: us-east-1
75 job_queue_state: ENABLED
76 priority: 1
77 compute_environment_order:
78 - order: 1
79 compute_environment: my_compute_env1
80 - order: 2
81 compute_environment: my_compute_env2
82 register: batch_job_queue_action
83
84 - name: show results
85 ansible.builtin.debug:
86 var: batch_job_queue_action
87 """
88
89 RETURN = r"""
90 ---
91 output:
92 description: "returns what action was taken, whether something was changed, invocation and response"
93 returned: always
94 sample:
95 batch_job_queue_action: updated
96 changed: false
97 response:
98 job_queue_arn: "arn:aws:batch:...."
99 job_queue_name: <name>
100 priority: 1
101 state: DISABLED
102 status: UPDATING
103 status_reason: "JobQueue Healthy"
104 type: dict
105 """
106
107 try:
108 from botocore.exceptions import BotoCoreError
109 from botocore.exceptions import ClientError
110 except ImportError:
111 pass # Handled by AnsibleAWSModule
112
113 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
114
115 from ansible_collections.amazon.aws.plugins.module_utils.batch import set_api_params
116
117 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
118
119 # ---------------------------------------------------------------------------------------------------
120 #
121 # Helper Functions & classes
122 #
123 # ---------------------------------------------------------------------------------------------------
124
125
126 def validate_params(module):
127 """
128 Performs basic parameter validation.
129
130 :param module:
131 """
132 return
133
134
135 # ---------------------------------------------------------------------------------------------------
136 #
137 # Batch Job Queue functions
138 #
139 # ---------------------------------------------------------------------------------------------------
140
141
142 def get_current_job_queue(module, client):
143 try:
144 environments = client.describe_job_queues(jobQueues=[module.params["job_queue_name"]])
145 return environments["jobQueues"][0] if len(environments["jobQueues"]) > 0 else None
146 except ClientError:
147 return None
148
149
150 def create_job_queue(module, client):
151 """
152 Adds a Batch job queue
153
154 :param module:
155 :param client:
156 :return:
157 """
158
159 changed = False
160
161 # set API parameters
162 params = ("job_queue_name", "priority")
163 api_params = set_api_params(module, params)
164
165 if module.params["job_queue_state"] is not None:
166 api_params["state"] = module.params["job_queue_state"]
167
168 api_params["computeEnvironmentOrder"] = get_compute_environment_order_list(module)
169
170 try:
171 if not module.check_mode:
172 client.create_job_queue(**api_params)
173 changed = True
174 except (BotoCoreError, ClientError) as e:
175 module.fail_json_aws(e, msg="Error creating compute environment")
176
177 return changed
178
179
180 def get_compute_environment_order_list(module):
181 compute_environment_order_list = []
182 for ceo in module.params["compute_environment_order"]:
183 compute_environment_order_list.append(dict(order=ceo["order"], computeEnvironment=ceo["compute_environment"]))
184 return compute_environment_order_list
185
186
187 def remove_job_queue(module, client):
188 """
189 Remove a Batch job queue
190
191 :param module:
192 :param client:
193 :return:
194 """
195
196 changed = False
197
198 # set API parameters
199 api_params = {"jobQueue": module.params["job_queue_name"]}
200
201 try:
202 if not module.check_mode:
203 client.delete_job_queue(**api_params)
204 changed = True
205 except (BotoCoreError, ClientError) as e:
206 module.fail_json_aws(e, msg="Error removing job queue")
207 return changed
208
209
210 def manage_state(module, client):
211 changed = False
212 current_state = "absent"
213 state = module.params["state"]
214 job_queue_state = module.params["job_queue_state"]
215 job_queue_name = module.params["job_queue_name"]
216 priority = module.params["priority"]
217 action_taken = "none"
218 response = None
219
220 check_mode = module.check_mode
221
222 # check if the job queue exists
223 current_job_queue = get_current_job_queue(module, client)
224 if current_job_queue:
225 current_state = "present"
226
227 if state == "present":
228 if current_state == "present":
229 updates = False
230 # Update Batch Job Queue configuration
231 job_kwargs = {"jobQueue": job_queue_name}
232
233 # Update configuration if needed
234 if job_queue_state and current_job_queue["state"] != job_queue_state:
235 job_kwargs.update({"state": job_queue_state})
236 updates = True
237 if priority is not None and current_job_queue["priority"] != priority:
238 job_kwargs.update({"priority": priority})
239 updates = True
240
241 new_compute_environment_order_list = get_compute_environment_order_list(module)
242 if new_compute_environment_order_list != current_job_queue["computeEnvironmentOrder"]:
243 job_kwargs["computeEnvironmentOrder"] = new_compute_environment_order_list
244 updates = True
245
246 if updates:
247 try:
248 if not check_mode:
249 client.update_job_queue(**job_kwargs)
250 changed = True
251 action_taken = "updated"
252 except (BotoCoreError, ClientError) as e:
253 module.fail_json_aws(e, msg="Unable to update job queue")
254
255 else:
256 # Create Job Queue
257 changed = create_job_queue(module, client)
258 action_taken = "added"
259
260 # Describe job queue
261 response = get_current_job_queue(module, client)
262 if not response:
263 module.fail_json(msg="Unable to get job queue information after creating/updating")
264 else:
265 if current_state == "present":
266 # remove the Job Queue
267 changed = remove_job_queue(module, client)
268 action_taken = "deleted"
269 return dict(changed=changed, batch_job_queue_action=action_taken, response=response)
270
271
272 # ---------------------------------------------------------------------------------------------------
273 #
274 # MAIN
275 #
276 # ---------------------------------------------------------------------------------------------------
277
278
279 def main():
280 """
281 Main entry point.
282
283 :return dict: changed, batch_job_queue_action, response
284 """
285
286 argument_spec = dict(
287 state=dict(required=False, default="present", choices=["present", "absent"]),
288 job_queue_name=dict(required=True),
289 job_queue_state=dict(required=False, default="ENABLED", choices=["ENABLED", "DISABLED"]),
290 priority=dict(type="int", required=True),
291 compute_environment_order=dict(type="list", required=True, elements="dict"),
292 )
293
294 module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)
295
296 client = module.client("batch")
297
298 validate_params(module)
299
300 results = manage_state(module, client)
301
302 module.exit_json(**camel_dict_to_snake_dict(results))
303
304
305 if __name__ == "__main__":
306 main()