示例#1
0
文件: hosts.py 项目: sasimpson/cinder
    def show(self, req, id):
        """Shows the volume usage info given by hosts.

        :param context: security context
        :param host: hostname
        :returns: expected to use HostShowTemplate.
            ex.::

                {'host': {'resource':D},..}
                D: {'host': 'hostname','project': 'admin',
                    'volume_count': 1, 'total_volume_gb': 2048}
        """
        host = id
        context = req.environ['cinder.context']
        if not context.is_admin:
            msg = _("Describe-resource is admin only functionality")
            raise webob.exc.HTTPForbidden(explanation=msg)

        try:
            host_ref = db.service_get_by_host_and_topic(context,
                                                        host,
                                                        CONF.volume_topic)
        except exception.ServiceNotFound:
            raise webob.exc.HTTPNotFound(explanation=_("Host not found"))

        # Getting total available/used resource
        # TODO(jdg): Add summary info for Snapshots
        volume_refs = db.volume_get_all_by_host(context, host_ref['host'])
        (count, sum) = db.volume_data_get_for_host(context,
                                                   host_ref['host'])

        snap_count_total = 0
        snap_sum_total = 0
        resources = [{'resource': {'host': host, 'project': '(total)',
                                   'volume_count': str(count),
                                   'total_volume_gb': str(sum),
                                   'snapshot_count': str(snap_count_total),
                                   'total_snapshot_gb': str(snap_sum_total)}}]

        project_ids = [v['project_id'] for v in volume_refs]
        project_ids = list(set(project_ids))
        for project_id in project_ids:
            (count, sum) = db.volume_data_get_for_project(context, project_id)
            (snap_count, snap_sum) = db.snapshot_data_get_for_project(
                context,
                project_id)
            resources.append(
                {'resource':
                    {'host': host,
                     'project': project_id,
                     'volume_count': str(count),
                     'total_volume_gb': str(sum),
                     'snapshot_count': str(snap_count),
                     'total_snapshot_gb': str(snap_sum)}})
            snap_count_total += int(snap_count)
            snap_sum_total += int(snap_sum)
        resources[0]['resource']['snapshot_count'] = str(snap_count_total)
        resources[0]['resource']['total_snapshot_gb'] = str(snap_sum_total)
        return {"host": resources}
示例#2
0
文件: hosts.py 项目: ArikaChen/cinder
    def show(self, req, id):
        """Shows the volume usage info given by hosts.

        :param context: security context
        :param host: hostname
        :returns: expected to use HostShowTemplate.
            ex.::

                {'host': {'resource':D},..}
                D: {'host': 'hostname','project': 'admin',
                    'volume_count': 1, 'total_volume_gb': 2048}
        """
        host = id
        context = req.environ['cinder.context']
        if not context.is_admin:
            msg = _("Describe-resource is admin only functionality")
            raise webob.exc.HTTPForbidden(explanation=msg)

        try:
            host_ref = db.service_get_by_host_and_topic(context,
                                                        host,
                                                        CONF.volume_topic)
        except exception.ServiceNotFound:
            raise webob.exc.HTTPNotFound(explanation=_("Host not found"))

        # Getting total available/used resource
        # TODO(jdg): Add summary info for Snapshots
        volume_refs = db.volume_get_all_by_host(context, host_ref['host'])
        (count, sum) = db.volume_data_get_for_host(context,
                                                   host_ref['host'])

        snap_count_total = 0
        snap_sum_total = 0
        resources = [{'resource': {'host': host, 'project': '(total)',
                      'volume_count': str(count),
                      'total_volume_gb': str(sum),
                      'snapshot_count': str(snap_count_total),
                      'total_snapshot_gb': str(snap_sum_total)}}]

        project_ids = [v['project_id'] for v in volume_refs]
        project_ids = list(set(project_ids))
        for project_id in project_ids:
            (count, sum) = db.volume_data_get_for_project(context, project_id)
            (snap_count, snap_sum) = db.snapshot_data_get_for_project(
                context,
                project_id)
            resources.append(
                {'resource':
                    {'host': host,
                     'project': project_id,
                     'volume_count': str(count),
                     'total_volume_gb': str(sum),
                     'snapshot_count': str(snap_count),
                     'total_snapshot_gb': str(snap_sum)}})
            snap_count_total += int(snap_count)
            snap_sum_total += int(snap_sum)
        resources[0]['resource']['snapshot_count'] = str(snap_count_total)
        resources[0]['resource']['total_snapshot_gb'] = str(snap_sum_total)
        return {"host": resources}
示例#3
0
 def _sync_volumes(self, context, project_id, session):
     """Sync volumes for this specific volume type."""
     (volumes, gigs) = db.volume_data_get_for_project(
         context,
         project_id,
         volume_type_id=self.volume_type_id,
         session=session)
     return {'volumes_%s' % self.volume_type_name: volumes}
