Example #1
0
    def test_tenant_quota_usages_neutron_fip_disabled(self):
        servers = [s for s in self.servers.list()
                   if s.tenant_id == self.request.user.tenant_id]

        api.base.is_service_enabled(IsA(http.HttpRequest),
                                    'volume').AndReturn(True)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                    'network').AndReturn(False)
        api.nova.flavor_list(IsA(http.HttpRequest)) \
            .AndReturn(self.flavors.list())
        api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.quotas.first())
        api.network.floating_ip_supported(IsA(http.HttpRequest)) \
            .AndReturn(False)
        search_opts = {'tenant_id': self.request.user.tenant_id}
        api.nova.server_list(IsA(http.HttpRequest), search_opts=search_opts,
                             all_tenants=True) \
            .AndReturn([servers, False])
        opts = {'all_tenants': 1, 'project_id': self.request.user.tenant_id}
        cinder.volume_list(IsA(http.HttpRequest), opts) \
            .AndReturn(self.volumes.list())
        cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \
            .AndReturn(self.cinder_volume_snapshots.list())
        cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.cinder_quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = self.get_usages()
        expected_output['floating_ips']['used'] = 0
        expected_output['floating_ips']['available'] = 1

        # Compare internal structure of usages to expected.
        self.assertItemsEqual(expected_output, quota_usages.usages)
Example #2
0
    def test_tenant_quota_usages(self):
        servers = [s for s in self.servers.list()
                   if s.tenant_id == self.request.user.tenant_id]

        api.base.is_service_enabled(IsA(http.HttpRequest),
                                  'volume').AndReturn(True)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                  'network').AndReturn(False)
        api.nova.flavor_list(IsA(http.HttpRequest)) \
                .AndReturn(self.flavors.list())
        api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
                .AndReturn(self.quotas.first())
        api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \
                .AndReturn(self.floating_ips.list())
        api.nova.server_list(IsA(http.HttpRequest)) \
                .AndReturn([servers, False])
        cinder.volume_list(IsA(http.HttpRequest)) \
                .AndReturn(self.volumes.list())
        cinder.volume_snapshot_list(IsA(http.HttpRequest)) \
                .AndReturn(self.snapshots.list())
        cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.cinder_quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = self.get_usages()

        # Compare internal structure of usages to expected.
        self.assertEquals(quota_usages.usages, expected_output)
Example #3
0
    def test_tenant_quota_usages(self):
        quotas.is_service_enabled(IsA(http.HttpRequest), "volume").AndReturn(True)
        api.nova.flavor_list(IsA(http.HttpRequest)).AndReturn(self.flavors.list())
        api.nova.tenant_quota_get(IsA(http.HttpRequest), "1").AndReturn(self.quotas.first())
        api.network.tenant_floating_ip_list(IsA(http.HttpRequest)).AndReturn(self.floating_ips.list())
        api.nova.server_list(IsA(http.HttpRequest)).AndReturn(self.servers.list())
        cinder.volume_list(IsA(http.HttpRequest)).AndReturn(self.volumes.list())
        cinder.tenant_quota_get(IsA(http.HttpRequest), "1").AndReturn(self.quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = {
            "injected_file_content_bytes": {"quota": 1},
            "metadata_items": {"quota": 1},
            "injected_files": {"quota": 1},
            "gigabytes": {"available": 920, "used": 80, "quota": 1000},
            "ram": {"available": 8976, "used": 1024, "quota": 10000},
            "floating_ips": {"available": 0, "used": 2, "quota": 1},
            "instances": {"available": 8, "used": 2, "quota": 10},
            "volumes": {"available": 0, "used": 3, "quota": 1},
            "cores": {"available": 8, "used": 2, "quota": 10},
        }

        # Compare internal structure of usages to expected.
        self.assertEquals(quota_usages.usages, expected_output)
Example #4
0
    def test_tenant_quota_usages(self):
        quotas.is_service_enabled(IsA(http.HttpRequest),
                                  'volume').AndReturn(True)
        api.nova.flavor_list(IsA(http.HttpRequest)) \
                .AndReturn(self.flavors.list())
        api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
                .AndReturn(self.quotas.first())
        api.nova.tenant_floating_ip_list(IsA(http.HttpRequest)) \
                .AndReturn(self.floating_ips.list())
        api.nova.server_list(IsA(http.HttpRequest)) \
                .AndReturn(self.servers.list())
        cinder.volume_list(IsA(http.HttpRequest)) \
                .AndReturn(self.volumes.list())
        cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = {
            'injected_file_content_bytes': {'quota': 1},
            'metadata_items': {'quota': 1},
            'injected_files': {'quota': 1},
            'gigabytes': {'available': 920, 'used': 80, 'quota': 1000},
            'ram': {'available': 8976, 'used': 1024, 'quota': 10000},
            'floating_ips': {'available': 0, 'used': 2, 'quota': 1},
            'instances': {'available': 8, 'used': 2, 'quota': 10},
            'volumes': {'available': 0, 'used': 3, 'quota': 1},
            'cores': {'available': 8, 'used': 2, 'quota': 10}
        }

        # Compare internal structure of usages to expected.
        self.assertEquals(quota_usages.usages, expected_output)
Example #5
0
    def test_tenant_quota_usages(self):
        servers = [s for s in self.servers.list()
                   if s.tenant_id == self.request.user.tenant_id]

        api.base.is_service_enabled(IsA(http.HttpRequest),
                                  'volume').AndReturn(True)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                  'network').AndReturn(False)
        api.nova.flavor_list(IsA(http.HttpRequest)) \
                .AndReturn(self.flavors.list())
        api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
                .AndReturn(self.quotas.first())
        api.network.floating_ip_supported(IsA(http.HttpRequest)) \
                .AndReturn(True)
        api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \
                .AndReturn(self.floating_ips.list())
        api.nova.server_list(IsA(http.HttpRequest)) \
                .AndReturn([servers, False])
        cinder.volume_list(IsA(http.HttpRequest)) \
                .AndReturn(self.volumes.list())
        cinder.volume_snapshot_list(IsA(http.HttpRequest)) \
                .AndReturn(self.snapshots.list())
        cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.cinder_quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = self.get_usages()

        # Compare internal structure of usages to expected.
        self.assertEqual(expected_output, quota_usages.usages)
