Esempio n. 1
0
    def test_create(self):
        with mock.patch.object(self.dbapi,
                               'create_volume_target',
                               autospec=True) as mock_db_create:
            mock_db_create.return_value = self.volume_target_dict
            new_target = objects.VolumeTarget(self.context,
                                              **self.volume_target_dict)
            new_target.create()

            mock_db_create.assert_called_once_with(self.volume_target_dict)
Esempio n. 2
0
    def post(self, target):
        """Create a new volume target.

        :param target: a volume target within the request body.

        :returns: API-serializable volume target object.

        :raises: OperationNotPermitted if accessed with specifying a parent
                 node.
        :raises: VolumeTargetBootIndexAlreadyExists if a volume target already
                 exists with the same node ID and boot index
        :raises: VolumeTargetAlreadyExists if a volume target with the same
                 UUID exists
        """
        context = api.request.context
        raise_node_not_found = False
        node = None
        owner = None
        lessee = None
        node_uuid = target.get('node_uuid')
        try:
            node = api_utils.replace_node_uuid_with_id(target)
            owner = node.owner
            lessee = node.lessee
        except exception.NotFound:
            raise_node_not_found = True
        api_utils.check_owner_policy('node', 'baremetal:volume:create',
                                     owner, lessee=lessee,
                                     conceal_node=False)
        if raise_node_not_found:
            raise exception.InvalidInput(fieldname='node_uuid',
                                         value=node_uuid)

        if self.parent_node_ident:
            raise exception.OperationNotPermitted()

        # NOTE(hshiina): UUID is mandatory for notification payload
        if not target.get('uuid'):
            target['uuid'] = uuidutils.generate_uuid()
        new_target = objects.VolumeTarget(context, **target)

        notify.emit_start_notification(context, new_target, 'create',
                                       node_uuid=node.uuid)
        with notify.handle_error_notification(context, new_target, 'create',
                                              node_uuid=node.uuid):
            new_target.create()
        notify.emit_end_notification(context, new_target, 'create',
                                     node_uuid=node.uuid)
        # Set the HTTP Location Header
        api.response.location = link.build_url('volume/targets',
                                               new_target.uuid)
        return convert_with_links(new_target)
Esempio n. 3
0
def get_test_volume_target(ctxt, **kw):
    """Return a VolumeTarget object with appropriate attributes.

    NOTE: The object leaves the attributes marked as changed, such
    that a create() could be used to commit it to the DB.
    """
    db_volume_target = db_utils.get_test_volume_target(**kw)
    # Let DB generate ID if it isn't specified explicitly
    if 'id' not in kw:
        del db_volume_target['id']
    volume_target = objects.VolumeTarget(ctxt)
    for key in db_volume_target:
        setattr(volume_target, key, db_volume_target[key])
    return volume_target
    def post(self, target):
        """Create a new volume target.

        :param target: a volume target within the request body.

        :returns: API-serializable volume target object.

        :raises: OperationNotPermitted if accessed with specifying a parent
                 node.
        :raises: VolumeTargetBootIndexAlreadyExists if a volume target already
                 exists with the same node ID and boot index
        :raises: VolumeTargetAlreadyExists if a volume target with the same
                 UUID exists
        """
        context = pecan.request.context
        cdict = context.to_policy_values()
        policy.authorize('baremetal:volume:create', cdict, cdict)

        if self.parent_node_ident:
            raise exception.OperationNotPermitted()

        target_dict = target.as_dict()
        # NOTE(hshiina): UUID is mandatory for notification payload
        if not target_dict.get('uuid'):
            target_dict['uuid'] = uuidutils.generate_uuid()

        new_target = objects.VolumeTarget(context, **target_dict)

        notify.emit_start_notification(context,
                                       new_target,
                                       'create',
                                       node_uuid=target.node_uuid)
        with notify.handle_error_notification(context,
                                              new_target,
                                              'create',
                                              node_uuid=target.node_uuid):
            new_target.create()
        notify.emit_end_notification(context,
                                     new_target,
                                     'create',
                                     node_uuid=target.node_uuid)
        # Set the HTTP Location Header
        pecan.response.location = link.build_url('volume/targets',
                                                 new_target.uuid)
        return VolumeTarget.convert_with_links(new_target)