def create_fs(module, blade): """Create Filesystem""" changed = True if not module.check_mode: try: if not module.params['size']: module.params['size'] = '32G' size = human_to_bytes(module.params['size']) nfsv3 = module.params['nfs'] if module.params['nfsv3'] is None else module.params['nfsv3'] if module.params['user_quota']: user_quota = human_to_bytes(module.params['user_quota']) else: user_quota = None if module.params['group_quota']: group_quota = human_to_bytes(module.params['group_quota']) else: group_quota = None api_version = blade.api_version.list_versions().versions if HARD_LIMIT_API_VERSION in api_version: if NFSV4_API_VERSION in api_version: fs_obj = FileSystem(name=module.params['name'], provisioned=size, fast_remove_directory_enabled=module.params['fastremove'], hard_limit_enabled=module.params['hard_limit'], snapshot_directory_enabled=module.params['snapshot'], nfs=NfsRule(v3_enabled=nfsv3, v4_1_enabled=module.params['nfsv4'], rules=module.params['nfs_rules']), smb=ProtocolRule(enabled=module.params['smb']), http=ProtocolRule(enabled=module.params['http']), default_user_quota=user_quota, default_group_quota=group_quota ) else: fs_obj = FileSystem(name=module.params['name'], provisioned=size, fast_remove_directory_enabled=module.params['fastremove'], hard_limit_enabled=module.params['hard_limit'], snapshot_directory_enabled=module.params['snapshot'], nfs=NfsRule(enabled=nfsv3, rules=module.params['nfs_rules']), smb=ProtocolRule(enabled=module.params['smb']), http=ProtocolRule(enabled=module.params['http']) ) else: fs_obj = FileSystem(name=module.params['name'], provisioned=size, fast_remove_directory_enabled=module.params['fastremove'], snapshot_directory_enabled=module.params['snapshot'], nfs=NfsRule(enabled=module.params['nfs'], rules=module.params['nfs_rules']), smb=ProtocolRule(enabled=module.params['smb']), http=ProtocolRule(enabled=module.params['http']) ) blade.file_systems.create_file_systems(fs_obj) except Exception: module.fail_json(msg="Failed to create filesystem {0}.".format(module.params['name'])) module.exit_json(changed=changed)
def delete_fs(module, blade): """ Delete Filesystem""" changed = True if not module.check_mode: try: api_version = blade.api_version.list_versions().versions if NFSV4_API_VERSION in api_version: blade.file_systems.update_file_systems(name=module.params['name'], attributes=FileSystem(nfs=NfsRule(v3_enabled=False, v4_1_enabled=False), smb=ProtocolRule(enabled=False), http=ProtocolRule(enabled=False), destroyed=True) ) else: blade.file_systems.update_file_systems(name=module.params['name'], attributes=FileSystem(nfs=NfsRule(enabled=False), smb=ProtocolRule(enabled=False), http=ProtocolRule(enabled=False), destroyed=True) ) if module.params['eradicate']: try: blade.file_systems.delete_file_systems(module.params['name']) except Exception: module.fail_json(msg="Failed to delete filesystem {0}.".format(module.params['name'])) except Exception: module.fail_json(msg="Failed to update filesystem {0} prior to deletion.".format(module.params['name'])) module.exit_json(changed=changed)
def modify_fs(module, blade): """Modify Filesystem""" changed = False attr = {} if not module.check_mode: fs = get_fs(module, blade) if fs.destroyed: attr['destroyed'] = False changed = True if module.params['size']: if human_to_bytes(module.params['size']) > fs.provisioned: attr['provisioned'] = human_to_bytes(module.params['size']) changed = True if module.params['nfs'] and not fs.nfs.enabled: attr['nfs'] = NfsRule(enabled=module.params['nfs']) changed = True if not module.params['nfs'] and fs.nfs.enabled: attr['nfs'] = NfsRule(enabled=module.params['nfs']) changed = True if module.params['nfs'] and fs.nfs.enabled: if fs.nfs.rules != module.params['nfs_rules']: attr['nfs'] = NfsRule(rules=module.params['nfs_rules']) changed = True if module.params['smb'] and not fs.smb.enabled: attr['smb'] = ProtocolRule(enabled=module.params['smb']) changed = True if not module.params['smb'] and fs.smb.enabled: attr['smb'] = ProtocolRule(enabled=module.params['smb']) changed = True if module.params['http'] and not fs.http.enabled: attr['http'] = ProtocolRule(enabled=module.params['http']) changed = True if not module.params['http'] and fs.http.enabled: attr['http'] = ProtocolRule(enabled=module.params['http']) changed = True if module.params['snapshot'] and not fs.snapshot_directory_enabled: attr['snapshot_directory_enabled'] = module.params['snapshot'] changed = True if not module.params['snapshot'] and fs.snapshot_directory_enabled: attr['snapshot_directory_enabled'] = module.params['snapshot'] changed = True if module.params['fastremove'] and not fs.fast_remove_directory_enabled: attr['fast_remove_directory_enabled'] = module.params['fastremove'] changed = True if not module.params['fastremove'] and fs.fast_remove_directory_enabled: attr['fast_remove_directory_enabled'] = module.params['fastremove'] changed = True api_version = blade.api_version.list_versions().versions if HARD_LIMIT_API_VERSION in api_version: if not module.params['hard_limit'] and fs.hard_limit_enabled: attr['hard_limit_enabled'] = module.params['hard_limit'] changed = True if changed: n_attr = FileSystem(**attr) try: blade.file_systems.update_file_systems( name=module.params['name'], attributes=n_attr) except: changed = False module.exit_json(changed=changed)
def _delete_fs(module, blade): """ In module Delete Filesystem""" api_version = blade.api_version.list_versions().versions if NFSV4_API_VERSION in api_version: blade.file_systems.update_file_systems( name=module.params['name'], attributes=FileSystem(nfs=NfsRule(v3_enabled=False, v4_1_enabled=False), smb=ProtocolRule(enabled=False), http=ProtocolRule(enabled=False), destroyed=True)) else: blade.file_systems.update_file_systems( name=module.params['name'], attributes=FileSystem(nfs=NfsRule(enabled=False), smb=ProtocolRule(enabled=False), http=ProtocolRule(enabled=False), destroyed=True)) blade.file_systems.delete_file_systems(module.params['name'])
def _delete_fs(module, blade): """ In module Delete Filesystem""" api_version = blade.api_version.list_versions().versions if NFSV4_API_VERSION in api_version: if MULTIPROTOCOL_API_VERSION in api_version: blade.file_systems.update_file_systems( name=module.params["name"], attributes=FileSystem( nfs=NfsRule(v3_enabled=False, v4_1_enabled=False), smb=ProtocolRule(enabled=False), http=ProtocolRule(enabled=False), multi_protocol=MultiProtocolRule( access_control_style="shared"), destroyed=True, ), ) else: blade.file_systems.update_file_systems( name=module.params["name"], attributes=FileSystem( nfs=NfsRule(v3_enabled=False, v4_1_enabled=False), smb=ProtocolRule(enabled=False), http=ProtocolRule(enabled=False), destroyed=True, ), ) else: blade.file_systems.update_file_systems( name=module.params["name"], attributes=FileSystem( nfs=NfsRule(enabled=False), smb=ProtocolRule(enabled=False), http=ProtocolRule(enabled=False), destroyed=True, ), ) blade.file_systems.delete_file_systems(module.params["name"])
def create_fs(module, blade): """Create Filesystem""" if not module.params['size']: module.params['size'] = '32G' size = human_to_bytes(module.params['size']) if not module.check_mode: try: api_version = blade.api_version.list_versions().versions if HARD_LIMIT_API_VERSION in api_version: fs_obj = FileSystem( name=module.params['name'], provisioned=size, fast_remove_directory_enabled=module.params['fastremove'], hard_limit_enabled=module.params['hard_limit'], snapshot_directory_enabled=module.params['snapshot'], nfs=NfsRule(enabled=module.params['nfs'], rules=module.params['nfs_rules']), smb=ProtocolRule(enabled=module.params['smb']), http=ProtocolRule(enabled=module.params['http'])) else: fs_obj = FileSystem( name=module.params['name'], provisioned=size, fast_remove_directory_enabled=module.params['fastremove'], snapshot_directory_enabled=module.params['snapshot'], nfs=NfsRule(enabled=module.params['nfs'], rules=module.params['nfs_rules']), smb=ProtocolRule(enabled=module.params['smb']), http=ProtocolRule(enabled=module.params['http'])) blade.file_systems.create_file_systems(fs_obj) changed = True except: changed = False module.exit_json(changed=changed)
def fs_delete(name, eradicate=False): """ Delete a share on a Pure Storage FlashBlade. Will return False if filesystem doesn't exist or is already in a deleted state. .. versionadded:: 2019.2.0 name : string name of filesystem eradicate : boolean (Optional) Eradicate filesystem after deletion if True. Default is False CLI Example: .. code-block:: bash salt '*' purefb.fs_delete foo eradicate=True """ blade = _get_blade() if _get_fs(name, blade) is not None: try: blade.file_systems.update_file_systems( name=name, attributes=FileSystem( nfs=NfsRule(enabled=False), smb=ProtocolRule(enabled=False), http=ProtocolRule(enabled=False), destroyed=True, ), ) except rest.ApiException: return False if eradicate is True: try: blade.file_systems.delete_file_systems(name) return True except rest.ApiException: return False else: return True else: return False
def restore_snapshot(module, blade): """Restore a filesystem back from the latest snapshot""" if not module.check_mode: snapname = get_latest_fssnapshot(module, blade) if snapname is not None: fs_attr = FileSystem(name=module.params['name'], source=Reference(name=snapname)) try: blade.file_systems.create_file_systems( overwrite=True, discard_non_snapshotted_data=True, file_system=fs_attr) changed = True except Exception: changed = False else: module.fail_json( msg='Filesystem {0} has no snapshots to restore from.'.format( module.params['name'])) module.exit_json(changed=changed)
def fs_update(name, rules, snapshot=False): ''' Update filesystem on a Pure Storage FlashBlade. Allows for change of NFS export rules and enabling/disabled of snapshotting capability. .. versionadded:: 2019.2.0 name : string name of filesystem rules : string NFS export rules for filesystem Refer to Pure Storage documentation for formatting rules. snapshot: boolean (Optional) Enable/Disable snapshots on the filesystem. Default is False CLI Example: .. code-block:: bash salt '*' purefb.fs_nfs_update foo rules='10.234.112.23(ro), 10.234.112.24(rw)' snapshot=True ''' blade = _get_blade() attr = {} _fs = _get_fs(name, blade) if _fs is not None: try: if _fs.nfs.enabled: attr['nfs'] = NfsRule(rules=rules) attr['snapshot_directory_enabled'] = snapshot n_attr = FileSystem(**attr) blade.file_systems.update_file_systems(name=name, attributes=n_attr) return True except rest.ApiException: return False else: return False
def delete_fs(module, blade): """ Delete Filesystem""" if not module.check_mode: try: blade.file_systems.update_file_systems( name=module.params['name'], attributes=FileSystem(nfs=NfsRule(enabled=False), smb=ProtocolRule(enabled=False), http=ProtocolRule(enabled=False), destroyed=True)) changed = True if module.params['eradicate']: try: blade.file_systems.delete_file_systems( module.params['name']) changed = True except: changed = False except: changed = False module.exit_json(changed=changed)
def fs_extend(name, size): ''' Resize an existing filesystem on a Pure Storage FlashBlade. Will return False if new size is less than or equal to existing size. .. versionadded:: 2019.2.0 name : string name of filesystem size : string New capacity of filesystem. Refer to Pure Storage documentation for formatting rules. CLI Example: .. code-block:: bash salt '*' purefb.fs_extend foo 10T ''' attr = {} blade = _get_blade() _fs = _get_fs(name, blade) if _fs is not None: if __utils__['stringutils.human_to_bytes'](size) > _fs.provisioned: try: attr['provisioned'] = __utils__['stringutils.human_to_bytes']( size) n_attr = FileSystem(**attr) blade.file_systems.update_file_systems(name=name, attributes=n_attr) return True except rest.ApiException: return False else: return False else: return False
fb = PurityFb(FB_MGMT) fb.disable_verify_ssl() try: fb.login(TOKEN) except rest.ApiException as e: print("Exception: %s\n" % e) try: # First, if the filesystem already exists, delete it. res = fb.file_systems.list_file_systems(names=[FS_NAME]) if len(res.items) == 1: print("Found existing filesystem {}, deleting.".format(FS_NAME)) fb.file_systems.update_file_systems(name=FS_NAME, attributes=FileSystem(nfs=NfsRule(v3_enabled=False))) fb.file_systems.update_file_systems(name=FS_NAME, attributes=FileSystem(destroyed=True)) fb.file_systems.delete_file_systems(name=FS_NAME) except rest.ApiException as e: print("Exception: %s\n" % e) try: print("Creating filesystem {}".format(FS_NAME)) fs_obj = FileSystem(name=FS_NAME, provisioned=FS_SIZE) fb.file_systems.create_file_systems(fs_obj) fb.file_systems.update_file_systems(name=FS_NAME, attributes=FileSystem(nfs=NfsRule(v3_enabled=True))) except rest.ApiException as e: print("Exception: %s\n" % e)
def fs_create(name, size=None, proto='NFS', nfs_rules='*(rw,no_root_squash)', snapshot=False): ''' Create a filesystem on a Pure Storage FlashBlade. Will return False if filesystem already exists. .. versionadded:: 2019.2.0 name : string name of filesystem (truncated to 63 characters) proto : string (Optional) Sharing protocol (NFS, CIFS or HTTP). If not specified default is NFS snapshot: boolean (Optional) Are snapshots enabled on the filesystem. Default is False nfs_rules : string (Optional) export rules for NFS. If not specified default is ``*(rw,no_root_squash)``. Refer to Pure Storage documentation for formatting rules. size : string if specified capacity of filesystem. If not specified default to 32G. Refer to Pure Storage documentation for formatting rules. CLI Example: .. code-block:: bash salt '*' purefb.fs_create foo proto=CIFS salt '*' purefb.fs_create foo size=10T ''' if len(name) > 63: name = name[0:63] blade = _get_blade() print(proto) if _get_fs(name, blade) is None: if size is None: size = __utils__['stringutils.human_to_bytes']('32G') else: size = __utils__['stringutils.human_to_bytes'](size) if proto.lower() == 'nfs': fs_obj = FileSystem( name=name, provisioned=size, fast_remove_directory_enabled=True, snapshot_directory_enabled=snapshot, nfs=NfsRule(enabled=True, rules=nfs_rules), ) elif proto.lower() == 'cifs': fs_obj = FileSystem( name=name, provisioned=size, fast_remove_directory_enabled=True, snapshot_directory_enabled=snapshot, smb=ProtocolRule(enabled=True), ) elif proto.lower() == 'http': fs_obj = FileSystem( name=name, provisioned=size, fast_remove_directory_enabled=True, snapshot_directory_enabled=snapshot, http=ProtocolRule(enabled=True), ) else: return False try: blade.file_systems.create_file_systems(fs_obj) return True except rest.ApiException: return False else: return False
def modify_fs(module, blade): """Modify Filesystem""" changed = False mod_fs = False attr = {} if module.params["policy"] and module.params["policy_state"] == "present": try: policy = blade.policies.list_policy_filesystems( policy_names=[module.params["policy"]], member_names=[module.params["name"]], ) except Exception: module.fail_json(msg="Policy {0} does not exist.".format( module.params["policy"])) if not policy.items: try: blade.policies.create_policy_filesystems( policy_names=[module.params["policy"]], member_names=[module.params["name"]], ) mod_fs = True except Exception: module.fail_json( msg="Failed to add filesystem {0} to policy {1}.".format( module.params["name"], module.params["polict"])) if module.params["policy"] and module.params["policy_state"] == "absent": try: policy = blade.policies.list_policy_filesystems( policy_names=[module.params["policy"]], member_names=[module.params["name"]], ) except Exception: module.fail_json(msg="Policy {0} does not exist.".format( module.params["policy"])) if len(policy.items) == 1: try: blade.policies.delete_policy_filesystems( policy_names=[module.params["policy"]], member_names=[module.params["name"]], ) mod_fs = True except Exception: module.fail_json( msg="Failed to remove filesystem {0} to policy {1}.". format(module.params["name"], module.params["polict"])) if module.params["user_quota"]: user_quota = human_to_bytes(module.params["user_quota"]) if module.params["group_quota"]: group_quota = human_to_bytes(module.params["group_quota"]) fsys = get_fs(module, blade) if fsys.destroyed: attr["destroyed"] = False mod_fs = True if module.params["size"]: if human_to_bytes(module.params["size"]) != fsys.provisioned: attr["provisioned"] = human_to_bytes(module.params["size"]) mod_fs = True api_version = blade.api_version.list_versions().versions if NFSV4_API_VERSION in api_version: if module.params["nfsv3"] and not fsys.nfs.v3_enabled: attr["nfs"] = NfsRule(v3_enabled=module.params["nfsv3"]) mod_fs = True if not module.params["nfsv3"] and fsys.nfs.v3_enabled: attr["nfs"] = NfsRule(v3_enabled=module.params["nfsv3"]) mod_fs = True if module.params["nfsv4"] and not fsys.nfs.v4_1_enabled: attr["nfs"] = NfsRule(v4_1_enabled=module.params["nfsv4"]) mod_fs = True if not module.params["nfsv4"] and fsys.nfs.v4_1_enabled: attr["nfs"] = NfsRule(v4_1_enabled=module.params["nfsv4"]) mod_fs = True if (module.params["nfsv3"] or module.params["nfsv4"] and fsys.nfs.v3_enabled or fsys.nfs.v4_1_enabled): if module.params["nfs_rules"] is not None: if fsys.nfs.rules != module.params["nfs_rules"]: attr["nfs"] = NfsRule(rules=module.params["nfs_rules"]) mod_fs = True if module.params[ "user_quota"] and user_quota != fsys.default_user_quota: attr["default_user_quota"] = user_quota mod_fs = True if module.params[ "group_quota"] and group_quota != fsys.default_group_quota: attr["default_group_quota"] = group_quota mod_fs = True else: if module.params["nfsv3"] and not fsys.nfs.enabled: attr["nfs"] = NfsRule(enabled=module.params["nfsv3"]) mod_fs = True if not module.params["nfsv3"] and fsys.nfs.enabled: attr["nfs"] = NfsRule(enabled=module.params["nfsv3"]) mod_fs = True if module.params["nfsv3"] and fsys.nfs.enabled: if fsys.nfs.rules != module.params["nfs_rules"]: attr["nfs"] = NfsRule(rules=module.params["nfs_rules"]) mod_fs = True if REPLICATION_API_VERSION in api_version: if module.params["smb"] and not fsys.smb.enabled: if MULTIPROTOCOL_API_VERSION in api_version: attr["smb"] = SmbRule(enabled=module.params["smb"]) else: attr["smb"] = SmbRule(enabled=module.params["smb"], acl_mode=module.params["smb_aclmode"]) mod_fs = True if not module.params["smb"] and fsys.smb.enabled: attr["smb"] = ProtocolRule(enabled=module.params["smb"]) mod_fs = True if (module.params["smb"] and fsys.smb.enabled and MULTIPROTOCOL_API_VERSION not in api_version): if fsys.smb.acl_mode != module.params["smb_aclmode"]: attr["smb"] = SmbRule(enabled=module.params["smb"], acl_mode=module.params["smb_aclmode"]) mod_fs = True else: if module.params["smb"] and not fsys.smb.enabled: attr["smb"] = ProtocolRule(enabled=module.params["smb"]) mod_fs = True if not module.params["smb"] and fsys.smb.enabled: attr["smb"] = ProtocolRule(enabled=module.params["smb"]) mod_fs = True if module.params["http"] and not fsys.http.enabled: attr["http"] = ProtocolRule(enabled=module.params["http"]) mod_fs = True if not module.params["http"] and fsys.http.enabled: attr["http"] = ProtocolRule(enabled=module.params["http"]) mod_fs = True if module.params["snapshot"] and not fsys.snapshot_directory_enabled: attr["snapshot_directory_enabled"] = module.params["snapshot"] mod_fs = True if not module.params["snapshot"] and fsys.snapshot_directory_enabled: attr["snapshot_directory_enabled"] = module.params["snapshot"] mod_fs = True if module.params["fastremove"] and not fsys.fast_remove_directory_enabled: attr["fast_remove_directory_enabled"] = module.params["fastremove"] mod_fs = True if not module.params["fastremove"] and fsys.fast_remove_directory_enabled: attr["fast_remove_directory_enabled"] = module.params["fastremove"] mod_fs = True if HARD_LIMIT_API_VERSION in api_version: if not module.params["hard_limit"] and fsys.hard_limit_enabled: attr["hard_limit_enabled"] = module.params["hard_limit"] mod_fs = True if module.params["hard_limit"] and not fsys.hard_limit_enabled: attr["hard_limit_enabled"] = module.params["hard_limit"] mod_fs = True if MULTIPROTOCOL_API_VERSION in api_version: if module.params[ "safeguard_acls"] and not fsys.multi_protocol.safeguard_acls: attr["multi_protocol"] = MultiProtocolRule(safeguard_acls=True) mod_fs = True if not module.params[ "safeguard_acls"] and fsys.multi_protocol.safeguard_acls: attr["multi_protocol"] = MultiProtocolRule(safeguard_acls=False) mod_fs = True if module.params[ "access_control"] != fsys.multi_protocol.access_control_style: attr["multi_protocol"] = MultiProtocolRule( access_control_style=module.params["access_control"]) mod_fs = True if REPLICATION_API_VERSION in api_version: if module.params["writable"] is not None: if not module.params["writable"] and fsys.writable: attr["writable"] = module.params["writable"] mod_fs = True if (module.params["writable"] and not fsys.writable and fsys.promotion_status == "promoted"): attr["writable"] = module.params["writable"] mod_fs = True if module.params["promote"] is not None: if module.params["promote"] and fsys.promotion_status != "promoted": attr["requested_promotion_state"] = "promoted" mod_fs = True if not module.params[ "promote"] and fsys.promotion_status == "promoted": # Demotion only allowed on filesystems in a replica-link try: blade.file_system_replica_links.list_file_system_replica_links( local_file_system_names=[module.params["name"] ]).items[0] except Exception: module.fail_json( msg="Filesystem {0} not demoted. Not in a replica-link" .format(module.params["name"])) attr["requested_promotion_state"] = module.params["promote"] mod_fs = True if mod_fs: changed = True if not module.check_mode: n_attr = FileSystem(**attr) if REPLICATION_API_VERSION in api_version: try: blade.file_systems.update_file_systems( name=module.params["name"], attributes=n_attr, discard_non_snapshotted_data=module. params["discard_snaps"], ) except rest.ApiException as err: message = json.loads(err.body)["errors"][0]["message"] module.fail_json( msg="Failed to update filesystem {0}. Error {1}". format(module.params["name"], message)) else: try: blade.file_systems.update_file_systems( name=module.params["name"], attributes=n_attr) except rest.ApiException as err: message = json.loads(err.body)["errors"][0]["message"] module.fail_json( msg="Failed to update filesystem {0}. Error {1}". format(module.params["name"], message)) module.exit_json(changed=changed)
def modify_fs(module, blade): """Modify Filesystem""" changed = True if not module.check_mode: mod_fs = False attr = {} if module.params['user_quota']: user_quota = human_to_bytes(module.params['user_quota']) if module.params['group_quota']: group_quota = human_to_bytes(module.params['group_quota']) fsys = get_fs(module, blade) if fsys.destroyed: attr['destroyed'] = False mod_fs = True if module.params['size']: if human_to_bytes(module.params['size']) != fsys.provisioned: attr['provisioned'] = human_to_bytes(module.params['size']) mod_fs = True api_version = blade.api_version.list_versions().versions if NFSV4_API_VERSION in api_version: if module.params['nfsv3'] and not fsys.nfs.v3_enabled: attr['nfs'] = NfsRule(v3_enabled=module.params['nfsv3']) mod_fs = True if not module.params['nfsv3'] and fsys.nfs.v3_enabled: attr['nfs'] = NfsRule(v3_enabled=module.params['nfsv3']) mod_fs = True if module.params['nfsv4'] and not fsys.nfs.v4_1_enabled: attr['nfs'] = NfsRule(v4_1_enabled=module.params['nfsv4']) mod_fs = True if not module.params['nfsv4'] and fsys.nfs.v4_1_enabled: attr['nfs'] = NfsRule(v4_1_enabled=module.params['nfsv4']) mod_fs = True if module.params['nfsv3'] or module.params['nfsv4'] and fsys.nfs.v3_enabled or fsys.nfs.v4_1_enabled: if fsys.nfs.rules != module.params['nfs_rules']: attr['nfs'] = NfsRule(rules=module.params['nfs_rules']) mod_fs = True if module.params['user_quota'] and user_quota != fsys.default_user_quota: attr['default_user_quota'] = user_quota mod_fs = True if module.params['group_quota'] and group_quota != fsys.default_group_quota: attr['default_group_quota'] = group_quota mod_fs = True else: if module.params['nfsv3'] and not fsys.nfs.enabled: attr['nfs'] = NfsRule(enabled=module.params['nfsv3']) mod_fs = True if not module.params['nfsv3'] and fsys.nfs.enabled: attr['nfs'] = NfsRule(enabled=module.params['nfsv3']) mod_fs = True if module.params['nfsv3'] and fsys.nfs.enabled: if fsys.nfs.rules != module.params['nfs_rules']: attr['nfs'] = NfsRule(rules=module.params['nfs_rules']) mod_fs = True if module.params['smb'] and not fsys.smb.enabled: attr['smb'] = ProtocolRule(enabled=module.params['smb']) mod_fs = True if not module.params['smb'] and fsys.smb.enabled: attr['smb'] = ProtocolRule(enabled=module.params['smb']) mod_fs = True if module.params['http'] and not fsys.http.enabled: attr['http'] = ProtocolRule(enabled=module.params['http']) mod_fs = True if not module.params['http'] and fsys.http.enabled: attr['http'] = ProtocolRule(enabled=module.params['http']) mod_fs = True if module.params['snapshot'] and not fsys.snapshot_directory_enabled: attr['snapshot_directory_enabled'] = module.params['snapshot'] mod_fs = True if not module.params['snapshot'] and fsys.snapshot_directory_enabled: attr['snapshot_directory_enabled'] = module.params['snapshot'] mod_fs = True if module.params['fastremove'] and not fsys.fast_remove_directory_enabled: attr['fast_remove_directory_enabled'] = module.params['fastremove'] mod_fs = True if not module.params['fastremove'] and fsys.fast_remove_directory_enabled: attr['fast_remove_directory_enabled'] = module.params['fastremove'] mod_fs = True api_version = blade.api_version.list_versions().versions if HARD_LIMIT_API_VERSION in api_version: if not module.params['hard_limit'] and fsys.hard_limit_enabled: attr['hard_limit_enabled'] = module.params['hard_limit'] mod_fs = True if module.params['hard_limit'] and not fsys.hard_limit_enabled: attr['hard_limit_enabled'] = module.params['hard_limit'] mod_fs = True if mod_fs: n_attr = FileSystem(**attr) try: blade.file_systems.update_file_systems(name=module.params['name'], attributes=n_attr) except Exception: module.fail_json(msg="Failed to update filesystem {0}.".format(module.params['name'])) else: changed = False module.exit_json(changed=changed)
def modify_fs(module, blade): """Modify Filesystem""" changed = True if not module.check_mode: changed = False mod_fs = False attr = {} if module.params['policy'] and module.params[ 'policy_state'] == 'present': try: policy = blade.policies.list_policy_filesystems( policy_names=[module.params['policy']], member_names=[module.params['name']]) except Exception: module.fail_json(msg='Policy {0} does not exist.'.format( module.params['policy'])) if not policy.items: try: blade.policies.create_policy_filesystems( policy_names=[module.params['policy']], member_names=[module.params['name']]) mod_fs = True except Exception: module.fail_json( msg='Failed to add filesystem {0} to policy {1}.'. format(module.params['name'], module.params['polict'])) if module.params['policy'] and module.params[ 'policy_state'] == 'absent': try: policy = blade.policies.list_policy_filesystems( policy_names=[module.params['policy']], member_names=[module.params['name']]) except Exception: module.fail_json(msg='Policy {0} does not exist.'.format( module.params['policy'])) if len(policy.items) == 1: try: blade.policies.delete_policy_filesystems( policy_names=[module.params['policy']], member_names=[module.params['name']]) mod_fs = True except Exception: module.fail_json( msg='Failed to remove filesystem {0} to policy {1}.'. format(module.params['name'], module.params['polict'])) if module.params['user_quota']: user_quota = human_to_bytes(module.params['user_quota']) if module.params['group_quota']: group_quota = human_to_bytes(module.params['group_quota']) fsys = get_fs(module, blade) if fsys.destroyed: attr['destroyed'] = False mod_fs = True if module.params['size']: if human_to_bytes(module.params['size']) != fsys.provisioned: attr['provisioned'] = human_to_bytes(module.params['size']) mod_fs = True api_version = blade.api_version.list_versions().versions if NFSV4_API_VERSION in api_version: if module.params['nfsv3'] and not fsys.nfs.v3_enabled: attr['nfs'] = NfsRule(v3_enabled=module.params['nfsv3']) mod_fs = True if not module.params['nfsv3'] and fsys.nfs.v3_enabled: attr['nfs'] = NfsRule(v3_enabled=module.params['nfsv3']) mod_fs = True if module.params['nfsv4'] and not fsys.nfs.v4_1_enabled: attr['nfs'] = NfsRule(v4_1_enabled=module.params['nfsv4']) mod_fs = True if not module.params['nfsv4'] and fsys.nfs.v4_1_enabled: attr['nfs'] = NfsRule(v4_1_enabled=module.params['nfsv4']) mod_fs = True if module.params['nfsv3'] or module.params[ 'nfsv4'] and fsys.nfs.v3_enabled or fsys.nfs.v4_1_enabled: if module.params['nfs_rules'] is not None: if fsys.nfs.rules != module.params['nfs_rules']: attr['nfs'] = NfsRule(rules=module.params['nfs_rules']) mod_fs = True if module.params[ 'user_quota'] and user_quota != fsys.default_user_quota: attr['default_user_quota'] = user_quota mod_fs = True if module.params[ 'group_quota'] and group_quota != fsys.default_group_quota: attr['default_group_quota'] = group_quota mod_fs = True else: if module.params['nfsv3'] and not fsys.nfs.enabled: attr['nfs'] = NfsRule(enabled=module.params['nfsv3']) mod_fs = True if not module.params['nfsv3'] and fsys.nfs.enabled: attr['nfs'] = NfsRule(enabled=module.params['nfsv3']) mod_fs = True if module.params['nfsv3'] and fsys.nfs.enabled: if fsys.nfs.rules != module.params['nfs_rules']: attr['nfs'] = NfsRule(rules=module.params['nfs_rules']) mod_fs = True if REPLICATION_API_VERSION in api_version: if module.params['smb'] and not fsys.smb.enabled: attr['smb'] = SmbRule(enabled=module.params['smb'], acl_mode=module.params['smb_aclmode']) mod_fs = True if not module.params['smb'] and fsys.smb.enabled: attr['smb'] = ProtocolRule(enabled=module.params['smb']) mod_fs = True if module.params['smb'] and fsys.smb.enabled: if fsys.smb.acl_mode != module.params['smb_aclmode']: attr['smb'] = SmbRule( enabled=module.params['smb'], acl_mode=module.params['smb_aclmode']) mod_fs = True else: if module.params['smb'] and not fsys.smb.enabled: attr['smb'] = ProtocolRule(enabled=module.params['smb']) mod_fs = True if not module.params['smb'] and fsys.smb.enabled: attr['smb'] = ProtocolRule(enabled=module.params['smb']) mod_fs = True if module.params['http'] and not fsys.http.enabled: attr['http'] = ProtocolRule(enabled=module.params['http']) mod_fs = True if not module.params['http'] and fsys.http.enabled: attr['http'] = ProtocolRule(enabled=module.params['http']) mod_fs = True if module.params['snapshot'] and not fsys.snapshot_directory_enabled: attr['snapshot_directory_enabled'] = module.params['snapshot'] mod_fs = True if not module.params['snapshot'] and fsys.snapshot_directory_enabled: attr['snapshot_directory_enabled'] = module.params['snapshot'] mod_fs = True if module.params[ 'fastremove'] and not fsys.fast_remove_directory_enabled: attr['fast_remove_directory_enabled'] = module.params['fastremove'] mod_fs = True if not module.params[ 'fastremove'] and fsys.fast_remove_directory_enabled: attr['fast_remove_directory_enabled'] = module.params['fastremove'] mod_fs = True if HARD_LIMIT_API_VERSION in api_version: if not module.params['hard_limit'] and fsys.hard_limit_enabled: attr['hard_limit_enabled'] = module.params['hard_limit'] mod_fs = True if module.params['hard_limit'] and not fsys.hard_limit_enabled: attr['hard_limit_enabled'] = module.params['hard_limit'] mod_fs = True if REPLICATION_API_VERSION in api_version: if module.params['writable'] is not None: if not module.params['writable'] and fsys.writable: attr['writable'] = module.params['writable'] mod_fs = True if module.params[ 'writable'] and not fsys.writable and fsys.promotion_status == 'promoted': attr['writable'] = module.params['writable'] mod_fs = True if module.params['promote'] is not None: if module.params[ 'promote'] and fsys.promotion_status != 'promoted': attr['requested_promotion_state'] = 'promoted' mod_fs = True if not module.params[ 'promote'] and fsys.promotion_status == 'promoted': # Demotion only allowed on filesystems in a replica-link try: blade.file_system_replica_links.list_file_system_replica_links( local_file_system_names=[module.params['name'] ]).items[0] except Exception: module.fail_json( msg= 'Filesystem {0} not demoted. Not in a replica-link' .format(module.params['name'])) attr['requested_promotion_state'] = module.params[ 'promote'] mod_fs = True if mod_fs: n_attr = FileSystem(**attr) try: blade.file_systems.update_file_systems( name=module.params['name'], attributes=n_attr) changed = True except Exception: module.fail_json(msg="Failed to update filesystem {0}.".format( module.params['name'])) module.exit_json(changed=changed)
def create_fs(module, blade): """Create Filesystem""" changed = True if not module.check_mode: try: if not module.params['nfs_rules']: module.params['nfs_rules'] = '*(rw,no_root_squash)' if not module.params['size']: module.params['size'] = '32G' size = human_to_bytes(module.params['size']) if module.params['user_quota']: user_quota = human_to_bytes(module.params['user_quota']) else: user_quota = None if module.params['group_quota']: group_quota = human_to_bytes(module.params['group_quota']) else: group_quota = None api_version = blade.api_version.list_versions().versions if HARD_LIMIT_API_VERSION in api_version: if NFSV4_API_VERSION in api_version: if REPLICATION_API_VERSION in api_version: fs_obj = FileSystem( name=module.params['name'], provisioned=size, fast_remove_directory_enabled=module. params['fastremove'], hard_limit_enabled=module.params['hard_limit'], snapshot_directory_enabled=module. params['snapshot'], nfs=NfsRule(v3_enabled=module.params['nfsv3'], v4_1_enabled=module.params['nfsv4'], rules=module.params['nfs_rules']), smb=SmbRule(enabled=module.params['smb'], acl_mode=module.params['smb_aclmode']), http=ProtocolRule(enabled=module.params['http']), default_user_quota=user_quota, default_group_quota=group_quota) else: fs_obj = FileSystem( name=module.params['name'], provisioned=size, fast_remove_directory_enabled=module. params['fastremove'], hard_limit_enabled=module.params['hard_limit'], snapshot_directory_enabled=module. params['snapshot'], nfs=NfsRule(v3_enabled=module.params['nfsv3'], v4_1_enabled=module.params['nfsv4'], rules=module.params['nfs_rules']), smb=ProtocolRule(enabled=module.params['smb']), http=ProtocolRule(enabled=module.params['http']), default_user_quota=user_quota, default_group_quota=group_quota) else: fs_obj = FileSystem( name=module.params['name'], provisioned=size, fast_remove_directory_enabled=module. params['fastremove'], hard_limit_enabled=module.params['hard_limit'], snapshot_directory_enabled=module.params['snapshot'], nfs=NfsRule(enabled=module.params['nfsv3'], rules=module.params['nfs_rules']), smb=ProtocolRule(enabled=module.params['smb']), http=ProtocolRule(enabled=module.params['http'])) else: fs_obj = FileSystem( name=module.params['name'], provisioned=size, fast_remove_directory_enabled=module.params['fastremove'], snapshot_directory_enabled=module.params['snapshot'], nfs=NfsRule(enabled=module.params['nfs'], rules=module.params['nfs_rules']), smb=ProtocolRule(enabled=module.params['smb']), http=ProtocolRule(enabled=module.params['http'])) blade.file_systems.create_file_systems(fs_obj) except Exception: module.fail_json(msg="Failed to create filesystem {0}.".format( module.params['name'])) if REPLICATION_API_VERSION in api_version: if module.params['policy']: try: blade.policies.list_policies( names=[module.params['policy']]) except Exception: _delete_fs(module, blade) module.fail_json(msg="Policy {0} doesn't exist.".format( module.params['policy'])) try: blade.policies.create_policy_filesystems( policy_names=[module.params['policy']], member_names=[module.params['name']]) except Exception: _delete_fs(module, blade) module.fail_json( msg= "Failed to apply policy {0} when creating filesystem {1}." .format(module.params['policy'], module.params['name'])) module.exit_json(changed=changed)
def create_fs(module, blade): """Create Filesystem""" changed = True if not module.check_mode: try: if not module.params["nfs_rules"]: module.params["nfs_rules"] = "*(rw,no_root_squash)" if module.params["size"]: size = human_to_bytes(module.params["size"]) else: size = 0 if module.params["user_quota"]: user_quota = human_to_bytes(module.params["user_quota"]) else: user_quota = None if module.params["group_quota"]: group_quota = human_to_bytes(module.params["group_quota"]) else: group_quota = None api_version = blade.api_version.list_versions().versions if HARD_LIMIT_API_VERSION in api_version: if NFSV4_API_VERSION in api_version: if REPLICATION_API_VERSION in api_version: if MULTIPROTOCOL_API_VERSION in api_version: if module.params[ "access_control"] == "nfs" and not ( module.params["nfsv3"] or module.params["nfsv4"]): module.fail_json( msg= "Cannot set access_control to nfs when NFS is not enabled." ) if (module.params["access_control"] in ["smb", "independent"] and not module.params["smb"]): module.fail_json( msg= "Cannot set access_control to smb or independent when SMB is not enabled." ) if module.params["safeguard_acls"] and ( module.params["access_control"] in ["mode-bits", "independent"] or module.params["smb"]): module.fail_json( msg= "ACL Safeguarding cannot be enabled with SMB or if access_control is mode-bits or independent." ) fs_obj = FileSystem( name=module.params["name"], provisioned=size, fast_remove_directory_enabled=module. params["fastremove"], hard_limit_enabled=module.params["hard_limit"], snapshot_directory_enabled=module. params["snapshot"], nfs=NfsRule( v3_enabled=module.params["nfsv3"], v4_1_enabled=module.params["nfsv4"], rules=module.params["nfs_rules"], ), smb=SmbRule(enabled=module.params["smb"]), http=ProtocolRule( enabled=module.params["http"]), multi_protocol=MultiProtocolRule( safeguard_acls=module. params["safeguard_acls"], access_control_style=module. params["access_control"], ), default_user_quota=user_quota, default_group_quota=group_quota, ) else: fs_obj = FileSystem( name=module.params["name"], provisioned=size, fast_remove_directory_enabled=module. params["fastremove"], hard_limit_enabled=module.params["hard_limit"], snapshot_directory_enabled=module. params["snapshot"], nfs=NfsRule( v3_enabled=module.params["nfsv3"], v4_1_enabled=module.params["nfsv4"], rules=module.params["nfs_rules"], ), smb=SmbRule( enabled=module.params["smb"], acl_mode=module.params["smb_aclmode"], ), http=ProtocolRule( enabled=module.params["http"]), default_user_quota=user_quota, default_group_quota=group_quota, ) else: fs_obj = FileSystem( name=module.params["name"], provisioned=size, fast_remove_directory_enabled=module. params["fastremove"], hard_limit_enabled=module.params["hard_limit"], snapshot_directory_enabled=module. params["snapshot"], nfs=NfsRule( v3_enabled=module.params["nfsv3"], v4_1_enabled=module.params["nfsv4"], rules=module.params["nfs_rules"], ), smb=ProtocolRule(enabled=module.params["smb"]), http=ProtocolRule(enabled=module.params["http"]), default_user_quota=user_quota, default_group_quota=group_quota, ) else: fs_obj = FileSystem( name=module.params["name"], provisioned=size, fast_remove_directory_enabled=module. params["fastremove"], hard_limit_enabled=module.params["hard_limit"], snapshot_directory_enabled=module.params["snapshot"], nfs=NfsRule( enabled=module.params["nfsv3"], rules=module.params["nfs_rules"], ), smb=ProtocolRule(enabled=module.params["smb"]), http=ProtocolRule(enabled=module.params["http"]), ) else: fs_obj = FileSystem( name=module.params["name"], provisioned=size, fast_remove_directory_enabled=module.params["fastremove"], snapshot_directory_enabled=module.params["snapshot"], nfs=NfsRule(enabled=module.params["nfs"], rules=module.params["nfs_rules"]), smb=ProtocolRule(enabled=module.params["smb"]), http=ProtocolRule(enabled=module.params["http"]), ) blade.file_systems.create_file_systems(fs_obj) except rest.ApiException as err: message = json.loads(err.body)["errors"][0]["message"] module.fail_json( msg="Failed to create filesystem {0}. Error: {1}".format( module.params["name"], message)) if REPLICATION_API_VERSION in api_version: if module.params["policy"]: try: blade.policies.list_policies( names=[module.params["policy"]]) except Exception: _delete_fs(module, blade) module.fail_json(msg="Policy {0} doesn't exist.".format( module.params["policy"])) try: blade.policies.create_policy_filesystems( policy_names=[module.params["policy"]], member_names=[module.params["name"]], ) except Exception: _delete_fs(module, blade) module.fail_json( msg= "Failed to apply policy {0} when creating filesystem {1}." .format(module.params["policy"], module.params["name"])) module.exit_json(changed=changed)