Example #6
0
    def test_tenant_quota_usages_neutron_fip_disabled(self):
        servers = [
            s for s in self.servers.list()
            if s.tenant_id == self.request.user.tenant_id
        ]

        cinder.is_volume_service_enabled(IsA(http.HttpRequest)).AndReturn(True)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                    'network').AndReturn(False)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                    'compute').MultipleTimes().AndReturn(True)
        api.nova.flavor_list(IsA(http.HttpRequest)) \
            .AndReturn(self.flavors.list())
        api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.quotas.first())
        api.neutron.floating_ip_supported(IsA(http.HttpRequest)) \
            .AndReturn(False)
        api.nova.server_list(IsA(http.HttpRequest)).AndReturn([servers, False])
        opts = {'all_tenants': 1, 'project_id': self.request.user.tenant_id}
        cinder.volume_list(IsA(http.HttpRequest), opts) \
            .AndReturn(self.volumes.list())
        cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \
            .AndReturn(self.cinder_volume_snapshots.list())
        cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.cinder_quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = self.get_usages()
        expected_output['floating_ips']['used'] = 0
        expected_output['floating_ips']['available'] = 1

        # Compare internal structure of usages to expected.
        self.assertItemsEqual(expected_output, quota_usages.usages)
Example #7
0
    def test_tenant_quota_usages_unlimited_quota(self):
        inf_quota = self.quotas.first()
        inf_quota['ram'] = -1
        servers = [s for s in self.servers.list()
                   if s.tenant_id == self.request.user.tenant_id]

        api.base.is_service_enabled(IsA(http.HttpRequest),
                                  'volume').AndReturn(True)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                  'network').AndReturn(False)
        api.nova.flavor_list(IsA(http.HttpRequest)) \
                .AndReturn(self.flavors.list())
        api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
                .AndReturn(inf_quota)
        api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \
                .AndReturn(self.floating_ips.list())
        api.nova.server_list(IsA(http.HttpRequest)) \
                .AndReturn([servers, False])
        cinder.volume_list(IsA(http.HttpRequest)) \
                .AndReturn(self.volumes.list())
        cinder.volume_snapshot_list(IsA(http.HttpRequest)) \
                .AndReturn(self.snapshots.list())
        cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.cinder_quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = self.get_usages()
        expected_output.update({'ram': {'available': float("inf"),
                                        'used': 1024,
                                        'quota': float("inf")}})

        # Compare internal structure of usages to expected.
        self.assertEquals(quota_usages.usages, expected_output)
