def get_protection_policy_id_by_name(self, protection_policy_name): try: if protection_policy_name is None: return None if len(protection_policy_name) == 0: return '' if utils.name_or_id(protection_policy_name) == "NAME": # Get the protection policy details using name protection_policy_info = self.conn.protection.\ get_protection_policy_by_name(protection_policy_name) if protection_policy_info: if len(protection_policy_info) > 1: error_msg = 'Multiple protection policies by the ' \ 'same name found' LOG.error(error_msg) self.module.fail_json(msg=error_msg) return protection_policy_info[0]['id'] else: # Get the protection policy details using id if self.conn.protection.get_protection_policy_details( protection_policy_name): return protection_policy_name error_msg = ("protection policy {0} not found".format( protection_policy_name)) LOG.error(error_msg) self.module.fail_json(msg=error_msg) except Exception as e: error_msg = "Get protection policy: {0} failed with " \ "error: {1}".format(protection_policy_name, str(e)) LOG.error(error_msg) self.module.fail_json(msg=error_msg)
def get_host_group_id_by_name(self, host_group_name): try: if host_group_name is None: return None if utils.name_or_id(host_group_name) == "NAME": # Get the host group details using name host_group_info = self.provisioning.\ get_host_group_by_name(host_group_name) if host_group_info: if len(host_group_info) > 1: error_msg = 'Multiple host groups by the same name ' \ 'found' LOG.error(error_msg) self.module.fail_json(msg=error_msg) return host_group_info[0]['id'] else: # Get the host group details using id if self.provisioning.get_host_group_details(host_group_name): return host_group_name error_msg = ("host group {0} not found".format(host_group_name)) LOG.error(error_msg) self.module.fail_json(msg=error_msg) except Exception as e: error_msg = "Get host group: {0} failed with " \ "error: {1}".format(host_group_name, str(e)) LOG.error(error_msg) self.module.fail_json(msg=error_msg)
def create_host_list(self, hosts): ''' A function which takes the hosts list given by the user (which could be either name or ID of each host), and converts it to a list of IDs. ''' host_list = [] try: for host in hosts: # Since the ID format of PowerStore is UUID # check if host is host_id id_or_name = utils.name_or_id(val=host) if id_or_name == self.VALID_ID and self.get_host(host): host_list.append(host) else: # check if host is host_name id = self.get_host_id_by_name(host) if id: host_list.append(id) else: error_msg = ("Host {0} not found".format(host)) LOG.error(error_msg) self.module.fail_json(msg=error_msg) return host_list except Exception as e: error_msg = ("Host {0} not found, error={1}".format(host, str(e))) LOG.error(error_msg) self.module.fail_json(msg=error_msg)
def get_vol_group_id_from_vg(self, volume_group): """Maps the volume group to Volume Group ID""" is_valid_uuid = utils.name_or_id(volume_group) if is_valid_uuid == "ID": try: vg = self.provisioning.get_volume_group_details( volume_group_id=volume_group) return vg['id'] except Exception as e: LOG.info("No volume group found by ID: {0}, " "looking it up by name. Error {1}".format( volume_group, str(e))) pass try: vg = \ self.provisioning.get_volume_group_by_name(volume_group) if vg: return vg[0]['id'] else: self.module.fail_json(msg="Volume Group {0} was not found on " "the array.".format(volume_group)) except Exception as e: self.module.fail_json(msg="Failed to get volume group: " "{0} by name with error: " "{1}".format(volume_group, str(e)))
def get_nas_server_id(self, nas_server): """ Get the NAS Server ID :param nas_server: Name/ID of the NAS Server :return: ID of the NAS Server """ nas_server_id = nas_server if nas_server and utils.name_or_id(nas_server) == "NAME": try: nas_server_id = self.provisioning.get_nas_server_by_name( nas_server)[0]['id'] return nas_server_id except Exception as e: error_msg = "Failed to get details of NAS server {0} with" \ " error: {1}".format(nas_server, str(e)) LOG.error(error_msg) self.module.fail_json(msg=error_msg) else: try: nas_details = self.provisioning.get_nas_server_details( nas_server_id) return nas_details['id'] except Exception as e: error_msg = "Failed to get details of NAS Server" \ " {0} with error: {1}".format(nas_server_id, str(e)) LOG.error(error_msg) self.module.fail_json(msg=error_msg)
def is_export_parent_matched(export_parent, nfs_export_details): """Match the entered filesystem/snapshot details with the filesystem/snapshot retrieved from the array """ fs_id_from_details = nfs_export_details['file_system']['id'] fs_name_from_details = nfs_export_details['file_system']['name'] if nfs_export_details and export_parent: if utils.name_or_id(export_parent) == "ID" and \ fs_id_from_details != export_parent: return False if utils.name_or_id(export_parent) == "NAME" and \ fs_name_from_details != export_parent: return False return True
def get_vol_id_from_volume(self, volume): """Maps the volume to volume ID""" is_valid_uuid = utils.name_or_id(volume) if is_valid_uuid == "ID": try: vol = self.provisioning.get_volume_details(volume) return vol['id'] except Exception as e: LOG.info("No volume found by ID: {0}, " "looking it up by name. Error: {1}".format( volume, str(e))) pass try: vol = \ self.provisioning.get_volume_by_name(volume) if vol: return vol[0]['id'] else: self.module.fail_json(msg="Volume {0} was not found on " "the array.".format(volume)) except Exception as e: self.module.fail_json(msg="Failed to get vol {0} by " "name with error " "{1}".format(volume, str(e)))
def get_filesystem_id(self, smb_parent, snapshot, nas_server): """ Get the filesystem/snapshot ID. :param smb_parent: Name/ID of the filesystem/snapshot. :param snapshot: Name/ID of the Snapshot. :param nas_server: Name/ID of the NAS Server :return: ID of the filesystem/snapshot . """ file_system_id = smb_parent nas_server_id = self.get_nas_server_id(nas_server) fs_details = None if smb_parent and utils.name_or_id(smb_parent) == "NAME": try: fs_details = self.provisioning.get_filesystem_by_name( smb_parent, nas_server_id) if not fs_details: self.module.fail_json( msg="No File System/Snapshot found with " "Name {0}".format(smb_parent)) file_system_id = fs_details[0]['id'] except Exception as e: error_msg = "Failed to get details of File System/Snapshot" \ " {0} with error: {1}".format(smb_parent, str(e)) LOG.error(error_msg) self.module.fail_json(msg=error_msg) if snapshot and fs_details[0]['filesystem_type'] == 'Primary': self.module.fail_json(msg="Please enter a valid snapshot," " filesystem passed") if not snapshot and fs_details[0]['filesystem_type'] == 'Snapshot': self.module.fail_json(msg="Please enter a valid Filesystem," " snapshot passed") return file_system_id
def is_nas_server_matched(nas_server, nfs_export_details): """Match the entered NAS server details with the NAS server details retrieved from the array """ nas_id_from_details = \ nfs_export_details['file_system']['nas_server']['id'] nas_name_from_details = \ nfs_export_details['file_system']['nas_server']['name'] if nfs_export_details and nas_server: if utils.name_or_id(nas_server) == "ID" and \ nas_id_from_details != nas_server: return False if utils.name_or_id(nas_server) == "NAME" and \ nas_name_from_details != nas_server: return False return True
def is_match_smb_parent(smb_parent, smb_share_details): """ Whether the entered Filesystem/Snapshot and Filesystem/Snapshot in the smb_share_details is same or not. :param smb_parent: Filesystem/Snapshot for which smb share exists. :param smb_share_details: Details of the SMB share. """ fs_id_from_details = smb_share_details['file_system']['id'] fs_name_from_details = smb_share_details['file_system']['name'] if smb_share_details and smb_parent: if (utils.name_or_id(smb_parent) == 'ID' and fs_id_from_details != smb_parent): return False if utils.name_or_id(smb_parent) == 'NAME' and \ fs_name_from_details != smb_parent: return False return True
def is_match_nas(nas_server, smb_share_details): """ Whether the entered nas_server and nas_server in the smb_share_details is same or not :param nas_server: ID/Name of NAS Server :param smb_share_details: The details of the SMB share. """ nas_id_from_details = \ smb_share_details['file_system']['nas_server']['id'] nas_name_from_details = \ smb_share_details['file_system']['nas_server']['name'] if smb_share_details and nas_server: if (utils.name_or_id(nas_server) == "ID" and nas_id_from_details != nas_server): return False if utils.name_or_id(nas_server) == "NAME" and \ nas_name_from_details != nas_server: return False return True
def get_fs_id_from_filesystem(self, filesystem, nas_server): """Get the id of the filesystem.""" is_valid_uuid = utils.name_or_id(filesystem) try: if is_valid_uuid == "NAME": # Get the filesystem details using name nas_server_id = nas_server if nas_server is not None: is_valid_uuid = utils.name_or_id(nas_server) if is_valid_uuid == "ID": nas_server_id = self.get_nas_server_id( nas_server_id=nas_server) else: nas_server_id = self.get_nas_server_id( nas_server_name=nas_server) else: error_msg = "Please provide NAS Server details along " \ "with filesystem" LOG.error(error_msg) self.module.fail_json(msg=error_msg) fs = self.provisioning.get_filesystem_by_name( filesystem_name=filesystem, nas_server_id=nas_server_id) if fs: return fs[0]['id'] else: # Get the filesystem details using id fs = self.provisioning.get_filesystem_details(filesystem) return fs['id'] error_msg = "Filesystem {0} not found on the array.".format( filesystem) LOG.error(error_msg) self.module.fail_json(msg=error_msg) except Exception as e: error_msg = "Failed to get the filesystem {0} by name with " \ "error {1}".format(filesystem, str(e)) LOG.error(error_msg) self.module.fail_json(msg=error_msg)
def get_nas_server(self, nas_server): """Get the details of NAS Server of a given Powerstore storage system""" try: msg = 'Getting NAS Server details {0}'.format(nas_server) LOG.info(msg) id_or_name = utils.name_or_id(val=nas_server) if id_or_name == self.IS_NAME: nas_details = self.provisioning.get_nas_server_by_name( nas_server_name=nas_server) if nas_details: # implement in sdk , workaround nas_details = nas_details[0]['id'] else: nas_details = self.provisioning.get_nas_server_details( nas_server_id=nas_server) if nas_details: # implement in sdk , workaround nas_details = nas_details['id'] if nas_details: msg = 'Successfully got NAS Server details {0} from ' \ 'powerstore array name : {1} ,global id' \ ' : {2}'.format(nas_details, self.cluster_name, self.cluster_global_id) LOG.info(msg) return nas_details else: msg = 'Failed to get NAS Server with id or name {0} from ' \ 'powerstore system'.format(nas_server) self.module.fail_json(msg=msg) except Exception as e: msg = 'Get NAS Server {0} for powerstore array name : {1} , ' \ 'global id : {2} failed with error' \ ' {3} '.format(nas_server, self.cluster_name, self.cluster_global_id, str(e)) LOG.error(msg) self.module.fail_json(msg=msg)
def get_protection_policy(self, protection_policy): """Get protection policy""" try: msg = 'Getting the details of protection policy' \ ' {0}'.format(protection_policy) LOG.info(msg) id_or_name = utils.name_or_id(val=protection_policy) if id_or_name == self.IS_NAME: resp = self.protection.get_protection_policy_by_name( name=protection_policy) if resp and len(resp) > 0: pp_id = resp[0]['id'] msg = 'Successfully got the details of protection ' \ 'policy name is {0} and id is ' \ '{1}'.format(protection_policy, pp_id) LOG.info(msg) return pp_id else: detail_resp = self.protection.get_protection_policy_details( policy_id=protection_policy) if detail_resp: pp_id = detail_resp['id'] msg = 'Successfully got the details of protection' \ ' policy name {0} and id is' \ ' {1}'.format(protection_policy, pp_id) LOG.info(msg) return pp_id msg = 'No protection policy present with name or id {0}'. \ format(protection_policy) LOG.debug(msg) self.module.fail_json(msg=msg) except Exception as e: msg = 'Get details of protection policy name or ID : {0} ' \ 'failed with error : {1} '.format(protection_policy, str(e)) LOG.error(msg) self.module.fail_json(msg=msg)
def get_filesystem_id(self, filesystem, nas_server): """ Get the filesystem ID. :param filesystem: Name/ID of the filesystem. :param nas_server: Name/ID of the NAS Server :return: ID of the filesystem. """ file_system_id = filesystem if filesystem and utils.name_or_id(filesystem) == "NAME": if not nas_server: self.module.fail_json(msg="NAS Server Name/ID is required" " along with File System Name." " Please enter NAS Server Name/ID") nas_server_id = self.get_nas_server_id(nas_server) try: fs_details = self.provisioning.get_filesystem_by_name( filesystem, nas_server_id) if not fs_details: self.module.fail_json(msg="No File System found with " "Name {0}".format(filesystem)) return fs_details[0]['id'] except Exception as e: error_msg = "Failed to get details of File System" \ " {0} with error: {1}".format(filesystem, str(e)) LOG.error(error_msg) self.module.fail_json(msg=error_msg) else: try: fs_details = self.provisioning.get_filesystem_details( file_system_id) return fs_details['id'] except Exception as e: error_msg = "Failed to get details of File System" \ " {0} with error: {1}".format(file_system_id, str(e)) LOG.error(error_msg) self.module.fail_json(msg=error_msg)
def remove_volumes_from_volume_group(self, vg_id, vol_list): """Remove volumes from volume group""" vol_group_details = self.get_volume_group_details(vg_id=vg_id) existing_volumes_in_vg = vol_group_details['volumes'] LOG.debug("Existing Volumes: {0}".format(existing_volumes_in_vg)) existing_vol_ids = [] for vol in existing_volumes_in_vg: if vol: existing_vol_ids.append(vol['id']) LOG.debug("Existing Volume IDs {0}".format(existing_vol_ids)) ids_to_remove = [] vol_name_list = [] vol_id_list = [] for each_vol in vol_list: if each_vol: identifier_type = utils.name_or_id(each_vol) if identifier_type == "ID" and not (each_vol in vol_id_list): vol_id_list.append(each_vol) elif identifier_type == "NAME" and not (each_vol in vol_name_list): vol_name_list.append(each_vol) """remove by name""" for vol in vol_name_list: id = self.get_volume_id_by_name(vol) if id and (id in existing_vol_ids): if id not in ids_to_remove: ids_to_remove.append(id) else: msg = "Unable to remove volume name {0} since is not " \ "present in volume group {1}".format(vol, vg_id) LOG.warn(msg) self.module.fail_json(msg=msg) """remove by id""" for vol in vol_id_list: if vol in existing_vol_ids: if vol not in ids_to_remove: ids_to_remove.append(vol) else: msg = "Unable to remove volume id {0} since is not " \ "present in volume group {1}".format(vol, vg_id) LOG.warn(msg) self.module.fail_json(msg=msg) LOG.debug("Volume IDs to Remove {0}".format(ids_to_remove)) if len(ids_to_remove) == 0: return False try: self.provisioning.remove_members_from_volume_group( vg_id, ids_to_remove) return True except PowerStoreException as pe: errormsg = "Remove existing volume(s) from volume group {0} " \ "failed with error {1}".format(vg_id, str(pe)) LOG.error(errormsg) self.module.fail_json(msg=errormsg) except Exception as e: errormsg = "Remove existing volume(s) from volume group {0} " \ "failed with error {1}".format(vg_id, str(e)) LOG.error(errormsg) self.module.fail_json(msg=errormsg)
def add_volumes_to_volume_group(self, vg_id, vol_list): """adds volumes to volume group""" vol_group_details = self.get_volume_group_details(vg_id=vg_id) existing_volumes_in_vg = vol_group_details['volumes'] LOG.debug("Existing Volumes: {0}".format(existing_volumes_in_vg)) existing_vol_ids = [] for vol in existing_volumes_in_vg: if vol: existing_vol_ids.append(vol['id']) LOG.debug("Existing Volume IDs {0}".format(existing_vol_ids)) ids_to_add = [] vol_name_list = [] vol_id_list = [] for each_vol in vol_list: if each_vol: identifier_type = utils.name_or_id(each_vol) if identifier_type == "ID" and not (each_vol in vol_id_list): vol_id_list.append(each_vol) elif identifier_type == "NAME" and not (each_vol in vol_name_list): vol_name_list.append(each_vol) """add volume by name""" for vol in vol_name_list: id = self.get_volume_id_by_name(vol) if id and (id not in existing_vol_ids): if id not in ids_to_add: ids_to_add.append(id) else: msg = "Unable to add volume name {0}, either it doesn't" \ " exist or already in volume group ".format(vol) LOG.warn(msg) self.module.fail_json(msg=msg) """add volume by id""" for vol in vol_id_list: """verifying if volume id exists in array""" vol_by_id = self.get_volume_details_by_id(volume_id=vol) if vol_by_id not in existing_vol_ids: if vol_by_id not in ids_to_add: ids_to_add.append(vol_by_id) else: msg = "Unable to add volume id {0}, either it doesn't" \ " exist or already in volume group ".format(vol) LOG.warn(msg) self.module.fail_json(msg=msg) LOG.debug("Volume IDs to add {0}".format(ids_to_add)) if len(ids_to_add) == 0: return False try: self.provisioning.add_members_to_volume_group(vg_id, ids_to_add) return True except PowerStoreException as pe: errormsg = "Add existing volumes to volume group {0} " \ "failed with error {1}".format(vg_id, str(pe)) LOG.error(errormsg) self.module.fail_json(msg=errormsg) except Exception as e: errormsg = "Add existing volumes to volume group {0} " \ "failed with error {1}".format(vg_id, str(e)) LOG.error(errormsg) self.module.fail_json(msg=errormsg)
def validate_input(self, export_parent): """Validate the input parameters""" host_type_list = [ 'no_access_hosts', 'read_only_hosts', 'read_write_hosts', 'read_only_root_hosts', 'read_write_root_hosts' ] param_list = [ 'nfs_export_name', 'filesystem', 'snapshot', 'nas_server' ] # Check if sufficient parameters are provided along with # nfs_export_name if self.module.params['nfs_export_name']: if export_parent and (utils.name_or_id(export_parent) == "NAME" and not self.module.params['nas_server']): self.module.fail_json(msg="Please provide NAS server details " "to uniquely identify NFS export.") if not export_parent and not self.module.params['nas_server']: self.module.fail_json(msg="Please provide " "filesystem/snapshot/NAS server " "details to uniquely identify NFS " "Export.") for param in param_list: if self.module.params[param] and len( self.module.params[param].strip()) == 0: error_msg = "Please provide valid {0}".format(param) self.module.fail_json(msg=error_msg) # Check if valid FQDN/IP is provided regex = re.compile(r'[a-zA-Z0-9_/.-:@]+$') for host_type in host_type_list: if self.module.params[host_type]: for host in self.module.params[host_type]: if regex.match(host) is None: error_msg = "Along with alphanumeric characters, " \ "only special characters allowed are" \ " ., _, -, /, :, @" self.module.fail_json(msg=error_msg) if self.module.params['host_state'] and all( self.module.params[host_type] is None for host_type in host_type_list): error_msg = 'Host state is given but hosts are not specified.' LOG.error(error_msg) self.module.fail_json(msg=error_msg) if not self.module.params['host_state'] and any( self.module.params[host_type] is not None for host_type in host_type_list): error_msg = 'Hosts are given but host state is not specified.' LOG.error(error_msg) self.module.fail_json(msg=error_msg) # Check if valid description is provided if self.module.params['description'] is not None: if self.module.params['description'].strip() == "": self.module.fail_json(msg="Empty description or white spaced" " description is not allowed. " "Please enter a valid description") if self.module.params['description'] != \ self.module.params['description'].strip(): self.module.fail_json(msg="Description starting or ending " "with white spaces is not allowed. " "Please enter a valid description.")
def perform_module_operation(self): """ Perform different actions on SMB share based on user parameters chosen in playbook """ state = self.module.params['state'] share_id = self.module.params['share_id'] share_name = self.module.params['share_name'] path = self.module.params['path'] filesystem = self.module.params['filesystem'] snapshot = self.module.params['snapshot'] nas_server = self.module.params['nas_server'] description = self.module.params['description'] is_branch_cache_enabled = \ self.module.params['is_branch_cache_enabled'] is_continuous_availability_enabled = \ self.module.params['is_continuous_availability_enabled'] is_encryption_enabled = self.module.params['is_encryption_enabled'] umask = self.module.params['umask'] if umask: self.validate_umask(umask) is_abe_enabled = self.module.params['is_ABE_enabled'] = \ self.module.params['is_abe_enabled'] del self.module.params['is_abe_enabled'] offline_availability = self.module.params['offline_availability'] if offline_availability: offline_availability = \ self.module.params['offline_availability'] = \ offline_availability.title() changed = False smb_parent = filesystem if filesystem else snapshot if share_name: if smb_parent and not (utils.name_or_id(smb_parent) == "ID" or nas_server): self.module.fail_json( msg="share_name, filesystem/snapshot name given. Please" " enter nas_server to uniquely identify SMB Share") if not smb_parent and not nas_server: self.module.fail_json( msg="share_name provided," " filesystem/snapshot/nas_server also required.") ''' Get the Details of the SMB Share ''' smb_share_details = self.get_smb_share(share_id, share_name, smb_parent, nas_server, path) ''' Creation of SMB Share ''' if state == 'present' and not smb_share_details: LOG.info("Creating a SMB share") if share_id: err_msg = "SMB share with share_id {0} not found" \ .format(share_id) self.module.fail_json(msg=err_msg) if share_name and not smb_parent: self.module.fail_json(msg="Creation Failed," " filesystem/snapshot required.") # If Filesystem/Snapshot Name is passed # and NAS Server is not passed. smb_parent_id = smb_parent if utils.name_or_id(smb_parent) == "NAME": if not nas_server: self.module.fail_json(msg="File System/Snapshot name is" " passed, nas_server required for" " creation of SMB share.") smb_parent_id = self.get_filesystem_id(smb_parent, snapshot, nas_server) changed = self.create_smb_share( share_name, smb_parent_id, path, description, is_branch_cache_enabled, offline_availability, is_abe_enabled, is_continuous_availability_enabled, is_encryption_enabled, umask) ''' Update the SMB share details ''' if state == 'present' and smb_share_details: share_id = share_id if share_id else smb_share_details['id'] update_flag = self.to_update_smb_share(smb_share_details) if update_flag: LOG.info("Updating attributes of SMB share") changed = self.update_smb_share( share_id, share_name, description, umask, is_branch_cache_enabled, offline_availability, is_abe_enabled, is_continuous_availability_enabled, is_encryption_enabled) ''' Delete the SMB share details ''' if state == 'absent' and smb_share_details: LOG.info("Deleting SMB share") changed = self.delete_smb_share(smb_share_details) ''' Update the changed state and SMB share details ''' self.result["changed"] = changed if state == 'present': self.result["smb_share_details"] = \ self.get_smb_share(share_id, share_name, smb_parent, nas_server, path) self.module.exit_json(**self.result)
def perform_module_operation(self): """ Perform different actions on volume group based on user parameter chosen in playbook """ vg_id = self.module.params['vg_id'] state = self.module.params['state'] vg_name = self.module.params['vg_name'] volumes = self.module.params['volumes'] vol_state = self.module.params['vol_state'] new_vg_name = self.module.params['new_vg_name'] description = self.module.params['description'] protection_policy = self.module.params['protection_policy'] is_write_order_consistent = self.module.params[ 'is_write_order_consistent'] volume_group = None volume_group = self.get_volume_group_details(vg_id=vg_id, name=vg_name) LOG.debug('volume_group details: {0}'.format(volume_group)) if protection_policy: prot_pol_identifier_type = utils.name_or_id(protection_policy) if prot_pol_identifier_type == "ID": protection_policy = self.get_protection_policy_details_by_id( protection_policy) if prot_pol_identifier_type == "NAME": protection_policy = self.get_protection_policy_id_by_name( protection_policy) modified = False if volume_group: modified = self.is_volume_group_modified(volume_group, protection_policy) LOG.debug('Modified Flag: {0}'.format(modified)) else: if not vg_name: self.module.fail_json(msg="vg_name is required to " "create a Volume Group") if new_vg_name: self.module.fail_json(msg="Invalid argument, " "new_vg_name is not required") if vg_id is None and volume_group: vg_id = volume_group['id'] if vg_name is None and volume_group: vg_name = volume_group['name'] result = dict( changed=False, create_vg='', modify_vg='', add_vols_to_vg='', remove_vols_from_vg='', delete_vg='', volume_group_details='', ) if state == 'present' and not volume_group: LOG.info('Creating volume group {0}'.format(vg_name)) result['create_vg'], resp = self.\ create_volume_group(vg_name, description, protection_policy, is_write_order_consistent) result['volume_group_details'] = resp volume_group = self.get_volume_group_details(vg_id=resp['id']) vg_id = volume_group['id'] elif state == 'absent' and volume_group: LOG.info('Deleting volume group {0}'.format(vg_id)) result['delete_vg'] = self.delete_volume_group(vg_id) if state == 'present' and vol_state == 'present-in-group' and \ volume_group and volumes: result['add_vols_to_vg'] = self.add_volumes_to_volume_group( vg_id, volumes) elif state == 'present' and vol_state == 'absent-in-group' and \ volume_group and volumes: LOG.info('Remove existing volume(s) from volume group {0}'.format( vg_id)) result['remove_vols_from_vg'] = self.\ remove_volumes_from_volume_group(vg_id, volumes) if state == 'present' and volume_group and modified: LOG.info("From Modify : {0}".format(protection_policy)) result['modify_vg'] = self.modify_volume_group( vg_id, new_vg_name, description, is_write_order_consistent, protection_policy) if state == 'present' and volume_group: updated_vg = self.get_volume_group_details(vg_id=vg_id) result['volume_group_details'] = updated_vg if result['create_vg'] or result['modify_vg'] or result[ 'add_vols_to_vg'] or result['remove_vols_from_vg'] or \ result['delete_vg']: result['changed'] = True self.module.exit_json(**result)
def perform_module_operation(self): """collect input""" name = self.module.params['name'] new_name = self.module.params['new_name'] snapshotrules = self.module.params['snapshotrules'] description = self.module.params['description'] snapshotrule_state = self.module.params['snapshotrule_state'] state = self.module.params['state'] prot_pol_id = self.module.params['protectionpolicy_id'] result = dict(changed=False, protectionpolicy_details='') clusters = self.get_clusters() if len(clusters) > 0: self.cluster_name = clusters[0]['name'] self.cluster_global_id = clusters[0]['id'] else: msg = "Unable to find any active cluster on this array " LOG.error(msg) self.module.fail_json(msg=msg) if not prot_pol_id and not name: msg = "Either prot_pol_id or name is required" LOG.error(msg) self.module.fail_json(msg=msg) protection_pol = self.get_protection_policy_details(name, prot_pol_id) '''populating id and name at the begining''' if prot_pol_id is None and protection_pol: prot_pol_id = protection_pol['id'] if name is None and protection_pol: name = protection_pol['name'] snapshotrule_ids = [] """get snapshotrule id from name""" if snapshotrules: for each_snap in snapshotrules: entity_type = utils.name_or_id(each_snap) if entity_type == 'NAME': is_present, sn = self.get_snapshot_rule_details( name=each_snap) if is_present: snapshotrule_ids.append(sn) else: msg = "snapshot rule name: {0} is not found on" \ " the array".format(each_snap) LOG.error(msg) self.module.fail_json(msg=msg) if entity_type == 'ID': '''if a valid id''' is_present, sn = self.get_snapshot_rule_details( id=each_snap) if is_present: snapshotrule_ids.append(sn) else: msg = "snapshot rule id: {0} is not found on the " \ "array".format(each_snap) LOG.error(msg) self.module.fail_json(msg=msg) """create operation""" if not protection_pol and state == "present": if len(snapshotrule_ids) == 0 \ or snapshotrule_state != "present-in-policy": msg = "we must add atleast 1 snapshot rule to protection " \ "policy and snapshotrule_state must be set to " \ "present-in-policy" LOG.error(msg) self.module.fail_json(msg=msg) result['changed'], resp = \ self.create_protection_policy( name=name, description=description, snapshot_rule_ids=snapshotrule_ids) result['protectionpolicy_details'] = resp self.module.exit_json(**result) """delete operation""" if protection_pol and state == "absent": result['changed'] = self.delete_protection_policy( policy_id=prot_pol_id) self.module.exit_json(**result) """modify operation,add/remove snapshot rules""" if protection_pol and snapshotrule_state: protection_pol = self.get_protection_policy_details(id=prot_pol_id) if new_name: resp = self.get_protection_policy_details(name=new_name) if resp: msg = "Protection policy with name {0} already " \ "exist".format(new_name) LOG.error(msg) self.module.fail_json(msg=msg) present_sn_list = [] for s_rule in protection_pol['snapshot_rules']: present_sn_list.append(s_rule.get('id')) """add snapshot rules""" if snapshotrule_state == "present-in-policy": to_be_added = [] for eachrule in snapshotrule_ids: if eachrule not in present_sn_list: to_be_added.append(eachrule) if to_be_added and len(to_be_added) > 0: result['protectionpolicy_details'] = self\ .modify_protection_policy( policy_id=prot_pol_id, name=new_name, description=description, add_snapshot_rule_ids=to_be_added) result['changed'] = True self.module.exit_json(**result) '''remove snapshot rules''' if snapshotrule_state == "absent-in-policy": to_be_removed = [] for eachrule in snapshotrule_ids: if eachrule in present_sn_list: to_be_removed.append(eachrule) if to_be_removed and len(to_be_removed) > 0: result['protectionpolicy_details'] = \ self.modify_protection_policy( policy_id=prot_pol_id, name=new_name, description=description, remove_snapshot_rule_ids=to_be_removed) result['changed'] = True self.module.exit_json(**result) '''modify operation name and description''' if protection_pol and new_name and name != new_name: resp = self.get_protection_policy_details(name=new_name) if resp: msg = "Protection policy with name {0} already " \ "exist".format(new_name) LOG.error(msg) self.module.fail_json(msg=msg) result['protectionpolicy_details'] = self.\ modify_protection_policy( policy_id=prot_pol_id, name=new_name, description=description) result['changed'] = True self.module.exit_json(**result) '''modify only description''' if protection_pol and description and state == 'present': protection_pol = self.get_protection_policy_details(id=prot_pol_id) present_description = protection_pol.get('description') if present_description != description: result['protectionpolicy_details'] = self.\ modify_protection_policy( policy_id=prot_pol_id, description=description) result['changed'] = True self.module.exit_json(**result) '''display details of protection policy''' if prot_pol_id and state == "present": resp = self.get_protection_policy_details(id=prot_pol_id) result['changed'] = False result['protectionpolicy_details'] = resp self.module.exit_json(**result)