def get_network_adapter_collection(uuid): """Get the Redfish Network Adapters Collection. Return NetworkAdapterCollection Redfish JSON. """ server_hardware = g.oneview_client.server_hardware.get_by_id(uuid).data nic = NetworkAdapterCollection(server_hardware) return ResponseBuilder.success(nic)
def get_storage_collection(uuid): """Get the Redfish Storage Collection. Return StorageCollection Redfish JSON. """ # It just verifies if server profile there is in Oneview g.oneview_client.server_profiles.get(uuid) storage = StorageCollection(uuid) return ResponseBuilder.success(storage)
def get_network_interface_collection(server_profile_uuid): """Get the Redfish Network Interfaces Collection. Return NetworkInterfaceCollection Redfish JSON. """ profile = g.oneview_client.server_profiles.get(server_profile_uuid) server_hardware = g.oneview_client.server_hardware\ .get(profile["serverHardwareUri"]) nic = NetworkInterfaceCollection(profile, server_hardware) return ResponseBuilder.success(nic)
def get_subscription_collection(): """Get the Redfish Subscription Collection. Get method to return SubscriptionCollection JSON when /redfish/v1/EventService/EventSubscriptions is requested. Returns: JSON: JSON with EventSubscriptions. """ # Build Subscription Collection object and validates it sc = SubscriptionCollection(util.get_all_subscriptions()) return ResponseBuilder.success(sc)
def get_computer_system(uuid): """Get the Redfish Computer System for a given UUID. Return ComputerSystem redfish JSON for a given server profile or server profile template UUID. Returns: JSON: Redfish json with ComputerSystem When Server hardware or Server profilte templates is not found calls abort(404) """ resource = _get_oneview_resource(uuid) category = resource["category"] if category == 'server-profile-templates': computer_system_resource = CapabilitiesObject(resource) elif category == 'server-profiles': server_hardware = g.oneview_client.server_hardware\ .get_by_uri(resource["serverHardwareUri"]).data server_hardware_type = g.oneview_client.server_hardware_types\ .get_by_uri(resource['serverHardwareTypeUri']).data computer_system_service = ComputerSystemService(g.oneview_client) drives = _get_drives_from_sp(resource) spt_uuid = computer_system_service.\ get_server_profile_template_from_sp(resource["uri"]) # Get external storage volumes from server profile volumes_uris = [ volume["volumeUri"] for volume in resource["sanStorage"]["volumeAttachments"] ] # Emptying volume list to suppress external storage changes for # current release. # In future, remove this line to enable external storage support volumes_uris = [] manager_uuid = get_manager_uuid(resource['serverHardwareTypeUri']) # Build Computer System object and validates it computer_system_resource = ComputerSystem.build_composed_system( server_hardware, server_hardware_type, resource, drives, spt_uuid, manager_uuid, volumes_uris) else: abort(status.HTTP_404_NOT_FOUND, 'Computer System UUID {} not found'.format(uuid)) return ResponseBuilder.success(computer_system_resource, {"ETag": "W/" + resource["eTag"]})
def get_resource_block(uuid): """Get the Redfish ResourceBlock for a given UUID. Return ResourceBlock redfish JSON for a given UUID. Logs exception of any error and return Internal Server Error or Not Found. Returns: JSON: Redfish json with ResourceBlock. """ zone_service = ZoneService(g.oneview_client) resource = _get_oneview_resource(uuid) category = resource["category"] if category == "server-hardware": result_resource_block = _build_computer_system_resource_block( uuid, resource) elif category == "server-profile-templates": zone_ids = zone_service.get_zone_ids_by_templates([resource]) if not zone_ids: abort(status.HTTP_404_NOT_FOUND, "Zone not found to ResourceBlock {}".format(uuid)) result_resource_block = \ ServerProfileTemplateResourceBlock(uuid, resource, zone_ids) elif category == "drives": drive_uuid = resource["uri"].split("/")[-1] drive_index_trees_uri = \ "/rest/index/trees/rest/drives/{}?parentDepth=3" drive_index_trees = g.oneview_client.connection.get( drive_index_trees_uri.format(drive_uuid)) server_profile_templs = \ g.oneview_client.server_profile_templates.get_all() zone_ids = zone_service.get_zone_ids_by_templates( server_profile_templs) result_resource_block = StorageResourceBlock( resource, drive_index_trees, zone_ids) else: abort(status.HTTP_404_NOT_FOUND, 'Resource block not found') return ResponseBuilder.success( result_resource_block, {"ETag": "W/" + resource["eTag"]})
def get_zone(zone_uuid): """Get the Redfish Zone. Return Resource Zone redfish JSON. Logs exception of any error and return Internal Server Error or Not Found. Returns: JSON: Redfish json with Resource Zone. """ template_id, enclosure_id = ZoneService.\ split_zone_id_to_spt_uuid_and_enclosure_id(zone_uuid) profile_template = g.oneview_client.server_profile_templates.get( template_id) sh_type_uri = profile_template['serverHardwareTypeUri'] enclosure_name = None if enclosure_id: enclosure = g.oneview_client.enclosures.get(enclosure_id) enclosure_name = enclosure["name"] drives = _get_drives(enclosure) sh_filter = [ "locationUri='{}'".format(enclosure['uri']), "serverHardwareTypeUri='{}'".format(sh_type_uri) ] else: drives = [] enclosure_group_uri = profile_template["enclosureGroupUri"] sh_filter = [ "serverGroupUri='{}'".format(enclosure_group_uri), "serverHardwareTypeUri='{}'".format(sh_type_uri) ] server_hardware_list = g.oneview_client.server_hardware.get_all( filter=sh_filter) zone_data = Zone(zone_uuid, profile_template, server_hardware_list, enclosure_name, drives) sh_count = len(server_hardware_list) blocks_count = len(zone_data.redfish["Links"]["ResourceBlocks"]) logging_service.debug( COUNTER_LOGGER_NAME, "Drives retrieved: " + str(len(drives)), "ServerHardware retrieved: " + str(sh_count), "ResourceBlocks listed on Zone: " + str(blocks_count)) return ResponseBuilder.success(zone_data)
def get_processor_collection(uuid): """Get the Redfish Resource Block System Processor Collection. Get method to return Resource Block System Processor Collection JSON. Returns: JSON: JSON with Resource Block System Processor Collection info. """ server_hardware = g.oneview_client.server_hardware.get_by_id(uuid).data processor_collection = ProcessorCollection(server_hardware) return ResponseBuilder.success(processor_collection, {"ETag": "W/" + server_hardware["eTag"]})
def get_network_port_collection(server_hardware_uuid, device_id): """Get the Redfish Network Port Collection. Return NetworkPortCollection Redfish JSON. """ if device_id < 1: abort(status.HTTP_500_INTERNAL_SERVER_ERROR) server_hardware = g.oneview_client. \ server_hardware.get(server_hardware_uuid) npc = NetworkPortCollection(server_hardware, device_id) return ResponseBuilder.success(npc)
def get_chassis(uuid): """Get the Redfish Chassis. Get method to return Chassis JSON when /redfish/v1/Chassis/id is requested. Returns: JSON: JSON with Chassis. """ category = '' cached_category = category_resource.get_category_by_resource_id(uuid) if cached_category: category = cached_category.resource.replace('_', '-') else: resource_index = g.oneview_client.index_resources.get_all( filter='uuid=' + uuid ) if not resource_index: abort(status.HTTP_404_NOT_FOUND, "Chassis {} not found".format(uuid)) category = resource_index[0]["category"] manager_uuid = get_manager_uuid(uuid) if category == 'server-hardware': server_hardware = g.oneview_client.server_hardware.get(uuid) etag = server_hardware['eTag'] ch = BladeChassis(server_hardware, manager_uuid) elif category == 'enclosures': enclosure = g.oneview_client.enclosures.get(uuid) etag = enclosure['eTag'] enclosure_environment_config = g.oneview_client.enclosures. \ get_environmental_configuration(uuid) ch = EnclosureChassis( enclosure, enclosure_environment_config, manager_uuid ) elif category == 'racks': racks = g.oneview_client.racks.get(uuid) etag = racks['eTag'] ch = RackChassis(racks) return ResponseBuilder.success(ch, {"ETag": "W/" + etag})
def get_computer_system(uuid): """Get the Redfish Computer System for a given UUID. Return ComputerSystem redfish JSON for a given server profile or server profile template UUID. Returns: JSON: Redfish json with ComputerSystem When Server hardware or Server profilte templates is not found calls abort(404) """ resource = _get_oneview_resource(uuid) category = resource["category"] if category == 'server-profile-templates': computer_system_resource = CapabilitiesObject(resource) elif category == 'server-profiles': server_hardware = g.oneview_client.server_hardware\ .get(resource["serverHardwareUri"]) server_hardware_type = g.oneview_client.server_hardware_types\ .get(resource['serverHardwareTypeUri']) computer_system_service = ComputerSystemService(g.oneview_client) drives = _get_drives_from_sp(resource) spt_uuid = computer_system_service.\ get_server_profile_template_from_sp(resource["uri"]) manager_uuid = get_manager_uuid(resource['serverHardwareTypeUri']) # Build Computer System object and validates it computer_system_resource = ComputerSystem.build_composed_system( server_hardware, server_hardware_type, resource, drives, spt_uuid, manager_uuid) else: abort(status.HTTP_404_NOT_FOUND, 'Computer System UUID {} not found'.format(uuid)) return ResponseBuilder.success( computer_system_resource, {"ETag": "W/" + resource["eTag"]})
def get_manager_collection(): """Get the Redfish Manager Collection. Return ManagerCollection redfish JSON. Logs exception of any error and return Internal Server Error or Not Found. Returns: JSON: Redfish json with ManagerCollection. Exceptions: Exception: Generic error, logs the exception and call abort(500). """ oneview_appliances = multiple_oneview.get_map_appliances() mc = ManagerCollection(oneview_appliances) return ResponseBuilder.success(mc)
def post_session(): """Posts Session The response to the POST request to create a session includes: - An X-Auth-Token header that contains a session auth token that the client can use an subsequent requests. - A Location header that contains a link to the newly created session resource. - The JSON response body that contains a full representation of the newly created session object. Exception: HPOneViewException: Invalid username or password. return abort(401) """ try: try: body = request.get_json() username = body["UserName"] password = body["Password"] except Exception: error_message = "Invalid JSON key. The JSON request body " \ "must have the keys UserName and Password" abort(status.HTTP_400_BAD_REQUEST, error_message) token, session_id = client_session.login(username, password) sess = Session(session_id) if subscription.add_subscription_from_file(): scmb.init_event_service(token) return ResponseBuilder.success_201( sess, { "Location": sess.redfish["@odata.id"], "X-Auth-Token": token }) except HPOneViewException as e: logging.exception('Unauthorized error: {}'.format(e)) abort(status.HTTP_401_UNAUTHORIZED)
def get_resource_block_computer_system(uuid): """Get Computer System of a Resource Block Return ResourceBlock Computer System redfish JSON for a given UUID. Logs exception of any error and return Internal Server Error or Not Found. Returns: JSON: Redfish json with ResourceBlock Computer System. """ server_hardware = g.oneview_client.server_hardware.get_by_id(uuid).data manager_uuid = get_manager_uuid(uuid) computer_system = ComputerSystem.build_physical_system( server_hardware, manager_uuid) return ResponseBuilder.success(computer_system, {"ETag": "W/" + server_hardware["eTag"]})
def get_volumeCollection(uuid): """Get the Redfish Volume Collection for a given UUID. Return Volume Collection Redfish JSON for a given server profile UUID. Returns: JSON: Redfish json with Volume Collection When Volume Collection is not found calls abort(404) """ server_profile = g.oneview_client.server_profiles.get(uuid) if len(server_profile["localStorage"]["sasLogicalJBODs"]) == 0 and \ len(server_profile["sanStorage"]["volumeAttachments"]) == 0: abort(status.HTTP_404_NOT_FOUND, "Volumes not found") volume_details = VolumeCollection(server_profile) return ResponseBuilder.success(volume_details)
def get_vlan_network_interface_collection_sp(sp_uuid, connection_id): """Get Redfish VLanNetworkInterfaceCollection for the given UUID and ID. Return VLanNetworkInterfaceCollection redfish JSON for a given ServerProfile UUID and connection ID. Returns: JSON: Redfish json with VLanNetworkInterfaceCollection. """ server_profile = \ g.oneview_client.server_profiles.get_by_id(sp_uuid).data connection = \ _get_connection_oneview_resource(server_profile, connection_id) vlan_collection = \ _get_vlan_network_interface_collection(connection["networkUri"]) return ResponseBuilder.success(vlan_collection)
def get_network_interface(server_profile_uuid, device_id): """Get the Redfish NetworkInterface for a given UUID and device_id. Return NetworkInterface Redfish JSON for a given hardware UUID and device_id. Parameters: server_profile_uuid: the UUID of the server profile device_id: The id of the network device Returns: JSON: Redfish json with NetworkInterface Exceptions: When server profile or hardware is not found calls abort(404) When other errors occur calls abort(500) """ try: device_id_validation = int(device_id) profile = g.oneview_client.server_profiles.get_by_id( server_profile_uuid).data server_hardware = g.oneview_client.server_hardware\ .get_by_uri(profile["serverHardwareUri"]).data if (device_id_validation - 1) < 0 or (device_id_validation - 1) >= \ len(server_hardware["portMap"]["deviceSlots"]): abort(status.HTTP_404_NOT_FOUND, "Network interface id {} not found.".format(device_id)) ni = NetworkInterface(device_id, profile, server_hardware) return ResponseBuilder.success(ni) except ValueError: # Failed to convert device_id to int logging.exception( "Failed to convert device id {} to integer.".format(device_id)) abort(status.HTTP_404_NOT_FOUND, "Network interface not found")
def get_computer_system_collection(): """Get the Redfish Computer System Collection. Get method to return ComputerSystemCollection JSON when /redfish/v1/Systems is requested. Returns: JSON: JSON with ComputerSystemCollection. """ server_hardware_list = g.oneview_client.server_hardware.get_all( filter="NOT 'serverProfileUri' = NULL") server_profile_tmpls = \ g.oneview_client.server_profile_templates.get_all() zone_service = ZoneService(g.oneview_client) zone_ids = zone_service.get_zone_ids_by_templates(server_profile_tmpls) csc = ComputerSystemCollection(server_hardware_list, server_profile_tmpls, zone_ids) return ResponseBuilder.success(csc)
def get_resource_block_ethernet_interface(uuid, id): """Get the Redfish ResourceBlock of type Network for the given UUID and ID. Return ResourceBlock redfish JSON for a given UUID and ID. Logs exception of any error and return Internal Server Error or Not Found. Returns: JSON: Redfish json with ResourceBlock. """ try: server_profile_template = \ g.oneview_client.server_profile_templates.get(uuid) connSettings = server_profile_template["connectionSettings"] connection = None for conn in connSettings["connections"]: if str(conn["id"]) == id: connection = conn break if not connection: raise OneViewRedfishError("Ethernet interface not found") network = g.oneview_client.ethernet_networks.get(conn["networkUri"]) ethernet_interface = ResourceBlockEthernetInterface( server_profile_template, connection, network) return ResponseBuilder.success( ethernet_interface, {"ETag": "W/" + server_profile_template["eTag"]}) except OneViewRedfishError as e: # In case of error log exception and abort logging.exception('Unexpected error: {}'.format(e)) abort(status.HTTP_404_NOT_FOUND, e.msg)
def get_drive(profile_id, drive_id): """Get the Redfish Storage for a given UUID. Return Drive Redfish JSON for a given server profile UUID and Drive ID. Logs exception of any error and return abort(500) Internal Server Error. Returns: JSON: Redfish json with Storage When server profile is not found calls abort(404) Exceptions: Logs the exception and call abort(500) """ drive_id_int = None logical_jbod = None try: drive_id_int = int(drive_id) except ValueError: abort(status.HTTP_400_BAD_REQUEST, "Drive id should be a integer") server_profile = g.oneview_client.server_profiles.get_by_id( profile_id).data sas_logical_jbods = _find_sas_logical_jbods_by(server_profile) logical_jbod = _get_logical_jbod(drive_id_int, logical_jbod, sas_logical_jbods) if logical_jbod is None: abort(status.HTTP_404_NOT_FOUND, "Drive {} not found" .format(drive_id)) drive_details = Drive.build_for_computer_system(drive_id_int, server_profile, logical_jbod) return ResponseBuilder.success(drive_details)
def get_storage_details(resource_block_uuid, storage_id): """Get the Redfish Storage details of a Storage ResourceBlock for a given ID. Return Storage redfish JSON for a given ID. Logs exception of any error and return Internal Server Error or Not Found. Returns: JSON: Redfish json with Storage detail information. """ if str(storage_id) != FROZEN_ID: abort( status.HTTP_404_NOT_FOUND, "Storage {} not found for ResourceBlock {}".format( storage_id, resource_block_uuid)) drive = g.oneview_client.index_resources.get('/rest/drives/' + resource_block_uuid) result = Storage.build_for_resource_block(drive) return ResponseBuilder.success(result)
def get_vlan_network_interface_collection_spt(spt_uuid, connection_id): """Get Redfish VLanNetworkInterfaceCollection for the given UUID and ID. Return VLanNetworkInterfaceCollection redfish JSON for a given ServerProfileTemplate UUID and connection ID. Logs exception of any error and return Internal Server Error or Not Found. Returns: JSON: Redfish json with VLanNetworkInterfaceCollection. """ server_profile_template = \ g.oneview_client.server_profile_templates.get_by_id(spt_uuid).data connection = \ _get_connection_oneview_resource(server_profile_template, connection_id) vlan_collection = \ _get_vlan_network_interface_collection(connection["networkUri"]) return ResponseBuilder.success(vlan_collection)
def get_vlan_network_interface_spt(spt_uuid, conn_id, vlan_id): """Get the Redfish VLanNetworkInterface for the given UUID, ID and VLanID. Return VLanNetowrkInterface redfish JSON for a given ServerProfileTemplate UUID, EthernetInterface ID and a VLan ID. Logs exception of any error and return Internal Server Error or Not Found. Returns: JSON: Redfish json with ResourceBlock. """ server_profile_template = \ g.oneview_client.server_profile_templates.get_by_id(spt_uuid).data _get_connection_oneview_resource(server_profile_template, conn_id) ethernet_network = \ g.oneview_client.ethernet_networks.get_by_id(vlan_id).data vlan = VLanNetworkInterface(ethernet_network, request.path) return ResponseBuilder.success(vlan)
def get_resource_block_collection(): """Get the Redfish ResourceBlock Collection. Return ResourceBlockCollection redfish JSON. Logs exception of any error and return Internal Server Error or Not Found. Returns: JSON: Redfish json with ResourceBlockCollection. """ # Gets all server hardware server_hardware_list = g.oneview_client.server_hardware.get_all() server_profile_template_list = g.oneview_client.\ server_profile_templates.get_all() drives_list = g.oneview_client.index_resources \ .get_all(category="drives", count=10000) # Build ResourceBlockCollection object and validates it cc = ResourceBlockCollection(server_hardware_list, server_profile_template_list, drives_list) return ResponseBuilder.success(cc)
def get_zone(uuid): """Get the Redfish Zone. Return Resource Zone redfish JSON. Logs exception of any error and return Internal Server Error or Not Found. Returns: JSON: Redfish json with Resource Zone. """ profile_template = g.oneview_client.server_profile_templates.get(uuid) encl_group_uri = profile_template['enclosureGroupUri'] sh_type_uri = profile_template['serverHardwareTypeUri'] available_targets = g.oneview_client\ .server_profiles\ .get_available_targets(enclosureGroupUri=encl_group_uri, serverHardwareTypeUri=sh_type_uri) zone_data = Zone(profile_template, available_targets) return ResponseBuilder.success(zone_data)
def get_resource_block(uuid): """Get the Redfish ResourceBlock for a given UUID. Return ResourceBlock redfish JSON for a given UUID. Logs exception of any error and return Internal Server Error or Not Found. Returns: JSON: Redfish json with ResourceBlock. """ zone_service = ZoneService(g.oneview_client) resource = _get_oneview_resource(uuid) category = resource["category"] if category == "server-hardware": result_resource_block = _build_computer_system_resource_block( uuid, resource) elif category == "server-profile-templates": zone_ids = zone_service.get_zone_ids_by_templates([resource]) if not zone_ids: abort(status.HTTP_404_NOT_FOUND, "Zone not found to ResourceBlock {}".format(uuid)) result_resource_block = \ ServerProfileTemplateResourceBlock(uuid, resource, zone_ids) elif category == "drives": drive_uuid = resource["uri"].split("/")[-1] drive_index_trees_uri = \ "/rest/index/trees/rest/drives/{}?parentDepth=3" drive_index_trees = g.oneview_client.connection.get( drive_index_trees_uri.format(drive_uuid)) server_profile_templs = \ g.oneview_client.server_profile_templates.get_all() zone_ids = zone_service.get_zone_ids_by_templates( server_profile_templs) result_resource_block = StorageResourceBlock( resource, drive_index_trees, zone_ids, None) elif category == "storage-volumes": volumeUri = resource["uri"] # filter STP's with volume attached server_profile_templs = \ g.oneview_client.server_profile_templates.get_all() filter_spt = list(filter(lambda i: i["sanStorage"][ "volumeAttachments"], server_profile_templs)) spt_with_volume = [spt for spt in filter_spt for volume in spt["sanStorage"]["volumeAttachments"] if volume.get("volumeUri") == volumeUri] zone_ids = [] if spt_with_volume: zone_ids = zone_service.get_zone_ids_by_templates(spt_with_volume) # filter SP's with volume attached volume_attachments = g.oneview_client. \ storage_volume_attachments.get_all( filter="storageVolumeUri='{}'".format(volumeUri)) server_profiles = [i.get('ownerUri') for i in volume_attachments] result_resource_block = StorageResourceBlock( resource, None, zone_ids, server_profiles) else: abort(status.HTTP_404_NOT_FOUND, 'Resource block not found') return ResponseBuilder.success( result_resource_block, {"ETag": "W/" + resource["eTag"]})
def not_found(error): """Creates a Not Found Error response""" logging.error(error.description) return ResponseBuilder.error_404(error)
def forbidden(error): return ResponseBuilder.error_403(error)
def internal_server_error(error): """General InternalServerError handler for the app""" logging.error(error) return ResponseBuilder.error_500(error)
def oneview_redfish_exception(exception): logging.exception(exception) return ResponseBuilder.oneview_redfish_exception(exception)