Example #8
0
    def _test_tenant_quota_usages(self,
                                  nova_quotas_enabled=True,
                                  with_compute=True,
                                  with_volume=True):

        cinder.is_volume_service_enabled(IsA(
            http.HttpRequest)).AndReturn(with_volume)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                    'network').AndReturn(False)
        api.base.is_service_enabled(
            IsA(http.HttpRequest),
            'compute').MultipleTimes().AndReturn(with_compute)
        if with_compute:
            servers = [
                s for s in self.servers.list()
                if s.tenant_id == self.request.user.tenant_id
            ]
            api.nova.flavor_list(IsA(http.HttpRequest)) \
                .AndReturn(self.flavors.list())
            api.network.floating_ip_supported(IsA(http.HttpRequest)) \
                .AndReturn(True)
            api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \
                .AndReturn(self.floating_ips.list())
            search_opts = {'tenant_id': self.request.user.tenant_id}
            api.nova.server_list(IsA(http.HttpRequest),
                                 search_opts=search_opts,
                                 all_tenants=True) \
                .AndReturn([servers, False])

            if nova_quotas_enabled:
                api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
                    .AndReturn(self.quotas.first())

        if with_volume:
            opts = {
                'all_tenants': 1,
                'project_id': self.request.user.tenant_id
            }
            cinder.volume_list(IsA(http.HttpRequest), opts) \
                .AndReturn(self.volumes.list())
            cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \
                .AndReturn(self.cinder_volume_snapshots.list())
            cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
                .AndReturn(self.cinder_quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = self.get_usages(
            nova_quotas_enabled=nova_quotas_enabled,
            with_volume=with_volume,
            with_compute=with_compute)

        # Compare internal structure of usages to expected.
        self.assertItemsEqual(expected_output, quota_usages.usages)
        # Compare available resources
        self.assertAvailableQuotasEqual(expected_output, quota_usages.usages)
Example #9
0
    def _test_tenant_quota_usages_with_target(self,
                                              targets,
                                              use_compute_call=True,
                                              use_flavor_list=False,
                                              use_cinder_call=False):
        cinder.is_volume_service_enabled(IsA(http.HttpRequest)).AndReturn(True)
        api.base.is_service_enabled(IsA(http.HttpRequest), 'network') \
            .AndReturn(False)
        api.base.is_service_enabled(IsA(http.HttpRequest), 'compute') \
            .MultipleTimes().AndReturn(True)

        if use_compute_call:
            servers = [
                s for s in self.servers.list()
                if s.tenant_id == self.request.user.tenant_id
            ]
            if use_flavor_list:
                api.nova.flavor_list(IsA(http.HttpRequest)) \
                    .AndReturn(self.flavors.list())
            search_opts = {'tenant_id': self.request.user.tenant_id}
            api.nova.server_list(IsA(http.HttpRequest),
                                 search_opts=search_opts) \
                    .AndReturn([servers, False])
            api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
                .AndReturn(self.quotas.first())

        if use_cinder_call:
            opts = {
                'all_tenants': 1,
                'project_id': self.request.user.tenant_id
            }
            cinder.volume_list(IsA(http.HttpRequest), opts) \
                .AndReturn(self.volumes.list())
            cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \
                .AndReturn(self.cinder_volume_snapshots.list())
            cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
                .AndReturn(self.cinder_quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request,
                                                  targets=targets)

        expected = self.get_usages()
        expected = dict((k, v) for k, v in expected.items() if k in targets)

        # Compare internal structure of usages to expected.
        self.assertItemsEqual(expected, quota_usages.usages)
        # Compare available resources
        self.assertAvailableQuotasEqual(expected, quota_usages.usages)
Example #10
0
    def _test_tenant_quota_usages(self, nova_quotas_enabled=True,
                                  with_compute=True, with_volume=True):

        cinder.is_volume_service_enabled(IsA(http.HttpRequest)).AndReturn(
            with_volume)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                    'network').AndReturn(False)
        api.base.is_service_enabled(
            IsA(http.HttpRequest), 'compute'
        ).MultipleTimes().AndReturn(with_compute)
        if with_compute:
            servers = [s for s in self.servers.list()
                       if s.tenant_id == self.request.user.tenant_id]
            api.nova.flavor_list(IsA(http.HttpRequest)) \
                .AndReturn(self.flavors.list())
            api.network.floating_ip_supported(IsA(http.HttpRequest)) \
                .AndReturn(True)
            api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \
                .AndReturn(self.floating_ips.list())
            search_opts = {'tenant_id': self.request.user.tenant_id}
            api.nova.server_list(IsA(http.HttpRequest),
                                 search_opts=search_opts,
                                 all_tenants=True) \
                .AndReturn([servers, False])

            if nova_quotas_enabled:
                api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
                    .AndReturn(self.quotas.first())

        if with_volume:
            opts = {'all_tenants': 1,
                    'project_id': self.request.user.tenant_id}
            cinder.volume_list(IsA(http.HttpRequest), opts) \
                .AndReturn(self.volumes.list())
            cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \
                .AndReturn(self.cinder_volume_snapshots.list())
            cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
                .AndReturn(self.cinder_quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = self.get_usages(
            nova_quotas_enabled=nova_quotas_enabled, with_volume=with_volume,
            with_compute=with_compute)

        # Compare internal structure of usages to expected.
        self.assertItemsEqual(expected_output, quota_usages.usages)
        # Compare available resources
        self.assertAvailableQuotasEqual(expected_output, quota_usages.usages)
Example #11
0
def _get_quota_data(request,
                    tenant_mode=True,
                    disabled_quotas=None,
                    tenant_id=None):
    quotasets = []
    if not tenant_id:
        tenant_id = request.user.tenant_id
    if disabled_quotas is None:
        disabled_quotas = get_disabled_quotas(request)

    qs = base.QuotaSet()

    if NOVA_QUOTA_FIELDS - disabled_quotas:
        if tenant_mode:
            quotasets.append(nova.tenant_quota_get(request, tenant_id))
        else:
            quotasets.append(nova.default_quota_get(request, tenant_id))

    if CINDER_QUOTA_FIELDS - disabled_quotas:
        try:
            if tenant_mode:
                quotasets.append(cinder.tenant_quota_get(request, tenant_id))
            else:
                quotasets.append(cinder.default_quota_get(request, tenant_id))
        except cinder.cinder_exception.ClientException:
            disabled_quotas.update(CINDER_QUOTA_FIELDS)
            msg = _("Unable to retrieve volume limit information.")
            exceptions.handle(request, msg)

    for quota in itertools.chain(*quotasets):
        if quota.name not in disabled_quotas:
            qs[quota.name] = quota.limit
    return qs
Example #12
0
def _get_quota_data(request, tenant_mode=True, disabled_quotas=None,
                    tenant_id=None):
    quotasets = []
    if not tenant_id:
        tenant_id = request.user.tenant_id
    if disabled_quotas is None:
        disabled_quotas = get_disabled_quotas(request)

    qs = base.QuotaSet()

    if NOVA_QUOTA_FIELDS - disabled_quotas:
        if tenant_mode:
            quotasets.append(nova.tenant_quota_get(request, tenant_id))
        else:
            quotasets.append(nova.default_quota_get(request, tenant_id))

    if CINDER_QUOTA_FIELDS - disabled_quotas:
        try:
            if tenant_mode:
                quotasets.append(cinder.tenant_quota_get(request, tenant_id))
            else:
                quotasets.append(cinder.default_quota_get(request, tenant_id))
        except cinder.cinder_exception.ClientException:
            disabled_quotas.update(CINDER_QUOTA_FIELDS)
            msg = _("Unable to retrieve volume limit information.")
            exceptions.handle(request, msg)

    for quota in itertools.chain(*quotasets):
        if quota.name not in disabled_quotas:
            qs[quota.name] = quota.limit
    return qs
Example #13
0
    def test_tenant_quota_usages_unlimited_quota(self):
        inf_quota = self.quotas.first()
        inf_quota['ram'] = -1
        servers = [
            s for s in self.servers.list()
            if s.tenant_id == self.request.user.tenant_id
        ]

        cinder.is_volume_service_enabled(IsA(http.HttpRequest)).AndReturn(True)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                    'network').AndReturn(False)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                    'compute').MultipleTimes().AndReturn(True)
        api.nova.flavor_list(IsA(http.HttpRequest)) \
            .AndReturn(self.flavors.list())
        api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(inf_quota)
        api.network.floating_ip_supported(IsA(http.HttpRequest)) \
            .AndReturn(True)
        api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \
            .AndReturn(self.floating_ips.list())
        search_opts = {'tenant_id': self.request.user.tenant_id}
        api.nova.server_list(IsA(http.HttpRequest), search_opts=search_opts,
                             all_tenants=True) \
            .AndReturn([servers, False])
        opts = {'all_tenants': 1, 'project_id': self.request.user.tenant_id}
        cinder.volume_list(IsA(http.HttpRequest), opts) \
            .AndReturn(self.volumes.list())
        cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \
            .AndReturn(self.cinder_volume_snapshots.list())
        cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.cinder_quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = self.get_usages()
        expected_output.update({
            'ram': {
                'available': float("inf"),
                'used': 1024,
                'quota': float("inf")
            }
        })

        # Compare internal structure of usages to expected.
        self.assertItemsEqual(expected_output, quota_usages.usages)