示例#4
0
文件: quota.py 项目: akutz/cinder
 def _sync_volumes(self, context, project_id, session):
     """Sync volumes for this specific volume type."""
     (volumes, gigs) = db.volume_data_get_for_project(
         context,
         project_id,
         volume_type_id=self.volume_type_id,
         session=session)
     return {'volumes_%s' % self.volume_type_name: volumes}
示例#5
0
 def test_volume_data_get_for_project(self):
     for i in xrange(3):
         for j in xrange(3):
             db.volume_create(self.ctxt, {'project_id': 'p%d' % i,
                                          'size': 100,
                                          'host': 'h-%d-%d' % (i, j),
                                          })
     for i in xrange(3):
         self.assertEqual((3, 300),
                          db.volume_data_get_for_project(
                              self.ctxt, 'p%d' % i))
示例#6
0
    def show(self, req, id):
        """Shows the volume usage info given by hosts.

        :param req: security context
        :param id: hostname
        :returns: dict -- the host resources dictionary.
            ex.::

                {'host': [{'resource': D},..]}
                D: {'host': 'hostname','project': 'admin',
                    'volume_count': 1, 'total_volume_gb': 2048}
        """
        host = id
        context = req.environ['cinder.context']
        context.authorize(policy.MANAGE_POLICY)

        # Not found exception will be handled at the wsgi level
        host_ref = objects.Service.get_by_host_and_topic(
            context, host, constants.VOLUME_TOPIC)

        # Getting total available/used resource on a host.
        volume_refs = db.volume_get_all_by_host(context, host_ref.host)
        (count, vol_sum) = db.volume_data_get_for_host(context,
                                                       host_ref.host)

        snap_count_total = 0
        snap_sum_total = 0
        resources = [{'resource': {'host': host, 'project': '(total)',
                                   'volume_count': str(count),
                                   'total_volume_gb': str(vol_sum),
                                   'snapshot_count': str(snap_count_total),
                                   'total_snapshot_gb': str(snap_sum_total)}}]

        project_ids = [v['project_id'] for v in volume_refs]
        project_ids = list(set(project_ids))
        for project_id in project_ids:
            (count, vol_sum) = db.volume_data_get_for_project(
                context, project_id, host=host_ref.host)
            (snap_count, snap_sum) = (
                objects.Snapshot.snapshot_data_get_for_project(
                    context, project_id, host=host_ref.host))
            resources.append(
                {'resource':
                    {'host': host,
                     'project': project_id,
                     'volume_count': str(count),
                     'total_volume_gb': str(vol_sum),
                     'snapshot_count': str(snap_count),
                     'total_snapshot_gb': str(snap_sum)}})
            snap_count_total += int(snap_count)
            snap_sum_total += int(snap_sum)
        resources[0]['resource']['snapshot_count'] = str(snap_count_total)
        resources[0]['resource']['total_snapshot_gb'] = str(snap_sum_total)
        return {"host": resources}
示例#7
0
def _sync_gigabytes(context, project_id, session):
    (_junk, vol_gigs) = db.volume_data_get_for_project(context,
                                                       project_id,
                                                       session=session)
    if CONF.no_snapshot_gb_quota:
        return {'gigabytes': vol_gigs}

    (_junk, snap_gigs) = db.snapshot_data_get_for_project(context,
                                                          project_id,
                                                          session=session)
    return {'gigabytes': vol_gigs + snap_gigs}
示例#8
0
文件: quota.py 项目: akutz/cinder
def _sync_gigabytes(context, project_id, session):
    (_junk, vol_gigs) = db.volume_data_get_for_project(context,
                                                       project_id,
                                                       session=session)
    if CONF.no_snapshot_gb_quota:
        return {'gigabytes': vol_gigs}

    (_junk, snap_gigs) = db.snapshot_data_get_for_project(context,
                                                          project_id,
                                                          session=session)
    return {'gigabytes': vol_gigs + snap_gigs}
