-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathazure_rm_storageaccount.py
1428 lines (1323 loc) · 63.7 KB
/
azure_rm_storageaccount.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#
# Copyright (c) 2016 Matt Davis, <[email protected]>
# Chris Houseknecht, <[email protected]>
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: azure_rm_storageaccount
version_added: "0.1.0"
short_description: Manage Azure storage accounts
description:
- Create, update or delete a storage account.
options:
allow_shared_key_access:
description:
- when Allow storage account key access is disabled, any requests to the account that are authorized with shared key, including shared access signature (SAS), will be denied.
type: boolean
default: True
identity:
description:
- Identity for the resource.
type: dict
contains:
type:
description:
- The identity type. Required. Known values are: "None", "SystemAssigned", "UserAssigned", and "SystemAssigned,UserAssigned".
type: str
sample: true
user_assigned_identities:
description:
- Gets or sets a list of key value pairs that describe the set of User Assigned identities that will be used with this storage account. The key is the ARM resource identifier of the identity. Only 1 User Assigned identity is permitted here.
type: str
sample: true
resource_group:
description:
- Name of the resource group to use.
required: true
type: str
aliases:
- resource_group_name
name:
description:
- Name of the storage account to update or create.
type: str
required: true
state:
description:
- State of the storage account. Use C(present) to create or update a storage account and use C(absent) to delete an account.
- C(failover) is used to failover the storage account to its secondary. This process can take up to a hour.
default: present
type: str
choices:
- absent
- present
- failover
location:
description:
- Valid Azure location. Defaults to location of the resource group.
type: str
account_type:
description:
- Type of storage account. Required when creating a storage account.
- C(Standard_ZRS) and C(Premium_LRS) accounts cannot be changed to other account types.
- Other account types cannot be changed to C(Standard_ZRS) or C(Premium_LRS).
type: str
choices:
- Premium_LRS
- Standard_GRS
- Standard_LRS
- Standard_RAGRS
- Standard_ZRS
- Premium_ZRS
- Standard_RAGZRS
- Standard_GZRS
aliases:
- type
custom_domain:
description:
- User domain assigned to the storage account.
- Must be a dictionary with I(name) and I(use_sub_domain) keys where I(name) is the CNAME source.
- Only one custom domain is supported per storage account at this time.
- To clear the existing custom domain, use an empty string for the custom domain name property.
- Can be added to an existing storage account. Will be ignored during storage account creation.
type: dict
aliases:
- custom_dns_domain_suffix
kind:
description:
- The kind of storage.
- The C(FileStorage) and (BlockBlobStorage) only used when I(account_type=Premium_LRS) or I(account_type=Premium_ZRS).
default: 'Storage'
type: str
choices:
- Storage
- StorageV2
- BlobStorage
- BlockBlobStorage
- FileStorage
is_hns_enabled:
description:
- Account HierarchicalNamespace enabled if sets to true.
- When I(is_hns_enabled=True), I(kind) cannot be C(Storage).
type: bool
access_tier:
description:
- The access tier for this storage account. Required when I(kind=BlobStorage).
type: str
choices:
- Hot
- Cool
force_delete_nonempty:
description:
- Attempt deletion if resource already exists and cannot be updated.
type: bool
default: False
aliases:
- force
https_only:
description:
- Allows https traffic only to storage service when set to C(True).
- If omitted, new account creation will default to True, while existing accounts will not be change.
type: bool
minimum_tls_version:
description:
- The minimum required version of Transport Layer Security (TLS) for requests to a storage account.
- If omitted, new account creation will default to null which is currently interpreted to TLS1_0. Existing accounts will not be modified.
type: str
choices:
- TLS1_0
- TLS1_1
- TLS1_2
version_added: "1.0.0"
public_network_access:
description:
- Allow or disallow public network access to Storage Account.
type: str
choices:
- Enabled
- Disabled
version_added: "1.12.0"
allow_blob_public_access:
description:
- Allows blob containers in account to be set for anonymous public access.
- If set to false, no containers in this account will be able to allow anonymous public access.
- If omitted, new account creation will default to null which is currently interpreted to True. Existing accounts will not be modified.
type: bool
version_added: "1.1.0"
network_acls:
description:
- Manages the Firewall and virtual networks settings of the storage account.
type: dict
suboptions:
default_action:
description:
- Default firewall traffic rule.
- If I(default_action=Allow) no other settings have effect.
type: str
choices:
- Allow
- Deny
default: Allow
bypass:
description:
- When I(default_action=Deny) this controls which Azure components can still reach the Storage Account.
- The list is comma separated.
- It can be any combination of the example C(AzureServices), C(Logging), C(Metrics).
- If no Azure components are allowed, explicitly set I(bypass="").
default: AzureServices
type: str
virtual_network_rules:
description:
- A list of subnets and their actions.
type: list
elements: dict
suboptions:
id:
description:
- The complete path to the subnet.
type: str
action:
description:
- The only logical I(action=Allow) because this setting is only accessible when I(default_action=Deny).
default: 'Allow'
type: str
ip_rules:
description:
- A list of IP addresses or ranges in CIDR format.
type: list
elements: dict
suboptions:
value:
description:
- The IP address or range.
type: str
action:
description:
- The only logical I(action=Allow) because this setting is only accessible when I(default_action=Deny).
default: 'Allow'
type: str
blob_cors:
description:
- Specifies CORS rules for the Blob service.
- You can include up to five CorsRule elements in the request.
- If no blob_cors elements are included in the argument list, nothing about CORS will be changed.
- If you want to delete all CORS rules and disable CORS for the Blob service, explicitly set I(blob_cors=[]).
type: list
elements: dict
suboptions:
allowed_origins:
description:
- A list of origin domains that will be allowed via CORS, or "*" to allow all domains.
type: list
elements: str
required: true
allowed_methods:
description:
- A list of HTTP methods that are allowed to be executed by the origin.
type: list
elements: str
required: true
max_age_in_seconds:
description:
- The number of seconds that the client/browser should cache a preflight response.
type: int
required: true
exposed_headers:
description:
- A list of response headers to expose to CORS clients.
type: list
elements: str
required: true
allowed_headers:
description:
- A list of headers allowed to be part of the cross-origin request.
type: list
elements: str
required: true
static_website:
description:
- Manage static website configuration for the storage account.
type: dict
version_added: "1.13.0"
suboptions:
enabled:
description:
- Indicates whether this account is hosting a static website.
type: bool
default: false
index_document:
description:
- The default name of the index page under each directory.
type: str
error_document404_path:
description:
- The absolute path of the custom 404 page.
type: str
encryption:
description:
- The encryption settings on the storage account.
type: dict
suboptions:
services:
description:
- List of services which support encryption.
type: dict
suboptions:
table:
description:
- The encryption function of the table storage service.
type: dict
suboptions:
enabled:
description:
- Whether to encrypt the table type.
type: bool
queue:
description:
- The encryption function of the queue storage service.
type: dict
suboptions:
enabled:
description:
- Whether to encrypt the queue type.
type: bool
file:
description:
- The encryption function of the file storage service.
type: dict
suboptions:
enabled:
description:
- Whether to encrypt the file type.
type: bool
blob:
description:
- The encryption function of the blob storage service.
type: dict
suboptions:
enabled:
description:
- Whether to encrypt the blob type.
type: bool
key_source:
description:
- The encryption keySource (provider).
type: str
default: Microsoft.Storage
choices:
- Microsoft.Storage
- Microsoft.Keyvault
key_vault_properties:
description:
- list of Microsoft Keyvault properties needed in order to create Storage account with encryption enabled with Microsoft KeyVault for CMK.
type: dict
contains:
key_vault_uri:
description:
- The Uri of KeyVault.
type: str
key_name:
description:
- The name of KeyVault key.
type: str
key_version:
description:
- The version of KeyVault key.
type: str
encryption_identity:
description:
- The identity to be used with service-side encryption at rest.
type: dict
contains:
encryption_user_assigned_identity:
description:
- Resource identifier of the UserAssigned identity to be associated with server-side encryption on the storage account.
type: str
require_infrastructure_encryption:
description:
- A boolean indicating whether or not the service applies a secondary layer of encryption with platform managed keys for data at rest.
type: bool
extends_documentation_fragment:
- azure.azcollection.azure
- azure.azcollection.azure_tags
author:
- Chris Houseknecht (@chouseknecht)
- Matt Davis (@nitzmahone)
'''
EXAMPLES = '''
- name: remove account, if it exists
azure_rm_storageaccount:
resource_group: myResourceGroup
name: clh0002
state: absent
- name: create an account
azure_rm_storageaccount:
resource_group: myResourceGroup
name: clh0002
type: Standard_RAGRS
tags:
testing: testing
delete: on-exit
- name: Create an account with kind of FileStorage
azure_rm_storageaccount:
resource_group: myResourceGroup
name: c1h0002
type: Premium_LRS
kind: FileStorage
tags:
testing: testing
- name: configure firewall and virtual networks
azure_rm_storageaccount:
resource_group: myResourceGroup
name: clh0002
type: Standard_RAGRS
network_acls:
bypass: AzureServices,Metrics
default_action: Deny
virtual_network_rules:
- id: /subscriptions/mySubscriptionId/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet
action: Allow
ip_rules:
- value: 1.2.3.4
action: Allow
- value: 123.234.123.0/24
action: Allow
- name: create an account with blob CORS
azure_rm_storageaccount:
resource_group: myResourceGroup
name: clh002
type: Standard_RAGRS
blob_cors:
- allowed_origins:
- http://www.example.com/
allowed_methods:
- GET
- POST
allowed_headers:
- x-ms-meta-data*
- x-ms-meta-target*
- x-ms-meta-abc
exposed_headers:
- x-ms-meta-*
max_age_in_seconds: 200
'''
RETURN = '''
state:
description:
- Current state of the storage account.
returned: always
type: complex
contains:
account_type:
description:
- Type of storage account.
returned: always
type: str
sample: Standard_RAGRS
custom_domain:
description:
- User domain assigned to the storage account.
returned: always
type: complex
contains:
name:
description:
- CNAME source.
returned: always
type: str
sample: testaccount
use_sub_domain:
description:
- Whether to use sub domain.
returned: always
type: bool
sample: true
encryption:
description:
- The encryption settings on the storage account.
type: complex
returned: always
contains:
key_source:
description:
- The encryption keySource (provider).
type: str
returned: always
sample: Microsoft.Storage
require_infrastructure_encryption:
description:
- A boolean indicating whether or not the service applies a secondary layer of encryption with platform managed keys for data at rest.
type: bool
returned: always
sample: false
services:
description:
- List of services which support encryption.
type: dict
returned: always
contains:
file:
description:
- The encryption function of the file storage service.
type: dict
returned: always
sample: {'enabled': true}
table:
description:
- The encryption function of the table storage service.
type: dict
returned: always
sample: {'enabled': true}
queue:
description:
- The encryption function of the queue storage service.
type: dict
returned: always
sample: {'enabled': true}
blob:
description:
- The encryption function of the blob storage service.
type: dict
returned: always
sample: {'enabled': true}
key_vault_properties:
description:
- list of Microsoft Keyvault properties needed in order to create Storage account with encryption enabled with Microsoft KeyVault for CMK.
type: dict
sample: false
contains:
key_vault_uri:
description:
- The Uri of KeyVault.
type: str
key_name:
description:
- The name of KeyVault key.
type: str
key_version:
description:
- The version of KeyVault key.
type: str
id:
description:
- Resource ID.
returned: always
type: str
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/clh0003"
is_hns_enabled:
description:
- Account HierarchicalNamespace enabled if sets to true.
type: bool
returned: always
sample: true
location:
description:
- Valid Azure location. Defaults to location of the resource group.
returned: always
type: str
sample: eastus2
name:
description:
- Name of the storage account to update or create.
returned: always
type: str
sample: clh0003
network_acls:
description:
- A set of firewall and virtual network rules
returned: always
type: dict
sample: {
"bypass": "AzureServices",
"default_action": "Deny",
"virtual_network_rules": [
{
"action": "Allow",
"id": "/subscriptions/mySubscriptionId/resourceGroups/myResourceGroup/ \
providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"
}
],
"ip_rules": [
{
"action": "Allow",
"value": "1.2.3.4"
},
{
"action": "Allow",
"value": "123.234.123.0/24"
}
]
}
primary_endpoints:
description:
- The URLs to retrieve the public I(blob), I(queue), or I(table) object from the primary location.
returned: always
type: dict
sample: {
"blob": "https://clh0003.blob.core.windows.net/",
"queue": "https://clh0003.queue.core.windows.net/",
"table": "https://clh0003.table.core.windows.net/"
}
primary_location:
description:
- The location of the primary data center for the storage account.
returned: always
type: str
sample: eastus2
provisioning_state:
description:
- The status of the storage account.
- Possible values include C(Creating), C(ResolvingDNS), C(Succeeded).
returned: always
type: str
sample: Succeeded
failover_in_progress:
description:
- Status indicating the storage account is currently failing over to its secondary location.
returned: always
type: bool
sample: False
resource_group:
description:
- The resource group's name.
returned: always
type: str
sample: Testing
secondary_endpoints:
description:
- The URLs to retrieve the public I(blob), I(queue), or I(table) object from the secondary location.
returned: always
type: dict
sample: {
"blob": "https://clh0003-secondary.blob.core.windows.net/",
"queue": "https://clh0003-secondary.queue.core.windows.net/",
"table": "https://clh0003-secondary.table.core.windows.net/"
}
secondary_location:
description:
- The location of the geo-replicated secondary for the storage account.
returned: always
type: str
sample: centralus
status_of_primary:
description:
- The status of the primary location of the storage account; either C(available) or C(unavailable).
returned: always
type: str
sample: available
status_of_secondary:
description:
- The status of the secondary location of the storage account; either C(available) or C(unavailable).
returned: always
type: str
sample: available
https_only:
description:
- Allows https traffic only to storage service when set to C(true).
returned: always
type: bool
sample: false
minimum_tls_version:
description:
- The minimum TLS version permitted on requests to storage.
returned: always
type: str
sample: TLS1_2
public_network_access:
description:
- Public network access to Storage Account allowed or disallowed.
returned: always
type: str
sample: Enabled
allow_blob_public_access:
description:
- Public access to all blobs or containers in the storage account allowed or disallowed.
returned: always
type: bool
sample: true
tags:
description:
- Resource tags.
returned: always
type: dict
sample: { 'tags1': 'value1' }
type:
description:
- The storage account type.
returned: always
type: str
sample: "Microsoft.Storage/storageAccounts"
static_website:
description:
- Static website configuration for the storage account.
returned: always
version_added: "1.13.0"
type: complex
contains:
enabled:
description:
- Whether this account is hosting a static website.
returned: always
type: bool
sample: true
index_document:
description:
- The default name of the index page under each directory.
returned: always
type: str
sample: index.html
error_document404_path:
description:
- The absolute path of the custom 404 page.
returned: always
type: str
sample: error.html
'''
import copy
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AZURE_SUCCESS_STATE, AzureRMModuleBase
from ansible.module_utils._text import to_native
cors_rule_spec = dict(
allowed_origins=dict(type='list', elements='str', required=True),
allowed_methods=dict(type='list', elements='str', required=True),
max_age_in_seconds=dict(type='int', required=True),
exposed_headers=dict(type='list', elements='str', required=True),
allowed_headers=dict(type='list', elements='str', required=True),
)
static_website_spec = dict(
enabled=dict(type='bool', default=False),
index_document=dict(type='str'),
error_document404_path=dict(type='str'),
)
file_spec = dict(
enabled=dict(type='bool')
)
queue_spec = dict(
enabled=dict(type='bool')
)
table_spec = dict(
enabled=dict(type='bool')
)
blob_spec = dict(
enabled=dict(type='bool')
)
def compare_cors(cors1, cors2):
if len(cors1) != len(cors2):
return False
copy2 = copy.copy(cors2)
for rule1 in cors1:
matched = False
for rule2 in copy2:
if (rule1['max_age_in_seconds'] == rule2['max_age_in_seconds']
and set(rule1['allowed_methods']) == set(rule2['allowed_methods'])
and set(rule1['allowed_origins']) == set(rule2['allowed_origins'])
and set(rule1['allowed_headers']) == set(rule2['allowed_headers'])
and set(rule1['exposed_headers']) == set(rule2['exposed_headers'])):
matched = True
copy2.remove(rule2)
if not matched:
return False
return True
class AzureRMStorageAccount(AzureRMModuleBase):
def __init__(self):
self.module_arg_spec = dict(
account_type=dict(type='str',
choices=['Premium_LRS', 'Standard_GRS', 'Standard_LRS', 'Standard_RAGRS', 'Standard_ZRS', 'Premium_ZRS',
'Standard_RAGZRS', 'Standard_GZRS'],
aliases=['type']),
custom_domain=dict(type='dict', aliases=['custom_dns_domain_suffix']),
location=dict(type='str'),
name=dict(type='str', required=True),
resource_group=dict(required=True, type='str', aliases=['resource_group_name']),
state=dict(default='present', choices=['present', 'absent', 'failover']),
force_delete_nonempty=dict(type='bool', default=False, aliases=['force']),
tags=dict(type='dict'),
kind=dict(type='str', default='Storage', choices=['Storage', 'StorageV2', 'BlobStorage', 'FileStorage', 'BlockBlobStorage']),
access_tier=dict(type='str', choices=['Hot', 'Cool']),
https_only=dict(type='bool'),
minimum_tls_version=dict(type='str', choices=['TLS1_0', 'TLS1_1', 'TLS1_2']),
public_network_access=dict(type='str', choices=['Enabled', 'Disabled']),
allow_blob_public_access=dict(type='bool'),
network_acls=dict(type='dict'),
blob_cors=dict(type='list', options=cors_rule_spec, elements='dict'),
static_website=dict(type='dict', options=static_website_spec),
is_hns_enabled=dict(type='bool'),
allow_shared_key_access=dict(type='bool'),
identity=dict(
type='dict',
options=dict(
type=dict(type='str', choices=["SystemAssigned", "UserAssigned", "None"]),
user_assigned_identities=dict(type='str', required=False)
)
),
encryption=dict(
type='dict',
options=dict(
services=dict(
type='dict',
options=dict(
blob=dict(
type='dict',
options=blob_spec
),
table=dict(
type='dict',
options=table_spec
),
queue=dict(
type='dict',
options=queue_spec
),
file=dict(
type='dict',
options=file_spec
)
)
),
require_infrastructure_encryption=dict(type='bool'),
key_source=dict(type='str', choices=["Microsoft.Storage", "Microsoft.Keyvault"], default='Microsoft.Storage'),
key_vault_properties=dict(
type='dict',
options=dict(
key_vault_uri=dict(type='str', required=True),
key_name=dict(type='str', required=True),
key_version=dict(type='str')
)
),
encryption_identity=dict(
type='dict',
options=dict(
encryption_user_assigned_identity=dict(type='str', required=False) # This will hold the resource ID of the user-assigned identity
)
)
)
)
)
self.results = dict(
changed=False,
state=dict()
)
self.account_dict = None
self.resource_group = None
self.name = None
self.state = None
self.location = None
self.account_type = None
self.custom_domain = None
self.tags = None
self.force_delete_nonempty = None
self.kind = None
self.access_tier = None
self.https_only = None
self.minimum_tls_version = None
self.public_network_access = None
self.allow_blob_public_access = None
self.network_acls = None
self.blob_cors = None
self.static_website = None
self.encryption = None
self.is_hns_enabled = None
self.allow_shared_key_access = None
self.identity = None
super(AzureRMStorageAccount, self).__init__(self.module_arg_spec,
supports_check_mode=True)
def exec_module(self, **kwargs):
for key in list(self.module_arg_spec.keys()) + ['tags']:
setattr(self, key, kwargs[key])
resource_group = self.get_resource_group(self.resource_group)
if not self.location:
# Set default location
self.location = resource_group.location
if len(self.name) < 3 or len(self.name) > 24:
self.fail("Parameter error: name length must be between 3 and 24 characters.")
if self.custom_domain:
if self.custom_domain.get('name', None) is None:
self.fail("Parameter error: expecting custom_domain to have a name attribute of type string.")
if self.custom_domain.get('use_sub_domain', None) is None:
self.fail("Parameter error: expecting custom_domain to have a use_sub_domain "
"attribute of type boolean.")
if self.kind in ['FileStorage', 'BlockBlobStorage', ] and self.account_type not in ['Premium_LRS', 'Premium_ZRS']:
self.fail("Parameter error: Storage account with {0} kind require account type is Premium_LRS or Premium_ZRS".format(self.kind))
self.account_dict = self.get_account()
if self.state == 'present' and self.account_dict and \
self.account_dict['provisioning_state'] != AZURE_SUCCESS_STATE:
self.fail("Error: storage account {0} has not completed provisioning. State is {1}. Expecting state "
"to be {2}.".format(self.name, self.account_dict['provisioning_state'], AZURE_SUCCESS_STATE))
if self.account_dict is not None:
self.results['state'] = self.account_dict
else:
self.results['state'] = dict()
if self.state == 'present':
if not self.account_dict:
self.results['state'] = self.create_account()
else:
self.update_account()
elif self.state == 'absent' and self.account_dict:
self.delete_account()
self.results['state'] = dict(Status='Deleted')
elif self.state == 'failover' and self.account_dict:
self.failover_account()
self.results['state'] = self.get_account()
# # Check if 'identity' parameter exists and process it
if hasattr(self, 'identity') and self.identity:
if 'user_assigned_identities' in self.identity and isinstance(self.identity['user_assigned_identities'], str):
# Convert the string to a dictionary
identity_resource_id = self.identity['user_assigned_identities']
self.identity['user_assigned_identities'] = {identity_resource_id: {}}
return self.results
def check_name_availability(self):
self.log('Checking name availability for {0}'.format(self.name))
try:
account_name = self.storage_models.StorageAccountCheckNameAvailabilityParameters(name=self.name)
self.storage_client.storage_accounts.check_name_availability(account_name)
except Exception as e:
self.log('Error attempting to validate name.')
self.fail("Error checking name availability: {0}".format(str(e)))
def get_account(self):
self.log('Get properties for account {0}'.format(self.name))
account_obj = None
blob_mgmt_props = None
blob_client_props = None
account_dict = None
try:
account_obj = self.storage_client.storage_accounts.get_properties(self.resource_group, self.name)
if account_obj.identity and 'user_assigned_identities' in account_obj.identity:
# Check if user_assigned_identities is a dictionary
if isinstance(account_obj.identity['user_assigned_identities'], dict):
# Assuming there's only one key in the dictionary,
# convert the dictionary to a string (the key of the dictionary)
identity_resource_id_keys = list(account_obj.identity['user_assigned_identities'].keys())
if identity_resource_id_keys:
account_obj.identity['user_assigned_identities'] = identity_resource_id_keys[0]
blob_mgmt_props = self.storage_client.blob_services.get_service_properties(self.resource_group, self.name)
if self.kind != "FileStorage":
blob_client_props = self.get_blob_service_client(self.resource_group, self.name).get_service_properties()
except Exception:
pass
if account_obj:
account_dict = self.account_obj_to_dict(account_obj, blob_mgmt_props, blob_client_props)
# print("account dict:{}".format(account_dict))
return account_dict
def account_obj_to_dict(self, account_obj, blob_mgmt_props=None, blob_client_props=None):
account_dict = dict(
id=account_obj.id,
name=account_obj.name,
location=account_obj.location,
failover_in_progress=(account_obj.failover_in_progress
if account_obj.failover_in_progress is not None else False),
resource_group=self.resource_group,
type=account_obj.type,
access_tier=account_obj.access_tier,
sku_tier=account_obj.sku.tier,
sku_name=account_obj.sku.name,
provisioning_state=account_obj.provisioning_state,
secondary_location=account_obj.secondary_location,
status_of_primary=account_obj.status_of_primary,
status_of_secondary=account_obj.status_of_secondary,
primary_location=account_obj.primary_location,
https_only=account_obj.enable_https_traffic_only,
minimum_tls_version=account_obj.minimum_tls_version,
public_network_access=account_obj.public_network_access,
allow_blob_public_access=account_obj.allow_blob_public_access,
network_acls=account_obj.network_rule_set,
is_hns_enabled=account_obj.is_hns_enabled if account_obj.is_hns_enabled else False,
allow_shared_key_access=account_obj.allow_shared_key_access if account_obj.allow_shared_key_access else False,
static_website=dict(
enabled=False,
index_document=None,
error_document404_path=None,
)
)
account_dict['custom_domain'] = None
if account_obj.custom_domain:
account_dict['custom_domain'] = dict(
name=account_obj.custom_domain.name,
use_sub_domain=account_obj.custom_domain.use_sub_domain
)
account_dict['primary_endpoints'] = None
if account_obj.primary_endpoints:
account_dict['primary_endpoints'] = dict(
blob=account_obj.primary_endpoints.blob,
queue=account_obj.primary_endpoints.queue,
table=account_obj.primary_endpoints.table
)
account_dict['secondary_endpoints'] = None