def test_get_odata_type_by_class(self):
        redfish_json_validator = RedfishJsonValidator('ServiceRoot')
        odata_type_schema_class = '#ServiceRoot.' + \
            service_root_version + '.ServiceRoot'

        zone_schema_name = 'Zone'
        odata_type_zone_schema = '#Zone.' + zone_version + '.Zone'

        self.assertEqual(redfish_json_validator.get_odata_type(),
                         odata_type_schema_class)
        self.assertEqual(
            redfish_json_validator.get_odata_type_by_schema(zone_schema_name),
            odata_type_zone_schema)
    def test_has_valid_config_file(self):
        # Tests if expected filed exists and are correctly populated by
        # the constructor

        redfish_json_validator = RedfishJsonValidator('ServiceRoot')
        self.assertIsInstance(redfish_json_validator.redfish,
                              collections.OrderedDict)
 def test_class_instantiation(self):
     # Tests if class is correctly instantiated
     try:
         redfish_json_validator = RedfishJsonValidator('ServiceRoot')
     except Exception as e:
         self.fail("Failed to instantiate RedfishJsonValidator class."
                   " Error: {}".format(e))
     self.assertIsInstance(redfish_json_validator, RedfishJsonValidator)
    def test_get_resource_by_id(self):
        redfish_json_validator = RedfishJsonValidator('ServiceRoot')

        # Loading server_hardware mockup value
        with open('oneview_redfish_toolkit/mockups/oneview/ServerHardware.json'
                  ) as f:
            server_hardware = json.load(f)

        device_slot = redfish_json_validator.get_resource_by_id(
            server_hardware["portMap"]["deviceSlots"], "deviceNumber", 3)

        json_device_slot = None

        for i in server_hardware["portMap"]["deviceSlots"]:
            if i["deviceNumber"] == 3:
                json_device_slot = i

        self.assertEqual(device_slot, json_device_slot)
示例#5
0
def create_composed_system():
    if not request.is_json:
        abort(status.HTTP_400_BAD_REQUEST,
              "The request content should be a valid JSON")

    body = request.get_json()
    result_location_uri = None

    try:
        RedfishJsonValidator.validate(body, 'ComputerSystem')
        service = ComputerSystemService(g.oneview_client)

        blocks = body["Links"]["ResourceBlocks"]
        block_ids = [block["@odata.id"].split("/")[-1] for block in blocks]

        # Should contain only one computer system entry
        system_blocks = _get_system_resource_blocks(block_ids)
        if not system_blocks:
            raise ValidationError(
                "Should have a Computer System Resource Block")

        system_block = system_blocks[0]
        service.validate_computer_system_resource_block_to_composition(
            system_block)

        # Check network block id with the Id attribute in the request
        network_blocks = _get_network_resource_blocks(block_ids)
        spt_id = body["Id"]

        if not (network_blocks and spt_id in network_blocks[0]["uri"]):
            raise ValidationError(
                "Should have a valid Network Resource Block")

        # It can contain zero or more Storage Block
        storage_blocks = _get_storage_resource_blocks(block_ids)

        spt = g.oneview_client.server_profile_templates.get(spt_id)

        server_profile = ComputerSystem.build_server_profile(
            body["Name"],
            body.get("Description"),
            spt,
            system_block,
            network_blocks,
            storage_blocks)

        service.power_off_server_hardware(system_block["uuid"],
                                          on_compose=True)

        task, resource_uri = service.create_composed_system(server_profile)

        if resource_uri:
            result_uuid = resource_uri.split("/")[-1]
            result_location_uri = ComputerSystem.BASE_URI + "/" + result_uuid
            server_profile_label = dict(
                resourceUri=resource_uri, labels=[spt_id.replace("-", " ")])
            g.oneview_client.labels.create(server_profile_label)
        elif task.get("taskErrors"):
            err_msg = reduce(
                lambda result, msg: result + msg["message"] + "\n",
                task["taskErrors"],
                "")
            abort(status.HTTP_403_FORBIDDEN, err_msg)

    except ValidationError as e:
        abort(status.HTTP_400_BAD_REQUEST, e.message)
    except KeyError as e:
        abort(status.HTTP_400_BAD_REQUEST,
              "Trying access an invalid key {}".format(e.args))
    except HPOneViewTaskError as e:
        abort(status.HTTP_403_FORBIDDEN, e.msg)

    if not result_location_uri:
        logging.error("It was not possible get the server profile URI when "
                      "creating a composed system")
        abort(status.HTTP_500_INTERNAL_SERVER_ERROR)

    return Response(status=status.HTTP_201_CREATED,
                    headers={"Location": result_location_uri},
                    mimetype="application/json")
    def test_get_resource_invalid_id(self):
        redfish_json_validator = RedfishJsonValidator('ServiceRoot')

        with self.assertRaises(OneViewRedfishError):
            redfish_json_validator.get_resource_by_id([], "deviceNumber",
                                                      "INVALID_ID")
    def test_get_resource_empty_list(self):
        redfish_json_validator = RedfishJsonValidator('ServiceRoot')

        with self.assertRaises(OneViewRedfishResourceNotFoundError):
            redfish_json_validator.get_resource_by_id([], "deviceNumber", 1)