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 OneViewModuleValueError(self.MSG_MISSING_KEY) if not interconnects: raise OneViewModuleResourceNotFound( self.MSG_INTERCONNECT_NOT_FOUND) return interconnects[0]
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)) self.current_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) if not hostname and not self.data.get("name"): raise OneViewModuleValueError(self.MSG_MANDATORY_FIELDS_MISSING)
def __present(self): changed = False msg = '' if not self.current_resource: if 'credentials' not in self.data: raise OneViewModuleValueError(self.MSG_CREDENTIALS_MANDATORY) if self.oneview_client.api_version < 500: self.current_resource = self.resource_client.add( self.data['credentials']) else: options = self.data['credentials'].copy() options['family'] = self.data.get('family', None) options['hostname'] = self.data.get('hostname', None) self.current_resource = self.resource_client.add(options) changed = True msg = self.MSG_ADDED else: merged_data = self.current_resource.data.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 compare(self.current_resource.data, merged_data): # update the resource self.current_resource.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=self.current_resource.data))
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 OneViewModuleValueError(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, 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 OneViewModuleValueError( self.MSG_MISSING_MANDATORY_ATTRIBUTES) else: merged_data = resource.copy() merged_data.update(data) if not 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() elif self.state == 'multiple_servers_added': changed, msg, ansible_facts = self.__add_multiple_rack_mount_servers( ) else: if not self.data.get('name'): raise OneViewModuleValueError( 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 OneViewModuleResourceNotFound( 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 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 OneViewModuleValueError(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 __validate_key(self): if 'name' not in self.data: raise OneViewModuleValueError(self.MSG_KEY_REQUIRED) if 'logicalInterconnectUri' not in self.data and 'logicalInterconnectName' not in self.data: raise OneViewModuleValueError(self.MSG_KEY_REQUIRED)
def __present(self): server_template_name = self.data.pop('serverProfileTemplateName', '') server_hardware_name = self.data.pop('serverHardwareName', '') changed = False created = False ServerProfileReplaceNamesByUris().replace(self.oneview_client, self.data) if server_hardware_name: selected_server_hardware = self.__get_server_hardware_by_name( server_hardware_name) if not selected_server_hardware: raise OneViewModuleValueError( self.MSG_HARDWARE_NOT_FOUND.format(server_hardware_name)) self.data['serverHardwareUri'] = selected_server_hardware['uri'] if server_template_name: self.server_template = self.server_profile_templates.get_by_name( server_template_name) if not self.server_template: raise OneViewModuleValueError( self.MSG_TEMPLATE_NOT_FOUND.format(server_template_name)) self.data['serverProfileTemplateUri'] = self.server_template.data[ 'uri'] elif self.data.get('serverProfileTemplateUri'): self.server_template = self.server_profile_templates.get_by_uri( self.data['serverProfileTemplateUri']) if not self.current_resource: self.current_resource = self.__create_profile() 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 self.data.get('serverHardwareUri' ) is None and server_hardware_uri_exists: self.data['serverHardwareUri'] = None # Auto assigns a Server Hardware to Server Profile if auto_assign_server_hardware is True and no SH uris/enclosure uri and bay exist if not self.current_resource.data.get('serverHardwareUri') and not self.data.get('serverHardwareUri') and self.auto_assign_server_hardware \ and not self.current_resource.data.get('enclosureUri') and not self.current_resource.data.get('enclosureBay') \ and not self.data.get('enclosureUri') and not self.data.get('enclosureBay'): self.data[ 'serverHardwareUri'] = self._auto_assign_server_profile() merged_data = ServerProfileMerger().merge_data( self.current_resource.data, self.data) self.__validations_for_os_custom_attributes( merged_data, self.current_resource.data) # removed the below fields as part of idempotency checks updated_data = deepcopy(merged_data) updated_data.pop('initialScopeUris', None) if not compare(self.current_resource.data, updated_data): self.__update_server_profile(merged_data) changed = True msg = self.MSG_UPDATED else: msg = self.MSG_ALREADY_PRESENT return created, changed, msg, self.current_resource.data
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 OneViewModuleValueError( 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 OneViewModuleValueError( 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 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 __validate_settings(self, data): if 'ethernetSettings' not in data and 'fcoeSettings' not in data: raise OneViewModuleValueError(self.MSG_NO_OPTIONS_PROVIDED)
def __validate_options(self, subresource_type, data): if subresource_type not in data: raise OneViewModuleValueError(self.MSG_NO_OPTIONS_PROVIDED)
def __get_by(self, attribute): if not self.data.get(attribute): raise OneViewModuleValueError(self.MSG_MANDATORY_FIELD_MISSING) else: return self.get_by_name(self.data[attribute])
def __check_present_consistency(self, data): if data.get('osVolumeURI') and data.get('localImageFilePath'): raise OneViewModuleValueError(self.MSG_CANT_CREATE_AND_UPLOAD)