def test_serialize_when_blade_chassis_has_computer_system(self):
        blade_chassis = BladeChassis(
            self.server_hardware, self.manager_uuid
        )

        result = json.loads(blade_chassis.serialize())

        self.assertEqualMockup(self.blade_chassis_mockup, result)
Beispiel #2
0
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.
    """
    try:
        resource_index = g.oneview_client.index_resources.get_all(
            filter='uuid=' + uuid)

        if resource_index:
            category = resource_index[0]["category"]
        else:
            raise OneViewRedfishError('Cannot find Index resource')

        if category == 'server-hardware':
            server_hardware = g.oneview_client.server_hardware.get(uuid)
            etag = server_hardware['eTag']
            ch = BladeChassis(server_hardware)
        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)
        elif category == 'racks':
            racks = g.oneview_client.racks.get(uuid)
            etag = racks['eTag']
            ch = RackChassis(racks)
        else:
            raise OneViewRedfishError('Chassis type not found')

        json_str = ch.serialize()

        response = Response(response=json_str,
                            status=status.HTTP_200_OK,
                            mimetype="application/json")
        response.headers.add("ETag", "W/" + etag)
        return response
    except HPOneViewException as e:
        # In case of error log exception and abort
        logging.exception(e)
        abort(status.HTTP_404_NOT_FOUND)

    except OneViewRedfishError as e:
        # In case of error log exception and abort
        logging.exception('Unexpected error: {}'.format(e))
        abort(status.HTTP_404_NOT_FOUND, "Chassis not found")

    except Exception as e:
        # In case of error log exception and abort
        logging.exception('Unexpected error: {}'.format(e))
        abort(status.HTTP_500_INTERNAL_SERVER_ERROR)
    def test_serialize(self):
        # Tests the serialize function result against known result

        try:
            blade_chassis = BladeChassis(self.server_hardware)
        except Exception as e:
            self.fail("Failed to instantiate Chassis class."
                      " Error: {}".format(e))

        try:
            result = json.loads(blade_chassis.serialize())
        except Exception as e:
            self.fail("Failed to serialize. Error: ".format(e))

        self.assertEqual(self.blade_chassis_mockup, result)
    def test_class_instantiation(self):
        # Tests if class is correctly instantiated and validated

        try:
            blade_chassis = BladeChassis(self.server_hardware)
        except Exception as e:
            self.fail("Failed to instantiate Chassis class."
                      " Error: {}".format(e))
        self.assertIsInstance(blade_chassis, BladeChassis)
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 test_serialize_when_blade_chassis_has_not_computer_system(self):
        server_hardware = copy.deepcopy(self.server_hardware)
        server_hardware["serverProfileUri"] = None

        blade_chassis = BladeChassis(
            server_hardware, self.manager_uuid
        )
        result = json.loads(blade_chassis.serialize())

        expected_blade_result = copy.deepcopy(self.blade_chassis_mockup)
        expected_blade_result["Links"]["ComputerSystems"] = []

        self.assertEqualMockup(expected_blade_result, result)

        server_hardware["serverProfileUri"] = ""

        blade_chassis = BladeChassis(
            server_hardware, self.manager_uuid
        )
        result = json.loads(blade_chassis.serialize())

        self.assertEqualMockup(expected_blade_result, result)