Example #14
0
    def _test_tenant_quota_usages_with_target(
            self, targets,
            use_compute_call=True,
            use_flavor_list=False, use_cinder_call=False):
        cinder.is_volume_service_enabled(IsA(http.HttpRequest)).AndReturn(True)
        api.base.is_service_enabled(IsA(http.HttpRequest), 'network') \
            .AndReturn(False)
        api.base.is_service_enabled(IsA(http.HttpRequest), 'compute') \
            .MultipleTimes().AndReturn(True)

        if use_compute_call:
            servers = [s for s in self.servers.list()
                       if s.tenant_id == self.request.user.tenant_id]
            if use_flavor_list:
                api.nova.flavor_list(IsA(http.HttpRequest)) \
                    .AndReturn(self.flavors.list())
            search_opts = {'tenant_id': self.request.user.tenant_id}
            api.nova.server_list(IsA(http.HttpRequest),
                                 search_opts=search_opts) \
                    .AndReturn([servers, False])
            api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
                .AndReturn(self.quotas.first())

        if use_cinder_call:
            opts = {'all_tenants': 1,
                    'project_id': self.request.user.tenant_id}
            cinder.volume_list(IsA(http.HttpRequest), opts) \
                .AndReturn(self.volumes.list())
            cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \
                .AndReturn(self.cinder_volume_snapshots.list())
            cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
                .AndReturn(self.cinder_quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request,
                                                  targets=targets)

        expected = self.get_usages()
        expected = dict((k, v) for k, v in expected.items() if k in targets)

        # Compare internal structure of usages to expected.
        self.assertItemsEqual(expected, quota_usages.usages)
        # Compare available resources
        self.assertAvailableQuotasEqual(expected, quota_usages.usages)
