Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
4 #
5 #
6 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11
12 DOCUMENTATION = '''
13 ---
14 module: azure_rm_storageaccountmanagementpolicy
15 version_added: "2.4.0"
16 short_description: Manage storage account management policies
17 description:
18 - Create, update or delete storage account management policies.
19 options:
20 resource_group:
21 description:
22 - Name of the resource group to use.
23 required: true
24 type: str
25 aliases:
26 - resource_group_name
27 storage_account_name:
28 description:
29 - Name of the storage account.
30 type: str
31 required: true
32 rules:
33 description:
34 - The Storage Account ManagementPolicies Rules.
35 type: list
36 elements: dict
37 suboptions:
38 name:
39 description:
40 - The name of the policy rule.
41 - A rule name can contain any combination of alpha numeric characters.
42 type: str
43 required: true
44 type:
45 description:
46 - The type of the policy rule.
47 type: str
48 required: true
49 choices:
50 - Lifecycle
51 enabled:
52 description:
53 - Whether to enabled the rule
54 type: bool
55 definition:
56 description:
57 - Whether to enabled the rule
58 required: true
59 type: dict
60 suboptions:
61 actions:
62 description:
63 - An object that defines the action set.
64 type: dict
65 required: true
66 suboptions:
67 base_blob:
68 description:
69 - The management policy action for base blob.
70 type: dict
71 suboptions:
72 tier_to_cool:
73 description:
74 - The function to tier blobs to cool storage.
75 - Support blobs currently at Hot tier.
76 type: dict
77 suboptions:
78 days_after_modification_greater_than:
79 description:
80 - Value indicating the age in days after last modification.
81 type: float
82 days_after_last_access_time_greater_than:
83 description:
84 - This property can only be used in conjunction with last access time tracking policy.
85 type: float
86 tier_to_archive:
87 description:
88 - The function to tier blobs to archive storage.
89 - Support blobs currently at Hot or Cool tier.
90 type: dict
91 suboptions:
92 days_after_modification_greater_than:
93 description:
94 - Value indicating the age in days after last modification.
95 type: float
96 days_after_last_access_time_greater_than:
97 description:
98 - This property can only be used in conjunction with last access time tracking policy.
99 type: float
100 delete:
101 description:
102 - The function to delete the blob.
103 type: dict
104 suboptions:
105 days_after_modification_greater_than:
106 description:
107 - Value indicating the age in days after last modification.
108 type: float
109 days_after_last_access_time_greater_than:
110 description:
111 - This property can only be used in conjunction with last access time tracking policy.
112 type: float
113 enable_auto_tier_to_hot_from_cool:
114 description:
115 - This property enables auto tiering of a blob from cool to hot on a blob access.
116 type: bool
117 snapshot:
118 description:
119 - The management policy action for snapshot.
120 type: dict
121 suboptions:
122 tier_to_cool:
123 description:
124 - The function to tier blob snapshot to cool storage.
125 - Support blob snapshot at Hot tier.
126 type: dict
127 suboptions:
128 days_after_creation_greater_than:
129 description:
130 - Value indicating the age in days after creation.
131 type: float
132 required: true
133 tier_to_archive:
134 description:
135 - The function to tier blob snapshot to archive storage.
136 - Support blob snapshot currently at Hot or Cool tier.
137 type: dict
138 suboptions:
139 days_after_creation_greater_than:
140 description:
141 - Value indicating the age in days after creation.
142 type: float
143 required: true
144 delete:
145 description:
146 - The function to delete the blob snapshot.
147 type: dict
148 suboptions:
149 days_after_creation_greater_than:
150 description:
151 - Value indicating the age in days after creation.
152 type: float
153 required: true
154 version:
155 description:
156 - The management policy action for version.
157 type: dict
158 suboptions:
159 tier_to_cool:
160 description:
161 - The function to tier blob version to cool storage.
162 - Support blob version currently at Hot tier.
163 type: dict
164 suboptions:
165 days_after_creation_greater_than:
166 description:
167 - Value indicating the age in days after creation.
168 type: float
169 required: true
170 tier_to_archive:
171 description:
172 - The function to tier blob version to archive storage.
173 - Support blob version currently at Hot or Cool tier.
174 type: dict
175 suboptions:
176 days_after_creation_greater_than:
177 description:
178 - Value indicating the age in days after creation.
179 type: float
180 required: true
181 delete:
182 description:
183 - The function to delete the blob version.
184 type: dict
185 suboptions:
186 days_after_creation_greater_than:
187 description:
188 - Value indicating the age in days after creation.
189 type: float
190 required: true
191 filters:
192 description:
193 - An object that defines the filter set.
194 type: dict
195 suboptions:
196 prefix_match:
197 description:
198 - An array of strings for prefixes to be match.
199 type: list
200 elements: str
201 blob_types:
202 description:
203 - An array of predefined enum values.
204 - Currently blockBlob supports all tiering and delete actions. Only delete actions are supported for C(appendBlob).
205 type: list
206 required: true
207 elements: str
208 choices:
209 - blockBlob
210 - appendBlob
211 blob_index_match:
212 description:
213 - An array of blob index tag based filters, there can be at most 10 tag filters.
214 type: list
215 elements: dict
216 suboptions:
217 name:
218 description:
219 - This is the filter tag name, it can have 1 - 128 characters.
220 type: str
221 required: true
222 op:
223 description:
224 - This is the comparison operator which is used for object comparison and filtering.
225 - Only C(==) (equality operator) is currently supported.
226 type: str
227 required: true
228 value:
229 description:
230 - This is the filter tag value field used for tag based filtering.
231 - It can have 0-256 characters.
232 type: str
233 required: true
234 state:
235 description:
236 - State of the storage account managed policy. Use C(present) add or update the policy rule.
237 - Use C(absent) to delete all policy rules.
238 default: present
239 type: str
240 choices:
241 - absent
242 - present
243
244 extends_documentation_fragment:
245 - azure.azcollection.azure
246
247 author:
248 - xuzhang3 (@xuzhang3)
249 - Fred-sun (@Fred-sun)
250 '''
251
252 EXAMPLES = '''
253 - name: Create storage account management policy with multi parameters
254 azure_rm_storageaccountmanagementpolicy:
255 resource_group: testRG
256 storage_account_name: testaccount
257 rules:
258 - name: olcmtest5
259 type: Lifecycle
260 enabled: false
261 definition:
262 actions:
263 base_blob:
264 enable_auto_tier_to_hot_from_cool: true
265 delete:
266 days_after_modification_greater_than: 33
267 days_after_last_access_time_greater_than: 33
268 tier_to_cool:
269 days_after_modification_greater_than: 33
270 days_after_last_access_time_greater_than: 33
271 tier_to_archive:
272 days_after_modification_greater_than: 33
273 days_after_last_access_time_greater_than: 33
274 snapshot:
275 tier_to_cool:
276 days_after_creation_greater_than: 33
277 tier_to_archive:
278 days_after_creation_greater_than: 33
279 delete:
280 days_after_creation_greater_than: 33
281 version:
282 tier_to_archive:
283 days_after_creation_greater_than: 33
284 tier_to_cool:
285 days_after_creation_greater_than: 33
286 delete:
287 days_after_creation_greater_than: 33
288 filters:
289 prefix_match:
290 - olcmtestcontainer2
291 blob_types:
292 - blockBlob
293 - appendBlob
294 blob_index_match:
295 - name: tags3
296 op: '=='
297 value: value3
298
299 - name: Delete management policy rules
300 azure_rm_storageaccountmanagementpolicy:
301 resource_group: "{{ resource_group }}"
302 storage_account_name: "st{{ rpfx }}"
303 state: absent
304 '''
305
306
307 RETURN = '''
308 state:
309 description:
310 - The data policy rules associated with the specified storage account.
311 returned: always
312 type: complex
313 contains:
314 id:
315 description:
316 - The data policy's ID.
317 returned: always
318 type: str
319 sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Storage/storageAccounts/sttest/managementPolicies/default"
320 resource_group:
321 description:
322 - The resource group name.
323 returned: always
324 type: str
325 sample: testRG
326 storage_account_name:
327 description:
328 - The storage account name.
329 returned: always
330 type: str
331 sample: teststname
332 type:
333 description:
334 - The type of the resource.
335 returned: always
336 type: str
337 sample: "Microsoft.Storage/storageAccounts/managementPolicies"
338 last_modified_time:
339 description:
340 - Returns the date and time the ManagementPolicies was last modified.
341 returned: always
342 type: str
343 sample: "2024-04-12T11:40:10.376465+00:00"
344 name:
345 description:
346 - The name of the resource.
347 returned: always
348 type: str
349 sample: DefaultManagementPolicy
350 policy:
351 description:
352 - The Storage Account ManagementPolicy.
353 returned: always
354 type: complex
355 contains:
356 rules:
357 description:
358 - The Storage Account ManagementPolicies Rules.
359 type: list
360 returned: always
361 sample: [
362 {
363 "definition": {
364 "actions": {
365 "base_blob": {
366 "delete": {
367 "days_after_last_access_time_greater_than": 33.0,
368 "days_after_modification_greater_than": 33.0
369 },
370 "enable_auto_tier_to_hot_from_cool": true,
371 "tier_to_archive": {
372 "days_after_last_access_time_greater_than": 33.0,
373 "days_after_modification_greater_than": 33.0
374 },
375 "tier_to_cool": {
376 "days_after_last_access_time_greater_than": 33.0,
377 "days_after_modification_greater_than": 33.0
378 }
379 },
380 "snapshot": {
381 "delete": {
382 "days_after_creation_greater_than": 33.0
383 },
384 "tier_to_archive": {
385 "days_after_creation_greater_than": 33.0
386 },
387 "tier_to_cool": {
388 "days_after_creation_greater_than": 33.0
389 }
390 },
391 "version": {
392 "delete": {
393 "days_after_creation_greater_than": 33.0
394 },
395 "tier_to_archive": {
396 "days_after_creation_greater_than": 33.0
397 },
398 "tier_to_cool": {
399 "days_after_creation_greater_than": 33.0
400 }
401 }
402 },
403 "filters": {
404 "blob_index_match": [
405 {
406 "name": "tags3",
407 "op": "==",
408 "value": "value3"
409 }
410 ],
411 "blob_types": [
412 "blockBlob",
413 "appendBlob"
414 ],
415 "prefix_match": [
416 "olcmtestcontainer2"
417 ]
418 }
419 },
420 "enabled": false,
421 "name": "olcmtest5",
422 "type": "Lifecycle"
423 }
424 ]
425 '''
426
427
428 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
429 try:
430 from azure.core.exceptions import ResourceNotFoundError
431 except Exception:
432 # This is handled in azure_rm_common
433 pass
434
435
436 class AzureRMStorageAccountManagementPolicy(AzureRMModuleBaseExt):
437
438 def __init__(self):
439
440 self.module_arg_spec = dict(
441 resource_group=dict(required=True, type='str', aliases=['resource_group_name']),
442 storage_account_name=dict(type='str', required=True),
443 state=dict(default='present', choices=['present', 'absent']),
444 rules=dict(
445 type='list',
446 elements='dict',
447 options=dict(
448 enabled=dict(type='bool'),
449 name=dict(type='str', required=True),
450 type=dict(type='str', required=True, choices=['Lifecycle']),
451 definition=dict(
452 type='dict',
453 required=True,
454 options=dict(
455 actions=dict(
456 type='dict',
457 required=True,
458 options=dict(
459 base_blob=dict(
460 type='dict',
461 options=dict(
462 tier_to_cool=dict(
463 type='dict',
464 options=dict(
465 days_after_modification_greater_than=dict(type='float'),
466 days_after_last_access_time_greater_than=dict(type='float')
467 )
468 ),
469 tier_to_archive=dict(
470 type='dict',
471 options=dict(
472 days_after_modification_greater_than=dict(type='float'),
473 days_after_last_access_time_greater_than=dict(type='float')
474 )
475 ),
476 delete=dict(
477 type='dict',
478 options=dict(
479 days_after_modification_greater_than=dict(type='float'),
480 days_after_last_access_time_greater_than=dict(type='float')
481 )
482 ),
483 enable_auto_tier_to_hot_from_cool=dict(type='bool')
484 )
485 ),
486 snapshot=dict(
487 type='dict',
488 options=dict(
489 tier_to_cool=dict(
490 type='dict',
491 options=dict(
492 days_after_creation_greater_than=dict(type='float', required=True)
493 )
494 ),
495 tier_to_archive=dict(
496 type='dict',
497 options=dict(
498 days_after_creation_greater_than=dict(type='float', required=True)
499 )
500 ),
501 delete=dict(
502 type='dict',
503 options=dict(
504 days_after_creation_greater_than=dict(type='float', required=True)
505 )
506 )
507 )
508 ),
509 version=dict(
510 type='dict',
511 options=dict(
512 tier_to_cool=dict(
513 type='dict',
514 options=dict(
515 days_after_creation_greater_than=dict(
516 type='float',
517 required=True
518 )
519 )
520 ),
521 tier_to_archive=dict(
522 type='dict',
523 options=dict(
524 days_after_creation_greater_than=dict(
525 type='float',
526 required=True
527 )
528 )
529 ),
530 delete=dict(
531 type='dict',
532 options=dict(
533 days_after_creation_greater_than=dict(
534 type='float',
535 required=True
536 )
537 )
538 )
539 )
540 )
541 )
542 ),
543 filters=dict(
544 type='dict',
545 options=dict(
546 prefix_match=dict(type='list', elements='str'),
547 blob_types=dict(type='list', elements='str', choices=['blockBlob', 'appendBlob'], required=True),
548 blob_index_match=dict(
549 type='list',
550 elements='dict',
551 options=dict(
552 name=dict(type='str', required=True),
553 op=dict(type='str', required=True),
554 value=dict(type='str', required=True)
555 )
556 )
557 )
558 )
559 )
560 )
561 )
562 )
563 )
564
565 self.results = dict(
566 changed=False,
567 state=dict()
568 )
569
570 self.resource_group = None
571 self.storage_account_name = None
572 self.state = None
573 self.rules = []
574
575 super(AzureRMStorageAccountManagementPolicy, self).__init__(self.module_arg_spec,
576 supports_tags=False,
577 supports_check_mode=True)
578
579 def exec_module(self, **kwargs):
580
581 for key in list(self.module_arg_spec.keys()):
582 setattr(self, key, kwargs[key])
583
584 managed_policy = self.get_management_policy()
585 changed = False
586
587 if self.state == 'present':
588 if managed_policy is not None:
589 rules = []
590 for item in managed_policy['policy']['rules']:
591 rules.append(item)
592 rules_name = [item['name'] for item in rules]
593 for item in self.rules:
594 if item['name'] in rules_name:
595 for tt in managed_policy['policy']['rules']:
596 if item['name'] == tt['name']:
597 old_item = tt
598 if not self.default_compare({}, item, old_item, '', dict(compare=[])):
599 rules.remove(old_item)
600 rules.append(item)
601 changed = True
602 else:
603 rules.append(item)
604 changed = True
605 if changed and not self.check_mode:
606 self.create_or_update_management_policy(rules)
607 else:
608 changed = True
609 if not self.check_mode:
610 self.create_or_update_management_policy(self.rules)
611 else:
612 if managed_policy is not None:
613 changed = True
614 if not self.check_mode:
615 self.delete_management_policy()
616
617 self.results['state'] = self.get_management_policy()
618 self.results['changed'] = changed
619
620 return self.results
621
622 def get_management_policy(self):
623 self.log('Get info for storage account management policy')
624
625 response = None
626 try:
627 response = self.storage_client.management_policies.get(self.resource_group, self.storage_account_name, 'default')
628 except ResourceNotFoundError as ec:
629 self.log("Failed to obtain the storage acount management policy, detail as {0}".format(ec))
630 return None
631 return self.format_to_dict(response)
632
633 def create_or_update_management_policy(self, rules):
634 self.log("Creating or updating storage account mangement policy")
635
636 try:
637 self.storage_client.management_policies.create_or_update(resource_group_name=self.resource_group,
638 account_name=self.storage_account_name,
639 management_policy_name='default',
640 properties=dict(policy=dict(rules=rules)))
641 except Exception as e:
642 self.log('Error creating or updating storage account management policy.')
643 self.fail("Failed to create or updating storage account management policy: {0}".format(str(e)))
644 return self.get_management_policy()
645
646 def delete_management_policy(self):
647 try:
648 self.storage_client.management_policies.delete(self.resource_group, self.storage_account_name, 'default')
649 except Exception as e:
650 self.fail("Failed to delete the storage account management policy: {0}".format(str(e)))
651
652 def format_to_dict(self, obj):
653 result = dict()
654 result['id'] = obj.id
655 result['resource_group'] = self.resource_group
656 result['storage_account_name'] = self.storage_account_name
657 result['name'] = obj.name
658 result['type'] = obj.type
659 result['last_modified_time'] = obj.last_modified_time
660 result['policy'] = dict(rules=[])
661 if obj.policy is not None:
662 result['policy'] = obj.policy.as_dict()
663
664 return result
665
666
667 def main():
668 AzureRMStorageAccountManagementPolicy()
669
670
671 if __name__ == '__main__':
672 main()