示例#1
0
    def delete(self, req, id):
        """Delete an server group."""
        context = _authorize_context(req)
        try:
            sg = objects.InstanceGroup.get_by_uuid(context, id)
        except nova.exception.InstanceGroupNotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.format_message())

        quotas = objects.Quotas(context=context)
        project_id, user_id = objects.quotas.ids_from_server_group(context, sg)
        try:
            # We have to add the quota back to the user that created
            # the server group
            quotas.reserve(project_id=project_id,
                           user_id=user_id,
                           server_groups=-1)
        except Exception:
            quotas = None
            LOG.exception(
                _LE("Failed to update usages deallocating "
                    "server group"))

        try:
            sg.destroy()
        except nova.exception.InstanceGroupNotFound as e:
            if quotas:
                quotas.rollback()
            raise webob.exc.HTTPNotFound(explanation=e.format_message())

        if quotas:
            quotas.commit()
示例#2
0
    def create(self, req, body):
        """Creates a new server group."""
        req_ver = req.api_version_request

        context = _authorize_context(req)

        quotas = objects.Quotas(context=context)
        try:
            quotas.reserve(project_id=context.project_id,
                           user_id=context.user_id,
                           server_groups=1)
        except nova.exception.OverQuota:
            msg = _("Quota exceeded, too many server groups.")
            raise exc.HTTPForbidden(explanation=msg)

        vals = body['server_group']
        sg = objects.InstanceGroup(context)
        sg.project_id = context.project_id
        sg.user_id = context.user_id
        try:
            sg.name = vals.get('name')
            sg.policies = vals.get('policies')
            sg.create()
        except ValueError as e:
            quotas.rollback()
            raise exc.HTTPBadRequest(explanation=e)

        quotas.commit()
        return {
            'server_group': self._format_server_group(context, sg, req_ver)
        }
示例#3
0
    def create(self, req, body):
        """Creates a new server group."""
        context = _authorize_context(req)

        policies = body['server_group']['policies']
        if ('anti-affinity' in policies and 'affinity' in policies):
            msg = _("Conflicting policies configured!")
            raise exc.HTTPBadRequest(explanation=msg)

        quotas = objects.Quotas()
        try:
            quotas.reserve(context,
                           project_id=context.project_id,
                           user_id=context.user_id,
                           server_groups=1)
        except nova.exception.OverQuota:
            msg = _("Quota exceeded, too many server groups.")
            raise exc.HTTPForbidden(explanation=msg)

        vals = body['server_group']
        sg = objects.InstanceGroup(context)
        sg.project_id = context.project_id
        sg.user_id = context.user_id
        try:
            sg.name = vals.get('name')
            sg.policies = vals.get('policies')
            sg.create()
        except ValueError as e:
            quotas.rollback()
            raise exc.HTTPBadRequest(explanation=e)

        quotas.commit()

        return {'server_group': self._format_server_group(context, sg)}
示例#4
0
 def _create_reservations(self, context, instance, original_task_state,project_id, user_id):
     instance_vcpus = instance.vcpus
     instance_memory_mb = instance.memory_mb
     # NOTE(wangpan): if the instance is resizing, and the resources
     #                are updated to new instance type, we should use
     #                the old instance type to create reservation.
     # see https://bugs.launchpad.net/nova/+bug/1099729 for more details
                         
     quotas = objects.Quotas(context)
     quotas.reserve(project_id=project_id,user_id=user_id,instances=-1,cores=-instance_vcpus,ram=-instance_memory_mb)
     return quotas
示例#5
0
def reserve_quota_delta(context, deltas, instance):
    """If there are deltas to reserve, construct a Quotas object and
    reserve the deltas for the given project.

    :param context:    The nova request context.
    :param deltas:     A dictionary of the proposed delta changes.
    :param instance:   The instance we're operating on, so that
                       quotas can use the correct project_id/user_id.
    :return: nova.objects.quotas.Quotas
    """
    quotas = objects.Quotas(context=context)
    if deltas:
        project_id, user_id = objects.quotas.ids_from_instance(
            context, instance)
        quotas.reserve(project_id=project_id, user_id=user_id, **deltas)
    return quotas
示例#6
0
    def test_reserve_quota_delta(self, mock_ids_from_instance,
                                 mock_reserve):
        quotas = objects.Quotas(context=context)
        inst = create_instance(self.context, params=None)
        inst.old_flavor = flavors.get_flavor_by_name('m1.tiny')
        inst.new_flavor = flavors.get_flavor_by_name('m1.medium')

        mock_ids_from_instance.return_value = (inst.project_id, inst.user_id)
        mock_reserve.return_value = quotas

        deltas = compute_utils.upsize_quota_delta(self.context,
                                                  inst.new_flavor,
                                                  inst.old_flavor)
        compute_utils.reserve_quota_delta(self.context, deltas, inst)
        mock_reserve.assert_called_once_with(project_id=inst.project_id,
                                             user_id=inst.user_id, **deltas)
示例#7
0
    def create(self, req, body):
        """Creates a new server group."""
        context = _authorize_context(req)

        try:
            self._validate_input_body(body, 'server_group')
        except nova.exception.InvalidInput as e:
            raise exc.HTTPBadRequest(explanation=e.format_message())

        quotas = None
        if self.ext_mgr.is_loaded('os-server-group-quotas'):
            quotas = objects.Quotas()
            try:
                quotas.reserve(context,
                               project_id=context.project_id,
                               user_id=context.user_id,
                               server_groups=1)
            except nova.exception.OverQuota:
                msg = _("Quota exceeded, too many server groups.")
                raise exc.HTTPForbidden(explanation=msg)

        vals = body['server_group']
        sg = objects.InstanceGroup(context)
        sg.project_id = context.project_id
        sg.user_id = context.user_id
        try:
            sg.name = vals.get('name')
            sg.policies = vals.get('policies')
            sg.create()
        except ValueError as e:
            if quotas:
                quotas.rollback()
            raise exc.HTTPBadRequest(explanation=e)

        if quotas:
            quotas.commit()

        return {'server_group': self._format_server_group(context, sg)}