Example #15
0
    def test_tenant_quota_usages_unlimited_quota(self):
        inf_quota = self.quotas.first()
        inf_quota['ram'] = -1
        servers = [s for s in self.servers.list()
                   if s.tenant_id == self.request.user.tenant_id]

        cinder.is_volume_service_enabled(
            IsA(http.HttpRequest)
        ).AndReturn(True)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                    'network').AndReturn(False)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                    'compute').MultipleTimes().AndReturn(True)
        api.nova.flavor_list(IsA(http.HttpRequest)) \
            .AndReturn(self.flavors.list())
        api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(inf_quota)
        api.network.floating_ip_supported(IsA(http.HttpRequest)) \
            .AndReturn(True)
        api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \
            .AndReturn(self.floating_ips.list())
        search_opts = {'tenant_id': self.request.user.tenant_id}
        api.nova.server_list(IsA(http.HttpRequest), search_opts=search_opts,
                             all_tenants=True) \
            .AndReturn([servers, False])
        opts = {'all_tenants': 1, 'project_id': self.request.user.tenant_id}
        cinder.volume_list(IsA(http.HttpRequest), opts) \
            .AndReturn(self.volumes.list())
        cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \
            .AndReturn(self.cinder_volume_snapshots.list())
        cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.cinder_quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = self.get_usages()
        expected_output.update({'ram': {'available': float("inf"),
                                        'used': 1024,
                                        'quota': float("inf")}})

        # Compare internal structure of usages to expected.
        self.assertItemsEqual(expected_output, quota_usages.usages)
