| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
ANSIBLE_METADATA = { |
| 13 |
"metadata_version": "1.1", |
| 14 |
"status": ["preview"], |
| 15 |
"supported_by": "community", |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
DOCUMENTATION = """ |
| 20 |
--- |
| 21 |
module: purefb_bucket |
| 22 |
version_added: "1.0.0" |
| 23 |
short_description: Manage Object Store Buckets on a Pure Storage FlashBlade. |
| 24 |
description: |
| 25 |
- This module managess object store (s3) buckets on Pure Storage FlashBlade. |
| 26 |
author: Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 27 |
options: |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Bucket Name. |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
account: |
| 34 |
description: |
| 35 |
- Object Store Account for Bucket. |
| 36 |
required: true |
| 37 |
type: str |
| 38 |
versioning: |
| 39 |
description: |
| 40 |
- State of S3 bucket versioning |
| 41 |
required: false |
| 42 |
default: absent |
| 43 |
type: str |
| 44 |
choices: [ "enabled", "suspended", "absent" ] |
| 45 |
state: |
| 46 |
description: |
| 47 |
- Create, delete or modifies a bucket. |
| 48 |
required: false |
| 49 |
default: present |
| 50 |
type: str |
| 51 |
choices: [ "present", "absent" ] |
| 52 |
eradicate: |
| 53 |
description: |
| 54 |
- Define whether to eradicate the bucket on delete or leave in trash. |
| 55 |
required: false |
| 56 |
type: bool |
| 57 |
default: false |
| 58 |
mode: |
| 59 |
description: |
| 60 |
- The type of bucket to be created. Also referred to a VSO Mode. |
| 61 |
- I(multi-site-writable) type can only be used after feature is |
| 62 |
enabled by Pure Technical Support |
| 63 |
type: str |
| 64 |
choices: [ "classic", "multi-site-writable" ] |
| 65 |
version_added: '1.10.0' |
| 66 |
quota: |
| 67 |
description: |
| 68 |
- The effective quota limit applied against the size of the bucket. |
| 69 |
- User byte quota in B, K, M, G, T or P units. |
| 70 |
- Range must be between 1 byte and 9.22 exabytes. |
| 71 |
- Setting to 0 will allow the bucket size to be unlimited. |
| 72 |
type: str |
| 73 |
version_added: '1.12.0' |
| 74 |
hard_limit: |
| 75 |
description: |
| 76 |
- Whether the I(quota) value is enforced or not. |
| 77 |
- If not provided the object store account default value will be used. |
| 78 |
type: bool |
| 79 |
version_added: '1.12.0' |
| 80 |
retention_lock: |
| 81 |
description: |
| 82 |
- Set retention lock level for the bucket |
| 83 |
- Once set to I(ratcheted) can only be lowered by Pure Technical Services |
| 84 |
type: str |
| 85 |
choices: [ "ratcheted", "unlocked" ] |
| 86 |
default: unlocked |
| 87 |
version_added: '1.12.0' |
| 88 |
retention_mode: |
| 89 |
description: |
| 90 |
- The retention mode used to apply locks on new objects if none is specified by the S3 client |
| 91 |
- I(safemode) is available if global Per-Bucket Safemode config is enabled |
| 92 |
- Use "" to clear |
| 93 |
- Once set to I(compliance) this can only be changed by contacting Pure Technical Services |
| 94 |
type: str |
| 95 |
choices: [ "compliance", "governance", "safemode", "" ] |
| 96 |
version_added: '1.12.0' |
| 97 |
object_lock_enabled: |
| 98 |
description: |
| 99 |
- If set to true, then S3 APIs relating to object lock may be used |
| 100 |
type: bool |
| 101 |
default: false |
| 102 |
version_added: '1.12.0' |
| 103 |
freeze_locked_objects: |
| 104 |
description: |
| 105 |
- If set to true, a locked object will be read-only and no new versions of |
| 106 |
the object may be created due to modifications |
| 107 |
- After enabling, can be disabled only by contacting Pure Technical Services |
| 108 |
type: bool |
| 109 |
default: false |
| 110 |
version_added: '1.12.0' |
| 111 |
default_retention: |
| 112 |
description: |
| 113 |
- The retention period, in days, used to apply locks on new objects if |
| 114 |
none is specified by the S3 client |
| 115 |
- Valid values between 1 and 365000 |
| 116 |
- Use "" to clear |
| 117 |
type: str |
| 118 |
version_added: '1.12.0' |
| 119 |
block_new_public_policies: |
| 120 |
description: |
| 121 |
- If set to true, adding bucket policies that grant public access to a bucket is not allowed. |
| 122 |
type: bool |
| 123 |
version_added: '1.15.0' |
| 124 |
block_public_access: |
| 125 |
description: |
| 126 |
- If set to true, access to a bucket with a public policy is restricted to only authenticated |
| 127 |
users within the account that bucket belongs to. |
| 128 |
type: bool |
| 129 |
version_added: '1.15.0' |
| 130 |
eradication_mode: |
| 131 |
description: |
| 132 |
- The eradication mode of the bucket. |
| 133 |
type: str |
| 134 |
choices: [ "permission-based", "retention-based" ] |
| 135 |
version_added: '1.17.0' |
| 136 |
manual_eradication: |
| 137 |
description: |
| 138 |
- The manual eradication status of the bucket. If false, the bucket cannot be eradicated after |
| 139 |
it has been destroyed, unless it is empty. If true, the bucket can be eradicated. |
| 140 |
type: bool |
| 141 |
version_added: '1.17.0' |
| 142 |
eradication_delay: |
| 143 |
description: |
| 144 |
- Minimum eradication delay in days. Automatically eradicate destroyed buckets after |
| 145 |
the delay time passes unless automatic eradication is delayed due to other configuration values. |
| 146 |
- Valid values are integer days from 1 to 30. Default is 1. |
| 147 |
type: int |
| 148 |
version_added: '1.17.0' |
| 149 |
context: |
| 150 |
description: |
| 151 |
- Name of fleet member on which to perform the operation. |
| 152 |
- This requires the array receiving the request is a member of a fleet |
| 153 |
and the context name to be a member of the same fleet. |
| 154 |
type: str |
| 155 |
default: "" |
| 156 |
version_added: "1.22.0" |
| 157 |
extends_documentation_fragment: |
| 158 |
- purestorage.flashblade.purestorage.fb |
| 159 |
""" |
| 160 |
|
| 161 |
EXAMPLES = """ |
| 162 |
- name: Create new bucket named foo in account bar |
| 163 |
purestorage.flashblade.purefb_bucket: |
| 164 |
name: foo |
| 165 |
quota: 10G |
| 166 |
hard_limit: false |
| 167 |
account: bar |
| 168 |
fb_url: 10.10.10.2 |
| 169 |
api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641 |
| 170 |
|
| 171 |
- name: Update bucket foo in account bar with new quota |
| 172 |
purestorage.flashblade.purefb_bucket: |
| 173 |
name: foo |
| 174 |
quota: 500B |
| 175 |
hard_limit: true |
| 176 |
account: bar |
| 177 |
fb_url: 10.10.10.2 |
| 178 |
api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641 |
| 179 |
|
| 180 |
- name: Remove quota limits from bucket named foo in account bar |
| 181 |
purestorage.flashblade.purefb_bucket: |
| 182 |
name: foo |
| 183 |
quota: 0 |
| 184 |
account: bar |
| 185 |
fb_url: 10.10.10.2 |
| 186 |
api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641 |
| 187 |
|
| 188 |
- name: Delete bucket named foo in account bar |
| 189 |
purestorage.flashblade.purefb_bucket: |
| 190 |
name: foo |
| 191 |
account: bar |
| 192 |
state: absent |
| 193 |
fb_url: 10.10.10.2 |
| 194 |
api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641 |
| 195 |
|
| 196 |
- name: Change bucket versioning state |
| 197 |
purestorage.flashblade.purefb_bucket: |
| 198 |
name: foo |
| 199 |
account: bar |
| 200 |
versioning: enabled |
| 201 |
fb_url: 10.10.10.2 |
| 202 |
api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641 |
| 203 |
|
| 204 |
- name: Recover deleted bucket named foo in account bar |
| 205 |
purestorage.flashblade.purefb_bucket: |
| 206 |
name: foo |
| 207 |
account: bar |
| 208 |
state: present |
| 209 |
fb_url: 10.10.10.2 |
| 210 |
api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641 |
| 211 |
|
| 212 |
- name: Eradicate bucket named foo in account bar |
| 213 |
purestorage.flashblade.purefb_bucket: |
| 214 |
name: foo |
| 215 |
account: bar |
| 216 |
state: absent |
| 217 |
eradicate: true |
| 218 |
fb_url: 10.10.10.2 |
| 219 |
api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641 |
| 220 |
""" |
| 221 |
|
| 222 |
RETURN = """ |
| 223 |
""" |
| 224 |
|
| 225 |
HAS_PYPURECLIENT = True |
| 226 |
try: |
| 227 |
from pypureclient.flashblade import ( |
| 228 |
BucketPost, |
| 229 |
ReferenceWritable, |
| 230 |
BucketPatch, |
| 231 |
ObjectLockConfigRequestBody, |
| 232 |
PublicAccessConfig, |
| 233 |
BucketAccessPolicyPost, |
| 234 |
BucketAccessPolicyRulePost, |
| 235 |
BucketAccessPolicyRulePrincipal, |
| 236 |
BucketEradicationConfig, |
| 237 |
) |
| 238 |
except ImportError: |
| 239 |
HAS_PYPURECLIENT = False |
| 240 |
|
| 241 |
from ansible.module_utils.basic import AnsibleModule |
| 242 |
from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import ( |
| 243 |
get_system, |
| 244 |
purefb_argument_spec, |
| 245 |
) |
| 246 |
from ansible_collections.purestorage.flashblade.plugins.module_utils.common import ( |
| 247 |
human_to_bytes, |
| 248 |
) |
| 249 |
|
| 250 |
SEC_PER_DAY = 86400000 |
| 251 |
VSO_VERSION = "2.5" |
| 252 |
QUOTA_VERSION = "2.8" |
| 253 |
MODE_VERSION = "2.12" |
| 254 |
WORM_VERSION = "2.13" |
| 255 |
CONTEXT_API_VERSION = "2.17" |
| 256 |
|
| 257 |
|
| 258 |
def get_s3acc(module, blade): |
| 259 |
"""Return Object Store Account or None""" |
| 260 |
api_version = list(blade.get_versions().items) |
| 261 |
if CONTEXT_API_VERSION in api_version: |
| 262 |
res = blade.get_object_store_accounts( |
| 263 |
context_names=[module.params["context"]], |
| 264 |
names=[module.params["account"]], |
| 265 |
) |
| 266 |
else: |
| 267 |
res = blade.get_object_store_accounts(names=[module.params["account"]]) |
| 268 |
if res.status_code == 200: |
| 269 |
return list(res.items)[0] |
| 270 |
return None |
| 271 |
|
| 272 |
|
| 273 |
def get_bucket(module, blade): |
| 274 |
"""Return Bucket or None""" |
| 275 |
api_version = list(blade.get_versions().items) |
| 276 |
if CONTEXT_API_VERSION in api_version: |
| 277 |
res = blade.get_buckets( |
| 278 |
context_names=[module.params["context"]], |
| 279 |
names=[module.params["name"]], |
| 280 |
) |
| 281 |
else: |
| 282 |
res = blade.get_buckets(names=[module.params["name"]]) |
| 283 |
if res.status_code == 200: |
| 284 |
return list(res.items)[0] |
| 285 |
return None |
| 286 |
|
| 287 |
|
| 288 |
def create_bucket(module, blade): |
| 289 |
"""Create bucket""" |
| 290 |
changed = True |
| 291 |
api_version = list(blade.get_versions().items) |
| 292 |
if not module.check_mode: |
| 293 |
if VSO_VERSION in api_version: |
| 294 |
if CONTEXT_API_VERSION in api_version: |
| 295 |
account_defaults = list( |
| 296 |
blade.get_object_store_accounts( |
| 297 |
names=[module.params["account"]], |
| 298 |
context_names=[module.params["context"]], |
| 299 |
).items |
| 300 |
)[0] |
| 301 |
else: |
| 302 |
account_defaults = list( |
| 303 |
blade.get_object_store_accounts( |
| 304 |
names=[module.params["account"]] |
| 305 |
).items |
| 306 |
)[0] |
| 307 |
if QUOTA_VERSION in api_version: |
| 308 |
if not module.params["hard_limit"]: |
| 309 |
module.params["hard_limit"] = account_defaults.hard_limit_enabled |
| 310 |
if module.params["quota"]: |
| 311 |
quota = str(human_to_bytes(module.params["quota"])) |
| 312 |
else: |
| 313 |
if not account_defaults.quota_limit: |
| 314 |
quota = "" |
| 315 |
else: |
| 316 |
quota = str(account_defaults.quota_limit) |
| 317 |
if not module.params["retention_mode"]: |
| 318 |
module.params["retention_mode"] = "" |
| 319 |
if not module.params["default_retention"]: |
| 320 |
module.params["default_retention"] = "" |
| 321 |
else: |
| 322 |
module.params["default_retention"] = str( |
| 323 |
int(module.params["default_retention"]) * 86400000 |
| 324 |
) |
| 325 |
if module.params["object_lock_enabled"]: |
| 326 |
bucket = BucketPost( |
| 327 |
account=ReferenceWritable(name=module.params["account"]), |
| 328 |
bucket_type=module.params["mode"], |
| 329 |
hard_limit_enabled=module.params["hard_limit"], |
| 330 |
quota_limit=quota, |
| 331 |
) |
| 332 |
else: |
| 333 |
bucket = BucketPost( |
| 334 |
account=ReferenceWritable(name=module.params["account"]), |
| 335 |
bucket_type=module.params["mode"], |
| 336 |
hard_limit_enabled=module.params["hard_limit"], |
| 337 |
quota_limit=quota, |
| 338 |
) |
| 339 |
else: |
| 340 |
bucket = BucketPost( |
| 341 |
account=ReferenceWritable(name=module.params["account"]), |
| 342 |
bucket_type=module.params["mode"], |
| 343 |
) |
| 344 |
if CONTEXT_API_VERSION in api_version: |
| 345 |
res = blade.post_buckets( |
| 346 |
names=[module.params["name"]], |
| 347 |
bucket=bucket, |
| 348 |
context_names=[module.params["context"]], |
| 349 |
) |
| 350 |
else: |
| 351 |
res = blade.post_buckets(names=[module.params["name"]], bucket=bucket) |
| 352 |
if res.status_code != 200: |
| 353 |
_delete_bucket(module, blade) |
| 354 |
module.fail_json( |
| 355 |
msg="Object Store Bucket {0} creation failed. Error: {1}".format( |
| 356 |
module.params["name"], |
| 357 |
res.errors[0].message, |
| 358 |
) |
| 359 |
) |
| 360 |
if module.params["versioning"] != "absent": |
| 361 |
if QUOTA_VERSION in api_version: |
| 362 |
bucket = BucketPatch( |
| 363 |
retention_lock=module.params["retention_lock"], |
| 364 |
object_lock_config=ObjectLockConfigRequestBody( |
| 365 |
default_retention_mode=module.params["retention_mode"], |
| 366 |
enabled=module.params["object_lock_enabled"], |
| 367 |
freeze_locked_objects=module.params[ |
| 368 |
"freeze_locked_objects" |
| 369 |
], |
| 370 |
default_retention=module.params["default_retention"], |
| 371 |
), |
| 372 |
versioning=module.params["versioning"], |
| 373 |
) |
| 374 |
else: |
| 375 |
bucket = BucketPatch( |
| 376 |
retention_lock=module.params["retention_lock"], |
| 377 |
versioning=module.params["versioning"], |
| 378 |
) |
| 379 |
else: |
| 380 |
if QUOTA_VERSION in api_version: |
| 381 |
bucket = BucketPatch( |
| 382 |
retention_lock=module.params["retention_lock"], |
| 383 |
object_lock_config=ObjectLockConfigRequestBody( |
| 384 |
default_retention_mode=module.params["retention_mode"], |
| 385 |
enabled=module.params["object_lock_enabled"], |
| 386 |
freeze_locked_objects=module.params[ |
| 387 |
"freeze_locked_objects" |
| 388 |
], |
| 389 |
default_retention=module.params["default_retention"], |
| 390 |
), |
| 391 |
versioning="none", |
| 392 |
) |
| 393 |
else: |
| 394 |
bucket = BucketPatch( |
| 395 |
retention_lock=module.params["retention_lock"], |
| 396 |
versioning="none", |
| 397 |
) |
| 398 |
|
| 399 |
if CONTEXT_API_VERSION in api_version: |
| 400 |
res = blade.patch_buckets( |
| 401 |
names=[module.params["name"]], |
| 402 |
bucket=bucket, |
| 403 |
context_names=[module.params["context"]], |
| 404 |
) |
| 405 |
else: |
| 406 |
res = blade.patch_buckets(names=[module.params["name"]], bucket=bucket) |
| 407 |
if res.status_code != 200: |
| 408 |
module.fail_json( |
| 409 |
msg="Object Store Bucket {0} creation update failed. Error: {1}".format( |
| 410 |
module.params["name"], |
| 411 |
res.errors[0].message, |
| 412 |
) |
| 413 |
) |
| 414 |
else: |
| 415 |
bucket = BucketPost( |
| 416 |
account=ReferenceWritable(name=module.params["account"]), |
| 417 |
) |
| 418 |
if CONTEXT_API_VERSION in api_version: |
| 419 |
res = blade.post_buckets( |
| 420 |
names=[module.params["name"]], |
| 421 |
bucket=bucket, |
| 422 |
context_names=[module.params["context"]], |
| 423 |
) |
| 424 |
else: |
| 425 |
res = blade.post_buckets(names=[module.params["name"]], bucket=bucket) |
| 426 |
if res.status_code != 200: |
| 427 |
_delete_bucket(module, blade) |
| 428 |
module.fail_json( |
| 429 |
msg="Object Store Bucket {0} creation failed. Error: {1}".format( |
| 430 |
module.params["name"], |
| 431 |
res.errors[0].message, |
| 432 |
) |
| 433 |
) |
| 434 |
if module.params["versioning"] != "absent": |
| 435 |
if CONTEXT_API_VERSION in api_version: |
| 436 |
res = blade.buckets.patch_buckets( |
| 437 |
names=[module.params["name"]], |
| 438 |
bucket=BucketPatch(versioning=module.params["versioning"]), |
| 439 |
context_names=[module.params["context"]], |
| 440 |
) |
| 441 |
else: |
| 442 |
res = blade.buckets.patch_buckets( |
| 443 |
names=[module.params["name"]], |
| 444 |
bucket=BucketPatch(versioning=module.params["versioning"]), |
| 445 |
) |
| 446 |
if res.status_code != 200: |
| 447 |
module.fail_json( |
| 448 |
msg="Object Store Bucket {0} created but versioning state failed. Error: {1}".format( |
| 449 |
module.params["name"], res.errors[0].message |
| 450 |
) |
| 451 |
) |
| 452 |
if MODE_VERSION in api_version: |
| 453 |
if not module.params["block_new_public_policies"]: |
| 454 |
module.params["block_new_public_policies"] = False |
| 455 |
if not module.params["block_public_access"]: |
| 456 |
module.params["block_public_access"] = False |
| 457 |
pac = BucketPatch( |
| 458 |
public_access_config=PublicAccessConfig( |
| 459 |
block_new_public_policies=module.params[ |
| 460 |
"block_new_public_policies" |
| 461 |
], |
| 462 |
block_public_access=module.params["block_public_access"], |
| 463 |
) |
| 464 |
) |
| 465 |
if CONTEXT_API_VERSION in api_version: |
| 466 |
res = blade.patch_buckets( |
| 467 |
bucket=pac, |
| 468 |
names=[module.params["name"]], |
| 469 |
context_names=[module.params["context"]], |
| 470 |
) |
| 471 |
else: |
| 472 |
res = blade.patch_buckets(bucket=pac, names=[module.params["name"]]) |
| 473 |
if res.status_code != 200: |
| 474 |
module.warn( |
| 475 |
msg="Failed to set Public Access config correctly for bucket {0}. " |
| 476 |
"Error: {1}".format(module.params["name"], res.errors[0].message) |
| 477 |
) |
| 478 |
if ( |
| 479 |
not module.params["block_public_access"] |
| 480 |
and not module.params["block_new_public_policies"] |
| 481 |
): |
| 482 |
|
| 483 |
|
| 484 |
policy = BucketAccessPolicyPost( |
| 485 |
name=module.params["name"], |
| 486 |
) |
| 487 |
if CONTEXT_API_VERSION in api_version: |
| 488 |
res = blade.post_buckets_bucket_access_policies( |
| 489 |
bucket_names=[module.params["name"]], |
| 490 |
policy=policy, |
| 491 |
context_names=[module.params["context"]], |
| 492 |
) |
| 493 |
else: |
| 494 |
res = blade.post_buckets_bucket_access_policies( |
| 495 |
bucket_names=[module.params["name"]], policy=policy |
| 496 |
) |
| 497 |
if res.status_code != 200: |
| 498 |
module.warn( |
| 499 |
msg="Failed to set bucket access policy for bucket {0}. Error: {1}".format( |
| 500 |
module.params["name"], res.errors[0].message |
| 501 |
) |
| 502 |
) |
| 503 |
rule = BucketAccessPolicyRulePost( |
| 504 |
actions=["s3:GetObject"], |
| 505 |
effect="allow", |
| 506 |
principals=BucketAccessPolicyRulePrincipal(all=True), |
| 507 |
resources=[module.params["name"] + "/*"], |
| 508 |
) |
| 509 |
if CONTEXT_API_VERSION in api_version: |
| 510 |
res = blade.post_buckets_bucket_access_policies_rules( |
| 511 |
bucket_names=[module.params["name"]], |
| 512 |
rule=rule, |
| 513 |
names=["default"], |
| 514 |
context_names=[module.params["context"]], |
| 515 |
) |
| 516 |
else: |
| 517 |
res = blade.post_buckets_bucket_access_policies_rules( |
| 518 |
bucket_names=[module.params["name"]], |
| 519 |
rule=rule, |
| 520 |
names=["default"], |
| 521 |
) |
| 522 |
if res.status_code != 200: |
| 523 |
module.warn( |
| 524 |
msg="Failed to set bucket access policy rule for bucket {0}. Error: {1}".format( |
| 525 |
module.params["name"], res.errors[0].message |
| 526 |
) |
| 527 |
) |
| 528 |
if WORM_VERSION in api_version and module.params["eradication_mode"]: |
| 529 |
if not module.params["eradication_delay"]: |
| 530 |
module.params["eradication_delay"] = SEC_PER_DAY |
| 531 |
else: |
| 532 |
module.params["eradication_delay"] = ( |
| 533 |
module.params["eradication_delay"] * SEC_PER_DAY |
| 534 |
) |
| 535 |
if not module.params["manual_eradication"]: |
| 536 |
module.params["manual_eradication"] = "disabled" |
| 537 |
else: |
| 538 |
module.params["manual_eradication"] = "enabled" |
| 539 |
worm = BucketPatch( |
| 540 |
eradication_config=BucketEradicationConfig( |
| 541 |
manual_eradication=module.params["manual_eradication"], |
| 542 |
eradication_mode=module.params["eradication_mode"], |
| 543 |
eradication_delay=module.params["eradication_delay"], |
| 544 |
) |
| 545 |
) |
| 546 |
if CONTEXT_API_VERSION in api_version: |
| 547 |
res = blade.patch_buckets( |
| 548 |
bucket=worm, |
| 549 |
names=[module.params["name"]], |
| 550 |
context_names=[module.params["context"]], |
| 551 |
) |
| 552 |
else: |
| 553 |
res = blade.patch_buckets(bucket=worm, names=[module.params["name"]]) |
| 554 |
if res.status_code != 200: |
| 555 |
module.warn( |
| 556 |
msg="Failed to set Bucket Eradication config correctly for bucket {0}. " |
| 557 |
"Error: {1}".format(module.params["name"], res.errors[0].message) |
| 558 |
) |
| 559 |
module.exit_json(changed=changed) |
| 560 |
|
| 561 |
|
| 562 |
def _delete_bucket(module, blade): |
| 563 |
api_version = list(blade.get_versions().items) |
| 564 |
if CONTEXT_API_VERSION in api_version: |
| 565 |
blade.patch_buckets( |
| 566 |
names=[module.params["name"]], |
| 567 |
bucket=BucketPatch(destroyed=True), |
| 568 |
context_names=[module.params["context"]], |
| 569 |
) |
| 570 |
blade.buckets.delete_buckets( |
| 571 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 572 |
) |
| 573 |
else: |
| 574 |
blade.patch_buckets( |
| 575 |
names=[module.params["name"]], |
| 576 |
bucket=BucketPatch(destroyed=True), |
| 577 |
context_names=[module.params["context"]], |
| 578 |
) |
| 579 |
blade.buckets.delete_buckets( |
| 580 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 581 |
) |
| 582 |
|
| 583 |
|
| 584 |
def delete_bucket(module, blade): |
| 585 |
"""Delete Bucket""" |
| 586 |
changed = True |
| 587 |
api_version = list(blade.get_versions().items) |
| 588 |
if not module.check_mode: |
| 589 |
if CONTEXT_API_VERSION in api_version: |
| 590 |
res = blade.patch_buckets( |
| 591 |
names=[module.params["name"]], |
| 592 |
bucket=BucketPatch(destroyed=True), |
| 593 |
context_names=[module.params["context"]], |
| 594 |
) |
| 595 |
else: |
| 596 |
res = blade.patch_buckets( |
| 597 |
names=[module.params["name"]], bucket=BucketPatch(destroyed=True) |
| 598 |
) |
| 599 |
if res.status_code != 200: |
| 600 |
module.warn( |
| 601 |
msg="Deletion for bucket {0} failed. " |
| 602 |
"Error: {1}".format(module.params["name"], res.errors[0].message) |
| 603 |
) |
| 604 |
if module.params["eradicate"]: |
| 605 |
if CONTEXT_API_VERSION in api_version: |
| 606 |
res = blade.delete_buckets( |
| 607 |
names=[module.params["name"]], |
| 608 |
context_names=[module.params["context"]], |
| 609 |
) |
| 610 |
else: |
| 611 |
res = blade.delete_buckets(names=[module.params["name"]]) |
| 612 |
if res.status_code != 200: |
| 613 |
module.warn( |
| 614 |
msg="Eradication for bucket {0} failed. " |
| 615 |
"Error: {1}".format(module.params["name"], res.errors[0].message) |
| 616 |
) |
| 617 |
module.exit_json(changed=changed) |
| 618 |
|
| 619 |
|
| 620 |
def recover_bucket(module, blade): |
| 621 |
"""Recover Bucket""" |
| 622 |
changed = True |
| 623 |
api_version = list(blade.get_versions().items) |
| 624 |
if not module.check_mode: |
| 625 |
if CONTEXT_API_VERSION in api_version: |
| 626 |
res = blade.patch_buckets( |
| 627 |
names=[module.params["name"]], |
| 628 |
bucket=BucketPatch(destroyed=False), |
| 629 |
context_names=[module.params["context"]], |
| 630 |
) |
| 631 |
else: |
| 632 |
res = blade.patch_buckets( |
| 633 |
names=[module.params["name"]], bucket=BucketPatch(destroyed=False) |
| 634 |
) |
| 635 |
if res.status_code != 200: |
| 636 |
module.fail_json( |
| 637 |
msg="Object Store Bucket {0} Recovery failed. Error: {1}".format( |
| 638 |
module.params["name"], res.errors[0].message |
| 639 |
) |
| 640 |
) |
| 641 |
module.exit_json(changed=changed) |
| 642 |
|
| 643 |
|
| 644 |
def update_bucket(module, blade, bucket): |
| 645 |
"""Update Bucket""" |
| 646 |
changed = False |
| 647 |
change_pac = False |
| 648 |
change_worm = False |
| 649 |
change_quota = False |
| 650 |
api_version = list(blade.get_versions().items) |
| 651 |
if CONTEXT_API_VERSION in api_version: |
| 652 |
bucket_detail = list( |
| 653 |
blade.get_buckets( |
| 654 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 655 |
).items |
| 656 |
)[0] |
| 657 |
else: |
| 658 |
bucket_detail = list(blade.get_buckets(names=[module.params["name"]]).items)[0] |
| 659 |
if VSO_VERSION in api_version: |
| 660 |
if module.params["mode"] and bucket_detail.bucket_type != module.params["mode"]: |
| 661 |
module.warn("Changing bucket type is not permitted.") |
| 662 |
if QUOTA_VERSION in api_version: |
| 663 |
if ( |
| 664 |
bucket_detail.retention_lock == "ratcheted" |
| 665 |
and getattr( |
| 666 |
bucket_detail.object_lock_config, "default_retention_mode", None |
| 667 |
) |
| 668 |
== "compliance" |
| 669 |
and module.params["retention_mode"] != "compliance" |
| 670 |
): |
| 671 |
module.warn( |
| 672 |
"Changing retention_mode can only be performed by Pure Technical Support." |
| 673 |
) |
| 674 |
if not module.params["object_lock_enabled"] and getattr( |
| 675 |
bucket_detail.object_lock_config, "enabled", False |
| 676 |
): |
| 677 |
module.warn("Object lock cannot be disabled.") |
| 678 |
if not module.params["freeze_locked_objects"] and getattr( |
| 679 |
bucket_detail.object_lock_config, "freeze_locked_objects", False |
| 680 |
): |
| 681 |
module.warn("Freeze locked onjects cannot be disabled.") |
| 682 |
default_retention = getattr( |
| 683 |
bucket_detail.object_lock_config, "default_retention" |
| 684 |
) |
| 685 |
if default_retention and default_retention > 1: |
| 686 |
if ( |
| 687 |
default_retention / 86400000 > int(module.params["default_retention"]) |
| 688 |
and bucket_detail.retention_lock == "ratcheted" |
| 689 |
): |
| 690 |
module.warn( |
| 691 |
"Default retention can only be reduced by Pure Technical Support." |
| 692 |
) |
| 693 |
|
| 694 |
if bucket.versioning != "none": |
| 695 |
if module.params["versioning"] == "absent" and bucket.versioning == "enabled": |
| 696 |
versioning = "suspended" |
| 697 |
else: |
| 698 |
versioning = module.params["versioning"] |
| 699 |
if bucket.versioning != versioning: |
| 700 |
changed = True |
| 701 |
if not module.check_mode: |
| 702 |
if CONTEXT_API_VERSION in api_version: |
| 703 |
res = blade.patch_buckets( |
| 704 |
names=[module.params["name"]], |
| 705 |
bucket=BucketPatch(versioning=versioning), |
| 706 |
context_names=[module.params["context"]], |
| 707 |
) |
| 708 |
else: |
| 709 |
res = blade.patch_buckets( |
| 710 |
names=[module.params["name"]], |
| 711 |
bucket=BucketPatch(versioning=versioning), |
| 712 |
) |
| 713 |
if res.status_code != 200: |
| 714 |
module.fail_json( |
| 715 |
msg="Object Store Bucket {0} versioning change failed. Error: {1}".format( |
| 716 |
module.params["name"], res.errors[0].message |
| 717 |
) |
| 718 |
) |
| 719 |
elif module.params["versioning"] != "absent": |
| 720 |
changed = True |
| 721 |
if not module.check_mode: |
| 722 |
if CONTEXT_API_VERSION in api_version: |
| 723 |
res = blade.patch_buckets( |
| 724 |
names=[module.params["name"]], |
| 725 |
bucket=BucketPatch(versioning=module.params["versioning"]), |
| 726 |
context_names=[module.params["context"]], |
| 727 |
) |
| 728 |
else: |
| 729 |
res = blade.patch_buckets( |
| 730 |
names=[module.params["name"]], |
| 731 |
bucket=BucketPatch(versioning=module.params["versioning"]), |
| 732 |
) |
| 733 |
if res.status_code != 200: |
| 734 |
module.fail_json( |
| 735 |
msg="Object Store Bucket {0} versioning change failed. Error: {1}".format( |
| 736 |
module.params["name"], res.errors[0].message |
| 737 |
) |
| 738 |
) |
| 739 |
if QUOTA_VERSION in api_version: |
| 740 |
current_quota = { |
| 741 |
"quota": bucket_detail.quota_limit, |
| 742 |
"hard": bucket_detail.hard_limit_enabled, |
| 743 |
} |
| 744 |
new_quota = { |
| 745 |
"quota": bucket_detail.quota_limit, |
| 746 |
"hard": bucket_detail.hard_limit_enabled, |
| 747 |
} |
| 748 |
if module.params["quota"]: |
| 749 |
quota = human_to_bytes(module.params["quota"]) |
| 750 |
if module.params["quota"] == "0": |
| 751 |
quota = None |
| 752 |
module.params["hard_limit"] = False |
| 753 |
if quota != current_quota["quota"]: |
| 754 |
change_quota = True |
| 755 |
new_quota["quota"] = human_to_bytes(module.params["quota"]) |
| 756 |
if ( |
| 757 |
module.params["hard_limit"] |
| 758 |
and module.params["hard_limit"] != current_quota["hard"] |
| 759 |
): |
| 760 |
change_quota = True |
| 761 |
new_quota["hard"] = module.params["hard_limit"] |
| 762 |
if current_quota != new_quota and not module.check_mode: |
| 763 |
if new_quota["quota"] is None or new_quota["quota"] == 0: |
| 764 |
bucket = BucketPatch( |
| 765 |
quota_limit="", |
| 766 |
hard_limit_enabled=False, |
| 767 |
) |
| 768 |
else: |
| 769 |
bucket = BucketPatch( |
| 770 |
quota_limit=str(new_quota["quota"]), |
| 771 |
hard_limit_enabled=new_quota["hard"], |
| 772 |
) |
| 773 |
if CONTEXT_API_VERSION in api_version: |
| 774 |
res = blade.patch_buckets( |
| 775 |
bucket=bucket, |
| 776 |
names=[module.params["name"]], |
| 777 |
context_names=[module.params["context"]], |
| 778 |
) |
| 779 |
else: |
| 780 |
res = blade.patch_buckets(bucket=bucket, names=[module.params["name"]]) |
| 781 |
if res.status_code != 200: |
| 782 |
module.fail_json( |
| 783 |
msg="Failed to update quota settings correctly for bucket {0}. " |
| 784 |
"Error: {1}".format(module.params["name"], res.errors[0].message) |
| 785 |
) |
| 786 |
if MODE_VERSION in api_version: |
| 787 |
current_pac = { |
| 788 |
"block_new_public_policies": bucket_detail.public_access_config.block_new_public_policies, |
| 789 |
"block_public_access": bucket_detail.public_access_config.block_public_access, |
| 790 |
} |
| 791 |
if module.params["block_new_public_policies"] is None: |
| 792 |
new_public_policies = current_pac["block_new_public_policies"] |
| 793 |
else: |
| 794 |
new_public_policies = module.params["block_new_public_policies"] |
| 795 |
if module.params["block_public_access"] is None: |
| 796 |
new_public_access = current_pac["block_public_access"] |
| 797 |
else: |
| 798 |
new_public_access = module.params["block_public_access"] |
| 799 |
new_pac = { |
| 800 |
"block_new_public_policies": new_public_policies, |
| 801 |
"block_public_access": new_public_access, |
| 802 |
} |
| 803 |
if current_pac != new_pac: |
| 804 |
change_pac = True |
| 805 |
pac = BucketPatch( |
| 806 |
public_access_config=PublicAccessConfig( |
| 807 |
block_new_public_policies=new_pac["block_new_public_policies"], |
| 808 |
block_public_access=new_pac["block_public_access"], |
| 809 |
) |
| 810 |
) |
| 811 |
if change_pac and not module.check_mode: |
| 812 |
if CONTEXT_API_VERSION in api_version: |
| 813 |
res = blade.patch_buckets( |
| 814 |
bucket=pac, |
| 815 |
names=[module.params["name"]], |
| 816 |
context_names=[module.params["context"]], |
| 817 |
) |
| 818 |
else: |
| 819 |
res = blade.patch_buckets(bucket=pac, names=[module.params["name"]]) |
| 820 |
if res.status_code != 200: |
| 821 |
module.fail_json( |
| 822 |
msg="Failed to update Public Access config correctly for bucket {0}. " |
| 823 |
"Error: {1}".format(module.params["name"], res.errors[0].message) |
| 824 |
) |
| 825 |
if WORM_VERSION in api_version: |
| 826 |
current_worm = { |
| 827 |
"eradication_delay": bucket_detail.eradication_config.eradication_delay, |
| 828 |
"manual_eradication": bucket_detail.eradication_config.manual_eradication, |
| 829 |
"eradication_mode": bucket_detail.eradication_config.eradication_mode, |
| 830 |
} |
| 831 |
if module.params["eradication_delay"] is None: |
| 832 |
new_delay = current_worm["eradication_delay"] |
| 833 |
else: |
| 834 |
new_delay = module.params["eradication_delay"] * SEC_PER_DAY |
| 835 |
if module.params["manual_eradication"] is None: |
| 836 |
new_manual = current_worm["manual_eradication"] |
| 837 |
else: |
| 838 |
if module.params["manual_eradication"]: |
| 839 |
new_manual = "enabled" |
| 840 |
else: |
| 841 |
new_manual = "disabled" |
| 842 |
if ( |
| 843 |
module.params["eradication_mode"] |
| 844 |
and module.params["eradication_mode"] != current_worm["eradication_mode"] |
| 845 |
): |
| 846 |
new_mode = module.params["eradication_mode"] |
| 847 |
else: |
| 848 |
new_mode = current_worm["eradication_mode"] |
| 849 |
new_worm = { |
| 850 |
"eradication_delay": new_delay, |
| 851 |
"manual_eradication": new_manual, |
| 852 |
"eradication_mode": new_mode, |
| 853 |
} |
| 854 |
if current_worm != new_worm: |
| 855 |
change_worm = True |
| 856 |
worm = BucketPatch( |
| 857 |
public_access_config=BucketEradicationConfig( |
| 858 |
eradication_delay=new_worm["eradication_delay"], |
| 859 |
manual_eradication=new_worm["manual_eradication"], |
| 860 |
eradication_mode=new_worm["eradication_mode"], |
| 861 |
) |
| 862 |
) |
| 863 |
if change_worm and not module.check_mode: |
| 864 |
if CONTEXT_API_VERSION in api_version: |
| 865 |
res = blade.patch_buckets( |
| 866 |
bucket=worm, |
| 867 |
names=[module.params["name"]], |
| 868 |
context_names=[module.params["context"]], |
| 869 |
) |
| 870 |
else: |
| 871 |
res = blade.patch_buckets(bucket=worm, names=[module.params["name"]]) |
| 872 |
if res.status_code != 200: |
| 873 |
module.fail_json( |
| 874 |
msg="Failed to update Eradication config correctly for bucket {0}. " |
| 875 |
"Error: {1}".format(module.params["name"], res.errors[0].message) |
| 876 |
) |
| 877 |
module.exit_json(changed=(changed or change_pac or change_worm or change_quota)) |
| 878 |
|
| 879 |
|
| 880 |
def eradicate_bucket(module, blade): |
| 881 |
"""Eradicate Bucket""" |
| 882 |
changed = True |
| 883 |
api_version = list(blade.get_versions().items) |
| 884 |
if not module.check_mode: |
| 885 |
if CONTEXT_API_VERSION in api_version: |
| 886 |
res = blade.delete_buckets( |
| 887 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 888 |
) |
| 889 |
else: |
| 890 |
res = blade.delete_buckets(names=[module.params["name"]]) |
| 891 |
if res.status_code != 200: |
| 892 |
module.fail_json( |
| 893 |
msg="Object Store Bucket {0} eradication failed. Error: {1}".format( |
| 894 |
module.params["name"], res.errors[0].message |
| 895 |
) |
| 896 |
) |
| 897 |
module.exit_json(changed=changed) |
| 898 |
|
| 899 |
|
| 900 |
def main(): |
| 901 |
argument_spec = purefb_argument_spec() |
| 902 |
argument_spec.update( |
| 903 |
dict( |
| 904 |
name=dict(required=True), |
| 905 |
account=dict(required=True), |
| 906 |
eradicate=dict(default="false", type="bool"), |
| 907 |
mode=dict( |
| 908 |
type="str", |
| 909 |
choices=["classic", "multi-site-writable"], |
| 910 |
), |
| 911 |
retention_mode=dict( |
| 912 |
type="str", choices=["compliance", "governance", "safemode", ""] |
| 913 |
), |
| 914 |
default_retention=dict(type="str"), |
| 915 |
retention_lock=dict( |
| 916 |
type="str", choices=["ratcheted", "unlocked"], default="unlocked" |
| 917 |
), |
| 918 |
hard_limit=dict(type="bool"), |
| 919 |
block_new_public_policies=dict(type="bool"), |
| 920 |
block_public_access=dict(type="bool"), |
| 921 |
object_lock_enabled=dict(type="bool", default=False), |
| 922 |
freeze_locked_objects=dict(type="bool", default=False), |
| 923 |
quota=dict(type="str"), |
| 924 |
versioning=dict( |
| 925 |
default="absent", choices=["enabled", "suspended", "absent"] |
| 926 |
), |
| 927 |
state=dict(default="present", choices=["present", "absent"]), |
| 928 |
eradication_delay=dict(type="int"), |
| 929 |
eradication_mode=dict( |
| 930 |
type="str", choices=["permission-based", "retention-based"] |
| 931 |
), |
| 932 |
manual_eradication=dict(type="bool"), |
| 933 |
context=dict(type="str", default=""), |
| 934 |
) |
| 935 |
) |
| 936 |
|
| 937 |
module = AnsibleModule(argument_spec, supports_check_mode=True) |
| 938 |
|
| 939 |
if not HAS_PYPURECLIENT: |
| 940 |
module.fail_json(msg="py-pure-client sdk is required") |
| 941 |
|
| 942 |
if ( |
| 943 |
module.params["eradication_delay"] |
| 944 |
and not 30 >= module.params["eradication_delay"] >= 1 |
| 945 |
): |
| 946 |
module.fail_json(msg="Eradication Delay must be between 1 and 30 days.") |
| 947 |
|
| 948 |
state = module.params["state"] |
| 949 |
blade = get_system(module) |
| 950 |
api_version = list(blade.get_versions().items) |
| 951 |
|
| 952 |
|
| 953 |
if MODE_VERSION in api_version: |
| 954 |
if not module.params["mode"]: |
| 955 |
module.params["mode"] = "multi-site-writable" |
| 956 |
elif not module.params["mode"]: |
| 957 |
module.params["mode"] = "classic" |
| 958 |
bucket = get_bucket(module, blade) |
| 959 |
if not get_s3acc(module, blade): |
| 960 |
module.fail_json( |
| 961 |
msg="Object Store Account {0} does not exist.".format( |
| 962 |
module.params["account"] |
| 963 |
) |
| 964 |
) |
| 965 |
if ( |
| 966 |
module.params["quota"] |
| 967 |
and human_to_bytes(module.params["quota"]) > 9223372036854775807 |
| 968 |
): |
| 969 |
module.fail_json(msg="Quota must not exceed 9.22 exabytes") |
| 970 |
if module.params["eradicate"] and state == "present": |
| 971 |
module.warn("Eradicate flag ignored without state=absent") |
| 972 |
|
| 973 |
if state == "present" and not bucket: |
| 974 |
create_bucket(module, blade) |
| 975 |
elif state == "present" and bucket and bucket.destroyed: |
| 976 |
recover_bucket(module, blade) |
| 977 |
elif state == "absent" and bucket and not bucket.destroyed: |
| 978 |
delete_bucket(module, blade) |
| 979 |
elif state == "present" and bucket: |
| 980 |
update_bucket(module, blade, bucket) |
| 981 |
elif ( |
| 982 |
state == "absent" and bucket and bucket.destroyed and module.params["eradicate"] |
| 983 |
): |
| 984 |
eradicate_bucket(module, blade) |
| 985 |
elif state == "absent" and not bucket: |
| 986 |
module.exit_json(changed=False) |
| 987 |
|
| 988 |
module.exit_json(changed=False) |
| 989 |
|
| 990 |
|
| 991 |
if __name__ == "__main__": |
| 992 |
main() |