示例#9
0
    def show(self, req, id):
        """Shows the volume usage info given by hosts.

        :param req: security context
        :param id: hostname
        :returns: dict -- the host resources dictionary.
            ex.::

                {'host': [{'resource': D},..]}
                D: {'host': 'hostname','project': 'admin',
                    'volume_count': 1, 'total_volume_gb': 2048}
        """
        host = id
        context = req.environ['cinder.context']
        context.authorize(policy.MANAGE_POLICY)

        # Not found exception will be handled at the wsgi level
        host_ref = objects.Service.get_by_host_and_topic(
            context, host, constants.VOLUME_TOPIC)

        # Getting total available/used resource on a host.
        volume_refs = db.volume_get_all_by_host(context, host_ref.host)
        (count, vol_sum) = db.volume_data_get_for_host(context,
                                                       host_ref.host)

        snap_count_total = 0
        snap_sum_total = 0
        resources = [{'resource': {'host': host, 'project': '(total)',
                                   'volume_count': str(count),
                                   'total_volume_gb': str(vol_sum),
                                   'snapshot_count': str(snap_count_total),
                                   'total_snapshot_gb': str(snap_sum_total)}}]

        project_ids = [v['project_id'] for v in volume_refs]
        project_ids = list(set(project_ids))
        for project_id in project_ids:
            (count, vol_sum) = db.volume_data_get_for_project(
                context, project_id, host=host_ref.host)
            (snap_count, snap_sum) = (
                objects.Snapshot.snapshot_data_get_for_project(
                    context, project_id, host=host_ref.host))
            resources.append(
                {'resource':
                    {'host': host,
                     'project': project_id,
                     'volume_count': str(count),
                     'total_volume_gb': str(vol_sum),
                     'snapshot_count': str(snap_count),
                     'total_snapshot_gb': str(snap_sum)}})
            snap_count_total += int(snap_count)
            snap_sum_total += int(snap_sum)
        resources[0]['resource']['snapshot_count'] = str(snap_count_total)
        resources[0]['resource']['total_snapshot_gb'] = str(snap_sum_total)
        return {"host": resources}
示例#10
0
    def _sync_gigabytes(self, context, project_id, session):
        """Sync gigabytes for this specific volume type."""
        key = 'gigabytes_%s' % self.volume_type_name
        (_junk, vol_gigs) = db.volume_data_get_for_project(
            context,
            project_id,
            volume_type_id=self.volume_type_id,
            session=session)
        if CONF.no_snapshot_gb_quota:
            return {key: vol_gigs}

        (_junk, snap_gigs) = db.snapshot_data_get_for_project(
            context,
            project_id,
            volume_type_id=self.volume_type_id,
            session=session)
        return {key: vol_gigs + snap_gigs}
示例#11
0
文件: quota.py 项目: akutz/cinder
    def _sync_gigabytes(self, context, project_id, session):
        """Sync gigabytes for this specific volume type."""
        key = 'gigabytes_%s' % self.volume_type_name
        (_junk, vol_gigs) = db.volume_data_get_for_project(
            context,
            project_id,
            volume_type_id=self.volume_type_id,
            session=session)
        if CONF.no_snapshot_gb_quota:
            return {key: vol_gigs}

        (_junk, snap_gigs) = db.snapshot_data_get_for_project(
            context,
            project_id,
            volume_type_id=self.volume_type_id,
            session=session)
        return {key: vol_gigs + snap_gigs}
示例#12
0
def allowed_volumes(context, requested_volumes, size):
    """Check quota and return min(requested_volumes, allowed_volumes)."""
    project_id = context.project_id
    context = context.elevated()
    size = int(size)
    requested_gigabytes = requested_volumes * size
    used_volumes, used_gigabytes = db.volume_data_get_for_project(context,
                                                                  project_id)
    quota = get_project_quotas(context, project_id)
    allowed_volumes = _get_request_allotment(requested_volumes, used_volumes,
                                             quota['volumes'])
    allowed_gigabytes = _get_request_allotment(requested_gigabytes,
                                               used_gigabytes,
                                               quota['gigabytes'])
    if size != 0:
        allowed_volumes = min(allowed_volumes,
                              int(allowed_gigabytes // size))
    return min(requested_volumes, allowed_volumes)
示例#13
0
def _sync_volumes(context, project_id, session):
    return dict(
        zip(('volumes', 'gigabytes'),
            db.volume_data_get_for_project(context,
                                           project_id,
                                           session=session)))
示例#14
0
文件: quota.py 项目: shaoyexin/cinder
def _sync_snapshots(context, project_id, session):
    return dict(zip(('snapshots', 'gigabytes'),
                db.volume_data_get_for_project(context,
                                               project_id,
                                               session=session)))
示例#15
0
 def test_volume_data_get_for_project(self):
     for i in xrange(3):
         for j in xrange(3):
             db.volume_create(self.ctxt, {"project_id": "p%d" % i, "size": 100, "host": "h-%d-%d" % (i, j)})
     for i in xrange(3):
         self.assertEqual((3, 300), db.volume_data_get_for_project(self.ctxt, "p%d" % i))
示例#16
0
def _sync_volumes(context, project_id, session):
    (volumes, gigs) = db.volume_data_get_for_project(context,
                                                     project_id,
                                                     session=session)
    return {'volumes': volumes}
示例#17
0
文件: quota.py 项目: akutz/cinder
def _sync_volumes(context, project_id, session):
    (volumes, gigs) = db.volume_data_get_for_project(context,
                                                     project_id,
                                                     session=session)
    return {'volumes': volumes}