| 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: purefb_s3acc |
| 20 |
version_added: '1.0.0' |
| 21 |
short_description: Create or delete FlashBlade Object Store accounts |
| 22 |
description: |
| 23 |
- Create or delete object store accounts on a Pure Stoage FlashBlade. |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Create or delete object store account |
| 30 |
default: present |
| 31 |
choices: [ absent, present ] |
| 32 |
type: str |
| 33 |
name: |
| 34 |
description: |
| 35 |
- The name of object store account |
| 36 |
type: str |
| 37 |
required: true |
| 38 |
quota: |
| 39 |
description: |
| 40 |
- The effective quota limit to be applied against the size of the account in bytes. |
| 41 |
- Values can be entered as K, M, T or P |
| 42 |
- If set to '' (empty string), the account is unlimited in size. |
| 43 |
version_added: 1.11.0 |
| 44 |
type: str |
| 45 |
hard_limit: |
| 46 |
description: |
| 47 |
- If set to true, the account size, as defined by I(quota_limit), is used as a hard limit quota. |
| 48 |
- If set to false, a hard limit quota will not be applied to the account, but soft quota alerts |
| 49 |
will still be sent if the account has a value set for I(quota_limit). |
| 50 |
version_added: 1.11.0 |
| 51 |
type: bool |
| 52 |
default_quota: |
| 53 |
description: |
| 54 |
- The value of this field will be used to configure the I(quota_limit) field of newly created buckets |
| 55 |
associated with this object store account, if the bucket creation does not specify its own value. |
| 56 |
- Values can be entered as K, M, T or P |
| 57 |
- If set to '' (empty string), the bucket default is unlimited in size. |
| 58 |
version_added: 1.11.0 |
| 59 |
type: str |
| 60 |
default_hard_limit: |
| 61 |
description: |
| 62 |
- The value of this field will be used to configure the I(hard_limit) field of newly created buckets |
| 63 |
associated with this object store account, if the bucket creation does not specify its own value. |
| 64 |
version_added: 1.11.0 |
| 65 |
type: bool |
| 66 |
block_new_public_policies: |
| 67 |
description: |
| 68 |
- If set to true, adding bucket policies that grant public access to a bucket is not allowed. |
| 69 |
type: bool |
| 70 |
version_added: 1.15.0 |
| 71 |
block_public_access: |
| 72 |
description: |
| 73 |
- If set to true, access to a bucket with a public policy is restricted to only authenticated |
| 74 |
users within the account that bucket belongs to. |
| 75 |
type: bool |
| 76 |
version_added: 1.15.0 |
| 77 |
context: |
| 78 |
description: |
| 79 |
- Name of fleet member on which to perform the operation. |
| 80 |
- This requires the array receiving the request is a member of a fleet |
| 81 |
and the context name to be a member of the same fleet. |
| 82 |
type: str |
| 83 |
default: "" |
| 84 |
version_added: "1.22.0" |
| 85 |
extends_documentation_fragment: |
| 86 |
- purestorage.flashblade.purestorage.fb |
| 87 |
""" |
| 88 |
|
| 89 |
EXAMPLES = r""" |
| 90 |
- name: Create object store account foo (with no quotas) |
| 91 |
purestorage.flashblade.purefb_s3acc: |
| 92 |
name: foo |
| 93 |
fb_url: 10.10.10.2 |
| 94 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 95 |
|
| 96 |
- name: Create object store account foo (with quotas) |
| 97 |
purestorage.flashblade.purefb_s3acc: |
| 98 |
name: foo |
| 99 |
quota: 20480000 |
| 100 |
hard_limit: true |
| 101 |
default_quota: 1024000 |
| 102 |
default_hard_limit: false |
| 103 |
fb_url: 10.10.10.2 |
| 104 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 105 |
|
| 106 |
- name: Delete object store account foo |
| 107 |
purestorage.flashblade.purefb_s3acc: |
| 108 |
name: foo |
| 109 |
state: absent |
| 110 |
fb_url: 10.10.10.2 |
| 111 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 112 |
""" |
| 113 |
|
| 114 |
RETURN = r""" |
| 115 |
""" |
| 116 |
|
| 117 |
HAS_PURESTORAGE = True |
| 118 |
try: |
| 119 |
from pypureclient.flashblade import ( |
| 120 |
ObjectStoreAccountPatch, |
| 121 |
BucketDefaults, |
| 122 |
PublicAccessConfig, |
| 123 |
) |
| 124 |
except ImportError: |
| 125 |
HAS_PURESTORAGE = False |
| 126 |
|
| 127 |
from ansible.module_utils.basic import AnsibleModule, human_to_bytes |
| 128 |
from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import ( |
| 129 |
get_system, |
| 130 |
purefb_argument_spec, |
| 131 |
) |
| 132 |
|
| 133 |
|
| 134 |
PUBLIC_API_VERSION = "2.12" |
| 135 |
CONTEXT_API_VERSION = "2.17" |
| 136 |
|
| 137 |
|
| 138 |
def get_s3acc(module, blade): |
| 139 |
"""Return Object Store Account or None""" |
| 140 |
api_version = list(blade.get_versions().items) |
| 141 |
if CONTEXT_API_VERSION in api_version: |
| 142 |
res = blade.get_object_store_accounts( |
| 143 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 144 |
) |
| 145 |
else: |
| 146 |
res = blade.get_object_store_accounts(names=[module.params["name"]]) |
| 147 |
if res.status_code == 200: |
| 148 |
return list(res.items)[0] |
| 149 |
return None |
| 150 |
|
| 151 |
|
| 152 |
def update_s3acc(module, blade): |
| 153 |
"""Update Object Store Account""" |
| 154 |
changed = False |
| 155 |
api_version = list(blade.get_versions().items) |
| 156 |
public = False |
| 157 |
if CONTEXT_API_VERSION in api_version: |
| 158 |
acc_settings = list( |
| 159 |
blade.get_object_store_accounts( |
| 160 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 161 |
).items |
| 162 |
)[0] |
| 163 |
else: |
| 164 |
acc_settings = list( |
| 165 |
blade.get_object_store_accounts(names=[module.params["name"]]).items |
| 166 |
)[0] |
| 167 |
if PUBLIC_API_VERSION in api_version: |
| 168 |
public = True |
| 169 |
current_account = { |
| 170 |
"hard_limit": acc_settings.hard_limit_enabled, |
| 171 |
"default_hard_limit": acc_settings.bucket_defaults.hard_limit_enabled, |
| 172 |
"quota": str(acc_settings.quota_limit), |
| 173 |
"default_quota": str(acc_settings.bucket_defaults.quota_limit), |
| 174 |
"block_new_public_policies": acc_settings.public_access_config.block_new_public_policies, |
| 175 |
"block_public_access": acc_settings.public_access_config.block_public_access, |
| 176 |
} |
| 177 |
else: |
| 178 |
current_account = { |
| 179 |
"hard_limit": acc_settings.hard_limit_enabled, |
| 180 |
"default_hard_limit": acc_settings.bucket_defaults.hard_limit_enabled, |
| 181 |
"quota": str(acc_settings.quota_limit), |
| 182 |
"default_quota": str(acc_settings.bucket_defaults.quota_limit), |
| 183 |
} |
| 184 |
if current_account["quota"] == "None": |
| 185 |
current_account["quota"] = "" |
| 186 |
if current_account["default_quota"] == "None": |
| 187 |
current_account["default_quota"] = "" |
| 188 |
if module.params["quota"] is None: |
| 189 |
module.params["quota"] = current_account["quota"] |
| 190 |
if module.params["default_quota"] is None: |
| 191 |
module.params["default_quota"] = current_account["default_quota"] |
| 192 |
if not module.params["default_quota"]: |
| 193 |
module.params["default_quota"] = "" |
| 194 |
if not module.params["quota"]: |
| 195 |
quota = "" |
| 196 |
else: |
| 197 |
quota = str(human_to_bytes(module.params["quota"])) |
| 198 |
if not module.params["default_quota"]: |
| 199 |
default_quota = "" |
| 200 |
else: |
| 201 |
default_quota = str(human_to_bytes(module.params["default_quota"])) |
| 202 |
if module.params["hard_limit"] is None: |
| 203 |
hard_limit = current_account["hard_limit"] |
| 204 |
else: |
| 205 |
hard_limit = module.params["hard_limit"] |
| 206 |
if module.params["default_hard_limit"] is None: |
| 207 |
default_hard_limit = current_account["default_hard_limit"] |
| 208 |
else: |
| 209 |
default_hard_limit = module.params["default_hard_limit"] |
| 210 |
if public: |
| 211 |
if module.params["block_new_public_policies"] is None: |
| 212 |
new_public_policies = current_account["block_new_public_policies"] |
| 213 |
else: |
| 214 |
new_public_policies = module.params["block_new_public_policies"] |
| 215 |
if module.params["block_public_access"] is None: |
| 216 |
public_access = current_account["block_public_access"] |
| 217 |
else: |
| 218 |
public_access = module.params["block_public_access"] |
| 219 |
new_account = { |
| 220 |
"hard_limit": hard_limit, |
| 221 |
"default_hard_limit": default_hard_limit, |
| 222 |
"quota": quota, |
| 223 |
"default_quota": default_quota, |
| 224 |
"block_new_public_policies": new_public_policies, |
| 225 |
"block_public_access": public_access, |
| 226 |
} |
| 227 |
else: |
| 228 |
new_account = { |
| 229 |
"hard_limit": module.params["hard_limit"], |
| 230 |
"default_hard_limit": module.params["default_hard_limit"], |
| 231 |
"quota": quota, |
| 232 |
"default_quota": default_quota, |
| 233 |
} |
| 234 |
if new_account != current_account: |
| 235 |
changed = True |
| 236 |
if not module.check_mode: |
| 237 |
if public: |
| 238 |
osa = ObjectStoreAccountPatch( |
| 239 |
hard_limit_enabled=new_account["hard_limit"], |
| 240 |
quota_limit=new_account["quota"], |
| 241 |
bucket_defaults=BucketDefaults( |
| 242 |
hard_limit_enabled=new_account["default_hard_limit"], |
| 243 |
quota_limit=new_account["default_quota"], |
| 244 |
), |
| 245 |
public_access_config=PublicAccessConfig( |
| 246 |
block_public_access=new_account["block_public_access"], |
| 247 |
block_new_public_policies=new_account[ |
| 248 |
"block_new_public_policies" |
| 249 |
], |
| 250 |
), |
| 251 |
) |
| 252 |
else: |
| 253 |
osa = ObjectStoreAccountPatch( |
| 254 |
hard_limit_enabled=new_account["hard_limit"], |
| 255 |
quota_limit=new_account["quota"], |
| 256 |
bucket_defaults=BucketDefaults( |
| 257 |
hard_limit_enabled=new_account["default_hard_limit"], |
| 258 |
quota_limit=new_account["default_quota"], |
| 259 |
), |
| 260 |
) |
| 261 |
if CONTEXT_API_VERSION in api_version: |
| 262 |
res = blade.patch_object_store_accounts( |
| 263 |
object_store_account=osa, |
| 264 |
names=[module.params["name"]], |
| 265 |
context_names=[module.params["context"]], |
| 266 |
) |
| 267 |
else: |
| 268 |
res = blade.patch_object_store_accounts( |
| 269 |
object_store_account=osa, names=[module.params["name"]] |
| 270 |
) |
| 271 |
if res.status_code != 200: |
| 272 |
module.fail_json( |
| 273 |
msg="Failed to update account {0}. " |
| 274 |
"Error: {1}".format(module.params["name"], res.errors[0].message) |
| 275 |
) |
| 276 |
|
| 277 |
module.exit_json(changed=changed) |
| 278 |
|
| 279 |
|
| 280 |
def create_s3acc(module, blade): |
| 281 |
"""Create Object Store Account""" |
| 282 |
changed = True |
| 283 |
api_version = list(blade.get_versions().items) |
| 284 |
if not module.check_mode: |
| 285 |
if CONTEXT_API_VERSION in api_version: |
| 286 |
res = blade.post_object_store_accounts( |
| 287 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 288 |
) |
| 289 |
else: |
| 290 |
res = blade.post_object_store_accounts(names=[module.params["name"]]) |
| 291 |
if res.status_code != 200: |
| 292 |
module.fail_json( |
| 293 |
msg="Object Store Account {0} creation failed. Error: {1}".format( |
| 294 |
module.params["name"], |
| 295 |
res.errors[0].message, |
| 296 |
) |
| 297 |
) |
| 298 |
if module.params["quota"] or module.params["default_quota"]: |
| 299 |
if not module.params["default_quota"]: |
| 300 |
default_quota = "" |
| 301 |
else: |
| 302 |
default_quota = str(human_to_bytes(module.params["default_quota"])) |
| 303 |
if not module.params["quota"]: |
| 304 |
quota = "" |
| 305 |
else: |
| 306 |
quota = str(human_to_bytes(module.params["quota"])) |
| 307 |
if not module.params["hard_limit"]: |
| 308 |
module.params["hard_limit"] = False |
| 309 |
if not module.params["default_hard_limit"]: |
| 310 |
module.params["default_hard_limit"] = False |
| 311 |
osa = ObjectStoreAccountPatch( |
| 312 |
hard_limit_enabled=module.params["hard_limit"], |
| 313 |
quota_limit=quota, |
| 314 |
bucket_defaults=BucketDefaults( |
| 315 |
hard_limit_enabled=module.params["default_hard_limit"], |
| 316 |
quota_limit=default_quota, |
| 317 |
), |
| 318 |
) |
| 319 |
if CONTEXT_API_VERSION in api_version: |
| 320 |
res = blade.patch_object_store_accounts( |
| 321 |
object_store_account=osa, |
| 322 |
names=[module.params["name"]], |
| 323 |
context_names=[module.params["context"]], |
| 324 |
) |
| 325 |
else: |
| 326 |
res = blade.patch_object_store_accounts( |
| 327 |
object_store_account=osa, names=[module.params["name"]] |
| 328 |
) |
| 329 |
if res.status_code != 200: |
| 330 |
if CONTEXT_API_VERSION in api_version: |
| 331 |
blade.object_store_accounts.delete_object_store_accounts( |
| 332 |
names=[module.params["name"]], |
| 333 |
context_names=[module.params["context"]], |
| 334 |
) |
| 335 |
else: |
| 336 |
blade.object_store_accounts.delete_object_store_accounts( |
| 337 |
names=[module.params["name"]] |
| 338 |
) |
| 339 |
module.fail_json( |
| 340 |
msg="Failed to set quotas correctly for account {0}. " |
| 341 |
"Error: {1}".format(module.params["name"], res.errors[0].message) |
| 342 |
) |
| 343 |
if PUBLIC_API_VERSION in api_version: |
| 344 |
if not module.params["block_new_public_policies"]: |
| 345 |
module.params["block_new_public_policies"] = False |
| 346 |
if not module.params["block_public_access"]: |
| 347 |
module.params["block_public_access"] = False |
| 348 |
osa = ObjectStoreAccountPatch( |
| 349 |
public_access_config=PublicAccessConfig( |
| 350 |
block_new_public_policies=module.params[ |
| 351 |
"block_new_public_policies" |
| 352 |
], |
| 353 |
block_public_access=module.params["block_public_access"], |
| 354 |
) |
| 355 |
) |
| 356 |
if CONTEXT_API_VERSION in api_version: |
| 357 |
res = blade.patch_object_store_accounts( |
| 358 |
object_store_account=osa, |
| 359 |
names=[module.params["name"]], |
| 360 |
context_names=[module.params["context"]], |
| 361 |
) |
| 362 |
else: |
| 363 |
res = blade.patch_object_store_accounts( |
| 364 |
object_store_account=osa, names=[module.params["name"]] |
| 365 |
) |
| 366 |
if res.status_code != 200: |
| 367 |
module.fail_json( |
| 368 |
msg="Failed to Public Access config correctly for account {0}. " |
| 369 |
"Error: {1}".format(module.params["name"], res.errors[0].message) |
| 370 |
) |
| 371 |
|
| 372 |
module.exit_json(changed=changed) |
| 373 |
|
| 374 |
|
| 375 |
def delete_s3acc(module, blade): |
| 376 |
"""Delete Object Store Account""" |
| 377 |
changed = True |
| 378 |
api_version = list(blade.get_versions().items) |
| 379 |
if not module.check_mode: |
| 380 |
if CONTEXT_API_VERSION in api_version: |
| 381 |
res = blade.get_object_store_users( |
| 382 |
names=[module.params["name"] + "/*'"], |
| 383 |
context_names=[module.params["context"]], |
| 384 |
) |
| 385 |
else: |
| 386 |
res = blade.get_object_store_users(names=[module.params["name"] + "/*'"]) |
| 387 |
if res.status_code == 200: |
| 388 |
module.fail_json( |
| 389 |
msg="Remove all Users from Object Store Account {0} \ |
| 390 |
before deletion".format( |
| 391 |
module.params["name"] |
| 392 |
) |
| 393 |
) |
| 394 |
else: |
| 395 |
if CONTEXT_API_VERSION in api_version: |
| 396 |
res = blade.delete_object_store_accounts( |
| 397 |
names=[module.params["name"]], |
| 398 |
context_names=[module.params["context"]], |
| 399 |
) |
| 400 |
else: |
| 401 |
res = blade.delete_object_store_accounts(names=[module.params["name"]]) |
| 402 |
if res.status_code != 200: |
| 403 |
module.fail_json( |
| 404 |
msg="Object Store Account {0} deletion failed. Error: {1}".format( |
| 405 |
module.params["name"], res.errors[0].message |
| 406 |
) |
| 407 |
) |
| 408 |
module.exit_json(changed=changed) |
| 409 |
|
| 410 |
|
| 411 |
def main(): |
| 412 |
argument_spec = purefb_argument_spec() |
| 413 |
argument_spec.update( |
| 414 |
dict( |
| 415 |
name=dict(required=True, type="str"), |
| 416 |
hard_limit=dict(type="bool"), |
| 417 |
default_hard_limit=dict(type="bool"), |
| 418 |
block_new_public_policies=dict(type="bool"), |
| 419 |
block_public_access=dict(type="bool"), |
| 420 |
quota=dict(type="str"), |
| 421 |
default_quota=dict(type="str"), |
| 422 |
state=dict(default="present", choices=["present", "absent"]), |
| 423 |
context=dict(type="str", default=""), |
| 424 |
) |
| 425 |
) |
| 426 |
|
| 427 |
module = AnsibleModule(argument_spec, supports_check_mode=True) |
| 428 |
|
| 429 |
state = module.params["state"] |
| 430 |
blade = get_system(module) |
| 431 |
|
| 432 |
if module.params["quota"] or module.params["default_quota"]: |
| 433 |
if not HAS_PURESTORAGE: |
| 434 |
module.fail_json(msg="py-pure-client sdk is required for to set quotas") |
| 435 |
|
| 436 |
upper = False |
| 437 |
for element in module.params["name"]: |
| 438 |
if element.isupper(): |
| 439 |
upper = True |
| 440 |
break |
| 441 |
if upper: |
| 442 |
module.warn("Changing account name to lowercase...") |
| 443 |
module.params["name"] = module.params["name"].lower() |
| 444 |
|
| 445 |
s3acc = get_s3acc(module, blade) |
| 446 |
|
| 447 |
if state == "absent" and s3acc: |
| 448 |
delete_s3acc(module, blade) |
| 449 |
elif state == "present" and s3acc: |
| 450 |
update_s3acc(module, blade) |
| 451 |
elif not s3acc and state == "present": |
| 452 |
create_s3acc(module, blade) |
| 453 |
else: |
| 454 |
module.exit_json(changed=False) |
| 455 |
|
| 456 |
|
| 457 |
if __name__ == "__main__": |
| 458 |
main() |