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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2020, Simon Dodsley (simon@purestorage.com)
# 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
ANSIBLE_METADATA = {
"metadata_version": "1.1",
"status": ["preview"],
"supported_by": "community",
}
DOCUMENTATION = r"""
---
module: purefa_policy
version_added: '1.5.0'
short_description: Manage FlashArray File System Policies
description:
- Manage FlashArray file system policies for NFS, SMB and snapshot
author:
- Pure Storage Ansible Team (@sdodsley) <pure-ansible-team@purestorage.com>
options:
name:
description:
- Name of the policy
type: str
required: true
state:
description:
- Define whether the policy should exist or not.
default: present
choices: [ absent, present ]
type: str
policy:
description:
- The type of policy to use
choices: [ nfs, smb, snapshot, quota, autodir ]
required: true
type: str
enabled:
description:
- Define if policy is enabled or not
type: bool
default: true
smb_anon_allowed:
description:
- Specifies whether access to information is allowed for anonymous users
type: bool
default: false
client:
description:
- Specifies which SMB or NFS clients are given access
- Accepted notation, IP, IP mask, or hostname
type: str
smb_encrypt:
description:
- Specifies whether the remote client is required to use SMB encryption
type: bool
default: false
nfs_access:
description:
- Specifies access control for the export
choices: [ root-squash, no-root-squash, all-squash ]
type: str
default: no-root-squash
nfs_permission:
description:
- Specifies which read-write client access permissions are allowed for the export
choices: [ ro, rw ]
default: rw
type: str
nfs_version:
description:
- NFS protocol version allowed for the export
type: list
elements: str
choices: [ nfsv3, nfsv4 ]
version_added: "1.22.0"
user_mapping:
description:
- Defines if user mapping is enabled
type: bool
default: true
version_added: 1.14.0
snap_at:
description:
- Specifies the number of hours since midnight at which to take a snapshot
or the hour including AM/PM
- Can only be set on the rule with the smallest I(snap_every) value.
- Cannot be set if the I(snap_every) value is not measured in days.
- Can only be set for at most one rule in the same policy.
type: str
snap_every:
description:
- Specifies the interval between snapshots, in minutes.
- The value for all rules must be multiples of one another.
- Must be unique for each rule in the same policy.
- Value must be between 5 and 525600.
type: int
snap_keep_for:
description:
- Specifies the period that snapshots are retained before they are eradicated, in minutes.
- Cannot be less than the I(snap_every) value of the rule.
- Value must be unique for each rule in the same policy.
- Value must be between 5 and 525600.
type: int
snap_client_name:
description:
- The customizable portion of the client visible snapshot name.
type: str
snap_suffix:
description:
- The snapshot suffix name
- The suffix value can only be set for one rule in the same policy
- The suffix value can only be set on a rule with the same ``keep_for`` value and ``every`` value
- The suffix value can only be set on the rule with the largest ``keep_for`` value
- If not specified, defaults to a monotonically increasing number generated by the system.
type: str
version_added: 1.10.0
rename:
description:
- New name of policy
type: str
directory:
description:
- Directories to have the quota rule applied to.
type: list
elements: str
version_added: 1.9.0
quota_limit:
description:
- Logical space limit of the share in M, G, T or P units. See examples.
- If size is not set at filesystem creation time the filesystem size becomes unlimited.
- This value cannot be set to 0.
type: str
version_added: 1.9.0
quota_notifications:
description:
- Targets to notify when usage approaches the quota limit.
- The list of notification targets is a comma-separated string
- If not specified, notification targets are not assigned.
type: list
elements: str
choices: [ user, group ]
version_added: 1.9.0
quota_enforced:
description:
- Defines if the directory quota is enforced.
default: true
type: bool
ignore_usage:
description:
- Flag used to override checks for quota management
operations.
- If set to true, directory usage is not checked against the
quota_limits that are set.
- If set to false, the actual logical bytes in use are prevented
from exceeding the limits set on the directory.
- Client operations might be impacted.
- If the limit exceeds the quota, the client operation is not allowed.
default: false
type: bool
version_added: 1.9.0
anonuid:
description:
- The ID to which any users whose UID is affected by I(access) of
I(root-squash) or I(all-squash) will be mapped to.
- Clear using "".
type: str
default: "65534"
version_added: 1.14.0
anongid:
description:
- The ID to which any users whose GID is affected by I(access) of
I(root-squash) or I(all-squash) will be mapped to.
- This is ignored when I(user_mapping) is enabled.
- Clear using "".
type: str
default: "65534"
version_added: 1.14.0
security:
description:
- The security flavors to use for accessing files on a mount point.
- If the server does not support the requested flavor, the mount operation fails.
- This operation updates all rules of the specified policy.
type: list
elements: str
choices: [ auth_sys, krb5, krb5i, krb5p ]
version_added: 1.25.0
access_based_enumeration:
description:
- Defines if access based enumeration for SMB is enabled
type: bool
default: false
version_added: 1.26.0
extends_documentation_fragment:
- purestorage.flasharray.purestorage.fa
"""
EXAMPLES = r"""
- name: Create an NFS policy with initial rule
purestorage.flasharray.purefa_policy:
name: export1
policy: nfs
nfs_access: root-squash
nfs_permission: ro
client: client1
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Create an empty NFS policy with no rules
purestorage.flasharray.purefa_policy:
name: export1
policy: nfs
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Create an empty snapshot policy with no rules
purestorage.flasharray.purefa_policy:
name: snap1
policy: snapshot
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Create an empty snapshot policy with single directory member
purestorage.flasharray.purefa_policy:
name: snap1
policy: snapshot
directory: "foo:bar"
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Disable a policy
purestorage.flasharray.purefa_policy:
name: export1
policy: nfs
enabled: false
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Add rule to existing NFS export policy
purestorage.flasharray.purefa_policy:
name: export1
policy: nfs
nfs_access: root-squash
nfs_permission: ro
client: client2
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Add rule to existing SMB export policy
purestorage.flasharray.purefa_policy:
name: export1
policy: smb
smb_encrypt: true
smb_anon_allowed: false
client: client1
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Add non-suffix rule to existing snapshot export policy
purestorage.flasharray.purefa_policy:
name: snap1
policy: snapshot
snap_client_name: foo
snap_every: 15
snap_keep_for: 1440
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Add suffix rule to existing snapshot export policy
purestorage.flasharray.purefa_policy:
name: snap1
policy: snapshot
snap_client_name: foo
snap_suffix: bar
snap_every: 1440
snap_keep_for: 1440
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Delete policy rule for a client
purestorage.flasharray.purefa_policy:
name: export1
policy: nfs
client: client2
state: absent
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Delete policy
purestorage.flasharray.purefa_policy:
name: export1
policy: nfs
state: absent
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Create directory quota policy for directory bar
purestorage.flasharray.purefa_policy:
name: foo
directory:
- "foo:root"
- "bar:bin"
policy: quota
quota_limit: 10G
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Delete directory quota policy foo
purestorage.flasharray.purefa_policy:
name: foo
policy: quota
state: absent
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Create empty directory quota policy foo
purestorage.flasharray.purefa_policy:
name: foo
policy: quota
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Detach directory "foo:bar" from quota policy quota1
purestorage.flasharray.purefa_policy:
name: quota1
directory:
- "foo:bar"
state: absent
policy: quota
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Remove quota rule from quota policy foo
purestorage.flasharray.purefa_policy:
name: foo
policy: quota
quota_limit: 10G
state: absent
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
"""
RETURN = r"""
"""
HAS_PURESTORAGE = True
try:
from pypureclient import flasharray
except ImportError:
HAS_PURESTORAGE = False
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
get_system,
get_array,
purefa_argument_spec,
)
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
LooseVersion,
)
from ansible_collections.purestorage.flasharray.plugins.module_utils.common import (
human_to_bytes,
convert_to_millisecs,
)
MIN_REQUIRED_API_VERSION = "2.3"
MIN_QUOTA_API_VERSION = "2.7"
MIN_SUFFIX_API_VERSION = "2.9"
USER_MAP_VERSION = "2.15"
ALL_SQUASH_VERSION = "2.16"
AUTODIR_VERSION = "2.24"
NFS_VERSION = "2.26"
SECURITY_VERSION = "2.29"
ABE_VERSION = "2.4"
def rename_policy(module, array):
"""Rename a file system policy"""
changed = False
target_exists = bool(
array.get_policies(names=[module.params["rename"]]).status_code == 200
)
if target_exists:
module.fail_json(
msg="Rename failed - Target policy {0} already exists".format(
module.params["rename"]
)
)
if not module.check_mode:
changed = True
if module.params["policy"] == "nfs":
res = array.patch_policies_nfs(
names=[module.params["name"]],
policy=flasharray.PolicyPatch(name=module.params["rename"]),
)
if res.status_code != 200:
module.fail_json(
msg="Failed to rename NFS policy {0} to {1}".format(
module.params["name"], module.params["rename"]
)
)
elif module.params["policy"] == "smb":
res = array.patch_policies_smb(
names=[module.params["name"]],
policy=flasharray.PolicyPatch(name=module.params["rename"]),
)
if res.status_code != 200:
module.fail_json(
msg="Failed to rename SMB policy {0} to {1}".format(
module.params["name"], module.params["rename"]
)
)
elif module.params["policy"] == "snapshot":
res = array.patch_policies_snapshot(
names=[module.params["name"]],
policy=flasharray.PolicyPatch(name=module.params["rename"]),
)
if res.status_code != 200:
module.fail_json(
msg="Failed to rename snapshot policy {0} to {1}".format(
module.params["name"], module.params["rename"]
)
)
else:
res = array.patch_policies_quota(
names=[module.params["name"]],
policy=flasharray.PolicyPatch(name=module.params["rename"]),
)
if res.status_code != 200:
module.fail_json(
msg="Failed to rename quota policy {0} to {1}".format(
module.params["name"], module.params["rename"]
)
)
module.exit_json(changed=changed)
def delete_policy(module, array):
"""Delete a file system policy or rule within a policy"""
changed = True
if not module.check_mode:
changed = False
if module.params["policy"] == "nfs":
if not module.params["client"]:
res = array.delete_policies_nfs(names=[module.params["name"]])
if res.status_code == 200:
changed = True
else:
module.fail_json(
msg="Deletion of NFS policy {0} failed. Error: {1}".format(
module.params["name"], res.errors[0].message
)
)
else:
rules = list(
array.get_policies_nfs_client_rules(
policy_names=[module.params["name"]]
).items
)
if rules:
rule_name = ""
for rule in range(0, len(rules)):
if rules[rule].client == module.params["client"]:
rule_name = rules[rule].name
break
if rule_name:
deleted = bool(
array.delete_policies_nfs_client_rules(
policy_names=[module.params["name"]], names=[rule_name]
).status_code
== 200
)
if deleted:
changed = True
else:
module.fail_json(
msg="Failed to delete client {0} from NFS policy {1}. Error: {2}".format(
module.params["client"],
module.params["name"],
deleted.errors[0].message,
)
)
elif module.params["policy"] == "smb":
if not module.params["client"]:
res = array.delete_policies_smb(names=[module.params["name"]])
if res.status_code == 200:
changed = True
else:
module.fail_json(
msg="Deletion of SMB policy {0} failed. Error: {1}".format(
module.params["name"], res.errors[0].message
)
)
else:
rules = list(
array.get_policies_smb_client_rules(
policy_names=[module.params["name"]]
).items
)
if rules:
rule_name = ""
for rule in range(0, len(rules)):
if rules[rule].client == module.params["client"]:
rule_name = rules[rule].name
break
if rule_name:
deleted = bool(
array.delete_policies_smb_client_rules(
policy_names=[module.params["name"]], names=[rule_name]
).status_code
== 200
)
if deleted:
changed = True
else:
module.fail_json(
msg="Failed to delete client {0} from SMB policy {1}. Error: {2}".format(
module.params["client"],
module.params["name"],
deleted.errors[0].message,
)
)
elif module.params["policy"] == "snapshot":
if not module.params["snap_client_name"] and not module.params["directory"]:
res = array.delete_policies_snapshot(names=[module.params["name"]])
if res.status_code == 200:
changed = True
else:
module.fail_json(
msg="Deletion of Snapshot policy {0} failed. Error: {1}".format(
module.params["name"], res.errors[0].message
)
)
if module.params["directory"]:
dirs = []
old_dirs = []
current_dirs = list(
array.get_directories_policies_snapshot(
policy_names=[module.params["name"]]
).items
)
if current_dirs:
for current_dir in range(0, len(current_dirs)):
dirs.append(current_dirs[current_dir].member.name)
for old_dir in range(0, len(module.params["directory"])):
if module.params["directory"][old_dir] in dirs:
old_dirs.append(module.params["directory"][old_dir])
else:
pass
if old_dirs:
changed = True
for rem_dir in range(0, len(old_dirs)):
if not module.check_mode:
directory_removed = (
array.delete_directories_policies_snapshot(
member_names=[old_dirs[rem_dir]],
policy_names=module.params["name"],
)
)
if directory_removed.status_code != 200:
module.fail_json(
msg="Failed to remove directory from Snapshot policy {0}. Error: {1}".format(
module.params["name"],
directory_removed.errors[0].message,
)
)
if module.params["snap_client_name"]:
rules = list(
array.get_policies_snapshot_rules(
policy_names=[module.params["name"]]
).items
)
if rules:
rule_name = ""
for rule in range(0, len(rules)):
if rules[rule].client_name == module.params["snap_client_name"]:
rule_name = rules[rule].name
break
if rule_name:
deleted = bool(
array.delete_policies_snapshot_rules(
policy_names=[module.params["name"]], names=[rule_name]
).status_code
== 200
)
if deleted:
changed = True
else:
module.fail_json(
msg="Failed to delete client {0} from Snapshot policy {1}. Error: {2}".format(
module.params["snap_client_name"],
module.params["name"],
deleted.errors[0].message,
)
)
elif module.params["policy"] == "autodir":
if not module.params["directory"]:
res = array.delete_policies_autodir(names=[module.params["name"]])
if res.status_code == 200:
changed = True
else:
module.fail_json(
msg="Deletion of Autodir policy {0} failed. Error: {1}".format(
module.params["name"], res.errors[0].message
)
)
if module.params["directory"]:
dirs = []
old_dirs = []
current_dirs = list(
array.get_directories_policies_autodir(
policy_names=[module.params["name"]]
).items
)
if current_dirs:
for current_dir in range(0, len(current_dirs)):
dirs.append(current_dirs[current_dir].member.name)
for old_dir in range(0, len(module.params["directory"])):
if module.params["directory"][old_dir] in dirs:
old_dirs.append(module.params["directory"][old_dir])
else:
pass
if old_dirs:
changed = True
for rem_dir in range(0, len(old_dirs)):
if not module.check_mode:
directory_removed = (
array.delete_directories_policies_autodir(
member_names=[old_dirs[rem_dir]],
policy_names=module.params["name"],
)
)
if directory_removed.status_code != 200:
module.fail_json(
msg="Failed to remove directory from Autodir policy {0}. Error: {1}".format(
module.params["name"],
directory_removed.errors[0].message,
)
)
else: # quota
if module.params["quota_limit"]:
quota_limit = human_to_bytes(module.params["quota_limit"])
rules = list(
array.get_policies_quota_rules(
policy_names=[module.params["name"]]
).items
)
if rules:
for rule in range(0, len(rules)):
if rules[rule].quota_limit == quota_limit:
if (
module.params["quota_enforced"] == rules[rule].enforced
and ",".join(module.params["quota_notifications"])
== rules[rule].notifications
):
res = array.delete_policies_quota_rules(
policy_names=[module.params["name"]],
names=[rules[rule].name],
)
if res.status_code == 200:
changed = True
else:
module.fail_json(
msg="Deletion of Quota rule failed. Error: {0}".format(
res.errors[0].message
)
)
if module.params["directory"]:
members = list(
array.get_policies_quota_members(
policy_names=[module.params["name"]]
).items
)
if members:
for member in range(0, len(members)):
if members[member].member.name in module.params["directory"]:
res = array.delete_policies_quota_members(
policy_names=[module.params["name"]],
member_names=[members[member].member.name],
member_types="directories",
)
if res.status_code != 200:
module.fail_json(
msg="Deletion of Quota member {0} from policy {1}. Error: {2}".format(
members[member].member.name,
module.params["name"],
res.errors[0].message,
)
)
else:
changed = True
if not module.params["quota_limit"] and not module.params["directory"]:
members = list(
array.get_policies_quota_members(
policy_names=[module.params["name"]]
).items
)
if members:
member_names = []
for member in range(0, len(members)):
member_names.append(members[member].member.name)
res = array.delete_policies_quota_members(
policy_names=[module.params["name"]],
member_names=member_names,
member_types="directories",
)
if res.status_code != 200:
module.fail_json(
msg="Deletion of Quota members {0} failed. Error: {1}".format(
module.params["name"], res.errors[0].message
)
)
res = array.delete_policies_quota(names=[module.params["name"]])
if res.status_code == 200:
changed = True
else:
module.fail_json(
msg="Deletion of Quota policy {0} failed. Error: {1}".format(
module.params["name"], res.errors[0].message
)
)
module.exit_json(changed=changed)
def create_policy(module, array, all_squash):
"""Create a file system export"""
changed = True
if not module.check_mode:
changed = False
if module.params["policy"] == "nfs":
created = array.post_policies_nfs(
names=[module.params["name"]],
policy=flasharray.PolicyPost(enabled=module.params["enabled"]),
)
if created.status_code == 200:
changed = True
if module.params["client"]:
if all_squash:
rules = flasharray.PolicyrulenfsclientpostRules(
access=module.params["nfs_access"],
anongid=module.params["anongid"],
anonuid=module.params["anonuid"],
client=module.params["client"],
permission=module.params["nfs_permission"],
)
else:
rules = flasharray.PolicyrulenfsclientpostRules(
access=module.params["nfs_access"],
client=module.params["client"],
permission=module.params["nfs_permission"],
)
rule = flasharray.PolicyRuleNfsClientPost(rules=[rules])
rule_created = array.post_policies_nfs_client_rules(
policy_names=[module.params["name"]], rules=rule
)
if rule_created.status_code != 200:
module.fail_json(
msg="Failed to create rule for NFS policy {0}. Error: {1}".format(
module.params["name"], rule_created.errors[0].message
)
)
policy = flasharray.PolicyNfsPatch(
user_mapping_enabled=module.params["user_mapping"],
)
res = array.patch_policies_nfs(
names=[module.params["name"]], policy=policy
)
if res.status_code != 200:
module.fail_json(
msg="Failed to set NFS policy user_mapping {0}. Error: {1}".format(
module.params["name"], res.errors[0].message
)
)
if (
LooseVersion(array.get_rest_version()) >= LooseVersion(NFS_VERSION)
and module.params["client"]
and module.params["nfs_version"]
):
policy = flasharray.PolicyNfsPatch(
nfs_version=module.params["nfs_version"],
)
res = array.patch_policies_nfs(
names=[module.params["name"]], policy=policy
)
if res.status_code != 200:
module.fail_json(
msg="Failed to set NFS policy version {0}. Error: {1}".format(
module.params["name"], res.errors[0].message
)
)
if (
LooseVersion(array.get_rest_version())
>= LooseVersion(SECURITY_VERSION)
and module.params["security"]
):
policy = flasharray.PolicyNfsPatch(
security=module.params["security"],
)
res = array.patch_policies_nfs(
names=[module.params["name"]], policy=policy
)
if res.status_code != 200:
module.fail_json(
msg="Failed to set NFS policy security {0}. Error: {1}".format(
module.params["name"], res.errors[0].message
)
)
else:
module.fail_json(
msg="Failed to create NFS policy {0}. Error: {1}".format(
module.params["name"], created.errors[0].message
)
)
elif module.params["policy"] == "smb":
created = array.post_policies_smb(
names=[module.params["name"]],
policy=flasharray.PolicyPost(enabled=module.params["enabled"]),
)
if created.status_code == 200:
if LooseVersion(ABE_VERSION) <= LooseVersion(array.get_rest_version()):
res = array.patch_policies_smb(
names=[module.params["name"]],
policy=flasharray.PolicySmbPatch(
access_based_enumeration_enabled=module.params[
"access_based_enumeration"
]
),
)
if res.status_code != 200:
module.fail_json(
msg="Failed to set SMB policy {0}. Error: {1}".format(
module.params["name"], res.errors[0].message
)
)
if module.params["client"]:
rules = flasharray.PolicyrulesmbclientpostRules(
anonymous_access_allowed=module.params["smb_anon_allowed"],
client=module.params["client"],
smb_encryption_required=module.params["smb_encrypt"],
)
rule = flasharray.PolicyRuleSmbClientPost(rules=[rules])
rule_created = array.post_policies_smb_client_rules(
policy_names=[module.params["name"]], rules=rule
)
if rule_created.status_code != 200:
module.fail_json(
msg="Failed to create rule for SMB policy {0}. Error: {1}".format(
module.params["name"], rule_created.errors[0].message
)
)
changed = True
else:
module.fail_json(
msg="Failed to create SMB policy {0}. Error: {1}".format(
module.params["name"], created.errors[0].message
)
)
elif module.params["policy"] == "snapshot":
suffix_enabled = bool(
LooseVersion(array.get_rest_version())
>= LooseVersion(MIN_SUFFIX_API_VERSION)
)
created = array.post_policies_snapshot(
names=[module.params["name"]],
policy=flasharray.PolicyPost(enabled=module.params["enabled"]),
)
if created.status_code == 200:
changed = True
if module.params["snap_client_name"]:
if module.params["snap_keep_for"] < module.params["snap_every"]:
module.fail_json(
msg="Retention period (snap_keep_for) cannot be less than snapshot interval (snap_every)."
)
if module.params["snap_at"]:
if not module.params["snap_every"] % 1440 == 0:
module.fail_json(
msg="snap_at time can only be set if snap_every is multiple of 1440"
)
if suffix_enabled:
rules = flasharray.PolicyrulesnapshotpostRules(
at=convert_to_millisecs(module.params["snap_at"]),
client_name=module.params["snap_client_name"],
every=module.params["snap_every"] * 60000,
keep_for=module.params["snap_keep_for"] * 60000,
suffix=module.params["snap_suffix"],
)
else:
rules = flasharray.PolicyrulesnapshotpostRules(
at=convert_to_millisecs(module.params["snap_at"]),
client_name=module.params["snap_client_name"],
every=module.params["snap_every"] * 60000,
keep_for=module.params["snap_keep_for"] * 60000,
)
else:
if suffix_enabled:
rules = flasharray.PolicyrulesnapshotpostRules(
client_name=module.params["snap_client_name"],
every=module.params["snap_every"] * 60000,
keep_for=module.params["snap_keep_for"] * 60000,
suffix=module.params["snap_suffix"],
)
else:
rules = flasharray.PolicyrulesnapshotpostRules(
client_name=module.params["snap_client_name"],
every=module.params["snap_every"] * 60000,
keep_for=module.params["snap_keep_for"] * 60000,
)
rule = flasharray.PolicyRuleSnapshotPost(rules=[rules])
rule_created = array.post_policies_snapshot_rules(
policy_names=[module.params["name"]], rules=rule
)
if rule_created.status_code != 200:
module.fail_json(
msg="Failed to create rule for Snapshot policy {0}. Error: {1}".format(
module.params["name"], rule_created.errors[0].message
)
)
if module.params["directory"]:
policies = flasharray.DirectoryPolicyPost(
policies=[
flasharray.DirectorypolicypostPolicies(
policy=flasharray.Reference(name=module.params["name"])
)
]
)
directory_added = array.post_directories_policies_snapshot(
member_names=module.params["directory"], policies=policies
)
if directory_added.status_code != 200:
module.fail_json(
msg="Failed to add directory for Snapshot policy {0}. Error: {1}".format(
module.params["name"],
directory_added.errors[0].message,
)
)
else:
module.fail_json(
msg="Failed to create Snapshot policy {0}. Error: {1}".format(
module.params["name"], created.errors[0].message
)
)
elif module.params["policy"] == "autodir":
created = array.post_policies_autodir(
names=[module.params["name"]],
policy=flasharray.PolicyPost(enabled=module.params["enabled"]),
)
if created.status_code == 200:
changed = True
if module.params["directory"]:
policies = flasharray.DirectoryPolicyPost(
policies=[
flasharray.DirectorypolicypostPolicies(
policy=flasharray.Reference(name=module.params["name"])
)
]
)
directory_added = array.post_directories_policies_autodir(
member_names=module.params["directory"], policies=policies
)
if directory_added.status_code != 200:
module.fail_json(
msg="Failed to add directory for Autodir policy {0}. Error: {1}".format(
module.params["name"],
directory_added.errors[0].message,
)
)
else:
module.fail_json(
msg="Failed to create Autodir policy {0}. Error: {1}".format(
module.params["name"], created.errors[0].message
)
)
else: # quota
created = array.post_policies_quota(
names=[module.params["name"]],
policy=flasharray.PolicyPost(enabled=module.params["enabled"]),
)
if created.status_code == 200:
changed = True
if module.params["quota_limit"]:
quota = human_to_bytes(module.params["quota_limit"])
rules = flasharray.PolicyrulequotapostRules(
enforced=module.params["quota_enforced"],
quota_limit=quota,
notifications=",".join(module.params["quota_notifications"]),
)
rule = flasharray.PolicyRuleQuotaPost(rules=[rules])
quota_created = array.post_policies_quota_rules(
policy_names=[module.params["name"]],
rules=rule,
ignore_usage=module.params["ignore_usage"],
)
if quota_created.status_code != 200:
module.fail_json(
msg="Failed to create rule for Quota policy {0}. Error: {1}".format(
module.params["name"], quota_created.errors[0].message
)
)
if module.params["directory"]:
members = []
for mem in range(0, len(module.params["directory"])):
members.append(
flasharray.PolicymemberpostMembers(
member=flasharray.ReferenceWithType(
name=module.params["directory"][mem],
resource_type="directories",
)
)
)
member = flasharray.PolicyMemberPost(members=members)
members_created = array.post_policies_quota_members(
policy_names=[module.params["name"]],
members=member,
ignore_usage=module.params["ignore_usage"],
)
if members_created.status_code != 200:
module.fail_json(
msg="Failed to add members to Quota policy {0}. Error: {1}".format(
module.params["name"],
members_created.errors[0].message,
)
)
else:
module.fail_json(
msg="Failed to create Quota policy {0}. Error: {1}".format(
module.params["name"], created.errors[0].message
)
)
module.exit_json(changed=changed)
def update_policy(module, array, api_version, all_squash):
"""Update an existing policy including add/remove rules"""
changed = changed_dir = changed_rule = changed_enable = changed_quota = (
changed_member
) = changed_user_map = changed_abe = changed_nfs = False
if module.params["policy"] == "nfs":
current_policy = list(
array.get_policies_nfs(names=[module.params["name"]]).items
)[0]
try:
current_enabled = current_policy.enabled
if USER_MAP_VERSION in api_version:
current_user_map = list(
array.get_policies_nfs(names=[module.params["name"]]).items
)[0].user_mapping_enabled
except Exception:
module.fail_json(
msg="Incorrect policy type specified for existing policy {0}".format(
module.params["name"]
)
)
if module.params["nfs_version"] and sorted(
module.params["nfs_version"]
) != sorted(getattr(current_policy, "nfs_version", [])):
changed_nfs = True
if not module.check_mode:
res = array.patch_policies_nfs(
names=[module.params["name"]],
policy=flasharray.PolicyNfsPatch(
nfs_version=module.params["nfs_version"]
),
)
if res.status_code != 200:
module.exit_json(
msg="Failed to change NFS version for NFS policy {0}".format(
module.params["name"]
)
)
if (
module.params["user_mapping"]
and current_user_map != module.params["user_mapping"]
):
changed_user_map = True
if not module.check_mode:
res = array.patch_policies_nfs(
names=[module.params["name"]],
policy=flasharray.PolicyNfsPatch(
user_mapping_enabled=module.params["user_mapping"]
),
)
if res.status_code != 200:
module.exit_json(
msg="Failed to enable/disable User Mapping for NFS policy {0}".format(
module.params["name"]
)
)
if current_enabled != module.params["enabled"]:
changed_enable = True
if not module.check_mode:
res = array.patch_policies_nfs(
names=[module.params["name"]],
policy=flasharray.PolicyPatch(enabled=module.params["enabled"]),
)
if res.status_code != 200:
module.exit_json(
msg="Failed to enable/disable NFS policy {0}".format(
module.params["name"]
)
)
if module.params["client"]:
rules = list(
array.get_policies_nfs_client_rules(
policy_names=[module.params["name"]]
).items
)
if rules:
rule_name = ""
for rule in range(0, len(rules)):
if rules[rule].client == module.params["client"]:
rule_name = rules[rule].name
break
if not rule_name:
if LooseVersion(NFS_VERSION) > LooseVersion(
array.get_rest_version()
):
if all_squash:
rules = flasharray.PolicyrulenfsclientpostRules(
permission=module.params["nfs_permission"],
client=module.params["client"],
anongid=module.params["anongid"],
anonuid=module.params["anonuid"],
access=module.params["nfs_access"],
)
else:
rules = flasharray.PolicyrulenfsclientpostRules(
permission=module.params["nfs_permission"],
client=module.params["client"],
access=module.params["nfs_access"],
nfs_version=module.params["nfs_version"],
)
elif (
LooseVersion(SECURITY_VERSION)
> LooseVersion(array.get_rest_version())
<= LooseVersion(NFS_VERSION)
):
if all_squash:
rules = flasharray.PolicyrulenfsclientpostRules(
permission=module.params["nfs_permission"],
client=module.params["client"],
anongid=module.params["anongid"],
anonuid=module.params["anonuid"],
access=module.params["nfs_access"],
nfs_version=module.params["nfs_version"],
)
else:
rules = flasharray.PolicyrulenfsclientpostRules(
permission=module.params["nfs_permission"],
client=module.params["client"],
access=module.params["nfs_access"],
)
else:
if module.params["security"]:
if all_squash:
rules = flasharray.PolicyrulenfsclientpostRules(
permission=module.params["nfs_permission"],
client=module.params["client"],
anongid=module.params["anongid"],
anonuid=module.params["anonuid"],
access=module.params["nfs_access"],
nfs_version=module.params["nfs_version"],
security=module.params["security"],
)
else:
rules = flasharray.PolicyrulenfsclientpostRules(
permission=module.params["nfs_permission"],
client=module.params["client"],
access=module.params["nfs_access"],
security=module.params["security"],
)
else:
if all_squash:
rules = flasharray.PolicyrulenfsclientpostRules(
permission=module.params["nfs_permission"],
client=module.params["client"],
anongid=module.params["anongid"],
anonuid=module.params["anonuid"],
access=module.params["nfs_access"],
nfs_version=module.params["nfs_version"],
)
else:
rules = flasharray.PolicyrulenfsclientpostRules(
permission=module.params["nfs_permission"],
client=module.params["client"],
access=module.params["nfs_access"],
)
rule = flasharray.PolicyRuleNfsClientPost(rules=[rules])
changed_rule = True
if not module.check_mode:
rule_created = array.post_policies_nfs_client_rules(
policy_names=[module.params["name"]], rules=rule
)
if rule_created.status_code != 200:
module.fail_json(
msg="Failed to create new rule for NFS policy {0}. Error: {1}".format(
module.params["name"],
rule_created.errors[0].message,
)
)
else:
if all_squash:
rules = flasharray.PolicyrulenfsclientpostRules(
permission=module.params["nfs_permission"],
anongid=module.params["anongid"],
anonuid=module.params["anonuid"],
client=module.params["client"],
access=module.params["nfs_access"],
)
else:
rules = flasharray.PolicyrulenfsclientpostRules(
permission=module.params["nfs_permission"],
client=module.params["client"],
access=module.params["nfs_access"],
)
rule = flasharray.PolicyRuleNfsClientPost(rules=[rules])
changed_rule = True
if not module.check_mode:
rule_created = array.post_policies_nfs_client_rules(
policy_names=[module.params["name"]], rules=rule
)
if rule_created.status_code != 200:
module.fail_json(
msg="Failed to create new rule for SMB policy {0}. Error: {1}".format(
module.params["name"], rule_created.errors[0].message
)
)
elif module.params["policy"] == "smb":
try:
current = list(array.get_policies_smb(names=[module.params["name"]]).items)[
0
]
current_enabled = current.enabled
current_access_based_enumeration = current.access_based_enumeration_enabled
except Exception:
module.fail_json(
msg="Incorrect policy type specified for existing policy {0}".format(
module.params["name"]
)
)
if (
"access_based_enumeration" in module.params
and current_access_based_enumeration
!= module.params["access_based_enumeration"]
):
changed_abe = True
if not module.check_mode:
res = array.patch_policies_smb(
names=[module.params["name"]],
policy=flasharray.PolicySmbPatch(
access_based_enumeration_enabled=module.params[
"access_based_enumeration"
]
),
)
if res.status_code != 200:
module.exit_json(
msg="Failed to enable/disable Access based enueration for SMB policy {0}".format(
module.params["name"]
)
)
if current_enabled != module.params["enabled"]:
changed_enable = True
if not module.check_mode:
res = array.patch_policies_smb(
names=[module.params["name"]],
policy=flasharray.PolicyPatch(enabled=module.params["enabled"]),
)
if res.status_code != 200:
module.exit_json(
msg="Failed to enable/disable SMB policy {0}".format(
module.params["name"]
)
)
if module.params["client"]:
rules = list(
array.get_policies_smb_client_rules(
policy_names=[module.params["name"]]
).items
)
if rules:
rule_name = ""
for rule in range(0, len(rules)):
if rules[rule].client == module.params["client"]:
rule_name = rules[rule].name
break
if not rule_name:
rules = flasharray.PolicyrulesmbclientpostRules(
anonymous_access_allowed=module.params["smb_anon_allowed"],
client=module.params["client"],
smb_encryption_required=module.params["smb_encrypt"],
)
rule = flasharray.PolicyRuleSmbClientPost(rules=[rules])
changed_rule = True
if not module.check_mode:
rule_created = array.post_policies_smb_client_rules(
policy_names=[module.params["name"]], rules=rule
)
if rule_created.status_code != 200:
module.fail_json(
msg="Failed to create new rule for SMB policy {0}. Error: {1}".format(
module.params["name"],
rule_created.errors[0].message,
)
)
else:
rules = flasharray.PolicyrulesmbclientpostRules(
anonymous_access_allowed=module.params["smb_anon_allowed"],
client=module.params["client"],
smb_encryption_required=module.params["smb_encrypt"],
)
rule = flasharray.PolicyRuleSmbClientPost(rules=[rules])
changed_rule = True
if not module.check_mode:
rule_created = array.post_policies_smb_client_rules(
policy_names=[module.params["name"]], rules=rule
)
if rule_created.status_code != 200:
module.fail_json(
msg="Failed to create new rule for SMB policy {0}. Error: {1}".format(
module.params["name"], rule_created.errors[0].message
)
)
elif module.params["policy"] == "snapshot":
suffix_enabled = bool(
LooseVersion(array.get_rest_version())
>= LooseVersion(MIN_SUFFIX_API_VERSION)
)
try:
current_enabled = list(
array.get_policies_snapshot(names=[module.params["name"]]).items
)[0].enabled
except Exception:
module.fail_json(
msg="Incorrect policy type specified for existing policy {0}".format(
module.params["name"]
)
)
if current_enabled != module.params["enabled"]:
changed_enable = True
if not module.check_mode:
res = array.patch_policies_snapshot(
names=[module.params["name"]],
policy=flasharray.PolicyPatch(enabled=module.params["enabled"]),
)
if res.status_code != 200:
module.exit_json(
msg="Failed to enable/disable snapshot policy {0}".format(
module.params["name"]
)
)
if module.params["directory"]:
dirs = []
new_dirs = []
current_dirs = list(
array.get_directories_policies_snapshot(
policy_names=[module.params["name"]]
).items
)
if current_dirs:
for current_dir in range(0, len(current_dirs)):
dirs.append(current_dirs[current_dir].member.name)
for new_dir in range(0, len(module.params["directory"])):
if module.params["directory"][new_dir] not in dirs:
changed_dir = True
new_dirs.append(module.params["directory"][new_dir])
else:
new_dirs = module.params["directory"]
if new_dirs:
policies = flasharray.DirectoryPolicyPost(
policies=[
flasharray.DirectorypolicypostPolicies(
policy=flasharray.Reference(name=module.params["name"])
)
]
)
changed_dir = True
for add_dir in range(0, len(new_dirs)):
if not module.check_mode:
directory_added = array.post_directories_policies_snapshot(
member_names=[new_dirs[add_dir]], policies=policies
)
if directory_added.status_code != 200:
module.fail_json(
msg="Failed to add new directory to Snapshot policy {0}. Error: {1}".format(
module.params["name"],
directory_added.errors[0].message,
)
)
if module.params["snap_client_name"]:
if module.params["snap_at"]:
if not module.params["snap_every"] % 1440 == 0:
module.fail_json(
msg="snap_at time can only be set if snap_every is multiple of 1440"
)
if module.params["snap_keep_for"] < module.params["snap_every"]:
module.fail_json(
msg="Retention period (snap_keep_for) cannot be less than snapshot interval (snap_every)."
)
if (
module.params["snap_keep_for"] != module.params["snap_every"]
and module.params["snap_suffix"]
):
module.fail_json(
msg="Suffix (snap_suufix) can only be applied when `snap_keep_for` and `snap_every` are equal."
)
rules = list(
array.get_policies_snapshot_rules(
policy_names=[module.params["name"]]
).items
)
if rules:
for rule in range(0, len(rules)):
if (
rules[rule].client_name == module.params["snap_client_name"]
and int(rules[rule].every / 60000)
== module.params["snap_every"]
and int(rules[rule].keep_for / 60000)
== module.params["snap_keep_for"]
):
module.exit_json(changed=False)
if module.params["snap_keep_for"] < module.params["snap_every"]:
module.fail_json(
msg="Retention period (snap_keep_for) cannot be less than snapshot interval (snap_every)."
)
if module.params["snap_at"]:
if not module.params["snap_every"] % 1440 == 0:
module.fail_json(
msg="snap_at time can only be set if snap_every is multiple of 1440"
)
if suffix_enabled:
rules = flasharray.PolicyrulesnapshotpostRules(
at=convert_to_millisecs(module.params["snap_at"]),
client_name=module.params["snap_client_name"],
every=module.params["snap_every"] * 60000,
keep_for=module.params["snap_keep_for"] * 60000,
suffix=module.params["snap_suffix"],
)
else:
rules = flasharray.PolicyrulesnapshotpostRules(
at=convert_to_millisecs(module.params["snap_at"]),
client_name=module.params["snap_client_name"],
every=module.params["snap_every"] * 60000,
keep_for=module.params["snap_keep_for"] * 60000,
)
else:
if suffix_enabled:
rules = flasharray.PolicyrulesnapshotpostRules(
client_name=module.params["snap_client_name"],
every=module.params["snap_every"] * 60000,
keep_for=module.params["snap_keep_for"] * 60000,
suffix=module.params["snap_suffix"],
)
else:
rules = flasharray.PolicyrulesnapshotpostRules(
client_name=module.params["snap_client_name"],
every=module.params["snap_every"] * 60000,
keep_for=module.params["snap_keep_for"] * 60000,
)
rule = flasharray.PolicyRuleSnapshotPost(rules=[rules])
changed_rule = True
if not module.check_mode:
rule_created = array.post_policies_snapshot_rules(
policy_names=[module.params["name"]], rules=rule
)
if rule_created.status_code != 200:
err_no = len(rule_created.errors) - 1
module.fail_json(
msg="Failed to create new rule for Snapshot policy {0}. Error: {1}".format(
module.params["name"],
rule_created.errors[err_no].message,
)
)
else:
if module.params["snap_keep_for"] < module.params["snap_every"]:
module.fail_json(
msg="Retention period (snap_keep_for) cannot be less than snapshot interval (snap_every)."
)
if module.params["snap_at"]:
if not module.params["snap_every"] % 1440 == 0:
module.fail_json(
msg="snap_at time can only be set if snap_every is multiple of 1440"
)
if suffix_enabled:
rules = flasharray.PolicyrulesnapshotpostRules(
at=convert_to_millisecs(module.params["snap_at"]),
client_name=module.params["snap_client_name"],
every=module.params["snap_every"] * 60000,
keep_for=module.params["snap_keep_for"] * 60000,
suffix=module.params["snap_suffix"],
)
else:
rules = flasharray.PolicyrulesnapshotpostRules(
at=convert_to_millisecs(module.params["snap_at"]),
client_name=module.params["snap_client_name"],
every=module.params["snap_every"] * 60000,
keep_for=module.params["snap_keep_for"] * 60000,
)
else:
if suffix_enabled:
rules = flasharray.PolicyrulesnapshotpostRules(
client_name=module.params["snap_client_name"],
every=module.params["snap_every"] * 60000,
keep_for=module.params["snap_keep_for"] * 60000,
suffix=module.params["snap_suffix"],
)
else:
rules = flasharray.PolicyrulesnapshotpostRules(
client_name=module.params["snap_client_name"],
every=module.params["snap_every"] * 60000,
keep_for=module.params["snap_keep_for"] * 60000,
)
rule = flasharray.PolicyRuleSnapshotPost(rules=[rules])
changed_rule = True
if not module.check_mode:
rule_created = array.post_policies_snapshot_rules(
policy_names=[module.params["name"]], rules=rule
)
if rule_created.status_code != 200:
err_no = len(rule_created.errors) - 1
module.fail_json(
msg="Failed to create new rule for Snapshot policy {0}. Error: {1}".format(
module.params["name"],
rule_created.errors[err_no].message,
)
)
elif module.params["policy"] == "autodir":
try:
current_enabled = list(
array.get_policies_autodir(names=[module.params["name"]]).items
)[0].enabled
except Exception:
module.fail_json(
msg="Incorrect policy type specified for existing policy {0}".format(
module.params["name"]
)
)
if current_enabled != module.params["enabled"]:
changed_enable = True
if not module.check_mode:
res = array.patch_policies_autodir(
names=[module.params["name"]],
policy=flasharray.PolicyPatch(enabled=module.params["enabled"]),
)
if res.status_code != 200:
module.exit_json(
msg="Failed to enable/disable autodir policy {0}".format(
module.params["name"]
)
)
if module.params["directory"]:
dirs = []
new_dirs = []
current_dirs = list(
array.get_directories_policies_autodir(
policy_names=[module.params["name"]]
).items
)
if current_dirs:
for current_dir in range(0, len(current_dirs)):
dirs.append(current_dirs[current_dir].member.name)
for new_dir in range(0, len(module.params["directory"])):
if module.params["directory"][new_dir] not in dirs:
changed_dir = True
new_dirs.append(module.params["directory"][new_dir])
else:
new_dirs = module.params["directory"]
if new_dirs:
policies = flasharray.DirectoryPolicyPost(
policies=[
flasharray.DirectorypolicypostPolicies(
policy=flasharray.Reference(name=module.params["name"])
)
]
)
changed_dir = True
for add_dir in range(0, len(new_dirs)):
if not module.check_mode:
directory_added = array.post_directories_policies_autodir(
member_names=[new_dirs[add_dir]], policies=policies
)
if directory_added.status_code != 200:
module.fail_json(
msg="Failed to add new directory to Autodir policy {0}. Error: {1}".format(
module.params["name"],
directory_added.errors[0].message,
)
)
else: # quota
current_enabled = list(
array.get_policies_quota(names=[module.params["name"]]).items
)[0].enabled
if current_enabled != module.params["enabled"]:
changed_quota = True
if not module.check_mode:
res = array.patch_policies_quota(
names=[module.params["name"]],
policy=flasharray.PolicyPatch(enabled=module.params["enabled"]),
)
if res.status_code != 200:
module.exit_json(
msg="Failed to enable/disable snapshot policy {0}".format(
module.params["name"]
)
)
if module.params["directory"]:
current_members = list(
array.get_policies_quota_members(
policy_names=[module.params["name"]]
).items
)
if current_members:
if module.params["state"] == "absent":
for member in range(0, len(current_members)):
if (
current_members[member].member.name
in module.params["directory"]
):
changed_member = True
if not module.check_mode:
res = array.delete_policies_quota_members(
policy_names=[module.params["name"]],
member_names=[current_members[member].member.name],
)
if res.status_code != 200:
module.fail_json(
msg="Failed to delete rule {0} from quota policy {1}. Error: {2}".format(
current_members[member].member.name,
module.params["name"],
rule_created.errors[0].message,
)
)
else:
members = []
cmembers = []
for cmem in range(0, len(current_members)):
cmembers.append(current_members[cmem].member.name)
mem_diff = list(set(module.params["directory"]) - set(cmembers))
if mem_diff:
for mem in range(0, len(mem_diff)):
members.append(
flasharray.PolicymemberpostMembers(
member=flasharray.ReferenceWithType(
name=mem_diff[mem],
resource_type="directories",
)
)
)
member = flasharray.PolicyMemberPost(members=members)
changed_member = True
if not module.check_mode:
members_created = array.post_policies_quota_members(
policy_names=[module.params["name"]],
members=member,
ignore_usage=module.params["ignore_usage"],
)
if members_created.status_code != 200:
module.fail_json(
msg="Failed to update members for Quota policy {0}. Error: {1}".format(
module.params["name"],
members_created.errors[0].message,
)
)
else:
members = []
for mem in range(0, len(module.params["directory"])):
members.append(
flasharray.PolicymemberpostMembers(
member=flasharray.ReferenceWithType(
name=module.params["directory"][mem],
resource_type="directories",
)
)
)
member = flasharray.PolicyMemberPost(members=members)
changed_member = True
if not module.check_mode:
members_created = array.post_policies_quota_members(
policy_names=[module.params["name"]],
members=member,
ignore_usage=module.params["ignore_usage"],
)
if members_created.status_code != 200:
module.fail_json(
msg="Failed to update members for Quota policy {0}. Error: {1}".format(
module.params["name"],
members_created.errors[0].message,
)
)
if module.params["quota_limit"]:
quota = human_to_bytes(module.params["quota_limit"])
current_rules = list(
array.get_policies_quota_rules(
policy_names=[module.params["name"]]
).items
)
if current_rules:
one_enforced = False
for check_rule in range(0, len(current_rules)):
if current_rules[check_rule].enforced:
one_enforced = True
for rule in range(0, len(current_rules)):
rule_exists = False
if not module.params["quota_notifications"]:
current_notifications = "none"
else:
current_notifications = ",".join(
module.params["quota_notifications"]
)
if bool(
(current_rules[rule].quota_limit == quota)
and (
current_rules[rule].enforced
== module.params["quota_enforced"]
)
and (current_rules[rule].notifications == current_notifications)
):
rule_exists = True
break
if not rule_exists:
if module.params["quota_enforced"] and one_enforced:
module.fail_json(
msg="Only one enforced rule can be defined per policy"
)
rules = flasharray.PolicyrulequotapostRules(
enforced=module.params["quota_enforced"],
quota_limit=quota,
notifications=",".join(module.params["quota_notifications"]),
)
rule = flasharray.PolicyRuleQuotaPost(rules=[rules])
changed_quota = True
if not module.check_mode:
quota_created = array.post_policies_quota_rules(
policy_names=[module.params["name"]],
rules=rule,
ignore_usage=module.params["ignore_usage"],
)
if quota_created.status_code != 200:
module.fail_json(
msg="Failed to add new rule to Quota policy {0}. Error: {1}".format(
module.params["name"],
quota_created.errors[0].message,
)
)
else:
rules = flasharray.PolicyrulequotapostRules(
enforced=module.params["quota_enforced"],
quota_limit=quota,
notifications=",".join(module.params["quota_notifications"]),
)
rule = flasharray.PolicyRuleQuotaPost(rules=[rules])
changed_quota = True
if not module.check_mode:
quota_created = array.post_policies_quota_rules(
policy_names=[module.params["name"]],
rules=rule,
ignore_usage=module.params["ignore_usage"],
)
if quota_created.status_code != 200:
module.fail_json(
msg="Failed to add rule to Quota policy {0}. Error: {1}".format(
module.params["name"], quota_created.errors[0].message
)
)
if (
changed_rule
or changed_enable
or changed_quota
or changed_member
or changed_dir
or changed_user_map
or changed_abe
or changed_nfs
):
changed = True
module.exit_json(changed=changed)
def main():
argument_spec = purefa_argument_spec()
argument_spec.update(
dict(
state=dict(type="str", default="present", choices=["absent", "present"]),
nfs_access=dict(
type="str",
default="no-root-squash",
choices=["root-squash", "no-root-squash", "all-squash"],
),
nfs_permission=dict(type="str", default="rw", choices=["rw", "ro"]),
policy=dict(
type="str",
required=True,
choices=["nfs", "smb", "snapshot", "quota", "autodir"],
),
name=dict(type="str", required=True),
rename=dict(type="str"),
client=dict(type="str"),
enabled=dict(type="bool", default=True),
snap_at=dict(type="str"),
snap_every=dict(type="int"),
snap_keep_for=dict(type="int"),
snap_client_name=dict(type="str"),
snap_suffix=dict(type="str"),
smb_anon_allowed=dict(type="bool", default=False),
smb_encrypt=dict(type="bool", default=False),
ignore_usage=dict(type="bool", default=False),
quota_enforced=dict(type="bool", default=True),
quota_limit=dict(type="str"),
anongid=dict(type="str", default="65534"),
anonuid=dict(type="str", default="65534"),
quota_notifications=dict(
type="list", elements="str", choices=["user", "group"]
),
user_mapping=dict(type="bool", default=True),
directory=dict(type="list", elements="str"),
nfs_version=dict(
type="list",
elements="str",
choices=["nfsv3", "nfsv4"],
),
security=dict(
type="list",
elements="str",
choices=["auth_sys", "krb5", "krb5i", "krb5p"],
),
access_based_enumeration=dict(type="bool", default=False),
)
)
required_together = [["snap_keep_for", "snap_every"]]
module = AnsibleModule(
argument_spec, required_together=required_together, supports_check_mode=True
)
if not HAS_PURESTORAGE:
module.fail_json(msg="py-pure-client sdk is required for this module")
array = get_system(module)
api_version = array._list_available_rest_versions()
if MIN_REQUIRED_API_VERSION not in api_version:
module.fail_json(
msg="FlashArray REST version not supported. "
"Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION)
)
if module.params["policy"] == "quota" and MIN_QUOTA_API_VERSION not in api_version:
module.fail_json(
msg="FlashArray REST version not supportedi for directory quotas. "
"Minimum version required: {0}".format(MIN_QUOTA_API_VERSION)
)
if module.params["policy"] == "autodir" and AUTODIR_VERSION not in api_version:
module.fail_json(
msg="FlashArray REST version not supported for autodir policies. "
"Minimum version required: {0}".format(AUTODIR_VERSION)
)
array = get_array(module)
state = module.params["state"]
if module.params["quota_notifications"]:
module.params["quota_notifications"].sort(reverse=True)
quota_notifications = []
[
quota_notifications.append(x)
for x in module.params["quota_notifications"]
if x not in quota_notifications
]
module.params["quota_notifications"] = quota_notifications
else:
module.params["quota_notifications"] = []
if (
module.params["nfs_access"] == "all-squash"
and ALL_SQUASH_VERSION not in api_version
):
module.fail_json(
msg="all-squash is not supported in this version of Purity//FA"
)
all_squash = ALL_SQUASH_VERSION in api_version
exists = bool(array.get_policies(names=[module.params["name"]]).status_code == 200)
if state == "present" and not exists:
create_policy(module, array, all_squash)
elif state == "present" and exists and module.params["rename"]:
rename_policy(module, array)
elif state == "present" and exists:
update_policy(module, array, api_version, all_squash)
elif state == "absent" and exists:
delete_policy(module, array)
module.exit_json(changed=False)
if __name__ == "__main__":
main()
|