def get_zone_collection():
    """Get the Redfish Zone Collection.

        Return ZoneCollection redfish JSON.

        Logs exception of any error and return Internal
        Server Error or Not Found.

        Returns:
            JSON: Redfish json with ZoneCollection.
    """

    server_profile_templates = \
        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_templates)

    zc = ZoneCollection(zone_ids)

    spt_count = len(server_profile_templates)
    zone_count = len(zc.redfish["Members"])
    logging_service.debug(
        COUNTER_LOGGER_NAME,
        "ServerProfileTemplates retrieved: " + str(spt_count),
        "Zones listed: " + str(zone_count))

    return ResponseBuilder.success(zc)
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 _build_computer_system_resource_block(uuid, server_hardware):
    eg_uri = server_hardware["serverGroupUri"]
    sht_uri = server_hardware["serverHardwareTypeUri"]

    filters = list()
    filters.append("enclosureGroupUri='{}'".format(eg_uri))
    filters.append("serverHardwareTypeUri='{}'".format(sht_uri))

    server_profile_templs = g.oneview_client \
        .server_profile_templates.get_all(filter=filters)

    zone_service = ZoneService(g.oneview_client)
    zone_ids = zone_service.get_zone_ids_by_templates(server_profile_templs)

    # Build ResourceBlock object and validates it
    return ServerHardwareResourceBlock(uuid, server_hardware, zone_ids)
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(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"]})