def create_bucket(module, blade): """Create bucket""" changed = False try: attr = Bucket() attr.account = Reference(name=module.params['account']) blade.buckets.create_buckets(names=[module.params['name']], account=attr) changed = True except Exception: module.fail_json(msg='Object Store Bucket {0}: Creation failed'.format(module.params['name'])) module.exit_json(changed=changed)
def create_bucket(module, blade): """Create bucket""" changed = True if not module.check_mode: try: api_version = blade.api_version.list_versions().versions if VERSIONING_VERSION in api_version: attr = BucketPost() attr.account = Reference(name=module.params["account"]) blade.buckets.create_buckets(names=[module.params["name"]], bucket=attr) else: attr = Bucket() attr.account = Reference(name=module.params["account"]) blade.buckets.create_buckets(names=[module.params["name"]], account=attr) if (module.params["versioning"] != "absent" and VERSIONING_VERSION in api_version): try: blade.buckets.update_buckets( names=[module.params["name"]], bucket=BucketPatch( versioning=module.params["versioning"]), ) except Exception: module.fail_json( msg= "Object Store Bucket {0} Created but versioning state failed" .format(module.params["name"])) except Exception: module.fail_json(msg="Object Store Bucket {0}: Creation failed". format(module.params["name"])) module.exit_json(changed=changed)
def delete_bucket(module, blade): """Delete Bucket""" changed = True if not module.check_mode: try: api_version = blade.api_version.list_versions().versions if VERSIONING_VERSION in api_version: blade.buckets.update_buckets( names=[module.params["name"]], bucket=BucketPatch(destroyed=True) ) else: blade.buckets.update_buckets( names=[module.params["name"]], destroyed=Bucket(destroyed=True) ) if module.params["eradicate"]: try: blade.buckets.delete_buckets(names=[module.params["name"]]) except Exception: module.fail_json( msg="Object Store Bucket {0}: Eradication failed".format( module.params["name"] ) ) except Exception: module.fail_json( msg="Object Store Bucket {0}: Deletion failed".format( module.params["name"] ) ) module.exit_json(changed=changed)
def recover_bucket(module, blade): """ Recover Bucket""" changed = False try: blade.buckets.update_buckets(names=[module.params['name']], destroyed=Bucket(destroyed=False)) changed = True except Exception: module.fail_json(msg='Object Store Bucket {0}: Recovery failed'.format(module.params['name'])) module.exit_json(changed=changed)
def recover_bucket(module, blade): """ Recover Bucket""" changed = True if not module.check_mode: try: api_version = blade.api_version.list_versions().versions if VERSIONING_VERSION in api_version: blade.buckets.update_buckets(names=[module.params['name']], bucket=BucketPatch(destroyed=False)) else: blade.buckets.update_buckets(names=[module.params['name']], destroyed=Bucket(destroyed=False)) except Exception: module.fail_json(msg='Object Store Bucket {0}: Recovery failed'.format(module.params['name'])) module.exit_json(changed=changed)
def delete_bucket(module, blade): """ Delete Bucket""" changed = False try: blade.buckets.update_buckets(names=[module.params['name']], destroyed=Bucket(destroyed=True)) changed = True if module.params['eradicate']: try: blade.buckets.delete_buckets(names=[module.params['name']]) changed = True except Exception: module.fail_json(msg='Object Store Bucket {0}: Eradication failed'.format(module.params['name'])) except Exception: module.fail_json(msg='Object Store Bucket {0}: Deletion failed'.format(module.params['name'])) module.exit_json(changed=changed)
def main(): parser = argparse.ArgumentParser() parser.add_argument("--account", default='datateam', help="Service account name.") parser.add_argument("--user", default='spark', help="Account user name.") parser.add_argument("--outfile", default='credentials', help="Output file for key credentials.") args = parser.parse_args() # Create PurityFb object for a certain array using environment variables. FB_MGMT = os.environ.get('FB_MGMT_VIP') TOKEN = os.environ.get('FB_MGMT_TOKEN') # Fail fast if necessary env variables not available. if not FB_MGMT or not TOKEN: print( "Requires environment variables for logging into FlashBlade REST: please set FB_MGMT_VIP and FB_MGMT_TOKEN" ) exit(1) # Step 1: login to the FlashBlade management API fb = PurityFb(FB_MGMT) fb.disable_verify_ssl() try: fb.login(TOKEN) except rest.ApiException as e: print("Exception: %s\n" % e) exit() # Step 2: Create service account try: res = fb.object_store_accounts.create_object_store_accounts( names=[args.account]) print("Creating service account {}".format(args.account)) except: print("Service account {} already exists".format(args.account)) # Step 3: Create user account accountuser = args.account + '/' + args.user try: # post the object store user object myobjuser on the array print("Creating user account {}".format(accountuser)) res = fb.object_store_users.create_object_store_users( names=[accountuser]) except: print("User %s creation failed.".format(accountuser)) # Step 4: Create access keys res = fb.object_store_access_keys.list_object_store_access_keys( filter="user.name=\'{}\'".format(accountuser)) if len(res.items) == 2: print("User {} cannot create more access keys.".format(accountuser)) exit(1) # generate access key and secret key for object store user # note: you need to handle the secret key since you can't retrieve it from the array after create accesskey = "" secretkey = "" try: res = fb.object_store_access_keys.create_object_store_access_keys( object_store_access_key={'user': { 'name': accountuser }}) accesskey = res.items[0].name secretkey = res.items[0].secret_access_key except rest.ApiException as e: print("Exception when creating object store access key: %s\n" % e) exit(1) # Step 5: Create bucket bucketname = args.user + "-working" print("Creating bucket %s\n" % bucketname) try: attr = Bucket() # Each bucket must be associated with a service account. attr.account = Reference(name=args.account) res = fb.buckets.create_buckets(names=[bucketname], account=attr) except rest.ApiException as e: print("Exception when creating bucket: %s\n" % e) # Output with open(args.outfile, "w") as outf: outf.write("AWS_ACCESS_KEY_ID={}\n".format(accesskey)) outf.write("AWS_SECRET_ACCESS_KEY_ID={}\n".format(secretkey)) print( "Access newly created bucket in Spark at s3a://{}/".format(bucketname)) fb.logout()