def test_create(self):
        with mock.patch.object(self.dbapi, 'create_volume_connector',
                               autospec=True) as mock_db_create:
            mock_db_create.return_value = self.volume_connector_dict
            new_connector = objects.VolumeConnector(
                self.context, **self.volume_connector_dict)
            new_connector.create()

            mock_db_create.assert_called_once_with(self.volume_connector_dict)
Exemple #2
0
    def post(self, connector):
        """Create a new volume connector.

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

        :returns: API-serializable volume connector object.

        :raises: OperationNotPermitted if accessed with specifying a parent
                 node.
        :raises: VolumeConnectorTypeAndIdAlreadyExists if a volume
                 connector already exists with the same type and connector_id
        :raises: VolumeConnectorAlreadyExists if a volume connector with the
                 same UUID already exists
        """
        context = api.request.context
        owner = None
        lessee = None
        raise_node_not_found = False
        node_uuid = connector.get('node_uuid')

        try:
            node = api_utils.replace_node_uuid_with_id(connector)
            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 connector.get('uuid'):
            connector['uuid'] = uuidutils.generate_uuid()

        new_connector = objects.VolumeConnector(context, **connector)

        notify.emit_start_notification(context, new_connector, 'create',
                                       node_uuid=node.uuid)
        with notify.handle_error_notification(context, new_connector,
                                              'create',
                                              node_uuid=node.uuid):
            new_connector.create()
        notify.emit_end_notification(context, new_connector, 'create',
                                     node_uuid=node.uuid)
        # Set the HTTP Location Header
        api.response.location = link.build_url('volume/connectors',
                                               new_connector.uuid)
        return convert_with_links(new_connector)
Exemple #3
0
def get_test_volume_connector(ctxt, **kw):
    """Return a VolumeConnector 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_connector = db_utils.get_test_volume_connector(**kw)
    # Let DB generate ID if it isn't specified explicitly
    if 'id' not in kw:
        del db_volume_connector['id']
    volume_connector = objects.VolumeConnector(ctxt)
    for key in db_volume_connector:
        setattr(volume_connector, key, db_volume_connector[key])
    return volume_connector
Exemple #4
0
    def post(self, connector):
        """Create a new volume connector.

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

        :returns: API-serializable volume connector object.

        :raises: OperationNotPermitted if accessed with specifying a parent
                 node.
        :raises: VolumeConnectorTypeAndIdAlreadyExists if a volume
                 connector already exists with the same type and connector_id
        :raises: VolumeConnectorAlreadyExists if a volume connector with the
                 same UUID already 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()

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

        new_connector = objects.VolumeConnector(context, **connector_dict)

        notify.emit_start_notification(context,
                                       new_connector,
                                       'create',
                                       node_uuid=connector.node_uuid)
        with notify.handle_error_notification(context,
                                              new_connector,
                                              'create',
                                              node_uuid=connector.node_uuid):
            new_connector.create()
        notify.emit_end_notification(context,
                                     new_connector,
                                     'create',
                                     node_uuid=connector.node_uuid)
        # Set the HTTP Location Header
        pecan.response.location = link.build_url('volume/connectors',
                                                 new_connector.uuid)
        return VolumeConnector.convert_with_links(new_connector)