Example #16
0
    def get_context_data(self, **kwargs):
        context = super(ExtendView, self).get_context_data(**kwargs)
        context['volume'] = self.get_object()
        args = (self.kwargs['volume_id'], )
        context['submit_url'] = reverse(self.submit_url, args=args)
        try:
            context['usages'] = quotas.tenant_limit_usages(self.request)
            _volume_types = self._get_volume_types()
            _volumes = cinder.volume_list(self.request)
            _snapshots = cinder.volume_snapshot_list(self.request)

            volume_types = {}
            volume_type_size = {}

            for volume_type in _volume_types:
                if volume_type['name'] == 'no_type':
                    continue
                volume_type_size[volume_type['name']] = 0

            for volume in _volumes:
                volume_types[volume.id] = volume.volume_type
                volume_type_size[volume.volume_type] = (volume_type_size.get(
                    volume.volume_type) or 0) + volume.size

            for snapshot in cinder.volume_snapshot_list(self.request):
                volume_type_size[volume_types[snapshot.volume_id]] = (
                    volume_type_size.get(volume_types.get(snapshot.volume_id))
                    or 0) + snapshot.size

            tenant_quotas = {}
            for quota in cinder.tenant_quota_get(self.request,
                                                 self.request.user.tenant_id):
                if quota.name.startswith('gigabytes_'):
                    tenant_quotas[quota.name[10:]] = quota.limit

            volume_type_usage = []
            for volume_type, total_size in volume_type_size.iteritems():
                volume_type_usage.append({
                    'name':
                    volume_type,
                    'usage':
                    total_size,
                    'quota':
                    tenant_quotas.get(volume_type) or -1
                })

            context['usages']['volume_type'] = volume_type_usage
            context['volume_types'] = json.dumps(_volume_types)

        except Exception:
            exceptions.handle(self.request)
        print "#" * 120
        print context
        return context
Example #17
0
    def test_tenant_quota_usages_with_id(self):
        tenant_id = 3
        cinder.is_volume_service_enabled(IsA(http.HttpRequest)).AndReturn(True)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                    'network').AndReturn(False)
        api.base.is_service_enabled(IsA(http.HttpRequest), 'compute') \
            .MultipleTimes().AndReturn(True)
        servers = [s for s in self.servers.list() if s.tenant_id == tenant_id]
        api.nova.flavor_list(IsA(http.HttpRequest)) \
            .AndReturn(self.flavors.list())
        opts = {'tenant_id': tenant_id,
                'all_tenants': True}
        api.nova.server_list(IsA(http.HttpRequest), search_opts=opts) \
            .AndReturn([servers, False])
        api.nova.tenant_quota_get(IsA(http.HttpRequest), tenant_id) \
            .AndReturn(self.quotas.first())

        opts = {'all_tenants': 1,
                'project_id': tenant_id}
        cinder.volume_list(IsA(http.HttpRequest), opts) \
            .AndReturn(self.volumes.list())
        cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \
            .AndReturn(self.cinder_volume_snapshots.list())
        cinder.tenant_quota_get(IsA(http.HttpRequest), tenant_id) \
            .AndReturn(self.cinder_quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request,
                                                  tenant_id=tenant_id)
        expected_output = self.get_usages(
            nova_quotas_enabled=True, with_volume=True,
            with_compute=True, tenant_id=tenant_id)

        # Compare internal structure of usages to expected.
        self.assertItemsEqual(expected_output, quota_usages.usages)
        # Compare available resources
        self.assertAvailableQuotasEqual(expected_output, quota_usages.usages)
Example #18
0
    def test_tenant_quota_usages(self):
        quotas.is_service_enabled(IsA(http.HttpRequest),
                                  'volume').AndReturn(True)
        api.nova.flavor_list(IsA(http.HttpRequest)) \
                .AndReturn(self.flavors.list())
        api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
                .AndReturn(self.quotas.first())
        api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \
                .AndReturn(self.floating_ips.list())
        api.nova.server_list(IsA(http.HttpRequest)) \
                .AndReturn([self.servers.list(), False])
        cinder.volume_list(IsA(http.HttpRequest)) \
                .AndReturn(self.volumes.list())
        cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = self.get_usages()

        # Compare internal structure of usages to expected.
        self.assertEquals(quota_usages.usages, expected_output)
