def __present(self, data, resource): server_template_name = data.pop('serverProfileTemplateName', '') server_hardware_name = data.pop('serverHardwareName', '') server_template = None changed = False created = False ServerProfileReplaceNamesByUris().replace(self.oneview_client, data) if server_hardware_name: selected_server_hardware = self.__get_server_hardware_by_name(server_hardware_name) if not selected_server_hardware: raise HPOneViewValueError(self.MSG_HARDWARE_NOT_FOUND.format(server_hardware_name)) data['serverHardwareUri'] = selected_server_hardware['uri'] if server_template_name: server_template = self.oneview_client.server_profile_templates.get_by_name(server_template_name) if not server_template: raise HPOneViewValueError(self.MSG_TEMPLATE_NOT_FOUND.format(server_template_name)) data['serverProfileTemplateUri'] = server_template['uri'] elif data.get('serverProfileTemplateUri'): server_template = self.oneview_client.server_profile_templates.get(data['serverProfileTemplateUri']) if not resource: resource = self.__create_profile(data, server_template) changed = True created = True msg = self.MSG_CREATED else: # This allows unassigning a profile if a SH key is specifically passed in as None if not self.auto_assign_server_hardware: server_hardware_uri_exists = False if 'serverHardwareUri' in self.module.params['data'].keys() or 'serverHardwareName' in self.module.params['data'].keys(): server_hardware_uri_exists = True if data.get('serverHardwareUri') is None and server_hardware_uri_exists: data['serverHardwareUri'] = None # Auto assigns a Server Hardware to Server Profile if auto_assign_server_hardware is True and no SH uris exist if not resource.get('serverHardwareUri') and not data.get('serverHardwareUri') and self.auto_assign_server_hardware: data['serverHardwareUri'] = self._auto_assign_server_profile(data, server_template) merged_data = ServerProfileMerger().merge_data(resource, data) self.__validations_for_os_custom_attributes(data, merged_data, resource) if not ResourceComparator.compare(resource, merged_data): resource = self.__update_server_profile(merged_data, resource) changed = True msg = self.MSG_UPDATED else: msg = self.MSG_ALREADY_PRESENT return created, changed, msg, resource
def execute_module(self): if self.data.get('connectionInfo'): for connection_hash in self.data.get('connectionInfo'): if connection_hash.get('name') == 'Host': resource_name = connection_hash.get('value') elif self.data.get('name'): resource_name = self.data.get('name') else: msg = 'A "name" or "connectionInfo" must be provided inside the "data" field for this operation. ' msg += 'If a "connectionInfo" is provided, the "Host" name is considered as the "name" for the resource.' raise HPOneViewValueError(msg.format()) resource = self.resource_client.get_by_name(resource_name) if self.state == 'present': changed, msg, san_manager = self.__present(resource) return dict(changed=changed, msg=msg, ansible_facts=dict(san_manager=san_manager)) elif self.state == 'absent': return self.resource_absent(resource, method='remove') elif self.state == 'connection_information_set': changed, msg, san_manager = self.__connection_information_set( resource) return dict(changed=changed, msg=msg, ansible_facts=dict(san_manager=san_manager))
def execute_module(self): changed, msg, ansible_facts = False, '', {} if self.state == 'discovered': changed, msg, ansible_facts = self.__discover(self.data) else: if not self.data.get('name'): raise HPOneViewValueError(self.MSG_MANDATORY_FIELD_MISSING) resource = self.get_by_name(self.data['name']) if self.state == 'present': return self.resource_present(resource, 'power_device', 'add') elif self.state == 'absent': return self.resource_absent(resource, 'remove') elif self.state == 'power_state_set': changed, msg, ansible_facts = self.__set_power_state( self.data, resource) elif self.state == 'refresh_state_set': changed, msg, ansible_facts = self.__set_refresh_state( self.data, resource) elif self.state == 'uid_state_set': changed, msg, ansible_facts = self.__set_uid_state( self.data, resource) return dict(changed=changed, msg=msg, ansible_facts=ansible_facts)
def __get_snapshot_by_name(self, resource, data): if 'name' not in data['snapshotParameters']: raise HPOneViewValueError(self.MSG_NO_OPTIONS_PROVIDED) result = self.resource_client.get_snapshot_by( resource['uri'], 'name', data['snapshotParameters']['name']) return result[0] if result else None
def __get_specific_attachment(self, params): attachment_uri = params.get('storageVolumeAttachmentUri') if attachment_uri: return [ self.oneview_client.storage_volume_attachments.get( attachment_uri) ] else: volume_uri = params.get('storageVolumeUri') profile_name = params.get('serverProfileName') if not profile_name or not (volume_uri or params.get('storageVolumeName')): raise HPOneViewValueError(self.ATTACHMENT_KEY_REQUIRED) if not volume_uri and params.get('storageVolumeName'): volumes = self.oneview_client.volumes.get_by( 'name', params.get('storageVolumeName')) if volumes: volume_uri = volumes[0]['uri'] uri = self.__search_attachment_uri.format(volume_uri, profile_name) attachments = self.oneview_client.storage_volume_attachments.get( uri) or {} return attachments.get('members')
def __present(self): if not self.data.get('hostname'): raise HPOneViewValueError( self.MSG_MANDATORY_FIELD_MISSING.format("data.hostname")) resource = self.__get_server_hardware(self.data['hostname']) scope_uris = self.data.pop('scopeUris', None) result = dict() if not resource: resource = self.oneview_client.server_hardware.add(self.data) result = dict(changed=True, msg=self.MSG_ADDED, ansible_facts={'server_hardware': resource}) else: result = dict(changed=False, msg=self.MSG_ALREADY_PRESENT, ansible_facts={'server_hardware': resource}) if scope_uris is not None: result = self.resource_scopes_set(result, 'server_hardware', scope_uris) return result
def __retrieve_differences(self, data, resource): if 'content' not in data: raise HPOneViewValueError(self.MSG_CONTENT_ATTRIBUTE_MANDATORY) differences = self.i3s_client.plan_scripts.retrieve_differences( resource['uri'], data['content']) return dict(changed=False, msg=self.MSG_DIFFERENCES_RETRIEVED, ansible_facts=dict(plan_script_differences=differences))
def __compliance(self): uris = self.data.get('logicalInterconnectUris') if not uris: if 'logicalInterconnectNames' not in self.data: raise HPOneViewValueError(self.MSG_NO_OPTIONS_PROVIDED) uris = self.__resolve_log_interconnect_names(self.data['logicalInterconnectNames']) self.resource_client.update_compliance_all(uris) return True, self.MSG_CONSISTENT, {}
def __get_provider_uri_by_display_name(self, data): display_name = data.get('providerDisplayName') provider_uri = self.resource_client.get_provider_uri(display_name) if not provider_uri: raise HPOneViewValueError( self.MSG_SAN_MANAGER_PROVIDER_DISPLAY_NAME_NOT_FOUND.format( display_name)) return provider_uri
def execute_module(self): if not self.data.get('name'): raise HPOneViewValueError(self.MSG_MANDATORY_FIELD_MISSING) resource = self.get_by_name(self.data['name']) if self.state == 'present': return self._present(self.data, resource) elif self.state == 'absent': return self.resource_absent(resource)
def __get_drive_enclosure(self, data): name = data.get('name') if not name: raise HPOneViewValueError(self.MSG_NAME_REQUIRED) else: result = self.oneview_client.drive_enclosures.get_by('name', name) if not result: raise HPOneViewResourceNotFound(self.MSG_NOT_FOUND) return result[0]
def __connection_information_set(self, resource): if not resource: return self.__present(resource) else: merged_data = resource.copy() merged_data.update(self.data) merged_data.pop('refreshState', None) if not self.data.get('connectionInfo', None): raise HPOneViewValueError( 'A connectionInfo field is required for this operation.') updated_san_manager = self.resource_client.update( resource=merged_data, id_or_uri=resource['uri']) return True, self.MSG_UPDATED, updated_san_manager
def execute_module(self): changed, msg, ansible_facts = False, '', {} if not self.data.get('name'): raise HPOneViewValueError(self.MSG_MANDATORY_FIELD_MISSING) resource = self.get_by_name(self.data['name']) if self.state == 'present': changed, msg, ansible_facts = self.__present(self.data, resource) return dict(changed=changed, msg=msg, ansible_facts=ansible_facts)
def __get_interconnect(self): interconnect_ip = self.module.params['ip'] interconnect_name = self.module.params['name'] if interconnect_ip: interconnects = self.oneview_client.interconnects.get_by('interconnectIP', interconnect_ip) or [] elif interconnect_name: interconnects = self.oneview_client.interconnects.get_by('name', interconnect_name) or [] else: raise HPOneViewValueError(self.MSG_MISSING_KEY) if not interconnects: raise HPOneViewResourceNotFound(self.MSG_INTERCONNECT_NOT_FOUND) return interconnects[0]
def __update(self, resource): new_name = self.data.pop('newName', None) if new_name: new_resource = self.get_by_name(new_name) if new_resource: raise HPOneViewValueError(self.MSG_NEW_NAME_INVALID) self.data['name'] = new_name merged_data = resource.copy() merged_data.update(self.data) updated_volume = self.resource_client.update(merged_data) return dict(changed=True, msg=self.MSG_UPDATED, ansible_facts=dict(storage_volume=updated_volume))
def execute_module(self): if self.data.get('uri'): resource = self.resource_client.get(self.data.get('uri')) elif self.data.get('name'): query = self.resource_client.get_all( filter="name='{}'".format(self.data.get('name'))) resource = query[0] if query and query[0].get( 'name') == self.data['name'] else None else: raise HPOneViewValueError(self.MSG_VALUE_ERROR) self.data['type'] = self.data.get('type', 'Subnet') if self.state == 'present': return self.resource_present(resource, 'id_pools_ipv4_subnet') elif self.state == 'absent': return self.resource_absent(resource)
def __get_resource_hostname(self, hostname_key, new_hostname_key): hostname = self.data.get(hostname_key, None) if 'credentials' in self.data and hostname is None: hostname = self.data['credentials'].get(hostname_key, None) if hostname: get_method = getattr(self.oneview_client.storage_systems, "get_by_{}".format(hostname_key)) resource = get_method(hostname) if self.data['credentials'].get(new_hostname_key): self.data['credentials'][hostname_key] = self.data[ 'credentials'].pop(new_hostname_key) elif self.data.get(new_hostname_key): self.data[hostname_key] = self.data.pop(new_hostname_key) return resource elif self.data.get('name'): return self.oneview_client.storage_systems.get_by_name( self.data['name']) else: raise HPOneViewValueError(self.MSG_MANDATORY_FIELDS_MISSING)
def __present(self, resource): changed = False msg = '' if not resource: if 'credentials' not in self.data: raise HPOneViewValueError(self.MSG_CREDENTIALS_MANDATORY) if self.oneview_client.api_version < 500: resource = self.oneview_client.storage_systems.add( self.data['credentials']) else: options = self.data['credentials'].copy() options['family'] = self.data.get('family', None) options['hostname'] = self.data.get('hostname', None) resource = self.oneview_client.storage_systems.add(options) changed = True msg = self.MSG_ADDED merged_data = resource.copy() merged_data.update(self.data) # remove password, it cannot be used in comparison if 'credentials' in merged_data and 'password' in merged_data[ 'credentials']: del merged_data['credentials']['password'] if not ResourceComparator.compare(resource, merged_data): # update the resource resource = self.oneview_client.storage_systems.update(merged_data) if not changed: changed = True msg = self.MSG_UPDATED else: msg = self.MSG_ALREADY_PRESENT return dict(changed=changed, msg=msg, ansible_facts=dict(storage_system=resource))
def __present(self, data, resource): changed = False msg = '' if "newName" in data: data["name"] = data.pop("newName") self.__replace_name_by_uris(data) self.__check_present_consistency(data) file_path = data.pop('localImageFilePath', None) if not resource: if data.get('osVolumeURI'): resource = self.i3s_client.golden_images.create(data) msg = self.MSG_CREATED changed = True elif file_path: resource = self.i3s_client.golden_images.upload(file_path, data) msg = self.MSG_UPLOADED changed = True else: raise HPOneViewValueError(self.MSG_MISSING_MANDATORY_ATTRIBUTES) else: merged_data = resource.copy() merged_data.update(data) if not ResourceComparator.compare(resource, merged_data): resource = self.i3s_client.golden_images.update(merged_data) changed = True msg = self.MSG_UPDATED else: msg = self.MSG_ALREADY_PRESENT return changed, msg, dict(golden_image=resource)
def execute_module(self): if self.state == 'present': return self.__present() else: if not self.data.get('name'): raise HPOneViewValueError( self.MSG_MANDATORY_FIELD_MISSING.format("data.name")) resource = self.__get_server_hardware(self.data['name']) if self.state == 'absent': return self.resource_absent(resource, method='remove') else: if not resource: raise HPOneViewResourceNotFound( self.MSG_SERVER_HARDWARE_NOT_FOUND) if self.state == 'power_state_set': changed, msg, ansible_facts = self.__set_power_state( resource) elif self.state == 'refresh_state_set': changed, msg, ansible_facts = self.__set_refresh_state( resource) elif self.state == 'ilo_firmware_version_updated': changed, msg, ansible_facts = self.__update_mp_firmware_version( resource) elif self.state == 'environmental_configuration_set': changed, msg, ansible_facts = self.__set_environmental_configuration( resource) else: changed, msg, ansible_facts = self.__patch(resource) return dict(changed=changed, msg=msg, ansible_facts=ansible_facts)
def __check_present_consistency(self, data): if data.get('osVolumeURI') and data.get('localImageFilePath'): raise HPOneViewValueError(self.MSG_CANT_CREATE_AND_UPLOAD)
def __get_by(self, attribute): if not self.data.get(attribute): raise HPOneViewValueError(self.MSG_MANDATORY_FIELD_MISSING) else: return self.get_by_name(self.data[attribute])
def __validate_key(self): if 'name' not in self.data: raise HPOneViewValueError(self.MSG_KEY_REQUIRED) if 'logicalInterconnectUri' not in self.data and 'logicalInterconnectName' not in self.data: raise HPOneViewValueError(self.MSG_KEY_REQUIRED)