コード例 #1
0
def fake_db_req(**updates):
    ctxt = context.RequestContext('fake-user', 'fake-project')
    instance_uuid = uuidutils.generate_uuid()
    instance = fake_instance.fake_instance_obj(ctxt,
                                               objects.Instance,
                                               uuid=instance_uuid)
    # This will always be set this way for an instance at build time
    instance.host = None
    block_devices = objects.BlockDeviceMappingList(objects=[
        fake_block_device.fake_bdm_object(
            context,
            fake_block_device.FakeDbBlockDeviceDict(
                source_type='blank',
                destination_type='local',
                guest_format='foo',
                device_type='disk',
                disk_bus='',
                boot_index=1,
                device_name='xvda',
                delete_on_termination=False,
                snapshot_id=None,
                volume_id=None,
                volume_size=0,
                image_id='bar',
                no_device=False,
                connection_info=None,
                tag='',
                instance_uuid=uuids.instance))
    ])
    tags = objects.TagList(
        objects=[objects.Tag(tag='tag1', resource_id=instance_uuid)])
    db_build_request = {
        'id': 1,
        'project_id': 'fake-project',
        'instance_uuid': instance_uuid,
        'instance': jsonutils.dumps(instance.obj_to_primitive()),
        'block_device_mappings':
        jsonutils.dumps(block_devices.obj_to_primitive()),
        'tags': jsonutils.dumps(tags.obj_to_primitive()),
        'created_at': datetime.datetime(2016, 1, 16),
        'updated_at': datetime.datetime(2016, 1, 16),
    }

    for name, field in objects.BuildRequest.fields.items():
        if name in db_build_request:
            continue
        if field.nullable:
            db_build_request[name] = None
        elif field.default != fields.UnspecifiedDefault:
            db_build_request[name] = field.default
        else:
            raise Exception('fake_db_req needs help with %s' % name)

    if updates:
        db_build_request.update(updates)

    return db_build_request
コード例 #2
0
ファイル: server_tags.py プロジェクト: ychen2u/stx-nova
    def update(self, req, server_id, id, body):
        context = req.environ["nova.context"]
        context.can(st_policies.POLICY_ROOT % 'update')
        im = _get_instance_mapping(context, server_id)

        with nova_context.target_cell(context, im.cell_mapping) as cctxt:
            instance = self._check_instance_in_valid_state(
                cctxt, server_id, 'update tag')

        try:
            jsonschema.validate(id, parameter_types.tag)
        except jsonschema.ValidationError as e:
            msg = (_("Tag '%(tag)s' is invalid. It must be a non empty string "
                     "without characters '/' and ','. Validation error "
                     "message: %(err)s") % {
                         'tag': id,
                         'err': e.message
                     })
            raise webob.exc.HTTPBadRequest(explanation=msg)

        try:
            with nova_context.target_cell(context, im.cell_mapping) as cctxt:
                tags = objects.TagList.get_by_resource_id(cctxt, server_id)
        except exception.InstanceNotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.format_message())

        if len(tags) >= objects.instance.MAX_TAG_COUNT:
            msg = (_("The number of tags exceeded the per-server limit %d") %
                   objects.instance.MAX_TAG_COUNT)
            raise webob.exc.HTTPBadRequest(explanation=msg)

        if id in _get_tags_names(tags):
            # NOTE(snikitin): server already has specified tag
            return webob.Response(status_int=204)

        try:
            with nova_context.target_cell(context, im.cell_mapping) as cctxt:
                tag = objects.Tag(context=cctxt, resource_id=server_id, tag=id)
                tag.create()
                instance.tags = objects.TagList.get_by_resource_id(
                    cctxt, server_id)
        except exception.InstanceNotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.format_message())

        notifications_base.send_instance_update_notification(
            context, instance, service="nova-api")

        response = webob.Response(status_int=201)
        response.headers['Location'] = self._view_builder.get_location(
            req, server_id, id)
        return response
コード例 #3
0
ファイル: server_tags.py プロジェクト: youbun/nova
    def update(self, req, server_id, id, body):
        context = req.environ["nova.context"]
        authorize(context, action='update')
        self._check_instance_in_valid_state(context, server_id, 'update tag')

        try:
            jsonschema.validate(id, schema.tag)
        except jsonschema.ValidationError as e:
            msg = (_("Tag '%(tag)s' is invalid. It must be a string without "
                     "characters '/' and ','. Validation error message: "
                     "%(err)s") % {
                         'tag': id,
                         'err': e.message
                     })
            raise exc.HTTPBadRequest(explanation=msg)

        try:
            tags = objects.TagList.get_by_resource_id(context, server_id)
        except exception.InstanceNotFound as e:
            raise exc.HTTPNotFound(explanation=e.format_message())

        if len(tags) >= objects.instance.MAX_TAG_COUNT:
            msg = (_("The number of tags exceeded the per-server limit %d") %
                   objects.instance.MAX_TAG_COUNT)
            raise exc.HTTPBadRequest(explanation=msg)

        if len(id) > objects.tag.MAX_TAG_LENGTH:
            msg = (_("Tag '%(tag)s' is too long. Maximum length of a tag "
                     "is %(length)d") % {
                         'tag': id,
                         'length': objects.tag.MAX_TAG_LENGTH
                     })
            raise exc.HTTPBadRequest(explanation=msg)

        if id in _get_tags_names(tags):
            # NOTE(snikitin): server already has specified tag
            return exc.HTTPNoContent()

        tag = objects.Tag(context=context, resource_id=server_id, tag=id)

        try:
            tag.create()
        except exception.InstanceNotFound as e:
            raise exc.HTTPNotFound(explanation=e.format_message())

        response = exc.HTTPCreated()
        response.headers['Location'] = self._view_builder.get_location(
            req, server_id, id)
        return response