Example #19
0
    def test_tenant_quota_usages(self):
        servers = [
            s for s in self.servers.list()
            if s.tenant_id == self.request.user.tenant_id
        ]

        api.base.is_service_enabled(IsA(http.HttpRequest),
                                    'volume').AndReturn(True)
        api.base.is_service_enabled(IsA(http.HttpRequest),
                                    'network').AndReturn(False)
        api.nova.flavor_list(IsA(http.HttpRequest)) \
            .AndReturn(self.flavors.list())
        api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.quotas.first())
        api.network.floating_ip_supported(IsA(http.HttpRequest)) \
            .AndReturn(True)
        api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \
            .AndReturn(self.floating_ips.list())
        search_opts = {'tenant_id': self.request.user.tenant_id}
        api.nova.server_list(IsA(http.HttpRequest), search_opts=search_opts,
                             all_tenants=True) \
            .AndReturn([servers, False])
        opts = {'all_tenants': 1, 'project_id': self.request.user.tenant_id}
        cinder.volume_list(IsA(http.HttpRequest), opts) \
            .AndReturn(self.volumes.list())
        cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \
            .AndReturn(self.cinder_volume_snapshots.list())
        cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.cinder_quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = self.get_usages()

        # Compare internal structure of usages to expected.
        self.assertItemsEqual(expected_output, quota_usages.usages)
Example #20
0
def _get_quota_data(request,
                    tenant_mode=True,
                    disabled_quotas=None,
                    tenant_id=None):
    quotasets = []
    if not tenant_id:
        tenant_id = request.user.tenant_id
    if disabled_quotas is None:
        disabled_quotas = get_disabled_quotas(request)

    qs = base.QuotaSet()

    if 'instances' not in disabled_quotas:
        if tenant_mode:
            quotasets.append(nova.tenant_quota_get(request, tenant_id))
        else:
            quotasets.append(nova.default_quota_get(request, tenant_id))

    if 'volumes' not in disabled_quotas:
        try:
            if tenant_mode:
                quotasets.append(cinder.tenant_quota_get(request, tenant_id))
            else:
                quotasets.append(cinder.default_quota_get(request, tenant_id))
        except cinder.cinder_exception.ClientException:
            disabled_quotas.update(CINDER_QUOTA_FIELDS)
            msg = _("Unable to retrieve volume limit information.")
            exceptions.handle(request, msg)

    #mj - add the tenant_quota info from neutron - this is replaced in Queens
    if 'network' not in disabled_quotas:
        try:
            if tenant_mode:
                quotasets.append(
                    neutron.tenant_quota_get(request, tenant_id=tenant_id))
            else:
                quotasets.append(neutron.default_quota_get(request, tenant_id))

        except Exception:
            disabled_quotas.update(NEUTRON_QUOTA_FIELDS)
            msg = _('Unable to retrieve Neutron quota information.')

    for quota in itertools.chain(*quotasets):
        if quota.name not in disabled_quotas:
            qs[quota.name] = quota.limit
    return qs
Example #21
0
    def get_help_text(self, extra_context=None):
        extra = {} if extra_context is None else dict(extra_context)
        try:
            extra['usages'] = api.nova.tenant_absolute_limits(self.request)

            for quota in cinder.tenant_quota_get(self.request,
                                                 self.request.user.tenant_id):
                if quota.name == 'gigabytes_Tier1':
                    extra['usages']['maxTotalTier1Size'] = quota.limit

            totalTier1Used = 0

            volumes = []  # needed later for getting snapshot type
            for volume in cinder.volume_list(self.request):
                if volume.volume_type == 'Tier1':
                    volumes.append(volume.id)
                    totalTier1Used += volume.size

            for snapshot in cinder.volume_snapshot_list(self.request):
                if snapshot.volume_id in volumes:
                    totalTier1Used += snapshot.size

            extra['usages']['totalTier1Used'] = totalTier1Used

            extra['usages_json'] = json.dumps(extra['usages'])

            flavors = json.dumps(
                [f._info for f in instance_utils.flavor_list(self.request)])
            extra['flavors'] = flavors
            images = image_utils.get_available_images(
                self.request, self.initial['project_id'], self._images_cache)
            if images is not None:
                attrs = [{
                    'id': i.id,
                    'min_disk': getattr(i, 'min_disk', 0),
                    'min_ram': getattr(i, 'min_ram', 0),
                    'size': functions.bytes_to_gigabytes(i.size)
                } for i in images]
                extra['images'] = json.dumps(attrs)

        except Exception:
            exceptions.handle(self.request,
                              _("Unable to retrieve quota information."))
        return super(SetInstanceDetailsAction, self).get_help_text(extra)
