| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = { |
| 12 |
"metadata_version": "1.1", |
| 13 |
"status": ["preview"], |
| 14 |
"supported_by": "community", |
| 15 |
} |
| 16 |
|
| 17 |
DOCUMENTATION = r""" |
| 18 |
--- |
| 19 |
module: purefa_policy |
| 20 |
version_added: '1.5.0' |
| 21 |
short_description: Manage FlashArray File System Policies |
| 22 |
description: |
| 23 |
- Manage FlashArray file system policies for NFS, SMB and snapshot |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- Name of the policy |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
state: |
| 33 |
description: |
| 34 |
- Define whether the policy should exist or not. |
| 35 |
default: present |
| 36 |
choices: [ absent, present ] |
| 37 |
type: str |
| 38 |
policy: |
| 39 |
description: |
| 40 |
- The type of policy to use |
| 41 |
choices: [ nfs, smb, snapshot, quota, autodir, password ] |
| 42 |
required: true |
| 43 |
type: str |
| 44 |
enabled: |
| 45 |
description: |
| 46 |
- Define if policy is enabled or not |
| 47 |
type: bool |
| 48 |
default: true |
| 49 |
smb_anon_allowed: |
| 50 |
description: |
| 51 |
- Specifies whether access to information is allowed for anonymous users |
| 52 |
type: bool |
| 53 |
default: false |
| 54 |
client: |
| 55 |
description: |
| 56 |
- Specifies which SMB or NFS clients are given access |
| 57 |
- Accepted notation, IP, IP mask, or hostname |
| 58 |
type: str |
| 59 |
smb_encrypt: |
| 60 |
description: |
| 61 |
- Specifies whether the remote client is required to use SMB encryption |
| 62 |
type: bool |
| 63 |
default: false |
| 64 |
nfs_access: |
| 65 |
description: |
| 66 |
- Specifies access control for the export |
| 67 |
choices: [ root-squash, no-root-squash, all-squash ] |
| 68 |
type: str |
| 69 |
default: no-root-squash |
| 70 |
nfs_permission: |
| 71 |
description: |
| 72 |
- Specifies which read-write client access permissions are allowed for the export |
| 73 |
choices: [ ro, rw ] |
| 74 |
default: rw |
| 75 |
type: str |
| 76 |
nfs_version: |
| 77 |
description: |
| 78 |
- NFS protocol version allowed for the export |
| 79 |
type: list |
| 80 |
elements: str |
| 81 |
choices: [ nfsv3, nfsv4 ] |
| 82 |
version_added: "1.22.0" |
| 83 |
user_mapping: |
| 84 |
description: |
| 85 |
- Defines if user mapping is enabled |
| 86 |
type: bool |
| 87 |
default: true |
| 88 |
version_added: 1.14.0 |
| 89 |
snap_at: |
| 90 |
description: |
| 91 |
- Specifies the number of hours since midnight at which to take a snapshot |
| 92 |
or the hour including AM/PM |
| 93 |
- Can only be set on the rule with the smallest I(snap_every) value. |
| 94 |
- Cannot be set if the I(snap_every) value is not measured in days. |
| 95 |
- Can only be set for at most one rule in the same policy. |
| 96 |
type: str |
| 97 |
snap_every: |
| 98 |
description: |
| 99 |
- Specifies the interval between snapshots, in minutes. |
| 100 |
- The value for all rules must be multiples of one another. |
| 101 |
- Must be unique for each rule in the same policy. |
| 102 |
- Value must be between 5 and 525600. |
| 103 |
type: int |
| 104 |
snap_keep_for: |
| 105 |
description: |
| 106 |
- Specifies the period that snapshots are retained before they are eradicated, in minutes. |
| 107 |
- Cannot be less than the I(snap_every) value of the rule. |
| 108 |
- Value must be unique for each rule in the same policy. |
| 109 |
- Value must be between 5 and 525600. |
| 110 |
type: int |
| 111 |
snap_client_name: |
| 112 |
description: |
| 113 |
- The customizable portion of the client visible snapshot name. |
| 114 |
type: str |
| 115 |
snap_suffix: |
| 116 |
description: |
| 117 |
- The snapshot suffix name |
| 118 |
- The suffix value can only be set for one rule in the same policy |
| 119 |
- The suffix value can only be set on a rule with the same ``keep_for`` value and ``every`` value |
| 120 |
- The suffix value can only be set on the rule with the largest ``keep_for`` value |
| 121 |
- If not specified, defaults to a monotonically increasing number generated by the system. |
| 122 |
type: str |
| 123 |
version_added: 1.10.0 |
| 124 |
rename: |
| 125 |
description: |
| 126 |
- New name of policy |
| 127 |
type: str |
| 128 |
directory: |
| 129 |
description: |
| 130 |
- Directories to have the quota rule applied to. |
| 131 |
type: list |
| 132 |
elements: str |
| 133 |
version_added: 1.9.0 |
| 134 |
quota_limit: |
| 135 |
description: |
| 136 |
- Logical space limit of the share in M, G, T or P units. See examples. |
| 137 |
- If size is not set at filesystem creation time the filesystem size becomes unlimited. |
| 138 |
- This value cannot be set to 0. |
| 139 |
type: str |
| 140 |
version_added: 1.9.0 |
| 141 |
quota_notifications: |
| 142 |
description: |
| 143 |
- Targets to notify when usage approaches the quota limit. |
| 144 |
- The list of notification targets is a comma-separated string |
| 145 |
- If not specified, notification targets are not assigned. |
| 146 |
type: list |
| 147 |
elements: str |
| 148 |
choices: [ user, group, none ] |
| 149 |
version_added: 1.9.0 |
| 150 |
quota_enforced: |
| 151 |
description: |
| 152 |
- Defines if the directory quota is enforced. |
| 153 |
default: true |
| 154 |
type: bool |
| 155 |
ignore_usage: |
| 156 |
description: |
| 157 |
- Flag used to override checks for quota management |
| 158 |
operations. |
| 159 |
- If set to true, directory usage is not checked against the |
| 160 |
quota_limits that are set. |
| 161 |
- If set to false, the actual logical bytes in use are prevented |
| 162 |
from exceeding the limits set on the directory. |
| 163 |
- Client operations might be impacted. |
| 164 |
- If the limit exceeds the quota, the client operation is not allowed. |
| 165 |
default: false |
| 166 |
type: bool |
| 167 |
version_added: 1.9.0 |
| 168 |
anonuid: |
| 169 |
description: |
| 170 |
- The ID to which any users whose UID is affected by I(access) of |
| 171 |
I(root-squash) or I(all-squash) will be mapped to. |
| 172 |
- Clear using "". |
| 173 |
type: str |
| 174 |
default: "65534" |
| 175 |
version_added: 1.14.0 |
| 176 |
anongid: |
| 177 |
description: |
| 178 |
- The ID to which any users whose GID is affected by I(access) of |
| 179 |
I(root-squash) or I(all-squash) will be mapped to. |
| 180 |
- This is ignored when I(user_mapping) is enabled. |
| 181 |
- Clear using "". |
| 182 |
type: str |
| 183 |
default: "65534" |
| 184 |
version_added: 1.14.0 |
| 185 |
security: |
| 186 |
description: |
| 187 |
- The security flavors to use for accessing files on a mount point. |
| 188 |
- If the server does not support the requested flavor, the mount operation fails. |
| 189 |
- This operation updates all rules of the specified policy. |
| 190 |
type: list |
| 191 |
elements: str |
| 192 |
choices: [ auth_sys, krb5, krb5i, krb5p ] |
| 193 |
version_added: 1.25.0 |
| 194 |
access_based_enumeration: |
| 195 |
description: |
| 196 |
- Defines if access based enumeration for SMB is enabled |
| 197 |
type: bool |
| 198 |
default: false |
| 199 |
version_added: 1.26.0 |
| 200 |
enforce_dictionary_check: |
| 201 |
description: |
| 202 |
- If I(true), test password against dictionary of known leaked passwords. |
| 203 |
- Only applies to passwords longer than 6 characters. |
| 204 |
type: bool |
| 205 |
version_added: 1.33.0 |
| 206 |
enforce_username_check: |
| 207 |
description: |
| 208 |
- If I(true), the username cannot be a substring of the password. |
| 209 |
- It only applies to usernames of 3 characters and longer |
| 210 |
type: bool |
| 211 |
version_added: 1.33.0 |
| 212 |
max_login_attempts: |
| 213 |
description: |
| 214 |
- Maximum number of failed logins before account is locked |
| 215 |
type: int |
| 216 |
version_added: 1.33.0 |
| 217 |
min_password_length: |
| 218 |
description: |
| 219 |
- Minimum user password length |
| 220 |
type: int |
| 221 |
version_added: 1.33.0 |
| 222 |
lockout_duration: |
| 223 |
description: |
| 224 |
- Account lockout duration, in seconds, after I(max_login_attempts) exceeded |
| 225 |
- Range between 1 second and 90 days (7776000 seconds) |
| 226 |
type: int |
| 227 |
version_added: 1.33.0 |
| 228 |
password_history: |
| 229 |
description: |
| 230 |
- The number of passwords tracked to prevent reuse of passwords. |
| 231 |
- A value of 0 will not check password history |
| 232 |
type: int |
| 233 |
version_added: 1.33.0 |
| 234 |
min_character_groups: |
| 235 |
description: |
| 236 |
- The minimum number of character groups required to be present in a password. |
| 237 |
type: int |
| 238 |
version_added: 1.33.0 |
| 239 |
min_characters_per_group: |
| 240 |
description: |
| 241 |
- The minimum number of characters per group to count the group as present. |
| 242 |
- Maximum is limited by the minimum password length divided by the number of character groups |
| 243 |
type: int |
| 244 |
version_added: 1.33.0 |
| 245 |
rule_name: |
| 246 |
description: |
| 247 |
- Name of rule to update for a quota policy |
| 248 |
type: str |
| 249 |
version_added: 1.34.0 |
| 250 |
context: |
| 251 |
description: |
| 252 |
- Name of fleet member on which to perform the operation. |
| 253 |
- This requires the array receiving the request is a member of a fleet |
| 254 |
and the context name to be a member of the same fleet. |
| 255 |
type: str |
| 256 |
default: "" |
| 257 |
version_added: '1.39.0' |
| 258 |
continuous_availability: |
| 259 |
description: |
| 260 |
- Defines if continuous availability on the policy. |
| 261 |
- When continuous availability is enabled on a policy, file shares are |
| 262 |
accessible during otherwise disruptive scenarios such as temporary |
| 263 |
network outages, controller upgrades or failovers. |
| 264 |
type: bool |
| 265 |
default: false |
| 266 |
version_added: 1.40.0 |
| 267 |
extends_documentation_fragment: |
| 268 |
- purestorage.flasharray.purestorage.fa |
| 269 |
""" |
| 270 |
|
| 271 |
EXAMPLES = r""" |
| 272 |
- name: Create an NFS policy with initial rule |
| 273 |
purestorage.flasharray.purefa_policy: |
| 274 |
name: export1 |
| 275 |
policy: nfs |
| 276 |
nfs_access: root-squash |
| 277 |
nfs_permission: ro |
| 278 |
client: client1 |
| 279 |
fa_url: 10.10.10.2 |
| 280 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 281 |
|
| 282 |
- name: Create an empty NFS policy with no rules |
| 283 |
purestorage.flasharray.purefa_policy: |
| 284 |
name: export1 |
| 285 |
policy: nfs |
| 286 |
fa_url: 10.10.10.2 |
| 287 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 288 |
|
| 289 |
- name: Create an empty snapshot policy with no rules |
| 290 |
purestorage.flasharray.purefa_policy: |
| 291 |
name: snap1 |
| 292 |
policy: snapshot |
| 293 |
fa_url: 10.10.10.2 |
| 294 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 295 |
|
| 296 |
- name: Create an empty snapshot policy with single directory member |
| 297 |
purestorage.flasharray.purefa_policy: |
| 298 |
name: snap1 |
| 299 |
policy: snapshot |
| 300 |
directory: "foo:bar" |
| 301 |
fa_url: 10.10.10.2 |
| 302 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 303 |
|
| 304 |
- name: Disable a policy |
| 305 |
purestorage.flasharray.purefa_policy: |
| 306 |
name: export1 |
| 307 |
policy: nfs |
| 308 |
enabled: false |
| 309 |
fa_url: 10.10.10.2 |
| 310 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 311 |
|
| 312 |
- name: Add rule to existing NFS export policy |
| 313 |
purestorage.flasharray.purefa_policy: |
| 314 |
name: export1 |
| 315 |
policy: nfs |
| 316 |
nfs_access: root-squash |
| 317 |
nfs_permission: ro |
| 318 |
client: client2 |
| 319 |
fa_url: 10.10.10.2 |
| 320 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 321 |
|
| 322 |
- name: Add rule to existing SMB export policy |
| 323 |
purestorage.flasharray.purefa_policy: |
| 324 |
name: export1 |
| 325 |
policy: smb |
| 326 |
smb_encrypt: true |
| 327 |
smb_anon_allowed: false |
| 328 |
client: client1 |
| 329 |
fa_url: 10.10.10.2 |
| 330 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 331 |
|
| 332 |
- name: Add non-suffix rule to existing snapshot export policy |
| 333 |
purestorage.flasharray.purefa_policy: |
| 334 |
name: snap1 |
| 335 |
policy: snapshot |
| 336 |
snap_client_name: foo |
| 337 |
snap_every: 15 |
| 338 |
snap_keep_for: 1440 |
| 339 |
fa_url: 10.10.10.2 |
| 340 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 341 |
|
| 342 |
- name: Add suffix rule to existing snapshot export policy |
| 343 |
purestorage.flasharray.purefa_policy: |
| 344 |
name: snap1 |
| 345 |
policy: snapshot |
| 346 |
snap_client_name: foo |
| 347 |
snap_suffix: bar |
| 348 |
snap_every: 1440 |
| 349 |
snap_keep_for: 1440 |
| 350 |
fa_url: 10.10.10.2 |
| 351 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 352 |
|
| 353 |
- name: Delete policy rule for a client |
| 354 |
purestorage.flasharray.purefa_policy: |
| 355 |
name: export1 |
| 356 |
policy: nfs |
| 357 |
client: client2 |
| 358 |
state: absent |
| 359 |
fa_url: 10.10.10.2 |
| 360 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 361 |
|
| 362 |
- name: Delete policy |
| 363 |
purestorage.flasharray.purefa_policy: |
| 364 |
name: export1 |
| 365 |
policy: nfs |
| 366 |
state: absent |
| 367 |
fa_url: 10.10.10.2 |
| 368 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 369 |
|
| 370 |
- name: Create directory quota policy for directory bar |
| 371 |
purestorage.flasharray.purefa_policy: |
| 372 |
name: foo |
| 373 |
directory: |
| 374 |
- "foo:root" |
| 375 |
- "bar:bin" |
| 376 |
policy: quota |
| 377 |
quota_limit: 10G |
| 378 |
fa_url: 10.10.10.2 |
| 379 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 380 |
|
| 381 |
- name: Delete directory quota policy foo |
| 382 |
purestorage.flasharray.purefa_policy: |
| 383 |
name: foo |
| 384 |
policy: quota |
| 385 |
state: absent |
| 386 |
fa_url: 10.10.10.2 |
| 387 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 388 |
|
| 389 |
- name: Create empty directory quota policy foo |
| 390 |
purestorage.flasharray.purefa_policy: |
| 391 |
name: foo |
| 392 |
policy: quota |
| 393 |
fa_url: 10.10.10.2 |
| 394 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 395 |
|
| 396 |
- name: Detach directory "foo:bar" from quota policy quota1 |
| 397 |
purestorage.flasharray.purefa_policy: |
| 398 |
name: quota1 |
| 399 |
directory: |
| 400 |
- "foo:bar" |
| 401 |
state: absent |
| 402 |
policy: quota |
| 403 |
fa_url: 10.10.10.2 |
| 404 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 405 |
|
| 406 |
- name: Remove quota rule from quota policy foo |
| 407 |
purestorage.flasharray.purefa_policy: |
| 408 |
name: foo |
| 409 |
policy: quota |
| 410 |
quota_limit: 10G |
| 411 |
state: absent |
| 412 |
fa_url: 10.10.10.2 |
| 413 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 414 |
|
| 415 |
- name: Update password police management |
| 416 |
purestorage.flasharray.purefa_policy: |
| 417 |
name: management |
| 418 |
policy: password |
| 419 |
max_login_attempts: 5 |
| 420 |
enforce_username_check: true |
| 421 |
enforce_dictionary_check: true |
| 422 |
min_password_length: 5 |
| 423 |
password_history: 2 |
| 424 |
fa_url: 10.10.10.2 |
| 425 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 426 |
""" |
| 427 |
|
| 428 |
RETURN = r""" |
| 429 |
""" |
| 430 |
|
| 431 |
HAS_PURESTORAGE = True |
| 432 |
try: |
| 433 |
from pypureclient.flasharray import ( |
| 434 |
PolicyPatch, |
| 435 |
PolicyRuleQuotaPatch, |
| 436 |
PolicyPost, |
| 437 |
PolicyrulenfsclientpostRules, |
| 438 |
PolicyRuleNfsClientPost, |
| 439 |
PolicyNfsPatch, |
| 440 |
PolicySmbPatch, |
| 441 |
PolicyrulesmbclientpostRules, |
| 442 |
PolicyRuleSmbClientPost, |
| 443 |
PolicyrulesnapshotpostRules, |
| 444 |
PolicyrulequotapatchRules, |
| 445 |
PolicyRuleSnapshotPost, |
| 446 |
DirectoryPolicyPost, |
| 447 |
DirectorypolicypostPolicies, |
| 448 |
PolicyRuleQuotaPost, |
| 449 |
PolicyrulequotapostRules, |
| 450 |
PolicyMemberPost, |
| 451 |
PolicymemberpostMembers, |
| 452 |
PolicyPassword, |
| 453 |
ReferenceWithType, |
| 454 |
Reference, |
| 455 |
) |
| 456 |
except ImportError: |
| 457 |
HAS_PURESTORAGE = False |
| 458 |
|
| 459 |
from ansible.module_utils.basic import AnsibleModule |
| 460 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 461 |
get_array, |
| 462 |
purefa_argument_spec, |
| 463 |
) |
| 464 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import ( |
| 465 |
LooseVersion, |
| 466 |
) |
| 467 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.common import ( |
| 468 |
human_to_bytes, |
| 469 |
convert_to_millisecs, |
| 470 |
) |
| 471 |
|
| 472 |
MIN_REQUIRED_API_VERSION = "2.3" |
| 473 |
MIN_QUOTA_API_VERSION = "2.7" |
| 474 |
MIN_SUFFIX_API_VERSION = "2.9" |
| 475 |
USER_MAP_VERSION = "2.15" |
| 476 |
ALL_SQUASH_VERSION = "2.16" |
| 477 |
AUTODIR_VERSION = "2.24" |
| 478 |
NFS_VERSION = "2.26" |
| 479 |
SECURITY_VERSION = "2.29" |
| 480 |
ABE_VERSION = "2.4" |
| 481 |
PASSWORD_VERSION = "2.34" |
| 482 |
CONTEXT_VERSION = "2.38" |
| 483 |
CA_VERSION = "2.43" |
| 484 |
|
| 485 |
|
| 486 |
def rename_policy(module, array): |
| 487 |
"""Rename a file system policy""" |
| 488 |
changed = False |
| 489 |
api_version = array.get_rest_version() |
| 490 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 491 |
res = array.get_policies( |
| 492 |
names=[module.params["rename"]], context_names=[module.params["context"]] |
| 493 |
) |
| 494 |
else: |
| 495 |
res = array.get_policies(names=[module.params["rename"]]) |
| 496 |
if res.status_code != 200: |
| 497 |
module.fail_json( |
| 498 |
msg="Rename failed - Target policy {0} already exists".format( |
| 499 |
module.params["rename"] |
| 500 |
) |
| 501 |
) |
| 502 |
if not module.check_mode: |
| 503 |
changed = True |
| 504 |
if module.params["policy"] == "nfs": |
| 505 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 506 |
res = array.patch_policies_nfs( |
| 507 |
names=[module.params["name"]], |
| 508 |
policy=PolicyPatch(name=module.params["rename"]), |
| 509 |
context_names=[module.params["context"]], |
| 510 |
) |
| 511 |
else: |
| 512 |
res = array.patch_policies_nfs( |
| 513 |
names=[module.params["name"]], |
| 514 |
policy=PolicyPatch(name=module.params["rename"]), |
| 515 |
) |
| 516 |
if res.status_code != 200: |
| 517 |
module.fail_json( |
| 518 |
msg="Failed to rename NFS policy {0} to {1}".format( |
| 519 |
module.params["name"], module.params["rename"] |
| 520 |
) |
| 521 |
) |
| 522 |
elif module.params["policy"] == "smb": |
| 523 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 524 |
res = array.patch_policies_smb( |
| 525 |
names=[module.params["name"]], |
| 526 |
policy=PolicyPatch(name=module.params["rename"]), |
| 527 |
context_names=[module.params["context"]], |
| 528 |
) |
| 529 |
else: |
| 530 |
res = array.patch_policies_smb( |
| 531 |
names=[module.params["name"]], |
| 532 |
policy=PolicyPatch(name=module.params["rename"]), |
| 533 |
) |
| 534 |
if res.status_code != 200: |
| 535 |
module.fail_json( |
| 536 |
msg="Failed to rename SMB policy {0} to {1}".format( |
| 537 |
module.params["name"], module.params["rename"] |
| 538 |
) |
| 539 |
) |
| 540 |
elif module.params["policy"] == "snapshot": |
| 541 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 542 |
res = array.patch_policies_snapshot( |
| 543 |
names=[module.params["name"]], |
| 544 |
policy=PolicyPatch(name=module.params["rename"]), |
| 545 |
context_names=[module.params["context"]], |
| 546 |
) |
| 547 |
else: |
| 548 |
res = array.patch_policies_snapshot( |
| 549 |
names=[module.params["name"]], |
| 550 |
policy=PolicyPatch(name=module.params["rename"]), |
| 551 |
) |
| 552 |
if res.status_code != 200: |
| 553 |
module.fail_json( |
| 554 |
msg="Failed to rename snapshot policy {0} to {1}".format( |
| 555 |
module.params["name"], module.params["rename"] |
| 556 |
) |
| 557 |
) |
| 558 |
elif module.params["policy"] == "password": |
| 559 |
module.fail_json(msg="Password policy rename is not yet supported") |
| 560 |
else: |
| 561 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 562 |
res = array.patch_policies_quota( |
| 563 |
names=[module.params["name"]], |
| 564 |
policy=PolicyPatch(name=module.params["rename"]), |
| 565 |
context_names=[module.params["context"]], |
| 566 |
) |
| 567 |
else: |
| 568 |
res = array.patch_policies_quota( |
| 569 |
names=[module.params["name"]], |
| 570 |
policy=PolicyPatch(name=module.params["rename"]), |
| 571 |
) |
| 572 |
if res.status_code != 200: |
| 573 |
module.fail_json( |
| 574 |
msg="Failed to rename quota policy {0} to {1}".format( |
| 575 |
module.params["name"], module.params["rename"] |
| 576 |
) |
| 577 |
) |
| 578 |
module.exit_json(changed=changed) |
| 579 |
|
| 580 |
|
| 581 |
def delete_policy(module, array): |
| 582 |
"""Delete a file system policy or rule within a policy""" |
| 583 |
changed = True |
| 584 |
api_version = array.get_rest_version() |
| 585 |
if not module.check_mode: |
| 586 |
changed = False |
| 587 |
if module.params["policy"] == "password": |
| 588 |
module.warn("Password policy deletion is not yet supported") |
| 589 |
changed = False |
| 590 |
if module.params["policy"] == "nfs": |
| 591 |
if not module.params["client"]: |
| 592 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 593 |
res = array.delete_policies_nfs( |
| 594 |
names=[module.params["name"]], |
| 595 |
context_names=[module.params["context"]], |
| 596 |
) |
| 597 |
else: |
| 598 |
res = array.delete_policies_nfs(names=[module.params["name"]]) |
| 599 |
if res.status_code == 200: |
| 600 |
changed = True |
| 601 |
else: |
| 602 |
module.fail_json( |
| 603 |
msg="Deletion of NFS policy {0} failed. Error: {1}".format( |
| 604 |
module.params["name"], res.errors[0].message |
| 605 |
) |
| 606 |
) |
| 607 |
else: |
| 608 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 609 |
rules = list( |
| 610 |
array.get_policies_nfs_client_rules( |
| 611 |
policy_names=[module.params["name"]], |
| 612 |
context_names=[module.params["context"]], |
| 613 |
).items |
| 614 |
) |
| 615 |
else: |
| 616 |
rules = list( |
| 617 |
array.get_policies_nfs_client_rules( |
| 618 |
policy_names=[module.params["name"]] |
| 619 |
).items |
| 620 |
) |
| 621 |
if rules: |
| 622 |
rule_name = "" |
| 623 |
for rule in rules: |
| 624 |
if rule.client == module.params["client"]: |
| 625 |
rule_name = rule.name |
| 626 |
break |
| 627 |
if rule_name: |
| 628 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 629 |
deleted = bool( |
| 630 |
array.delete_policies_nfs_client_rules( |
| 631 |
policy_names=[module.params["name"]], |
| 632 |
names=[rule_name], |
| 633 |
context_names=[module.params["context"]], |
| 634 |
).status_code |
| 635 |
== 200 |
| 636 |
) |
| 637 |
else: |
| 638 |
deleted = bool( |
| 639 |
array.delete_policies_nfs_client_rules( |
| 640 |
policy_names=[module.params["name"]], |
| 641 |
names=[rule_name], |
| 642 |
).status_code |
| 643 |
== 200 |
| 644 |
) |
| 645 |
if deleted: |
| 646 |
changed = True |
| 647 |
else: |
| 648 |
module.fail_json( |
| 649 |
msg="Failed to delete client {0} from NFS policy {1}. Error: {2}".format( |
| 650 |
module.params["client"], |
| 651 |
module.params["name"], |
| 652 |
deleted.errors[0].message, |
| 653 |
) |
| 654 |
) |
| 655 |
elif module.params["policy"] == "smb": |
| 656 |
if not module.params["client"]: |
| 657 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 658 |
res = array.delete_policies_smb( |
| 659 |
names=[module.params["name"]], |
| 660 |
context_names=[module.params["context"]], |
| 661 |
) |
| 662 |
else: |
| 663 |
res = array.delete_policies_smb(names=[module.params["name"]]) |
| 664 |
if res.status_code == 200: |
| 665 |
changed = True |
| 666 |
else: |
| 667 |
module.fail_json( |
| 668 |
msg="Deletion of SMB policy {0} failed. Error: {1}".format( |
| 669 |
module.params["name"], res.errors[0].message |
| 670 |
) |
| 671 |
) |
| 672 |
else: |
| 673 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 674 |
rules = list( |
| 675 |
array.get_policies_smb_client_rules( |
| 676 |
policy_names=[module.params["name"]], |
| 677 |
context_names=[module.params["context"]], |
| 678 |
).items |
| 679 |
) |
| 680 |
else: |
| 681 |
rules = list( |
| 682 |
array.get_policies_smb_client_rules( |
| 683 |
policy_names=[module.params["name"]] |
| 684 |
).items |
| 685 |
) |
| 686 |
if rules: |
| 687 |
rule_name = "" |
| 688 |
for rule in rules: |
| 689 |
if rule.client == module.params["client"]: |
| 690 |
rule_name = rule.name |
| 691 |
break |
| 692 |
if rule_name: |
| 693 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 694 |
deleted = bool( |
| 695 |
array.delete_policies_smb_client_rules( |
| 696 |
policy_names=[module.params["name"]], |
| 697 |
names=[rule_name], |
| 698 |
context_names=[module.params["context"]], |
| 699 |
).status_code |
| 700 |
== 200 |
| 701 |
) |
| 702 |
else: |
| 703 |
deleted = bool( |
| 704 |
array.delete_policies_smb_client_rules( |
| 705 |
policy_names=[module.params["name"]], |
| 706 |
names=[rule_name], |
| 707 |
).status_code |
| 708 |
== 200 |
| 709 |
) |
| 710 |
if deleted: |
| 711 |
changed = True |
| 712 |
else: |
| 713 |
module.fail_json( |
| 714 |
msg="Failed to delete client {0} from SMB policy {1}. Error: {2}".format( |
| 715 |
module.params["client"], |
| 716 |
module.params["name"], |
| 717 |
deleted.errors[0].message, |
| 718 |
) |
| 719 |
) |
| 720 |
elif module.params["policy"] == "snapshot": |
| 721 |
if not module.params["snap_client_name"] and not module.params["directory"]: |
| 722 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 723 |
res = array.delete_policies_snapshot( |
| 724 |
names=[module.params["name"]], |
| 725 |
context_names=[module.params["context"]], |
| 726 |
) |
| 727 |
else: |
| 728 |
res = array.delete_policies_snapshot(names=[module.params["name"]]) |
| 729 |
if res.status_code == 200: |
| 730 |
changed = True |
| 731 |
else: |
| 732 |
module.fail_json( |
| 733 |
msg="Deletion of Snapshot policy {0} failed. Error: {1}".format( |
| 734 |
module.params["name"], res.errors[0].message |
| 735 |
) |
| 736 |
) |
| 737 |
if module.params["directory"]: |
| 738 |
dirs = [] |
| 739 |
old_dirs = [] |
| 740 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 741 |
current_dirs = list( |
| 742 |
array.get_directories_policies_snapshot( |
| 743 |
policy_names=[module.params["name"]], |
| 744 |
context_names=[module.params["context"]], |
| 745 |
).items |
| 746 |
) |
| 747 |
else: |
| 748 |
current_dirs = list( |
| 749 |
array.get_directories_policies_snapshot( |
| 750 |
policy_names=[module.params["name"]] |
| 751 |
).items |
| 752 |
) |
| 753 |
if current_dirs: |
| 754 |
for current_dir in current_dirs: |
| 755 |
dirs.append(current_dir.member.name) |
| 756 |
for old_dir in module.params["directory"]: |
| 757 |
if old_dir in dirs: |
| 758 |
old_dirs.append(old_dir) |
| 759 |
else: |
| 760 |
pass |
| 761 |
if old_dirs: |
| 762 |
changed = True |
| 763 |
for rem_dir in old_dirs: |
| 764 |
if not module.check_mode: |
| 765 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion( |
| 766 |
api_version |
| 767 |
): |
| 768 |
directory_removed = ( |
| 769 |
array.delete_directories_policies_snapshot( |
| 770 |
member_names=[rem_dir], |
| 771 |
policy_names=module.params["name"], |
| 772 |
context_names=[module.params["context"]], |
| 773 |
) |
| 774 |
) |
| 775 |
else: |
| 776 |
directory_removed = ( |
| 777 |
array.delete_directories_policies_snapshot( |
| 778 |
member_names=[rem_dir], |
| 779 |
policy_names=module.params["name"], |
| 780 |
) |
| 781 |
) |
| 782 |
if directory_removed.status_code != 200: |
| 783 |
module.fail_json( |
| 784 |
msg="Failed to remove directory from Snapshot policy {0}. Error: {1}".format( |
| 785 |
module.params["name"], |
| 786 |
directory_removed.errors[0].message, |
| 787 |
) |
| 788 |
) |
| 789 |
if module.params["snap_client_name"]: |
| 790 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 791 |
rules = list( |
| 792 |
array.get_policies_snapshot_rules( |
| 793 |
policy_names=[module.params["name"]], |
| 794 |
context_names=[module.params["context"]], |
| 795 |
).items |
| 796 |
) |
| 797 |
else: |
| 798 |
rules = list( |
| 799 |
array.get_policies_snapshot_rules( |
| 800 |
policy_names=[module.params["name"]] |
| 801 |
).items |
| 802 |
) |
| 803 |
if rules: |
| 804 |
rule_name = "" |
| 805 |
for rule in rules: |
| 806 |
if rule.client_name == module.params["snap_client_name"]: |
| 807 |
rule_name = rule.name |
| 808 |
break |
| 809 |
if rule_name: |
| 810 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 811 |
deleted = bool( |
| 812 |
array.delete_policies_snapshot_rules( |
| 813 |
policy_names=[module.params["name"]], |
| 814 |
names=[rule_name], |
| 815 |
context_names=[module.params["context"]], |
| 816 |
).status_code |
| 817 |
== 200 |
| 818 |
) |
| 819 |
else: |
| 820 |
deleted = bool( |
| 821 |
array.delete_policies_snapshot_rules( |
| 822 |
policy_names=[module.params["name"]], |
| 823 |
names=[rule_name], |
| 824 |
).status_code |
| 825 |
== 200 |
| 826 |
) |
| 827 |
if deleted: |
| 828 |
changed = True |
| 829 |
else: |
| 830 |
module.fail_json( |
| 831 |
msg="Failed to delete client {0} from Snapshot policy {1}. Error: {2}".format( |
| 832 |
module.params["snap_client_name"], |
| 833 |
module.params["name"], |
| 834 |
deleted.errors[0].message, |
| 835 |
) |
| 836 |
) |
| 837 |
elif module.params["policy"] == "autodir": |
| 838 |
if not module.params["directory"]: |
| 839 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 840 |
res = array.delete_policies_autodir( |
| 841 |
names=[module.params["name"]], |
| 842 |
context_names=[module.params["context"]], |
| 843 |
) |
| 844 |
else: |
| 845 |
res = array.delete_policies_autodir(names=[module.params["name"]]) |
| 846 |
if res.status_code == 200: |
| 847 |
changed = True |
| 848 |
else: |
| 849 |
module.fail_json( |
| 850 |
msg="Deletion of Autodir policy {0} failed. Error: {1}".format( |
| 851 |
module.params["name"], res.errors[0].message |
| 852 |
) |
| 853 |
) |
| 854 |
if module.params["directory"]: |
| 855 |
dirs = [] |
| 856 |
old_dirs = [] |
| 857 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 858 |
current_dirs = list( |
| 859 |
array.get_directories_policies_autodir( |
| 860 |
policy_names=[module.params["name"]], |
| 861 |
context_names=[module.params["context"]], |
| 862 |
).items |
| 863 |
) |
| 864 |
else: |
| 865 |
current_dirs = list( |
| 866 |
array.get_directories_policies_autodir( |
| 867 |
policy_names=[module.params["name"]] |
| 868 |
).items |
| 869 |
) |
| 870 |
if current_dirs: |
| 871 |
for current_dir in current_dirs: |
| 872 |
dirs.append(current_dir.member.name) |
| 873 |
for old_dir in module.params["directory"]: |
| 874 |
if old_dir in dirs: |
| 875 |
old_dirs.append(old_dir) |
| 876 |
else: |
| 877 |
pass |
| 878 |
if old_dirs: |
| 879 |
changed = True |
| 880 |
for rem_dir in old_dirs: |
| 881 |
if not module.check_mode: |
| 882 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion( |
| 883 |
api_version |
| 884 |
): |
| 885 |
directory_removed = ( |
| 886 |
array.delete_directories_policies_autodir( |
| 887 |
member_names=[rem_dir], |
| 888 |
policy_names=module.params["name"], |
| 889 |
context_names=[module.params["context"]], |
| 890 |
) |
| 891 |
) |
| 892 |
else: |
| 893 |
directory_removed = ( |
| 894 |
array.delete_directories_policies_autodir( |
| 895 |
member_names=[rem_dir], |
| 896 |
policy_names=module.params["name"], |
| 897 |
) |
| 898 |
) |
| 899 |
if directory_removed.status_code != 200: |
| 900 |
module.fail_json( |
| 901 |
msg="Failed to remove directory from Autodir policy {0}. Error: {1}".format( |
| 902 |
module.params["name"], |
| 903 |
directory_removed.errors[0].message, |
| 904 |
) |
| 905 |
) |
| 906 |
else: |
| 907 |
if module.params["quota_limit"]: |
| 908 |
quota_limit = human_to_bytes(module.params["quota_limit"]) |
| 909 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 910 |
rules = list( |
| 911 |
array.get_policies_quota_rules( |
| 912 |
policy_names=[module.params["name"]], |
| 913 |
context_names=[module.params["context"]], |
| 914 |
).items |
| 915 |
) |
| 916 |
else: |
| 917 |
rules = list( |
| 918 |
array.get_policies_quota_rules( |
| 919 |
policy_names=[module.params["name"]] |
| 920 |
).items |
| 921 |
) |
| 922 |
if rules: |
| 923 |
for rule in rules: |
| 924 |
if rule.quota_limit == quota_limit: |
| 925 |
if ( |
| 926 |
module.params["quota_enforced"] == rule.enforced |
| 927 |
and ",".join(module.params["quota_notifications"]) |
| 928 |
== rules[rule].notifications |
| 929 |
): |
| 930 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion( |
| 931 |
api_version |
| 932 |
): |
| 933 |
res = array.delete_policies_quota_rules( |
| 934 |
policy_names=[module.params["name"]], |
| 935 |
names=[rule.name], |
| 936 |
context_names=[module.params["context"]], |
| 937 |
) |
| 938 |
else: |
| 939 |
res = array.delete_policies_quota_rules( |
| 940 |
policy_names=[module.params["name"]], |
| 941 |
names=[rule.name], |
| 942 |
) |
| 943 |
if res.status_code == 200: |
| 944 |
changed = True |
| 945 |
else: |
| 946 |
module.fail_json( |
| 947 |
msg="Deletion of Quota rule failed. Error: {0}".format( |
| 948 |
res.errors[0].message |
| 949 |
) |
| 950 |
) |
| 951 |
if module.params["directory"]: |
| 952 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 953 |
members = list( |
| 954 |
array.get_policies_quota_members( |
| 955 |
policy_names=[module.params["name"]], |
| 956 |
context_names=[module.params["context"]], |
| 957 |
).items |
| 958 |
) |
| 959 |
else: |
| 960 |
members = list( |
| 961 |
array.get_policies_quota_members( |
| 962 |
policy_names=[module.params["name"]] |
| 963 |
).items |
| 964 |
) |
| 965 |
if members: |
| 966 |
for member in members: |
| 967 |
if member.member.name in module.params["directory"]: |
| 968 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion( |
| 969 |
api_version |
| 970 |
): |
| 971 |
res = array.delete_policies_quota_members( |
| 972 |
policy_names=[module.params["name"]], |
| 973 |
member_names=[member.member.name], |
| 974 |
member_types="directories", |
| 975 |
context_names=[module.params["context"]], |
| 976 |
) |
| 977 |
else: |
| 978 |
res = array.delete_policies_quota_members( |
| 979 |
policy_names=[module.params["name"]], |
| 980 |
member_names=[member.member.name], |
| 981 |
member_types="directories", |
| 982 |
) |
| 983 |
if res.status_code != 200: |
| 984 |
module.fail_json( |
| 985 |
msg="Deletion of Quota member {0} from policy {1}. Error: {2}".format( |
| 986 |
member.member.name, |
| 987 |
module.params["name"], |
| 988 |
res.errors[0].message, |
| 989 |
) |
| 990 |
) |
| 991 |
else: |
| 992 |
changed = True |
| 993 |
if not module.params["quota_limit"] and not module.params["directory"]: |
| 994 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 995 |
members = list( |
| 996 |
array.get_policies_quota_members( |
| 997 |
policy_names=[module.params["name"]], |
| 998 |
context_names=[module.params["context"]], |
| 999 |
).items |
| 1000 |
) |
| 1001 |
else: |
| 1002 |
members = list( |
| 1003 |
array.get_policies_quota_members( |
| 1004 |
policy_names=[module.params["name"]] |
| 1005 |
).items |
| 1006 |
) |
| 1007 |
if members: |
| 1008 |
member_names = [] |
| 1009 |
for member in members: |
| 1010 |
member_names.append(member.member.name) |
| 1011 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1012 |
res = array.delete_policies_quota_members( |
| 1013 |
policy_names=[module.params["name"]], |
| 1014 |
member_names=member_names, |
| 1015 |
member_types="directories", |
| 1016 |
context_names=[module.params["context"]], |
| 1017 |
) |
| 1018 |
else: |
| 1019 |
res = array.delete_policies_quota_members( |
| 1020 |
policy_names=[module.params["name"]], |
| 1021 |
member_names=member_names, |
| 1022 |
member_types="directories", |
| 1023 |
) |
| 1024 |
if res.status_code != 200: |
| 1025 |
module.fail_json( |
| 1026 |
msg="Deletion of Quota members {0} failed. Error: {1}".format( |
| 1027 |
module.params["name"], res.errors[0].message |
| 1028 |
) |
| 1029 |
) |
| 1030 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1031 |
res = array.delete_policies_quota( |
| 1032 |
names=[module.params["name"]], |
| 1033 |
context_names=[module.params["context"]], |
| 1034 |
) |
| 1035 |
else: |
| 1036 |
res = array.delete_policies_quota(names=[module.params["name"]]) |
| 1037 |
if res.status_code == 200: |
| 1038 |
changed = True |
| 1039 |
else: |
| 1040 |
module.fail_json( |
| 1041 |
msg="Deletion of Quota policy {0} failed. Error: {1}".format( |
| 1042 |
module.params["name"], res.errors[0].message |
| 1043 |
) |
| 1044 |
) |
| 1045 |
module.exit_json(changed=changed) |
| 1046 |
|
| 1047 |
|
| 1048 |
def create_policy(module, array, all_squash): |
| 1049 |
"""Create a file system export""" |
| 1050 |
api_version = array.get_rest_version() |
| 1051 |
changed = True |
| 1052 |
if not module.check_mode: |
| 1053 |
changed = False |
| 1054 |
if module.params["policy"] == "nfs": |
| 1055 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1056 |
res = array.post_policies_nfs( |
| 1057 |
names=[module.params["name"]], |
| 1058 |
policy=PolicyPost(enabled=module.params["enabled"]), |
| 1059 |
context_names=[module.params["context"]], |
| 1060 |
) |
| 1061 |
else: |
| 1062 |
res = array.post_policies_nfs( |
| 1063 |
names=[module.params["name"]], |
| 1064 |
policy=PolicyPost(enabled=module.params["enabled"]), |
| 1065 |
) |
| 1066 |
|
| 1067 |
if res.status_code == 200: |
| 1068 |
changed = True |
| 1069 |
if module.params["client"]: |
| 1070 |
if all_squash: |
| 1071 |
rules = PolicyrulenfsclientpostRules( |
| 1072 |
access=module.params["nfs_access"], |
| 1073 |
anongid=module.params["anongid"], |
| 1074 |
anonuid=module.params["anonuid"], |
| 1075 |
client=module.params["client"], |
| 1076 |
permission=module.params["nfs_permission"], |
| 1077 |
) |
| 1078 |
else: |
| 1079 |
rules = PolicyrulenfsclientpostRules( |
| 1080 |
access=module.params["nfs_access"], |
| 1081 |
client=module.params["client"], |
| 1082 |
permission=module.params["nfs_permission"], |
| 1083 |
) |
| 1084 |
rule = PolicyRuleNfsClientPost(rules=[rules]) |
| 1085 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1086 |
res = array.post_policies_nfs_client_rules( |
| 1087 |
policy_names=[module.params["name"]], |
| 1088 |
rules=rule, |
| 1089 |
context_names=[module.params["context"]], |
| 1090 |
) |
| 1091 |
else: |
| 1092 |
res = array.post_policies_nfs_client_rules( |
| 1093 |
policy_names=[module.params["name"]], rules=rule |
| 1094 |
) |
| 1095 |
if res.status_code != 200: |
| 1096 |
module.fail_json( |
| 1097 |
msg="Failed to create rule for NFS policy {0}. Error: {1}".format( |
| 1098 |
module.params["name"], res.errors[0].message |
| 1099 |
) |
| 1100 |
) |
| 1101 |
policy = PolicyNfsPatch( |
| 1102 |
user_mapping_enabled=module.params["user_mapping"], |
| 1103 |
) |
| 1104 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1105 |
res = array.patch_policies_nfs( |
| 1106 |
names=[module.params["name"]], |
| 1107 |
policy=policy, |
| 1108 |
context_names=[module.params["context"]], |
| 1109 |
) |
| 1110 |
else: |
| 1111 |
res = array.patch_policies_nfs( |
| 1112 |
names=[module.params["name"]], policy=policy |
| 1113 |
) |
| 1114 |
if res.status_code != 200: |
| 1115 |
module.fail_json( |
| 1116 |
msg="Failed to set NFS policy user_mapping {0}. Error: {1}".format( |
| 1117 |
module.params["name"], res.errors[0].message |
| 1118 |
) |
| 1119 |
) |
| 1120 |
if ( |
| 1121 |
LooseVersion(array.get_rest_version()) >= LooseVersion(NFS_VERSION) |
| 1122 |
and module.params["client"] |
| 1123 |
and module.params["nfs_version"] |
| 1124 |
): |
| 1125 |
policy = PolicyNfsPatch( |
| 1126 |
nfs_version=module.params["nfs_version"], |
| 1127 |
) |
| 1128 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1129 |
res = array.patch_policies_nfs( |
| 1130 |
names=[module.params["name"]], |
| 1131 |
policy=policy, |
| 1132 |
context_names=[module.params["context"]], |
| 1133 |
) |
| 1134 |
else: |
| 1135 |
res = array.patch_policies_nfs( |
| 1136 |
names=[module.params["name"]], policy=policy |
| 1137 |
) |
| 1138 |
if res.status_code != 200: |
| 1139 |
module.fail_json( |
| 1140 |
msg="Failed to set NFS policy version {0}. Error: {1}".format( |
| 1141 |
module.params["name"], res.errors[0].message |
| 1142 |
) |
| 1143 |
) |
| 1144 |
if ( |
| 1145 |
LooseVersion(array.get_rest_version()) |
| 1146 |
>= LooseVersion(SECURITY_VERSION) |
| 1147 |
and module.params["security"] |
| 1148 |
): |
| 1149 |
policy = PolicyNfsPatch( |
| 1150 |
security=module.params["security"], |
| 1151 |
) |
| 1152 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1153 |
res = array.patch_policies_nfs( |
| 1154 |
names=[module.params["name"]], |
| 1155 |
policy=policy, |
| 1156 |
context_names=[module.params["context"]], |
| 1157 |
) |
| 1158 |
else: |
| 1159 |
res = array.patch_policies_nfs( |
| 1160 |
names=[module.params["name"]], policy=policy |
| 1161 |
) |
| 1162 |
if res.status_code != 200: |
| 1163 |
module.fail_json( |
| 1164 |
msg="Failed to set NFS policy security {0}. Error: {1}".format( |
| 1165 |
module.params["name"], res.errors[0].message |
| 1166 |
) |
| 1167 |
) |
| 1168 |
else: |
| 1169 |
module.fail_json( |
| 1170 |
msg="Failed to create NFS policy {0}. Error: {1}".format( |
| 1171 |
module.params["name"], res.errors[0].message |
| 1172 |
) |
| 1173 |
) |
| 1174 |
elif module.params["policy"] == "smb": |
| 1175 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1176 |
res = array.post_policies_smb( |
| 1177 |
names=[module.params["name"]], |
| 1178 |
policy=PolicyPost(enabled=module.params["enabled"]), |
| 1179 |
context_names=[module.params["context"]], |
| 1180 |
) |
| 1181 |
else: |
| 1182 |
res = array.post_policies_smb( |
| 1183 |
names=[module.params["name"]], |
| 1184 |
policy=PolicyPost(enabled=module.params["enabled"]), |
| 1185 |
) |
| 1186 |
if res.status_code == 200: |
| 1187 |
if LooseVersion(ABE_VERSION) <= LooseVersion(array.get_rest_version()): |
| 1188 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1189 |
if LooseVersion(CA_VERSION) <= LooseVersion(api_version): |
| 1190 |
res = array.patch_policies_smb( |
| 1191 |
names=[module.params["name"]], |
| 1192 |
context_names=[module.params["context"]], |
| 1193 |
policy=PolicySmbPatch( |
| 1194 |
access_based_enumeration_enabled=module.params[ |
| 1195 |
"access_based_enumeration" |
| 1196 |
], |
| 1197 |
continuous_availability_enabled=module.params[ |
| 1198 |
"continuous_availability" |
| 1199 |
], |
| 1200 |
), |
| 1201 |
) |
| 1202 |
else: |
| 1203 |
res = array.patch_policies_smb( |
| 1204 |
names=[module.params["name"]], |
| 1205 |
context_names=[module.params["context"]], |
| 1206 |
policy=PolicySmbPatch( |
| 1207 |
access_based_enumeration_enabled=module.params[ |
| 1208 |
"access_based_enumeration" |
| 1209 |
] |
| 1210 |
), |
| 1211 |
) |
| 1212 |
else: |
| 1213 |
res = array.patch_policies_smb( |
| 1214 |
names=[module.params["name"]], |
| 1215 |
policy=PolicySmbPatch( |
| 1216 |
access_based_enumeration_enabled=module.params[ |
| 1217 |
"access_based_enumeration" |
| 1218 |
] |
| 1219 |
), |
| 1220 |
) |
| 1221 |
if res.status_code != 200: |
| 1222 |
module.fail_json( |
| 1223 |
msg="Failed to set SMB policy {0}. Error: {1}".format( |
| 1224 |
module.params["name"], res.errors[0].message |
| 1225 |
) |
| 1226 |
) |
| 1227 |
if module.params["client"]: |
| 1228 |
rules = PolicyrulesmbclientpostRules( |
| 1229 |
anonymous_access_allowed=module.params["smb_anon_allowed"], |
| 1230 |
client=module.params["client"], |
| 1231 |
smb_encryption_required=module.params["smb_encrypt"], |
| 1232 |
) |
| 1233 |
rule = PolicyRuleSmbClientPost(rules=[rules]) |
| 1234 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1235 |
res = array.post_policies_smb_client_rules( |
| 1236 |
policy_names=[module.params["name"]], |
| 1237 |
rules=rule, |
| 1238 |
context_names=[module.params["context"]], |
| 1239 |
) |
| 1240 |
else: |
| 1241 |
res = array.post_policies_smb_client_rules( |
| 1242 |
policy_names=[module.params["name"]], rules=rule |
| 1243 |
) |
| 1244 |
if res.status_code != 200: |
| 1245 |
module.fail_json( |
| 1246 |
msg="Failed to create rule for SMB policy {0}. Error: {1}".format( |
| 1247 |
module.params["name"], res.errors[0].message |
| 1248 |
) |
| 1249 |
) |
| 1250 |
changed = True |
| 1251 |
else: |
| 1252 |
module.fail_json( |
| 1253 |
msg="Failed to create SMB policy {0}. Error: {1}".format( |
| 1254 |
module.params["name"], res.errors[0].message |
| 1255 |
) |
| 1256 |
) |
| 1257 |
elif module.params["policy"] == "snapshot": |
| 1258 |
suffix_enabled = bool( |
| 1259 |
LooseVersion(array.get_rest_version()) |
| 1260 |
>= LooseVersion(MIN_SUFFIX_API_VERSION) |
| 1261 |
) |
| 1262 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1263 |
res = array.post_policies_snapshot( |
| 1264 |
names=[module.params["name"]], |
| 1265 |
policy=PolicyPost(enabled=module.params["enabled"]), |
| 1266 |
context_names=[module.params["context"]], |
| 1267 |
) |
| 1268 |
else: |
| 1269 |
res = array.post_policies_snapshot( |
| 1270 |
names=[module.params["name"]], |
| 1271 |
policy=PolicyPost(enabled=module.params["enabled"]), |
| 1272 |
) |
| 1273 |
if res.status_code == 200: |
| 1274 |
changed = True |
| 1275 |
if module.params["snap_client_name"]: |
| 1276 |
if module.params["snap_keep_for"] < module.params["snap_every"]: |
| 1277 |
module.fail_json( |
| 1278 |
msg="Retention period (snap_keep_for) cannot be less than snapshot interval (snap_every)." |
| 1279 |
) |
| 1280 |
if module.params["snap_at"]: |
| 1281 |
if not module.params["snap_every"] % 1440 == 0: |
| 1282 |
module.fail_json( |
| 1283 |
msg="snap_at time can only be set if snap_every is multiple of 1440" |
| 1284 |
) |
| 1285 |
if suffix_enabled: |
| 1286 |
rules = PolicyrulesnapshotpostRules( |
| 1287 |
at=convert_to_millisecs(module.params["snap_at"]), |
| 1288 |
client_name=module.params["snap_client_name"], |
| 1289 |
every=module.params["snap_every"] * 60000, |
| 1290 |
keep_for=module.params["snap_keep_for"] * 60000, |
| 1291 |
suffix=module.params["snap_suffix"], |
| 1292 |
) |
| 1293 |
else: |
| 1294 |
rules = PolicyrulesnapshotpostRules( |
| 1295 |
at=convert_to_millisecs(module.params["snap_at"]), |
| 1296 |
client_name=module.params["snap_client_name"], |
| 1297 |
every=module.params["snap_every"] * 60000, |
| 1298 |
keep_for=module.params["snap_keep_for"] * 60000, |
| 1299 |
) |
| 1300 |
else: |
| 1301 |
if suffix_enabled: |
| 1302 |
rules = PolicyrulesnapshotpostRules( |
| 1303 |
client_name=module.params["snap_client_name"], |
| 1304 |
every=module.params["snap_every"] * 60000, |
| 1305 |
keep_for=module.params["snap_keep_for"] * 60000, |
| 1306 |
suffix=module.params["snap_suffix"], |
| 1307 |
) |
| 1308 |
else: |
| 1309 |
rules = PolicyrulesnapshotpostRules( |
| 1310 |
client_name=module.params["snap_client_name"], |
| 1311 |
every=module.params["snap_every"] * 60000, |
| 1312 |
keep_for=module.params["snap_keep_for"] * 60000, |
| 1313 |
) |
| 1314 |
rule = PolicyRuleSnapshotPost(rules=[rules]) |
| 1315 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1316 |
res = array.post_policies_snapshot_rules( |
| 1317 |
policy_names=[module.params["name"]], |
| 1318 |
rules=rule, |
| 1319 |
context_names=[module.params["context"]], |
| 1320 |
) |
| 1321 |
else: |
| 1322 |
res = array.post_policies_snapshot_rules( |
| 1323 |
policy_names=[module.params["name"]], rules=rule |
| 1324 |
) |
| 1325 |
if res.status_code != 200: |
| 1326 |
module.fail_json( |
| 1327 |
msg="Failed to create rule for Snapshot policy {0}. Error: {1}".format( |
| 1328 |
module.params["name"], res.errors[0].message |
| 1329 |
) |
| 1330 |
) |
| 1331 |
if module.params["directory"]: |
| 1332 |
policies = DirectoryPolicyPost( |
| 1333 |
policies=[ |
| 1334 |
DirectorypolicypostPolicies( |
| 1335 |
policy=Reference(name=module.params["name"]) |
| 1336 |
) |
| 1337 |
] |
| 1338 |
) |
| 1339 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1340 |
res = array.post_directories_policies_snapshot( |
| 1341 |
member_names=module.params["directory"], |
| 1342 |
policies=policies, |
| 1343 |
context_names=[module.params["context"]], |
| 1344 |
) |
| 1345 |
else: |
| 1346 |
res = array.post_directories_policies_snapshot( |
| 1347 |
member_names=module.params["directory"], policies=policies |
| 1348 |
) |
| 1349 |
if res.status_code != 200: |
| 1350 |
module.fail_json( |
| 1351 |
msg="Failed to add directory for Snapshot policy {0}. Error: {1}".format( |
| 1352 |
module.params["name"], |
| 1353 |
res.errors[0].message, |
| 1354 |
) |
| 1355 |
) |
| 1356 |
else: |
| 1357 |
module.fail_json( |
| 1358 |
msg="Failed to create Snapshot policy {0}. Error: {1}".format( |
| 1359 |
module.params["name"], res.errors[0].message |
| 1360 |
) |
| 1361 |
) |
| 1362 |
elif module.params["policy"] == "password": |
| 1363 |
module.fail_json(msg="Password policy creation is not yet supported") |
| 1364 |
elif module.params["policy"] == "autodir": |
| 1365 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1366 |
res = array.post_policies_autodir( |
| 1367 |
names=[module.params["name"]], |
| 1368 |
policy=PolicyPost(enabled=module.params["enabled"]), |
| 1369 |
context_names=[module.params["context"]], |
| 1370 |
) |
| 1371 |
else: |
| 1372 |
res = array.post_policies_autodir( |
| 1373 |
names=[module.params["name"]], |
| 1374 |
policy=PolicyPost(enabled=module.params["enabled"]), |
| 1375 |
) |
| 1376 |
if res.status_code == 200: |
| 1377 |
changed = True |
| 1378 |
if module.params["directory"]: |
| 1379 |
policies = DirectoryPolicyPost( |
| 1380 |
policies=[ |
| 1381 |
DirectorypolicypostPolicies( |
| 1382 |
policy=Reference(name=module.params["name"]) |
| 1383 |
) |
| 1384 |
] |
| 1385 |
) |
| 1386 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1387 |
res = array.post_directories_policies_autodir( |
| 1388 |
member_names=module.params["directory"], |
| 1389 |
policies=policies, |
| 1390 |
context_names=[module.params["context"]], |
| 1391 |
) |
| 1392 |
else: |
| 1393 |
res = array.post_directories_policies_autodir( |
| 1394 |
member_names=module.params["directory"], policies=policies |
| 1395 |
) |
| 1396 |
if res.status_code != 200: |
| 1397 |
module.fail_json( |
| 1398 |
msg="Failed to add directory for Autodir policy {0}. Error: {1}".format( |
| 1399 |
module.params["name"], |
| 1400 |
res.errors[0].message, |
| 1401 |
) |
| 1402 |
) |
| 1403 |
else: |
| 1404 |
module.fail_json( |
| 1405 |
msg="Failed to create Autodir policy {0}. Error: {1}".format( |
| 1406 |
module.params["name"], res.errors[0].message |
| 1407 |
) |
| 1408 |
) |
| 1409 |
else: |
| 1410 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1411 |
res = array.post_policies_quota( |
| 1412 |
names=[module.params["name"]], |
| 1413 |
policy=PolicyPost(enabled=module.params["enabled"]), |
| 1414 |
context_names=[module.params["context"]], |
| 1415 |
) |
| 1416 |
else: |
| 1417 |
res = array.post_policies_quota( |
| 1418 |
names=[module.params["name"]], |
| 1419 |
policy=PolicyPost(enabled=module.params["enabled"]), |
| 1420 |
) |
| 1421 |
if res.status_code == 200: |
| 1422 |
changed = True |
| 1423 |
if module.params["quota_limit"]: |
| 1424 |
quota = human_to_bytes(module.params["quota_limit"]) |
| 1425 |
rules = PolicyrulequotapostRules( |
| 1426 |
enforced=module.params["quota_enforced"], |
| 1427 |
quota_limit=quota, |
| 1428 |
notifications=",".join(module.params["quota_notifications"]), |
| 1429 |
) |
| 1430 |
rule = PolicyRuleQuotaPost(rules=[rules]) |
| 1431 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1432 |
res = array.post_policies_quota_rules( |
| 1433 |
policy_names=[module.params["name"]], |
| 1434 |
rules=rule, |
| 1435 |
ignore_usage=module.params["ignore_usage"], |
| 1436 |
context_names=[module.params["context"]], |
| 1437 |
) |
| 1438 |
else: |
| 1439 |
res = array.post_policies_quota_rules( |
| 1440 |
policy_names=[module.params["name"]], |
| 1441 |
rules=rule, |
| 1442 |
ignore_usage=module.params["ignore_usage"], |
| 1443 |
) |
| 1444 |
if res.status_code != 200: |
| 1445 |
module.fail_json( |
| 1446 |
msg="Failed to create rule for Quota policy {0}. Error: {1}".format( |
| 1447 |
module.params["name"], res.errors[0].message |
| 1448 |
) |
| 1449 |
) |
| 1450 |
if module.params["directory"]: |
| 1451 |
members = [] |
| 1452 |
for mem in module.params["directory"]: |
| 1453 |
members.append( |
| 1454 |
PolicymemberpostMembers( |
| 1455 |
member=ReferenceWithType( |
| 1456 |
name=mem, |
| 1457 |
resource_type="directories", |
| 1458 |
) |
| 1459 |
) |
| 1460 |
) |
| 1461 |
member = PolicyMemberPost(members=members) |
| 1462 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1463 |
res = array.post_policies_quota_members( |
| 1464 |
policy_names=[module.params["name"]], |
| 1465 |
members=member, |
| 1466 |
ignore_usage=module.params["ignore_usage"], |
| 1467 |
context_names=[module.params["context"]], |
| 1468 |
) |
| 1469 |
else: |
| 1470 |
res = array.post_policies_quota_members( |
| 1471 |
policy_names=[module.params["name"]], |
| 1472 |
members=member, |
| 1473 |
ignore_usage=module.params["ignore_usage"], |
| 1474 |
) |
| 1475 |
if res.status_code != 200: |
| 1476 |
module.fail_json( |
| 1477 |
msg="Failed to add members to Quota policy {0}. Error: {1}".format( |
| 1478 |
module.params["name"], |
| 1479 |
res.errors[0].message, |
| 1480 |
) |
| 1481 |
) |
| 1482 |
else: |
| 1483 |
module.fail_json( |
| 1484 |
msg="Failed to create Quota policy {0}. Error: {1}".format( |
| 1485 |
module.params["name"], res.errors[0].message |
| 1486 |
) |
| 1487 |
) |
| 1488 |
module.exit_json(changed=changed) |
| 1489 |
|
| 1490 |
|
| 1491 |
def update_policy(module, array, api_version, all_squash): |
| 1492 |
"""Update an existing policy including add/remove rules""" |
| 1493 |
changed = changed_dir = changed_rule = changed_enable = changed_quota = ( |
| 1494 |
changed_member |
| 1495 |
) = changed_user_map = changed_abe = changed_ca = changed_nfs = changed_rule = False |
| 1496 |
if module.params["policy"] == "nfs": |
| 1497 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1498 |
current_policy = list( |
| 1499 |
array.get_policies_nfs( |
| 1500 |
names=[module.params["name"]], |
| 1501 |
context_names=[module.params["context"]], |
| 1502 |
).items |
| 1503 |
)[0] |
| 1504 |
else: |
| 1505 |
current_policy = list( |
| 1506 |
array.get_policies_nfs(names=[module.params["name"]]).items |
| 1507 |
)[0] |
| 1508 |
current_enabled = current_policy.enabled |
| 1509 |
res = {} |
| 1510 |
if LooseVersion(USER_MAP_VERSION) <= LooseVersion(api_version): |
| 1511 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1512 |
res = array.get_policies_nfs( |
| 1513 |
names=[module.params["name"]], |
| 1514 |
context_names=[module.params["context"]], |
| 1515 |
) |
| 1516 |
else: |
| 1517 |
res = array.get_policies_nfs(names=[module.params["name"]]) |
| 1518 |
if res.status_code != 200: |
| 1519 |
module.fail_json( |
| 1520 |
msg="Incorrect policy type specified for existing policy {0}".format( |
| 1521 |
module.params["name"] |
| 1522 |
) |
| 1523 |
) |
| 1524 |
else: |
| 1525 |
current_user_map = list(res.items)[0].user_mapping_enabled |
| 1526 |
if module.params["nfs_version"] and sorted( |
| 1527 |
module.params["nfs_version"] |
| 1528 |
) != sorted(getattr(current_policy, "nfs_version", [])): |
| 1529 |
changed_nfs = True |
| 1530 |
if not module.check_mode: |
| 1531 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1532 |
res = array.patch_policies_nfs( |
| 1533 |
names=[module.params["name"]], |
| 1534 |
policy=PolicyNfsPatch(nfs_version=module.params["nfs_version"]), |
| 1535 |
context_names=[module.params["context"]], |
| 1536 |
) |
| 1537 |
else: |
| 1538 |
res = array.patch_policies_nfs( |
| 1539 |
names=[module.params["name"]], |
| 1540 |
policy=PolicyNfsPatch(nfs_version=module.params["nfs_version"]), |
| 1541 |
) |
| 1542 |
if res.status_code != 200: |
| 1543 |
module.exit_json( |
| 1544 |
msg="Failed to change NFS version for NFS policy {0}".format( |
| 1545 |
module.params["name"] |
| 1546 |
) |
| 1547 |
) |
| 1548 |
if ( |
| 1549 |
module.params["user_mapping"] |
| 1550 |
and current_user_map != module.params["user_mapping"] |
| 1551 |
): |
| 1552 |
changed_user_map = True |
| 1553 |
if not module.check_mode: |
| 1554 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1555 |
res = array.patch_policies_nfs( |
| 1556 |
names=[module.params["name"]], |
| 1557 |
policy=PolicyNfsPatch( |
| 1558 |
user_mapping_enabled=module.params["user_mapping"] |
| 1559 |
), |
| 1560 |
context_names=[module.params["context"]], |
| 1561 |
) |
| 1562 |
else: |
| 1563 |
res = array.patch_policies_nfs( |
| 1564 |
names=[module.params["name"]], |
| 1565 |
policy=PolicyNfsPatch( |
| 1566 |
user_mapping_enabled=module.params["user_mapping"] |
| 1567 |
), |
| 1568 |
) |
| 1569 |
if res.status_code != 200: |
| 1570 |
module.exit_json( |
| 1571 |
msg="Failed to enable/disable User Mapping for NFS policy {0}".format( |
| 1572 |
module.params["name"] |
| 1573 |
) |
| 1574 |
) |
| 1575 |
if current_enabled != module.params["enabled"]: |
| 1576 |
changed_enable = True |
| 1577 |
if not module.check_mode: |
| 1578 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1579 |
res = array.patch_policies_nfs( |
| 1580 |
names=[module.params["name"]], |
| 1581 |
policy=PolicyPatch(enabled=module.params["enabled"]), |
| 1582 |
context_names=[module.params["context"]], |
| 1583 |
) |
| 1584 |
else: |
| 1585 |
res = array.patch_policies_nfs( |
| 1586 |
names=[module.params["name"]], |
| 1587 |
policy=PolicyPatch(enabled=module.params["enabled"]), |
| 1588 |
) |
| 1589 |
if res.status_code != 200: |
| 1590 |
module.exit_json( |
| 1591 |
msg="Failed to enable/disable NFS policy {0}".format( |
| 1592 |
module.params["name"] |
| 1593 |
) |
| 1594 |
) |
| 1595 |
if module.params["client"]: |
| 1596 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1597 |
rules = list( |
| 1598 |
array.get_policies_nfs_client_rules( |
| 1599 |
policy_names=[module.params["name"]], |
| 1600 |
context_names=[module.params["context"]], |
| 1601 |
).items |
| 1602 |
) |
| 1603 |
else: |
| 1604 |
rules = list( |
| 1605 |
array.get_policies_nfs_client_rules( |
| 1606 |
policy_names=[module.params["name"]] |
| 1607 |
).items |
| 1608 |
) |
| 1609 |
if rules: |
| 1610 |
rule_name = "" |
| 1611 |
for rule in rules: |
| 1612 |
if rule.client == module.params["client"]: |
| 1613 |
rule_name = rule.name |
| 1614 |
break |
| 1615 |
if not rule_name: |
| 1616 |
if LooseVersion(NFS_VERSION) > LooseVersion(api_version): |
| 1617 |
if all_squash: |
| 1618 |
rules = PolicyrulenfsclientpostRules( |
| 1619 |
permission=module.params["nfs_permission"], |
| 1620 |
client=module.params["client"], |
| 1621 |
anongid=module.params["anongid"], |
| 1622 |
anonuid=module.params["anonuid"], |
| 1623 |
access=module.params["nfs_access"], |
| 1624 |
) |
| 1625 |
else: |
| 1626 |
rules = PolicyrulenfsclientpostRules( |
| 1627 |
permission=module.params["nfs_permission"], |
| 1628 |
client=module.params["client"], |
| 1629 |
access=module.params["nfs_access"], |
| 1630 |
nfs_version=module.params["nfs_version"], |
| 1631 |
) |
| 1632 |
elif ( |
| 1633 |
LooseVersion(SECURITY_VERSION) |
| 1634 |
> LooseVersion(api_version) |
| 1635 |
<= LooseVersion(NFS_VERSION) |
| 1636 |
): |
| 1637 |
if all_squash: |
| 1638 |
rules = PolicyrulenfsclientpostRules( |
| 1639 |
permission=module.params["nfs_permission"], |
| 1640 |
client=module.params["client"], |
| 1641 |
anongid=module.params["anongid"], |
| 1642 |
anonuid=module.params["anonuid"], |
| 1643 |
access=module.params["nfs_access"], |
| 1644 |
nfs_version=module.params["nfs_version"], |
| 1645 |
) |
| 1646 |
else: |
| 1647 |
rules = PolicyrulenfsclientpostRules( |
| 1648 |
permission=module.params["nfs_permission"], |
| 1649 |
client=module.params["client"], |
| 1650 |
access=module.params["nfs_access"], |
| 1651 |
) |
| 1652 |
else: |
| 1653 |
if module.params["security"]: |
| 1654 |
if all_squash: |
| 1655 |
rules = PolicyrulenfsclientpostRules( |
| 1656 |
permission=module.params["nfs_permission"], |
| 1657 |
client=module.params["client"], |
| 1658 |
anongid=module.params["anongid"], |
| 1659 |
anonuid=module.params["anonuid"], |
| 1660 |
access=module.params["nfs_access"], |
| 1661 |
nfs_version=module.params["nfs_version"], |
| 1662 |
security=module.params["security"], |
| 1663 |
) |
| 1664 |
else: |
| 1665 |
rules = PolicyrulenfsclientpostRules( |
| 1666 |
permission=module.params["nfs_permission"], |
| 1667 |
client=module.params["client"], |
| 1668 |
access=module.params["nfs_access"], |
| 1669 |
security=module.params["security"], |
| 1670 |
) |
| 1671 |
else: |
| 1672 |
if all_squash: |
| 1673 |
rules = PolicyrulenfsclientpostRules( |
| 1674 |
permission=module.params["nfs_permission"], |
| 1675 |
client=module.params["client"], |
| 1676 |
anongid=module.params["anongid"], |
| 1677 |
anonuid=module.params["anonuid"], |
| 1678 |
access=module.params["nfs_access"], |
| 1679 |
nfs_version=module.params["nfs_version"], |
| 1680 |
) |
| 1681 |
else: |
| 1682 |
rules = PolicyrulenfsclientpostRules( |
| 1683 |
permission=module.params["nfs_permission"], |
| 1684 |
client=module.params["client"], |
| 1685 |
access=module.params["nfs_access"], |
| 1686 |
) |
| 1687 |
rule = PolicyRuleNfsClientPost(rules=[rules]) |
| 1688 |
changed_rule = True |
| 1689 |
if not module.check_mode: |
| 1690 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1691 |
res = array.post_policies_nfs_client_rules( |
| 1692 |
policy_names=[module.params["name"]], |
| 1693 |
rules=rule, |
| 1694 |
context_names=[module.params["context"]], |
| 1695 |
) |
| 1696 |
else: |
| 1697 |
res = array.post_policies_nfs_client_rules( |
| 1698 |
policy_names=[module.params["name"]], rules=rule |
| 1699 |
) |
| 1700 |
if res.status_code != 200: |
| 1701 |
module.fail_json( |
| 1702 |
msg="Failed to create new rule for NFS policy {0}. Error: {1}".format( |
| 1703 |
module.params["name"], |
| 1704 |
res.errors[0].message, |
| 1705 |
) |
| 1706 |
) |
| 1707 |
else: |
| 1708 |
if all_squash: |
| 1709 |
rules = PolicyrulenfsclientpostRules( |
| 1710 |
permission=module.params["nfs_permission"], |
| 1711 |
anongid=module.params["anongid"], |
| 1712 |
anonuid=module.params["anonuid"], |
| 1713 |
client=module.params["client"], |
| 1714 |
access=module.params["nfs_access"], |
| 1715 |
) |
| 1716 |
else: |
| 1717 |
rules = PolicyrulenfsclientpostRules( |
| 1718 |
permission=module.params["nfs_permission"], |
| 1719 |
client=module.params["client"], |
| 1720 |
access=module.params["nfs_access"], |
| 1721 |
) |
| 1722 |
rule = PolicyRuleNfsClientPost(rules=[rules]) |
| 1723 |
changed_rule = True |
| 1724 |
if not module.check_mode: |
| 1725 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1726 |
res = array.post_policies_nfs_client_rules( |
| 1727 |
policy_names=[module.params["name"]], |
| 1728 |
rules=rule, |
| 1729 |
context_names=[module.params["context"]], |
| 1730 |
) |
| 1731 |
else: |
| 1732 |
res = array.post_policies_nfs_client_rules( |
| 1733 |
policy_names=[module.params["name"]], rules=rule |
| 1734 |
) |
| 1735 |
if res.status_code != 200: |
| 1736 |
module.fail_json( |
| 1737 |
msg="Failed to create new rule for SMB policy {0}. Error: {1}".format( |
| 1738 |
module.params["name"], res.errors[0].message |
| 1739 |
) |
| 1740 |
) |
| 1741 |
elif module.params["policy"] == "smb": |
| 1742 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1743 |
res = array.get_policies_smb( |
| 1744 |
names=[module.params["name"]], |
| 1745 |
context_names=[module.params["context"]], |
| 1746 |
) |
| 1747 |
else: |
| 1748 |
res = array.get_policies_smb(names=[module.params["name"]]) |
| 1749 |
if res.status_code == 200: |
| 1750 |
current = list(res.items)[0] |
| 1751 |
current_enabled = current.enabled |
| 1752 |
current_access_based_enumeration = getattr( |
| 1753 |
current, "access_based_enumeration_enabled", False |
| 1754 |
) |
| 1755 |
current_continuous_availability = getattr( |
| 1756 |
current, "continuous_availability_enabled", False |
| 1757 |
) |
| 1758 |
else: |
| 1759 |
module.fail_json( |
| 1760 |
msg="Incorrect policy type specified for existing policy {0}".format( |
| 1761 |
module.params["name"] |
| 1762 |
) |
| 1763 |
) |
| 1764 |
if ( |
| 1765 |
"access_based_enumeration" in module.params |
| 1766 |
and current_access_based_enumeration |
| 1767 |
!= module.params["access_based_enumeration"] |
| 1768 |
): |
| 1769 |
changed_abe = True |
| 1770 |
if not module.check_mode: |
| 1771 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1772 |
res = array.patch_policies_smb( |
| 1773 |
names=[module.params["name"]], |
| 1774 |
policy=PolicySmbPatch( |
| 1775 |
access_based_enumeration_enabled=module.params[ |
| 1776 |
"access_based_enumeration" |
| 1777 |
] |
| 1778 |
), |
| 1779 |
context_names=[module.params["context"]], |
| 1780 |
) |
| 1781 |
else: |
| 1782 |
res = array.patch_policies_smb( |
| 1783 |
names=[module.params["name"]], |
| 1784 |
policy=PolicySmbPatch( |
| 1785 |
access_based_enumeration_enabled=module.params[ |
| 1786 |
"access_based_enumeration" |
| 1787 |
] |
| 1788 |
), |
| 1789 |
) |
| 1790 |
if res.status_code != 200: |
| 1791 |
module.exit_json( |
| 1792 |
msg="Failed to enable/disable Access based enueration for SMB policy {0}".format( |
| 1793 |
module.params["name"] |
| 1794 |
) |
| 1795 |
) |
| 1796 |
if ( |
| 1797 |
LooseVersion(CA_VERSION) <= LooseVersion(api_version) |
| 1798 |
and current_continuous_availability |
| 1799 |
!= module.params["continuous_availability"] |
| 1800 |
): |
| 1801 |
changed_ca = True |
| 1802 |
if not module.check_mode: |
| 1803 |
res = array.patch_policies_smb( |
| 1804 |
names=[module.params["name"]], |
| 1805 |
policy=PolicySmbPatch( |
| 1806 |
continuous_availability_enabled=module.params[ |
| 1807 |
"continuous_availability" |
| 1808 |
] |
| 1809 |
), |
| 1810 |
context_names=[module.params["context"]], |
| 1811 |
) |
| 1812 |
if res.status_code != 200: |
| 1813 |
module.exit_json( |
| 1814 |
msg="Failed to enable/disable Continuous Availability for SMB policy {0}".format( |
| 1815 |
module.params["name"] |
| 1816 |
) |
| 1817 |
) |
| 1818 |
if current_enabled != module.params["enabled"]: |
| 1819 |
changed_enable = True |
| 1820 |
if not module.check_mode: |
| 1821 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1822 |
res = array.patch_policies_smb( |
| 1823 |
names=[module.params["name"]], |
| 1824 |
policy=PolicyPatch(enabled=module.params["enabled"]), |
| 1825 |
context_names=[module.params["context"]], |
| 1826 |
) |
| 1827 |
else: |
| 1828 |
res = array.patch_policies_smb( |
| 1829 |
names=[module.params["name"]], |
| 1830 |
policy=PolicyPatch(enabled=module.params["enabled"]), |
| 1831 |
) |
| 1832 |
if res.status_code != 200: |
| 1833 |
module.exit_json( |
| 1834 |
msg="Failed to enable/disable SMB policy {0}".format( |
| 1835 |
module.params["name"] |
| 1836 |
) |
| 1837 |
) |
| 1838 |
if module.params["client"]: |
| 1839 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1840 |
rules = list( |
| 1841 |
array.get_policies_smb_client_rules( |
| 1842 |
policy_names=[module.params["name"]], |
| 1843 |
context_names=[module.params["context"]], |
| 1844 |
).items |
| 1845 |
) |
| 1846 |
else: |
| 1847 |
rules = list( |
| 1848 |
array.get_policies_smb_client_rules( |
| 1849 |
policy_names=[module.params["name"]] |
| 1850 |
).items |
| 1851 |
) |
| 1852 |
if rules: |
| 1853 |
rule_name = "" |
| 1854 |
for rule in rules: |
| 1855 |
if rule.client == module.params["client"]: |
| 1856 |
rule_name = rule.name |
| 1857 |
break |
| 1858 |
if not rule_name: |
| 1859 |
rules = PolicyrulesmbclientpostRules( |
| 1860 |
anonymous_access_allowed=module.params["smb_anon_allowed"], |
| 1861 |
client=module.params["client"], |
| 1862 |
smb_encryption_required=module.params["smb_encrypt"], |
| 1863 |
) |
| 1864 |
rule = PolicyRuleSmbClientPost(rules=[rules]) |
| 1865 |
changed_rule = True |
| 1866 |
if not module.check_mode: |
| 1867 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1868 |
res = array.post_policies_smb_client_rules( |
| 1869 |
policy_names=[module.params["name"]], |
| 1870 |
rules=rule, |
| 1871 |
context_names=[module.params["context"]], |
| 1872 |
) |
| 1873 |
else: |
| 1874 |
res = array.post_policies_smb_client_rules( |
| 1875 |
policy_names=[module.params["name"]], rules=rule |
| 1876 |
) |
| 1877 |
if res.status_code != 200: |
| 1878 |
module.fail_json( |
| 1879 |
msg="Failed to create new rule for SMB policy {0}. Error: {1}".format( |
| 1880 |
module.params["name"], |
| 1881 |
res.errors[0].message, |
| 1882 |
) |
| 1883 |
) |
| 1884 |
else: |
| 1885 |
rules = PolicyrulesmbclientpostRules( |
| 1886 |
anonymous_access_allowed=module.params["smb_anon_allowed"], |
| 1887 |
client=module.params["client"], |
| 1888 |
smb_encryption_required=module.params["smb_encrypt"], |
| 1889 |
) |
| 1890 |
rule = PolicyRuleSmbClientPost(rules=[rules]) |
| 1891 |
changed_rule = True |
| 1892 |
if not module.check_mode: |
| 1893 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1894 |
res = array.post_policies_smb_client_rules( |
| 1895 |
policy_names=[module.params["name"]], |
| 1896 |
rules=rule, |
| 1897 |
context_names=[module.params["context"]], |
| 1898 |
) |
| 1899 |
else: |
| 1900 |
res = array.post_policies_smb_client_rules( |
| 1901 |
policy_names=[module.params["name"]], rules=rule |
| 1902 |
) |
| 1903 |
if res.status_code != 200: |
| 1904 |
module.fail_json( |
| 1905 |
msg="Failed to create new rule for SMB policy {0}. Error: {1}".format( |
| 1906 |
module.params["name"], res.errors[0].message |
| 1907 |
) |
| 1908 |
) |
| 1909 |
elif module.params["policy"] == "snapshot": |
| 1910 |
suffix_enabled = bool( |
| 1911 |
LooseVersion(api_version) >= LooseVersion(MIN_SUFFIX_API_VERSION) |
| 1912 |
) |
| 1913 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1914 |
res = array.get_policies_snapshot( |
| 1915 |
names=[module.params["name"]], |
| 1916 |
context_names=[module.params["context"]], |
| 1917 |
) |
| 1918 |
else: |
| 1919 |
res = array.get_policies_snapshot(names=[module.params["name"]]) |
| 1920 |
if res.status_code == 200: |
| 1921 |
current_enabled = list(res.items)[0].enabled |
| 1922 |
else: |
| 1923 |
module.fail_json( |
| 1924 |
msg="Incorrect policy type specified for existing policy {0}".format( |
| 1925 |
module.params["name"] |
| 1926 |
) |
| 1927 |
) |
| 1928 |
if current_enabled != module.params["enabled"]: |
| 1929 |
changed_enable = True |
| 1930 |
if not module.check_mode: |
| 1931 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1932 |
res = array.patch_policies_snapshot( |
| 1933 |
names=[module.params["name"]], |
| 1934 |
policy=PolicyPatch(enabled=module.params["enabled"]), |
| 1935 |
context_names=[module.params["context"]], |
| 1936 |
) |
| 1937 |
else: |
| 1938 |
res = array.patch_policies_snapshot( |
| 1939 |
names=[module.params["name"]], |
| 1940 |
policy=PolicyPatch(enabled=module.params["enabled"]), |
| 1941 |
) |
| 1942 |
if res.status_code != 200: |
| 1943 |
module.exit_json( |
| 1944 |
msg="Failed to enable/disable snapshot policy {0}".format( |
| 1945 |
module.params["name"] |
| 1946 |
) |
| 1947 |
) |
| 1948 |
if module.params["directory"]: |
| 1949 |
dirs = [] |
| 1950 |
new_dirs = [] |
| 1951 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1952 |
current_dirs = list( |
| 1953 |
array.get_directories_policies_snapshot( |
| 1954 |
policy_names=[module.params["name"]], |
| 1955 |
context_names=[module.params["context"]], |
| 1956 |
).items |
| 1957 |
) |
| 1958 |
else: |
| 1959 |
current_dirs = list( |
| 1960 |
array.get_directories_policies_snapshot( |
| 1961 |
policy_names=[module.params["name"]] |
| 1962 |
).items |
| 1963 |
) |
| 1964 |
if current_dirs: |
| 1965 |
for current_dir in current_dirs: |
| 1966 |
dirs.append(current_dir.member.name) |
| 1967 |
for new_dir in module.params["directory"]: |
| 1968 |
if new_dir not in dirs: |
| 1969 |
changed_dir = True |
| 1970 |
new_dirs.append(new_dir) |
| 1971 |
else: |
| 1972 |
new_dirs = module.params["directory"] |
| 1973 |
if new_dirs: |
| 1974 |
policies = DirectoryPolicyPost( |
| 1975 |
policies=[ |
| 1976 |
DirectorypolicypostPolicies( |
| 1977 |
policy=Reference(name=module.params["name"]) |
| 1978 |
) |
| 1979 |
] |
| 1980 |
) |
| 1981 |
changed_dir = True |
| 1982 |
for add_dir in new_dirs: |
| 1983 |
if not module.check_mode: |
| 1984 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 1985 |
res = array.post_directories_policies_snapshot( |
| 1986 |
member_names=[add_dir], |
| 1987 |
policies=policies, |
| 1988 |
context_names=[module.params["context"]], |
| 1989 |
) |
| 1990 |
else: |
| 1991 |
res = array.post_directories_policies_snapshot( |
| 1992 |
member_names=[add_dir], policies=policies |
| 1993 |
) |
| 1994 |
if res.status_code != 200: |
| 1995 |
module.fail_json( |
| 1996 |
msg="Failed to add new directory to Snapshot policy {0}. Error: {1}".format( |
| 1997 |
module.params["name"], |
| 1998 |
res.errors[0].message, |
| 1999 |
) |
| 2000 |
) |
| 2001 |
if module.params["snap_client_name"]: |
| 2002 |
if module.params["snap_at"]: |
| 2003 |
if not module.params["snap_every"] % 1440 == 0: |
| 2004 |
module.fail_json( |
| 2005 |
msg="snap_at time can only be set if snap_every is multiple of 1440" |
| 2006 |
) |
| 2007 |
if module.params["snap_keep_for"] < module.params["snap_every"]: |
| 2008 |
module.fail_json( |
| 2009 |
msg="Retention period (snap_keep_for) cannot be less than snapshot interval (snap_every)." |
| 2010 |
) |
| 2011 |
if ( |
| 2012 |
module.params["snap_keep_for"] != module.params["snap_every"] |
| 2013 |
and module.params["snap_suffix"] |
| 2014 |
): |
| 2015 |
module.fail_json( |
| 2016 |
msg="Suffix (snap_suufix) can only be applied when `snap_keep_for` and `snap_every` are equal." |
| 2017 |
) |
| 2018 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2019 |
rules = list( |
| 2020 |
array.get_policies_snapshot_rules( |
| 2021 |
policy_names=[module.params["name"]], |
| 2022 |
context_names=[module.params["context"]], |
| 2023 |
).items |
| 2024 |
) |
| 2025 |
else: |
| 2026 |
rules = list( |
| 2027 |
array.get_policies_snapshot_rules( |
| 2028 |
policy_names=[module.params["name"]] |
| 2029 |
).items |
| 2030 |
) |
| 2031 |
if rules: |
| 2032 |
for rule in rules: |
| 2033 |
if ( |
| 2034 |
rule.client_name == module.params["snap_client_name"] |
| 2035 |
and int(rule.every / 60000) == module.params["snap_every"] |
| 2036 |
and int(rule.keep_for / 60000) == module.params["snap_keep_for"] |
| 2037 |
): |
| 2038 |
module.exit_json(changed=False) |
| 2039 |
if module.params["snap_keep_for"] < module.params["snap_every"]: |
| 2040 |
module.fail_json( |
| 2041 |
msg="Retention period (snap_keep_for) cannot be less than snapshot interval (snap_every)." |
| 2042 |
) |
| 2043 |
if module.params["snap_at"]: |
| 2044 |
if not module.params["snap_every"] % 1440 == 0: |
| 2045 |
module.fail_json( |
| 2046 |
msg="snap_at time can only be set if snap_every is multiple of 1440" |
| 2047 |
) |
| 2048 |
if suffix_enabled: |
| 2049 |
rules = PolicyrulesnapshotpostRules( |
| 2050 |
at=convert_to_millisecs(module.params["snap_at"]), |
| 2051 |
client_name=module.params["snap_client_name"], |
| 2052 |
every=module.params["snap_every"] * 60000, |
| 2053 |
keep_for=module.params["snap_keep_for"] * 60000, |
| 2054 |
suffix=module.params["snap_suffix"], |
| 2055 |
) |
| 2056 |
else: |
| 2057 |
rules = PolicyrulesnapshotpostRules( |
| 2058 |
at=convert_to_millisecs(module.params["snap_at"]), |
| 2059 |
client_name=module.params["snap_client_name"], |
| 2060 |
every=module.params["snap_every"] * 60000, |
| 2061 |
keep_for=module.params["snap_keep_for"] * 60000, |
| 2062 |
) |
| 2063 |
else: |
| 2064 |
if suffix_enabled: |
| 2065 |
rules = PolicyrulesnapshotpostRules( |
| 2066 |
client_name=module.params["snap_client_name"], |
| 2067 |
every=module.params["snap_every"] * 60000, |
| 2068 |
keep_for=module.params["snap_keep_for"] * 60000, |
| 2069 |
suffix=module.params["snap_suffix"], |
| 2070 |
) |
| 2071 |
else: |
| 2072 |
rules = PolicyrulesnapshotpostRules( |
| 2073 |
client_name=module.params["snap_client_name"], |
| 2074 |
every=module.params["snap_every"] * 60000, |
| 2075 |
keep_for=module.params["snap_keep_for"] * 60000, |
| 2076 |
) |
| 2077 |
rule = PolicyRuleSnapshotPost(rules=[rules]) |
| 2078 |
changed_rule = True |
| 2079 |
if not module.check_mode: |
| 2080 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2081 |
res = array.post_policies_snapshot_rules( |
| 2082 |
policy_names=[module.params["name"]], |
| 2083 |
rules=rule, |
| 2084 |
context_names=[module.params["context"]], |
| 2085 |
) |
| 2086 |
else: |
| 2087 |
res = array.post_policies_snapshot_rules( |
| 2088 |
policy_names=[module.params["name"]], rules=rule |
| 2089 |
) |
| 2090 |
if res.status_code != 200: |
| 2091 |
err_no = len(res.errors) - 1 |
| 2092 |
module.fail_json( |
| 2093 |
msg="Failed to create new rule for Snapshot policy {0}. Error: {1}".format( |
| 2094 |
module.params["name"], |
| 2095 |
res.errors[err_no].message, |
| 2096 |
) |
| 2097 |
) |
| 2098 |
else: |
| 2099 |
if module.params["snap_keep_for"] < module.params["snap_every"]: |
| 2100 |
module.fail_json( |
| 2101 |
msg="Retention period (snap_keep_for) cannot be less than snapshot interval (snap_every)." |
| 2102 |
) |
| 2103 |
if module.params["snap_at"]: |
| 2104 |
if not module.params["snap_every"] % 1440 == 0: |
| 2105 |
module.fail_json( |
| 2106 |
msg="snap_at time can only be set if snap_every is multiple of 1440" |
| 2107 |
) |
| 2108 |
if suffix_enabled: |
| 2109 |
rules = PolicyrulesnapshotpostRules( |
| 2110 |
at=convert_to_millisecs(module.params["snap_at"]), |
| 2111 |
client_name=module.params["snap_client_name"], |
| 2112 |
every=module.params["snap_every"] * 60000, |
| 2113 |
keep_for=module.params["snap_keep_for"] * 60000, |
| 2114 |
suffix=module.params["snap_suffix"], |
| 2115 |
) |
| 2116 |
else: |
| 2117 |
rules = PolicyrulesnapshotpostRules( |
| 2118 |
at=convert_to_millisecs(module.params["snap_at"]), |
| 2119 |
client_name=module.params["snap_client_name"], |
| 2120 |
every=module.params["snap_every"] * 60000, |
| 2121 |
keep_for=module.params["snap_keep_for"] * 60000, |
| 2122 |
) |
| 2123 |
else: |
| 2124 |
if suffix_enabled: |
| 2125 |
rules = PolicyrulesnapshotpostRules( |
| 2126 |
client_name=module.params["snap_client_name"], |
| 2127 |
every=module.params["snap_every"] * 60000, |
| 2128 |
keep_for=module.params["snap_keep_for"] * 60000, |
| 2129 |
suffix=module.params["snap_suffix"], |
| 2130 |
) |
| 2131 |
else: |
| 2132 |
rules = PolicyrulesnapshotpostRules( |
| 2133 |
client_name=module.params["snap_client_name"], |
| 2134 |
every=module.params["snap_every"] * 60000, |
| 2135 |
keep_for=module.params["snap_keep_for"] * 60000, |
| 2136 |
) |
| 2137 |
rule = PolicyRuleSnapshotPost(rules=[rules]) |
| 2138 |
changed_rule = True |
| 2139 |
if not module.check_mode: |
| 2140 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2141 |
res = array.post_policies_snapshot_rules( |
| 2142 |
policy_names=[module.params["name"]], |
| 2143 |
rules=rule, |
| 2144 |
context_names=[module.params["context"]], |
| 2145 |
) |
| 2146 |
else: |
| 2147 |
res = array.post_policies_snapshot_rules( |
| 2148 |
policy_names=[module.params["name"]], rules=rule |
| 2149 |
) |
| 2150 |
if res.status_code != 200: |
| 2151 |
err_no = len(res.errors) - 1 |
| 2152 |
module.fail_json( |
| 2153 |
msg="Failed to create new rule for Snapshot policy {0}. Error: {1}".format( |
| 2154 |
module.params["name"], |
| 2155 |
res.errors[err_no].message, |
| 2156 |
) |
| 2157 |
) |
| 2158 |
elif module.params["policy"] == "autodir": |
| 2159 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2160 |
res = array.get_policies_autodir( |
| 2161 |
names=[module.params["name"]], |
| 2162 |
context_names=[module.params["context"]], |
| 2163 |
) |
| 2164 |
else: |
| 2165 |
res = array.get_policies_autodir(names=[module.params["name"]]) |
| 2166 |
if res.status_code == 200: |
| 2167 |
current_enabled = list(res.items)[0].enabled |
| 2168 |
else: |
| 2169 |
module.fail_json( |
| 2170 |
msg="Incorrect policy type specified for existing policy {0}".format( |
| 2171 |
module.params["name"] |
| 2172 |
) |
| 2173 |
) |
| 2174 |
if current_enabled != module.params["enabled"]: |
| 2175 |
changed_enable = True |
| 2176 |
if not module.check_mode: |
| 2177 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2178 |
res = array.patch_policies_autodir( |
| 2179 |
names=[module.params["name"]], |
| 2180 |
policy=PolicyPatch(enabled=module.params["enabled"]), |
| 2181 |
context_names=[module.params["context"]], |
| 2182 |
) |
| 2183 |
else: |
| 2184 |
res = array.patch_policies_autodir( |
| 2185 |
names=[module.params["name"]], |
| 2186 |
policy=PolicyPatch(enabled=module.params["enabled"]), |
| 2187 |
) |
| 2188 |
if res.status_code != 200: |
| 2189 |
module.exit_json( |
| 2190 |
msg="Failed to enable/disable autodir policy {0}".format( |
| 2191 |
module.params["name"] |
| 2192 |
) |
| 2193 |
) |
| 2194 |
if module.params["directory"]: |
| 2195 |
dirs = [] |
| 2196 |
new_dirs = [] |
| 2197 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2198 |
current_dirs = list( |
| 2199 |
array.get_directories_policies_autodir( |
| 2200 |
policy_names=[module.params["name"]], |
| 2201 |
context_names=[module.params["context"]], |
| 2202 |
).items |
| 2203 |
) |
| 2204 |
else: |
| 2205 |
current_dirs = list( |
| 2206 |
array.get_directories_policies_autodir( |
| 2207 |
policy_names=[module.params["name"]] |
| 2208 |
).items |
| 2209 |
) |
| 2210 |
if current_dirs: |
| 2211 |
for current_dir in current_dirs: |
| 2212 |
dirs.append(current_dir.member.name) |
| 2213 |
for new_dir in module.params["directory"]: |
| 2214 |
if new_dir not in dirs: |
| 2215 |
changed_dir = True |
| 2216 |
new_dirs.append(new_dir) |
| 2217 |
else: |
| 2218 |
new_dirs = module.params["directory"] |
| 2219 |
if new_dirs: |
| 2220 |
policies = DirectoryPolicyPost( |
| 2221 |
policies=[ |
| 2222 |
DirectorypolicypostPolicies( |
| 2223 |
policy=Reference(name=module.params["name"]) |
| 2224 |
) |
| 2225 |
] |
| 2226 |
) |
| 2227 |
changed_dir = True |
| 2228 |
for add_dir in new_dirs: |
| 2229 |
if not module.check_mode: |
| 2230 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2231 |
res = array.post_directories_policies_autodir( |
| 2232 |
member_names=[add_dir], |
| 2233 |
policies=policies, |
| 2234 |
context_names=[module.params["context"]], |
| 2235 |
) |
| 2236 |
else: |
| 2237 |
res = array.post_directories_policies_autodir( |
| 2238 |
member_names=[add_dir], policies=policies |
| 2239 |
) |
| 2240 |
if res.status_code != 200: |
| 2241 |
module.fail_json( |
| 2242 |
msg="Failed to add new directory to Autodir policy {0}. Error: {1}".format( |
| 2243 |
module.params["name"], |
| 2244 |
res.errors[0].message, |
| 2245 |
) |
| 2246 |
) |
| 2247 |
elif module.params["policy"] == "password": |
| 2248 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2249 |
res = array.get_policies_password( |
| 2250 |
names=[module.params["name"]], |
| 2251 |
context_names=[module.params["context"]], |
| 2252 |
) |
| 2253 |
else: |
| 2254 |
res = array.get_policies_password(names=[module.params["name"]]) |
| 2255 |
if res.status_code == 200: |
| 2256 |
current_enabled = list(res.items)[0].enabled |
| 2257 |
else: |
| 2258 |
module.fail_json( |
| 2259 |
msg="Incorrect policy type specified for existing policy {0}".format( |
| 2260 |
module.params["name"] |
| 2261 |
) |
| 2262 |
) |
| 2263 |
if current_enabled != module.params["enabled"]: |
| 2264 |
changed_enable = True |
| 2265 |
if not module.check_mode: |
| 2266 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2267 |
res = array.patch_policies_password( |
| 2268 |
names=[module.params["name"]], |
| 2269 |
policy=PolicyPatch(enabled=module.params["enabled"]), |
| 2270 |
context_names=[module.params["context"]], |
| 2271 |
) |
| 2272 |
else: |
| 2273 |
res = array.patch_policies_password( |
| 2274 |
names=[module.params["name"]], |
| 2275 |
policy=PolicyPatch(enabled=module.params["enabled"]), |
| 2276 |
) |
| 2277 |
if res.status_code != 200: |
| 2278 |
module.exit_json( |
| 2279 |
msg="Failed to enable/disable password policy {0}".format( |
| 2280 |
module.params["name"] |
| 2281 |
) |
| 2282 |
) |
| 2283 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2284 |
pwd_policy = list( |
| 2285 |
array.get_policies_password( |
| 2286 |
names=[module.params["name"]], |
| 2287 |
context_names=[module.params["context"]], |
| 2288 |
).items |
| 2289 |
)[0] |
| 2290 |
else: |
| 2291 |
pwd_policy = list( |
| 2292 |
array.get_policies_password(names=[module.params["name"]]).items |
| 2293 |
)[0] |
| 2294 |
current_pwd_policy = { |
| 2295 |
"enforce_dictionary_check": pwd_policy.enforce_dictionary_check, |
| 2296 |
"enforce_username_check": pwd_policy.enforce_username_check, |
| 2297 |
"lockout_duration": pwd_policy.lockout_duration, |
| 2298 |
"min_character_groups": pwd_policy.min_character_groups, |
| 2299 |
"min_characters_per_group": pwd_policy.min_characters_per_group, |
| 2300 |
"min_password_length": pwd_policy.min_password_length, |
| 2301 |
"min_password_age": getattr(pwd_policy, "min_password_age", 0), |
| 2302 |
"max_login_attempts": getattr(pwd_policy, "max_login_attempts", 0), |
| 2303 |
"password_history": getattr(pwd_policy, "password_history", 0), |
| 2304 |
} |
| 2305 |
new_pwd_policy = current_pwd_policy.copy() |
| 2306 |
if ( |
| 2307 |
module.params["enforce_dictionary_check"] |
| 2308 |
!= current_pwd_policy["enforce_dictionary_check"] |
| 2309 |
): |
| 2310 |
new_pwd_policy["enforce_dictionary_check"] = module.params[ |
| 2311 |
"enforce_dictionary_check" |
| 2312 |
] |
| 2313 |
changed = True |
| 2314 |
if ( |
| 2315 |
module.params["enforce_username_check"] |
| 2316 |
!= current_pwd_policy["enforce_username_check"] |
| 2317 |
): |
| 2318 |
new_pwd_policy["enforce_username_check"] = module.params[ |
| 2319 |
"enforce_username_check" |
| 2320 |
] |
| 2321 |
changed = True |
| 2322 |
if ( |
| 2323 |
module.params["min_character_groups"] |
| 2324 |
!= current_pwd_policy["min_character_groups"] |
| 2325 |
): |
| 2326 |
new_pwd_policy["min_character_groups"] = module.params[ |
| 2327 |
"min_character_groups" |
| 2328 |
] |
| 2329 |
changed = True |
| 2330 |
if ( |
| 2331 |
module.params["min_characters_per_group"] |
| 2332 |
!= current_pwd_policy["min_characters_per_group"] |
| 2333 |
): |
| 2334 |
new_pwd_policy["min_characters_per_group"] = module.params[ |
| 2335 |
"min_characters_per_group" |
| 2336 |
] |
| 2337 |
changed = True |
| 2338 |
if ( |
| 2339 |
module.params["min_password_length"] |
| 2340 |
!= current_pwd_policy["min_password_length"] |
| 2341 |
): |
| 2342 |
new_pwd_policy["min_password_length"] = module.params["min_password_length"] |
| 2343 |
changed = True |
| 2344 |
if module.params["min_password_age"] != current_pwd_policy["min_password_age"]: |
| 2345 |
new_pwd_policy["min_password_age"] = module.params["min_password_age"] |
| 2346 |
changed = True |
| 2347 |
if ( |
| 2348 |
module.params["password_history"] |
| 2349 |
and module.params["password_history"] |
| 2350 |
!= current_pwd_policy["password_history"] |
| 2351 |
): |
| 2352 |
new_pwd_policy["password_history"] = module.params["password_history"] |
| 2353 |
changed = True |
| 2354 |
if ( |
| 2355 |
module.params["max_login_attempts"] |
| 2356 |
and module.params["max_login_attempts"] |
| 2357 |
!= current_pwd_policy["max_login_attempts"] |
| 2358 |
): |
| 2359 |
new_pwd_policy["max_login_attempts"] = module.params["max_login_attempts"] |
| 2360 |
changed = True |
| 2361 |
if module.params["lockout_duration"]: |
| 2362 |
if ( |
| 2363 |
module.params["lockout_duration"] * 1000 |
| 2364 |
!= current_pwd_policy["lockout_duration"] |
| 2365 |
): |
| 2366 |
current_pwd_policy["lockout_duration"] = ( |
| 2367 |
module.params["lockout_duration"] * 1000 |
| 2368 |
) |
| 2369 |
changed = True |
| 2370 |
if changed and not module.check_mode: |
| 2371 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2372 |
res = array.patch_policies_password( |
| 2373 |
names=[module.params["name"]], |
| 2374 |
context_names=[module.params["context"]], |
| 2375 |
policy=PolicyPassword( |
| 2376 |
max_login_attempts=new_pwd_policy.max_login_attempts, |
| 2377 |
min_password_length=new_pwd_policy.min_password_length, |
| 2378 |
password_history=new_pwd_policy.password_history, |
| 2379 |
min_password_age=new_pwd_policy.min_password_age, |
| 2380 |
min_character_groups=new_pwd_policy.min_character_groups, |
| 2381 |
min_characters_per_group=new_pwd_policy.min_characters_per_group, |
| 2382 |
enforce_username_check=new_pwd_policy.enforce_username_check, |
| 2383 |
enforce_dictionary_check=new_pwd_policy.enforce_dictionary_check, |
| 2384 |
lockout_duration=new_pwd_policy.lockout_duration, |
| 2385 |
), |
| 2386 |
) |
| 2387 |
else: |
| 2388 |
res = array.patch_policies_password( |
| 2389 |
names=[module.params["name"]], |
| 2390 |
policy=PolicyPassword( |
| 2391 |
max_login_attempts=new_pwd_policy.max_login_attempts, |
| 2392 |
min_password_length=new_pwd_policy.min_password_length, |
| 2393 |
password_history=new_pwd_policy.password_history, |
| 2394 |
min_password_age=new_pwd_policy.min_password_age, |
| 2395 |
min_character_groups=new_pwd_policy.min_character_groups, |
| 2396 |
min_characters_per_group=new_pwd_policy.min_characters_per_group, |
| 2397 |
enforce_username_check=new_pwd_policy.enforce_username_check, |
| 2398 |
enforce_dictionary_check=new_pwd_policy.enforce_dictionary_check, |
| 2399 |
lockout_duration=new_pwd_policy.lockout_duration, |
| 2400 |
), |
| 2401 |
) |
| 2402 |
if res.status_code != 200: |
| 2403 |
module.fail_json( |
| 2404 |
msg="Failed to change password policy {0}. Error: {1}".format( |
| 2405 |
module.params["name"], |
| 2406 |
res.errors[0].message, |
| 2407 |
) |
| 2408 |
) |
| 2409 |
else: |
| 2410 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2411 |
current_enabled = list( |
| 2412 |
array.get_policies_quota( |
| 2413 |
names=[module.params["name"]], |
| 2414 |
context_names=[module.params["context"]], |
| 2415 |
).items |
| 2416 |
)[0].enabled |
| 2417 |
else: |
| 2418 |
current_enabled = list( |
| 2419 |
array.get_policies_quota(names=[module.params["name"]]).items |
| 2420 |
)[0].enabled |
| 2421 |
if current_enabled != module.params["enabled"]: |
| 2422 |
changed_quota = True |
| 2423 |
if not module.check_mode: |
| 2424 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2425 |
res = array.patch_policies_quota( |
| 2426 |
names=[module.params["name"]], |
| 2427 |
policy=PolicyPatch(enabled=module.params["enabled"]), |
| 2428 |
context_names=[module.params["context"]], |
| 2429 |
) |
| 2430 |
else: |
| 2431 |
res = array.patch_policies_quota( |
| 2432 |
names=[module.params["name"]], |
| 2433 |
policy=PolicyPatch(enabled=module.params["enabled"]), |
| 2434 |
) |
| 2435 |
if res.status_code != 200: |
| 2436 |
module.exit_json( |
| 2437 |
msg="Failed to enable/disable snapshot policy {0}".format( |
| 2438 |
module.params["name"] |
| 2439 |
) |
| 2440 |
) |
| 2441 |
if module.params["directory"]: |
| 2442 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2443 |
current_members = list( |
| 2444 |
array.get_policies_quota_members( |
| 2445 |
policy_names=[module.params["name"]], |
| 2446 |
context_names=[module.params["context"]], |
| 2447 |
).items |
| 2448 |
) |
| 2449 |
else: |
| 2450 |
current_members = list( |
| 2451 |
array.get_policies_quota_members( |
| 2452 |
policy_names=[module.params["name"]] |
| 2453 |
).items |
| 2454 |
) |
| 2455 |
if current_members: |
| 2456 |
if module.params["state"] == "absent": |
| 2457 |
for member in current_members: |
| 2458 |
if member.member.name in module.params["directory"]: |
| 2459 |
changed_member = True |
| 2460 |
if not module.check_mode: |
| 2461 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion( |
| 2462 |
api_version |
| 2463 |
): |
| 2464 |
res = array.delete_policies_quota_members( |
| 2465 |
policy_names=[module.params["name"]], |
| 2466 |
member_names=[member.member.name], |
| 2467 |
context_names=[module.params["context"]], |
| 2468 |
) |
| 2469 |
else: |
| 2470 |
res = array.delete_policies_quota_members( |
| 2471 |
policy_names=[module.params["name"]], |
| 2472 |
member_names=[member.member.name], |
| 2473 |
) |
| 2474 |
if res.status_code != 200: |
| 2475 |
module.fail_json( |
| 2476 |
msg="Failed to delete rule {0} from quota policy {1}. Error: {2}".format( |
| 2477 |
member.member.name, |
| 2478 |
module.params["name"], |
| 2479 |
res.errors[0].message, |
| 2480 |
) |
| 2481 |
) |
| 2482 |
else: |
| 2483 |
members = [] |
| 2484 |
cmembers = [] |
| 2485 |
for cmem in current_members: |
| 2486 |
cmembers.append(cmem.member.name) |
| 2487 |
mem_diff = list(set(module.params["directory"]) - set(cmembers)) |
| 2488 |
if mem_diff: |
| 2489 |
for mem in mem_diff: |
| 2490 |
members.append( |
| 2491 |
PolicymemberpostMembers( |
| 2492 |
member=ReferenceWithType( |
| 2493 |
name=mem, |
| 2494 |
resource_type="directories", |
| 2495 |
) |
| 2496 |
) |
| 2497 |
) |
| 2498 |
member = PolicyMemberPost(members=members) |
| 2499 |
changed_member = True |
| 2500 |
if not module.check_mode: |
| 2501 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion( |
| 2502 |
api_version |
| 2503 |
): |
| 2504 |
res = array.post_policies_quota_members( |
| 2505 |
policy_names=[module.params["name"]], |
| 2506 |
members=member, |
| 2507 |
ignore_usage=module.params["ignore_usage"], |
| 2508 |
context_names=[module.params["context"]], |
| 2509 |
) |
| 2510 |
else: |
| 2511 |
res = array.post_policies_quota_members( |
| 2512 |
policy_names=[module.params["name"]], |
| 2513 |
members=member, |
| 2514 |
ignore_usage=module.params["ignore_usage"], |
| 2515 |
) |
| 2516 |
if res.status_code != 200: |
| 2517 |
module.fail_json( |
| 2518 |
msg="Failed to update members for Quota policy {0}. Error: {1}".format( |
| 2519 |
module.params["name"], |
| 2520 |
res.errors[0].message, |
| 2521 |
) |
| 2522 |
) |
| 2523 |
else: |
| 2524 |
members = [] |
| 2525 |
for mem in module.params["directory"]: |
| 2526 |
members.append( |
| 2527 |
PolicymemberpostMembers( |
| 2528 |
member=ReferenceWithType( |
| 2529 |
name=mem, |
| 2530 |
resource_type="directories", |
| 2531 |
) |
| 2532 |
) |
| 2533 |
) |
| 2534 |
member = PolicyMemberPost(members=members) |
| 2535 |
changed_member = True |
| 2536 |
if not module.check_mode: |
| 2537 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2538 |
res = array.post_policies_quota_members( |
| 2539 |
policy_names=[module.params["name"]], |
| 2540 |
members=member, |
| 2541 |
ignore_usage=module.params["ignore_usage"], |
| 2542 |
context_names=[module.params["context"]], |
| 2543 |
) |
| 2544 |
else: |
| 2545 |
res = array.post_policies_quota_members( |
| 2546 |
policy_names=[module.params["name"]], |
| 2547 |
members=member, |
| 2548 |
ignore_usage=module.params["ignore_usage"], |
| 2549 |
) |
| 2550 |
if res.status_code != 200: |
| 2551 |
module.fail_json( |
| 2552 |
msg="Failed to update members for Quota policy {0}. Error: {1}".format( |
| 2553 |
module.params["name"], |
| 2554 |
res.errors[0].message, |
| 2555 |
) |
| 2556 |
) |
| 2557 |
if module.params["quota_limit"]: |
| 2558 |
quota = human_to_bytes(module.params["quota_limit"]) |
| 2559 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2560 |
current_rules = list( |
| 2561 |
array.get_policies_quota_rules( |
| 2562 |
policy_names=[module.params["name"]], |
| 2563 |
context_names=[module.params["context"]], |
| 2564 |
).items |
| 2565 |
) |
| 2566 |
else: |
| 2567 |
current_rules = list( |
| 2568 |
array.get_policies_quota_rules( |
| 2569 |
policy_names=[module.params["name"]] |
| 2570 |
).items |
| 2571 |
) |
| 2572 |
if current_rules: |
| 2573 |
if module.params["rule_name"]: |
| 2574 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2575 |
res = array.get_policies_quota_rules( |
| 2576 |
policy_names=[module.params["name"]], |
| 2577 |
filter="name='" + module.params["rule_name"] + "'", |
| 2578 |
context_names=[module.params["context"]], |
| 2579 |
) |
| 2580 |
else: |
| 2581 |
res = array.get_policies_quota_rules( |
| 2582 |
policy_names=[module.params["name"]], |
| 2583 |
filter="name='" + module.params["rule_name"] + "'", |
| 2584 |
) |
| 2585 |
if res.status_code != 200: |
| 2586 |
module.fail_json( |
| 2587 |
msg="Rule update failed. Error: {0}".format( |
| 2588 |
res.errors[0].message |
| 2589 |
) |
| 2590 |
) |
| 2591 |
rule_config = list(res.items)[0] |
| 2592 |
current_rule_config = { |
| 2593 |
"enforced": rule_config.enforced, |
| 2594 |
"quota": rule_config.quota_limit, |
| 2595 |
"notifications": rule_config.notifications, |
| 2596 |
} |
| 2597 |
new_rule_config = { |
| 2598 |
"enforced": rule_config.enforced, |
| 2599 |
"quota": rule_config.quota_limit, |
| 2600 |
"notifications": rule_config.notifications, |
| 2601 |
} |
| 2602 |
if ( |
| 2603 |
current_rule_config["enforced"] |
| 2604 |
!= module.params["quota_enforced"] |
| 2605 |
): |
| 2606 |
new_rule_config["quota"] = module.params["quota_enforced"] |
| 2607 |
if ( |
| 2608 |
module.params["quota_limit"] |
| 2609 |
and current_rule_config["quota"] != quota |
| 2610 |
): |
| 2611 |
new_rule_config["quota"] = quota |
| 2612 |
if module.params["quota_notifications"]: |
| 2613 |
if "none" in module.params["quota_notifications"]: |
| 2614 |
module.params["quota_notifications"] == ["none"] |
| 2615 |
new_notifications = ",".join( |
| 2616 |
sorted(module.params["quota_notifications"], reverse=True) |
| 2617 |
) |
| 2618 |
if new_notifications != current_rule_config["notifications"]: |
| 2619 |
new_rule_config["notifications"] = new_notifications |
| 2620 |
if new_rule_config != current_rule_config: |
| 2621 |
changed_rule = True |
| 2622 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2623 |
res = array.patch_policies_quota_rules( |
| 2624 |
policy_names=[module.params["name"]], |
| 2625 |
ignore_usage=module.params["ignore_usage"], |
| 2626 |
names=[module.params["rule_name"]], |
| 2627 |
context_names=[module.params["context"]], |
| 2628 |
rules=PolicyRuleQuotaPatch( |
| 2629 |
rules=[ |
| 2630 |
PolicyrulequotapatchRules( |
| 2631 |
enforced=new_rule_config["enforced"], |
| 2632 |
quota_limit=new_rule_config["quota"], |
| 2633 |
notifications=new_rule_config[ |
| 2634 |
"notifications" |
| 2635 |
], |
| 2636 |
) |
| 2637 |
] |
| 2638 |
), |
| 2639 |
) |
| 2640 |
else: |
| 2641 |
res = array.patch_policies_quota_rules( |
| 2642 |
policy_names=[module.params["name"]], |
| 2643 |
ignore_usage=module.params["ignore_usage"], |
| 2644 |
names=[module.params["rule_name"]], |
| 2645 |
rules=PolicyRuleQuotaPatch( |
| 2646 |
rules=[ |
| 2647 |
PolicyrulequotapatchRules( |
| 2648 |
enforced=new_rule_config["enforced"], |
| 2649 |
quota_limit=new_rule_config["quota"], |
| 2650 |
notifications=new_rule_config[ |
| 2651 |
"notifications" |
| 2652 |
], |
| 2653 |
) |
| 2654 |
] |
| 2655 |
), |
| 2656 |
) |
| 2657 |
if res.status_code != 200: |
| 2658 |
module.fail_json( |
| 2659 |
msg="Failed to update rule {0} for quota {1}. Error: {2}".format( |
| 2660 |
module.params["rule_name"], |
| 2661 |
module.params["name"], |
| 2662 |
res.errors[0].message, |
| 2663 |
) |
| 2664 |
) |
| 2665 |
else: |
| 2666 |
one_enforced = False |
| 2667 |
for check_rule in current_rules: |
| 2668 |
if check_rule.enforced: |
| 2669 |
one_enforced = True |
| 2670 |
for rule in current_rules: |
| 2671 |
rule_exists = False |
| 2672 |
if not module.params["quota_notifications"]: |
| 2673 |
current_notifications = "none" |
| 2674 |
else: |
| 2675 |
if "none" in module.params["quota_notifications"]: |
| 2676 |
module.params["quota_notifications"] == ["none"] |
| 2677 |
current_notifications = ",".join( |
| 2678 |
module.params["quota_notifications"] |
| 2679 |
) |
| 2680 |
if bool( |
| 2681 |
(rule.quota_limit == quota) |
| 2682 |
and (rule.enforced == module.params["quota_enforced"]) |
| 2683 |
and (rule.notifications == current_notifications) |
| 2684 |
): |
| 2685 |
rule_exists = True |
| 2686 |
break |
| 2687 |
if not rule_exists: |
| 2688 |
if module.params["quota_enforced"] and one_enforced: |
| 2689 |
module.fail_json( |
| 2690 |
msg="Only one enforced rule can be defined per policy" |
| 2691 |
) |
| 2692 |
if "none" in module.params["quota_notifications"]: |
| 2693 |
module.params["quota_notifications"] == ["none"] |
| 2694 |
notifications = ",".join(module.params["quota_notifications"]) |
| 2695 |
rules = PolicyrulequotapostRules( |
| 2696 |
enforced=module.params["quota_enforced"], |
| 2697 |
quota_limit=quota, |
| 2698 |
notifications=notifications, |
| 2699 |
) |
| 2700 |
rule = PolicyRuleQuotaPost(rules=[rules]) |
| 2701 |
changed_quota = True |
| 2702 |
if not module.check_mode: |
| 2703 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion( |
| 2704 |
api_version |
| 2705 |
): |
| 2706 |
res = array.post_policies_quota_rules( |
| 2707 |
policy_names=[module.params["name"]], |
| 2708 |
rules=rule, |
| 2709 |
ignore_usage=module.params["ignore_usage"], |
| 2710 |
context_names=[module.params["context"]], |
| 2711 |
) |
| 2712 |
else: |
| 2713 |
res = array.post_policies_quota_rules( |
| 2714 |
policy_names=[module.params["name"]], |
| 2715 |
rules=rule, |
| 2716 |
ignore_usage=module.params["ignore_usage"], |
| 2717 |
) |
| 2718 |
if res.status_code != 200: |
| 2719 |
module.fail_json( |
| 2720 |
msg="Failed to add new rule to Quota policy {0}. Error: {1}".format( |
| 2721 |
module.params["name"], |
| 2722 |
res.errors[0].message, |
| 2723 |
) |
| 2724 |
) |
| 2725 |
else: |
| 2726 |
rules = PolicyrulequotapostRules( |
| 2727 |
enforced=module.params["quota_enforced"], |
| 2728 |
quota_limit=quota, |
| 2729 |
notifications=",".join(module.params["quota_notifications"]), |
| 2730 |
) |
| 2731 |
rule = PolicyRuleQuotaPost(rules=[rules]) |
| 2732 |
changed_quota = True |
| 2733 |
if not module.check_mode: |
| 2734 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 2735 |
res = array.post_policies_quota_rules( |
| 2736 |
policy_names=[module.params["name"]], |
| 2737 |
rules=rule, |
| 2738 |
ignore_usage=module.params["ignore_usage"], |
| 2739 |
context_names=[module.params["context"]], |
| 2740 |
) |
| 2741 |
else: |
| 2742 |
res = array.post_policies_quota_rules( |
| 2743 |
policy_names=[module.params["name"]], |
| 2744 |
rules=rule, |
| 2745 |
ignore_usage=module.params["ignore_usage"], |
| 2746 |
) |
| 2747 |
if res.status_code != 200: |
| 2748 |
module.fail_json( |
| 2749 |
msg="Failed to add rule to Quota policy {0}. Error: {1}".format( |
| 2750 |
module.params["name"], res.errors[0].message |
| 2751 |
) |
| 2752 |
) |
| 2753 |
|
| 2754 |
if ( |
| 2755 |
changed_rule |
| 2756 |
or changed_enable |
| 2757 |
or changed_quota |
| 2758 |
or changed_member |
| 2759 |
or changed_dir |
| 2760 |
or changed_user_map |
| 2761 |
or changed_abe |
| 2762 |
or changed_ca |
| 2763 |
or changed_nfs |
| 2764 |
or changed_rule |
| 2765 |
): |
| 2766 |
changed = True |
| 2767 |
module.exit_json(changed=changed) |
| 2768 |
|
| 2769 |
|
| 2770 |
def main(): |
| 2771 |
argument_spec = purefa_argument_spec() |
| 2772 |
argument_spec.update( |
| 2773 |
dict( |
| 2774 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 2775 |
nfs_access=dict( |
| 2776 |
type="str", |
| 2777 |
default="no-root-squash", |
| 2778 |
choices=["root-squash", "no-root-squash", "all-squash"], |
| 2779 |
), |
| 2780 |
nfs_permission=dict(type="str", default="rw", choices=["rw", "ro"]), |
| 2781 |
policy=dict( |
| 2782 |
type="str", |
| 2783 |
required=True, |
| 2784 |
choices=["nfs", "smb", "snapshot", "quota", "autodir", "password"], |
| 2785 |
), |
| 2786 |
name=dict(type="str", required=True), |
| 2787 |
rename=dict(type="str"), |
| 2788 |
client=dict(type="str"), |
| 2789 |
enabled=dict(type="bool", default=True), |
| 2790 |
snap_at=dict(type="str"), |
| 2791 |
snap_every=dict(type="int"), |
| 2792 |
snap_keep_for=dict(type="int"), |
| 2793 |
snap_client_name=dict(type="str"), |
| 2794 |
snap_suffix=dict(type="str"), |
| 2795 |
smb_anon_allowed=dict(type="bool", default=False), |
| 2796 |
smb_encrypt=dict(type="bool", default=False), |
| 2797 |
ignore_usage=dict(type="bool", default=False), |
| 2798 |
quota_enforced=dict(type="bool", default=True), |
| 2799 |
quota_limit=dict(type="str"), |
| 2800 |
anongid=dict(type="str", default="65534"), |
| 2801 |
anonuid=dict(type="str", default="65534"), |
| 2802 |
quota_notifications=dict( |
| 2803 |
type="list", elements="str", choices=["user", "group", "none"] |
| 2804 |
), |
| 2805 |
user_mapping=dict(type="bool", default=True), |
| 2806 |
directory=dict(type="list", elements="str"), |
| 2807 |
nfs_version=dict( |
| 2808 |
type="list", |
| 2809 |
elements="str", |
| 2810 |
choices=["nfsv3", "nfsv4"], |
| 2811 |
), |
| 2812 |
security=dict( |
| 2813 |
type="list", |
| 2814 |
elements="str", |
| 2815 |
choices=["auth_sys", "krb5", "krb5i", "krb5p"], |
| 2816 |
), |
| 2817 |
access_based_enumeration=dict(type="bool", default=False), |
| 2818 |
continuous_availability=dict(type="bool", default=False), |
| 2819 |
enforce_dictionary_check=dict(type="bool"), |
| 2820 |
enforce_username_check=dict(type="bool"), |
| 2821 |
lockout_duration=dict(type="int"), |
| 2822 |
min_character_groups=dict(type="int"), |
| 2823 |
min_characters_per_group=dict(type="int"), |
| 2824 |
min_password_length=dict(type="int", no_log=False), |
| 2825 |
password_history=dict(type="int", no_log=False), |
| 2826 |
max_login_attempts=dict(type="int"), |
| 2827 |
rule_name=dict(type="str"), |
| 2828 |
context=dict(type="str", default=""), |
| 2829 |
) |
| 2830 |
) |
| 2831 |
|
| 2832 |
required_together = [["snap_keep_for", "snap_every"]] |
| 2833 |
module = AnsibleModule( |
| 2834 |
argument_spec, required_together=required_together, supports_check_mode=True |
| 2835 |
) |
| 2836 |
|
| 2837 |
if not HAS_PURESTORAGE: |
| 2838 |
module.fail_json(msg="py-pure-client sdk is required for this module") |
| 2839 |
|
| 2840 |
array = get_array(module) |
| 2841 |
api_version = array.get_rest_version() |
| 2842 |
if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version): |
| 2843 |
module.fail_json( |
| 2844 |
msg="FlashArray REST version not supported. " |
| 2845 |
"Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION) |
| 2846 |
) |
| 2847 |
if module.params["policy"] == "quota" and LooseVersion( |
| 2848 |
MIN_QUOTA_API_VERSION |
| 2849 |
) > LooseVersion(api_version): |
| 2850 |
module.fail_json( |
| 2851 |
msg="FlashArray REST version not supported for directory quotas. " |
| 2852 |
"Minimum version required: {0}".format(MIN_QUOTA_API_VERSION) |
| 2853 |
) |
| 2854 |
if module.params["policy"] == "autodir" and LooseVersion( |
| 2855 |
AUTODIR_VERSION |
| 2856 |
) > LooseVersion(api_version): |
| 2857 |
module.fail_json( |
| 2858 |
msg="FlashArray REST version not supported for autodir policies. " |
| 2859 |
"Minimum version required: {0}".format(AUTODIR_VERSION) |
| 2860 |
) |
| 2861 |
if module.params["policy"] == "password" and LooseVersion( |
| 2862 |
PASSWORD_VERSION |
| 2863 |
) > LooseVersion(api_version): |
| 2864 |
module.fail_json( |
| 2865 |
msg="FlashArray REST version not supported for password policies. " |
| 2866 |
"Minimum version required: {0}".format(PASSWORD_VERSION) |
| 2867 |
) |
| 2868 |
if ( |
| 2869 |
module.params["policy"] == "password" |
| 2870 |
and module.params["lockout_duration"] |
| 2871 |
and not 1 <= module.params["lockout_duration"] <= 7776000 |
| 2872 |
): |
| 2873 |
module.fail_json(msg="Lockout duration must be between 1 and 7776000 seconds") |
| 2874 |
state = module.params["state"] |
| 2875 |
if module.params["quota_notifications"]: |
| 2876 |
module.params["quota_notifications"].sort(reverse=True) |
| 2877 |
quota_notifications = [] |
| 2878 |
for noti in module.params["quota_notifications"]: |
| 2879 |
if noti not in quota_notifications: |
| 2880 |
quota_notifications.append(noti) |
| 2881 |
module.params["quota_notifications"] = quota_notifications |
| 2882 |
else: |
| 2883 |
module.params["quota_notifications"] = [] |
| 2884 |
|
| 2885 |
if ( |
| 2886 |
module.params["nfs_access"] == "all-squash" |
| 2887 |
and ALL_SQUASH_VERSION not in api_version |
| 2888 |
): |
| 2889 |
module.fail_json( |
| 2890 |
msg="all-squash is not supported in this version of Purity//FA" |
| 2891 |
) |
| 2892 |
|
| 2893 |
all_squash = ALL_SQUASH_VERSION in api_version |
| 2894 |
exists = bool(array.get_policies(names=[module.params["name"]]).status_code == 200) |
| 2895 |
|
| 2896 |
if state == "present" and not exists: |
| 2897 |
create_policy(module, array, all_squash) |
| 2898 |
elif state == "present" and exists and module.params["rename"]: |
| 2899 |
rename_policy(module, array) |
| 2900 |
elif state == "present" and exists: |
| 2901 |
update_policy(module, array, api_version, all_squash) |
| 2902 |
elif state == "absent" and exists: |
| 2903 |
delete_policy(module, array) |
| 2904 |
|
| 2905 |
module.exit_json(changed=False) |
| 2906 |
|
| 2907 |
|
| 2908 |
if __name__ == "__main__": |
| 2909 |
main() |