Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_batchaccountapplicationpackage.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 DOCUMENTATION = '''
11 ---
12 module: azure_rm_batchaccountapplicationpackage
13 version_added: "3.0.0"
14 short_description: Manages a Batch Account Application Package on Azure
15 description:
16 - Create, update and delete instance of Azure Batch Account Application Package.
17
18 options:
19 resource_group:
20 description:
21 - The name of the resource group in which to create the Batch Account Application Package.
22 required: true
23 type: str
24 batch_account_name:
25 description:
26 - The name of the Batch Account.
27 required: true
28 type: str
29 application_name:
30 description:
31 - The name of the Batch Account Application.
32 type: str
33 required: true
34 name:
35 description:
36 - The name of the Batch Account Application Package.
37 required: true
38 type: str
39 format:
40 description:
41 - The format of the application package, if the package is active.
42 - Sample as C(zip).
43 type: str
44 is_activate:
45 description:
46 - Whether to activates the specified application package.
47 type: bool
48 default: false
49 state:
50 description:
51 - Assert the state of the Batch Account Application Package.
52 - Use C(present) to create or update a Batch Account Application Package and C(absent) to delete it.
53 default: present
54 type: str
55 choices:
56 - present
57 - absent
58
59 extends_documentation_fragment:
60 - azure.azcollection.azure
61
62 author:
63 - xuzhang3 (@xuzhang3)
64 - Fred Sun (@Fred-sun)
65 '''
66
67 EXAMPLES = '''
68 - name: Create Batch Account Application Package
69 azure_rm_batchaccountapplicationpackage:
70 resource_group: MyResGroup
71 application_name: mybatchaccountapplication
72 batch_account_name: mybatchaccount
73 name: version01
74
75 - name: Activate the Batch Account Application Package
76 azure_rm_batchaccountapplicationpackage:
77 resource_group: MyResGroup
78 application_name: mybatchaccountapplication
79 batch_account_name: mybatchaccount
80 name: version01
81 is_activate: true
82 format: zip
83
84 - name: Delete Batch Account Application Package
85 azure_rm_batchaccountapplicationpackage:
86 resource_group: MyResGroup
87 application_name: mybatchaccountapplication
88 batch_account_name: mybatchaccount
89 name: version01
90 state: absent
91 '''
92
93 RETURN = '''
94 state:
95 description:
96 - The specified application package/
97 type: complex
98 returned: always
99 contains:
100 id:
101 description:
102 - The ID of the application package.
103 type: str
104 returned: always
105 sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Batch/batchAccounts/batch01/applications/app01/versions/version01"
106 resource_group:
107 description:
108 - The resource group of the application package.
109 type: str
110 returned: always
111 sample: testRG
112 name:
113 description:
114 - The name of the resource.
115 type: str
116 returned: always
117 sample: version01
118 batch_account_name:
119 description:
120 - The name of the batch account.
121 type: str
122 returned: always
123 sample: batch01
124 application_name:
125 description:
126 - The name of the application.
127 type: str
128 returned: always
129 sample: app01
130 etag:
131 description:
132 - The ETag of the resource, used for concurrency statements.
133 type: str
134 returned: always
135 sample: 0x8DCFCE1E9B31502
136 format:
137 description:
138 - The format of the application package, if the package is active.
139 type: str
140 returned: always
141 sample: zip
142 last_activation_time:
143 description:
144 - The time at which the package was last activated, if the package is active.
145 type: str
146 returned: always
147 sample: '2024-11-04T15:03:59.834538Z'
148 state:
149 description:
150 - The current state of the application package.
151 type: str
152 returned: always
153 sample: Active
154 storage_url:
155 description:
156 - The URL for the application package in Azure Storage.
157 type: str
158 returned: always
159 sample: null
160 storage_url_expiry:
161 description:
162 - The UTC time at which the Azure Storage URL will expire.
163 type: str
164 returned: always
165 sample: null
166 type:
167 description:
168 - The type of the batch account application package.
169 type: str
170 returned: always
171 sample: Microsoft.Batch/batchAccounts/applications/versions
172 '''
173
174 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBase
175
176 try:
177 from azure.core.polling import LROPoller
178 from azure.core.exceptions import HttpResponseError
179 except ImportError:
180 # This is handled in azure_rm_common
181 pass
182
183
184 class AzureRMBatchAccountApplicationPackage(AzureRMModuleBase):
185 """Configuration class for an Azure RM Batch Account Application Package resource"""
186
187 def __init__(self):
188 self.module_arg_spec = dict(
189 resource_group=dict(
190 required=True,
191 type='str'
192 ),
193 batch_account_name=dict(
194 type='str',
195 required=True,
196 ),
197 application_name=dict(
198 type='str',
199 required=True,
200 ),
201 name=dict(
202 required=True,
203 type='str'
204 ),
205 format=dict(
206 type='str'
207 ),
208 is_activate=dict(
209 type='bool',
210 default=False
211 ),
212 state=dict(
213 type='str',
214 default='present',
215 choices=['present', 'absent']
216 )
217 )
218
219 required_if = [('is_activate', True, ['format'])]
220 self.resource_group = None
221 self.batch_account_name = None
222 self.application_name = None
223 self.name = None
224 self.is_activate = None
225 self.results = dict(changed=False)
226 self.state = None
227 self.body = dict()
228
229 super(AzureRMBatchAccountApplicationPackage, self).__init__(derived_arg_spec=self.module_arg_spec,
230 supports_check_mode=True,
231 required_if=required_if,
232 supports_tags=False)
233
234 def exec_module(self, **kwargs):
235 """Main module execution method"""
236
237 for key in list(self.module_arg_spec.keys()):
238 if hasattr(self, key):
239 setattr(self, key, kwargs[key])
240 elif kwargs[key] is not None:
241 self.body[key] = kwargs[key]
242
243 response = None
244 changed = False
245
246 old_response = self.get_application_package()
247
248 if not old_response:
249 self.log("Batch Account Application Package instance doesn't exist")
250 if self.state == 'absent':
251 self.log("Old instance didn't exist")
252 else:
253 if not self.check_mode:
254 changed = True
255 response = self.create_application_package()
256 if self.is_activate:
257 self.activate_application_package()
258 else:
259 self.log("Batch Account Application Package instance already exists")
260 if self.state == 'absent':
261 if not self.check_mode:
262 changed = True
263 response = self.delete_application_package()
264 else:
265 if not self.check_mode:
266 if self.is_activate:
267 changed = True
268 self.activate_application_package()
269
270 self.results = dict(
271 changed=changed,
272 state=response if response else old_response,
273 )
274 return self.results
275
276 def create_application_package(self):
277 '''
278 Creates Batch Account Application Package with the specified configuration.
279 '''
280 self.log("Creating the Batch Account Application Package instance {0}".format(self.name))
281
282 try:
283 response = self.batch_account_client.application_package.create(resource_group_name=self.resource_group,
284 account_name=self.batch_account_name,
285 application_name=self.application_name,
286 version_name=self.name,
287 parameters=self.body)
288 if isinstance(response, LROPoller):
289 response = self.get_poller_result(response)
290 except Exception as exc:
291 self.log('Error attempting to create the Batch Account Application Package instance.')
292 self.fail("Error creating the Batch Account Application Package instance: {0}".format(str(exc)))
293 return response.as_dict()
294
295 def activate_application_package(self):
296 '''
297 Update Batch Account Application Package with the specified configuration.
298 '''
299 self.log("Updating the Batch Account Application Package instance {0}".format(self.name))
300
301 try:
302 response = self.batch_account_client.application_package.activate(resource_group_name=self.resource_group,
303 account_name=self.batch_account_name,
304 application_name=self.application_name,
305 version_name=self.name,
306 parameters=self.body)
307 if isinstance(response, LROPoller):
308 response = self.get_poller_result(response)
309 except Exception as exc:
310 self.log('Error attempting to update the Batch Account Application Package instance.')
311 self.fail("Error updating the Batch Account Application Package instance: {0}".format(str(exc)))
312 return response.as_dict()
313
314 def delete_application_package(self):
315 '''
316 Deletes specified Batch Account Application Package instance in the specified subscription and resource group.
317 :return: True
318 '''
319 self.log("Deleting the Batch Account Application Package instance {0}".format(self.name))
320 try:
321 self.batch_account_client.application_package.delete(resource_group_name=self.resource_group,
322 account_name=self.batch_account_name,
323 application_name=self.application_name,
324 version_name=self.name)
325 except Exception as e:
326 self.log('Error attempting to delete the Batch Account Application Package instance.')
327 self.fail("Error deleting the Batch Account Application Package instance: {0}".format(str(e)))
328
329 return True
330
331 def get_application_package(self):
332 '''
333 Gets the properties of the specified Batch Account Application Package
334 :return: deserialized Batch Account Application Package instance state dictionary
335 '''
336 self.log("Checking if the Batch Account Application Package instance {0} is present".format(self.name))
337 found = False
338 try:
339 response = self.batch_account_client.application_package.get(resource_group_name=self.resource_group,
340 account_name=self.batch_account_name,
341 application_name=self.application_name,
342 version_name=self.name)
343 found = True
344 self.log("Response : {0}".format(response))
345 self.log("Batch Account Application Package instance : {0} found".format(response.name))
346 except HttpResponseError as e:
347 self.log('Did not find the Batch Account Application Package instance. Exception as {0}'.format(e))
348 if found is True:
349 return self.format_item(response.as_dict())
350 return False
351
352 def format_item(self, item):
353 result = {
354 'resource_group': self.resource_group,
355 'batch_account_name': self.batch_account_name,
356 'application_name': self.application_name,
357 'id': item['id'],
358 'name': item['name'],
359 'type': item['type'],
360 'etag': item['etag'],
361 'state': item['state'],
362 'format': item.get('format'),
363 'storage_url': item.get('storage_url'),
364 'storage_url_expiry': item.get('storage_url_expiry'),
365 'last_activation_time': item.get('last_activation_time'),
366 } if item is not None else None
367 return result
368
369
370 def main():
371 """Main execution"""
372 AzureRMBatchAccountApplicationPackage()
373
374
375 if __name__ == '__main__':
376 main()