Example #22
0
    def __init__(self, request, *args, **kwargs):
        super(CreateForm, self).__init__(request, *args, **kwargs)
        volume_types = cinder.volume_type_list(request)
        tenant_quotas = {}
        for quota in cinder.tenant_quota_get(request, request.user.tenant_id):
            if quota.name.startswith('gigabytes_'):
                tenant_quotas[quota.name[10:]] = quota.limit
        self.fields['type'].choices = [("no_type", _("No volume type"))] + \
                                      [(type.name, "%s - quota: %sGB" % (type.name, str(tenant_quotas.get(type.name) or "#!ERR")))
                                       for type in volume_types]

        if "snapshot_id" in request.GET:
            self.prepare_source_fields_if_snapshot_specified(request)
        elif 'image_id' in request.GET:
            self.prepare_source_fields_if_image_specified(request)
        elif 'volume_id' in request.GET:
            self.prepare_source_fields_if_volume_specified(request)
        else:
            self.prepare_source_fields_default(request)
Example #23
0
def get_tenant_quota_data(request, disabled_quotas=None, tenant_id=None):
    quotasets = []
    if not tenant_id:
        tenant_id = request.user.tenant_id
    if disabled_quotas is None:
        disabled_quotas = get_disabled_quotas(request)

    if NOVA_QUOTA_FIELDS - disabled_quotas:
        try:
            quotasets.append(nova.tenant_quota_get(request, tenant_id))
        except Exception:
            disabled_quotas.update(NOVA_QUOTA_FIELDS)
            msg = _('Unable to retrieve Nova quota information.')
            exceptions.handle(request, msg)

    if CINDER_QUOTA_FIELDS - disabled_quotas:
        try:
            quotasets.append(cinder.tenant_quota_get(request, tenant_id))
        except cinder.cinder_exception.ClientException:
            disabled_quotas.update(CINDER_QUOTA_FIELDS)
            msg = _("Unable to retrieve volume limit information.")
            exceptions.handle(request, msg)

    if NEUTRON_QUOTA_FIELDS - disabled_quotas:
        try:
            quotasets.append(neutron.tenant_quota_get(request, tenant_id))
        except Exception:
            disabled_quotas.update(NEUTRON_QUOTA_FIELDS)
            msg = _('Unable to retrieve Neutron quota information.')
            exceptions.handle(request, msg)

    qs = base.QuotaSet()
    for quota in itertools.chain(*quotasets):
        if quota.name not in disabled_quotas and quota.name in QUOTA_FIELDS:
            qs[quota.name] = quota.limit
    return qs
Example #24
0
    def test_tenant_quota_usages(self):
        quotas.is_service_enabled(IsA(http.HttpRequest),
                                  'volume').AndReturn(True)
        api.nova.flavor_list(IsA(http.HttpRequest)) \
                .AndReturn(self.flavors.list())
        api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \
                .AndReturn(self.quotas.first())
        api.nova.tenant_floating_ip_list(IsA(http.HttpRequest)) \
                .AndReturn(self.floating_ips.list())
        api.nova.server_list(IsA(http.HttpRequest)) \
                .AndReturn(self.servers.list())
        cinder.volume_list(IsA(http.HttpRequest)) \
                .AndReturn(self.volumes.list())
        cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \
            .AndReturn(self.quotas.first())

        self.mox.ReplayAll()

        quota_usages = quotas.tenant_quota_usages(self.request)
        expected_output = {
            'injected_file_content_bytes': {
                'quota': 1
            },
            'metadata_items': {
                'quota': 1
            },
            'injected_files': {
                'quota': 1
            },
            'gigabytes': {
                'available': 920,
                'used': 80,
                'quota': 1000
            },
            'ram': {
                'available': 8976,
                'used': 1024,
                'quota': 10000
            },
            'floating_ips': {
                'available': 0,
                'used': 2,
                'quota': 1
            },
            'instances': {
                'available': 8,
                'used': 2,
                'quota': 10
            },
            'volumes': {
                'available': 0,
                'used': 3,
                'quota': 1
            },
            'cores': {
                'available': 8,
                'used': 2,
                'quota': 10
            }
        }

        # Compare internal structure of usages to expected.
        self.assertEquals(quota_usages.usages, expected_output)