# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from collections import defaultdict
import itertools
import logging
from django.utils.translation import ugettext_lazy as _
from horizon import exceptions
from horizon.utils.memoized import memoized # noqa
from horizon.utils import threaded
from openstack_dashboard.api import base
from openstack_dashboard.api import cinder
from openstack_dashboard.api import network
from openstack_dashboard.api import neutron
from openstack_dashboard.api import nova
from openstack_dashboard import policy
LOG = logging.getLogger(__name__)
NOVA_QUOTA_FIELDS = ("metadata_items",
"cores",
"instances",
"injected_files",
"injected_file_content_bytes",
"ram",
"floating_ips",
"fixed_ips",
"security_groups",
"security_group_rules",)
MISSING_QUOTA_FIELDS = ("key_pairs",
"injected_file_path_bytes",)
CINDER_QUOTA_FIELDS = ("volumes",
"snapshots",
"gigabytes",)
NEUTRON_QUOTA_FIELDS = ("network",
"subnet",
"port",
"router",
"floatingip",
"security_group",
"security_group_rule",
)
QUOTA_FIELDS = NOVA_QUOTA_FIELDS + CINDER_QUOTA_FIELDS + NEUTRON_QUOTA_FIELDS
QUOTA_NAMES = {
"metadata_items": _('Metadata Items'),
"cores": _('VCPUs'),
"instances": _('Instances'),
"injected_files": _('Injected Files'),
"injected_file_content_bytes": _('Injected File Content Bytes'),
"ram": _('RAM (MB)'),
"floating_ips": _('Floating IPs'),
"fixed_ips": _('Fixed IPs'),
"security_groups": _('Security Groups'),
"security_group_rules": _('Security Group Rules'),
"key_pairs": _('Key Pairs'),
"injected_file_path_bytes": _('Injected File Path Bytes'),
"volumes": _('Volumes'),
"snapshots": _('Volume Snapshots'),
"gigabytes": _('Total Size of Volumes and Snapshots (GB)'),
"network": _("Networks"),
"subnet": _("Subnets"),
"port": _("Ports"),
"router": _("Routers"),
"floatingip": _('Floating IPs'),
"security_group": _("Security Groups"),
"security_group_rule": _("Security Group Rules")
}
[docs]class QuotaUsage(dict):
"""Tracks quota limit, used, and available for a given set of quotas."""
def __init__(self):
self.usages = defaultdict(dict)
def __contains__(self, key):
return key in self.usages
def __getitem__(self, key):
return self.usages[key]
def __setitem__(self, key, value):
raise NotImplementedError("Directly setting QuotaUsage values is not "
"supported. Please use the add_quota and "
"tally methods.")
def __repr__(self):
return repr(dict(self.usages))
[docs] def get(self, key, default=None):
return self.usages.get(key, default)
[docs] def add_quota(self, quota):
"""Adds an internal tracking reference for the given quota."""
if quota.limit is None or quota.limit == -1:
# Handle "unlimited" quotas.
self.usages[quota.name]['quota'] = float("inf")
self.usages[quota.name]['available'] = float("inf")
else:
self.usages[quota.name]['quota'] = int(quota.limit)
[docs] def tally(self, name, value):
"""Adds to the "used" metric for the given quota."""
value = value or 0 # Protection against None.
# Start at 0 if this is the first value.
if 'used' not in self.usages[name]:
self.usages[name]['used'] = 0
# Increment our usage and update the "available" metric.
self.usages[name]['used'] += int(value) # Fail if can't coerce to int.
self.update_available(name)
[docs] def update_available(self, name):
"""Updates the "available" metric for the given quota."""
quota = self.usages.get(name, {}).get('quota', float('inf'))
available = quota - self.usages[name]['used']
if available < 0:
available = 0
self.usages[name]['available'] = available
@threaded.promise
def _get_quota_data(request, method_name, disabled_quotas=None,
tenant_id=None):
if not tenant_id:
tenant_id = request.user.tenant_id
if disabled_quotas is None:
disabled_quotas = get_disabled_quotas(request)
quotasets = []
qs = base.QuotaSet()
@threaded.promise
def get_compute_quotasets():
return getattr(nova, method_name)(request, tenant_id)
@threaded.promise
def get_volume_quotasets():
return getattr(cinder, method_name)(request, tenant_id)
compute_quotasets_promise = get_compute_quotasets()
volume_quotasets_promise = get_volume_quotasets()
quotasets.append(compute_quotasets_promise.result)
if 'volumes' not in disabled_quotas:
try:
quotasets.append(volume_quotasets_promise.result)
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
[docs]def get_default_quota_data(request, disabled_quotas=None, tenant_id=None):
promise = _get_quota_data(request,
"default_quota_get",
disabled_quotas=disabled_quotas,
tenant_id=tenant_id)
return promise.result
[docs]def get_tenant_quota_data(request, disabled_quotas=None, tenant_id=None):
quotas_promise = _get_quota_data(request,
"tenant_quota_get",
disabled_quotas=disabled_quotas,
tenant_id=tenant_id)
if not disabled_quotas:
# TODO(jpichon): There is no API to get the default system quotas
# in Neutron (cf. LP#1204956), so for now handle tenant quotas here.
# This should be handled in _get_quota_data() eventually.
return quotas_promise.result
else:
neutron_quotas_promise = _get_neutron_quotas(
request, disabled_quotas, tenant_id)
qs = quotas_promise.result
neutron_quotas = neutron_quotas_promise.result
if 'floating_ips' in disabled_quotas:
# Neutron with quota extension disabled
if 'floatingip' in disabled_quotas:
qs.add(base.QuotaSet({'floating_ips': -1}))
# Neutron with quota extension enabled
else:
# Rename floatingip to floating_ips since that's how it's
# expected in some places (e.g. Security & Access'
# Floating IPs)
fips_quota = neutron_quotas.get('floatingip').limit
qs.add(base.QuotaSet({'floating_ips': fips_quota}))
if 'security_groups' in disabled_quotas:
if 'security_group' in disabled_quotas:
qs.add(base.QuotaSet({'security_groups': -1}))
# Neutron with quota extension enabled
else:
# Rename security_group to security_groups since that's how
# it's expected in some places (e.g. Security & Access'
# Security Groups)
sec_quota = neutron_quotas.get('security_group').limit
qs.add(base.QuotaSet({'security_groups': sec_quota}))
if 'network' in disabled_quotas:
for item in qs.items:
if item.name == 'networks':
qs.items.remove(item)
break
else:
net_quota = neutron_quotas.get('network').limit
qs.add(base.QuotaSet({'networks': net_quota}))
if 'subnet' in disabled_quotas:
for item in qs.items:
if item.name == 'subnets':
qs.items.remove(item)
break
else:
net_quota = neutron_quotas.get('subnet').limit
qs.add(base.QuotaSet({'subnets': net_quota}))
if 'router' in disabled_quotas:
for item in qs.items:
if item.name == 'routers':
qs.items.remove(item)
break
else:
router_quota = neutron_quotas.get('router').limit
qs.add(base.QuotaSet({'routers': router_quota}))
return qs
@threaded.promise
def _get_neutron_quotas(request, disabled_quotas, tenant_id):
# Check if neutron is enabled by looking for network and router
if 'network' and 'router' not in disabled_quotas:
tenant_id = tenant_id or request.user.tenant_id
return neutron.tenant_quota_get(request, tenant_id)
else:
return {}
[docs]def get_disabled_quotas(request):
disabled_quotas = set([])
# Nova
if not nova.can_set_quotas():
disabled_quotas.update(NOVA_QUOTA_FIELDS)
# Cinder
if not cinder.is_volume_service_enabled(request):
disabled_quotas.update(CINDER_QUOTA_FIELDS)
# Neutron
if not base.is_service_enabled(request, 'network'):
disabled_quotas.update(NEUTRON_QUOTA_FIELDS)
else:
# Remove the nova network quotas
disabled_quotas.update(['floating_ips', 'fixed_ips'])
if neutron.is_extension_supported(request, 'security-group'):
# If Neutron security group is supported, disable Nova quotas
disabled_quotas.update(['security_groups', 'security_group_rules'])
else:
# If Nova security group is used, disable Neutron quotas
disabled_quotas.update(['security_group', 'security_group_rule'])
try:
if not neutron.is_quotas_extension_supported(request):
disabled_quotas.update(NEUTRON_QUOTA_FIELDS)
except Exception:
LOG.exception("There was an error checking if the Neutron "
"quotas extension is enabled.")
return disabled_quotas
@threaded.promise
def _get_tenant_compute_data(request, disabled_quotas, tenant_id):
@threaded.promise
def get_instances():
if tenant_id:
# determine if the user has permission to view across projects
# there are cases where an administrator wants to check the quotas
# on a project they are not scoped to
all_tenants = policy.check(
(("compute", "compute:get_all_tenants"),), request)
instances, has_more = nova.server_list(
request, search_opts={'tenant_id': tenant_id},
all_tenants=all_tenants)
else:
instances, has_more = nova.server_list(request)
return instances
@threaded.promise
def get_flavors():
return dict([(f.id, f) for f in nova.flavor_list(request)])
instances_promise = get_instances()
flavors_promise = get_flavors()
flavors = flavors_promise.result
instances = instances_promise.result
# Fetch deleted flavors if necessary.
missing_flavors = [instance.flavor['id'] for instance in instances
if instance.flavor['id'] not in flavors]
for missing in missing_flavors:
if missing not in flavors:
try:
flavors[missing] = nova.flavor_get(request, missing)
except Exception:
flavors[missing] = {}
exceptions.handle(request, ignore=True)
return {'instances': instances, 'flavors': flavors}
def _update_tenant_compute_usages(usages, instances=None, flavors=None):
if instances is None:
instances = []
if flavors is None:
flavors = []
usages.tally('instances', len(instances))
# Sum our usage based on the flavors of the instances.
for flavor in [flavors[instance.flavor['id']] for instance in instances]:
usages.tally('cores', getattr(flavor, 'vcpus', None))
usages.tally('ram', getattr(flavor, 'ram', None))
# Initialize the tally if no instances have been launched yet
if len(instances) == 0:
usages.tally('cores', 0)
usages.tally('ram', 0)
@threaded.promise
def _get_tenant_network_data(request, disabled_quotas, tenant_id):
@threaded.promise
def get_floating_ips():
try:
if network.floating_ip_supported(request):
return 'floating_ips', network.tenant_floating_ip_list(request)
except Exception:
pass
@threaded.promise
def get_sec_groups():
if 'security_group' not in disabled_quotas:
return 'security_groups', network.security_group_list(request)
@threaded.promise
def get_networks():
if 'network' not in disabled_quotas:
networks = neutron.network_list(request, shared=False)
if tenant_id:
networks = [net for net in networks
if net.tenant_id == tenant_id]
# get shared networks
shared_networks = neutron.network_list(request, shared=True)
if tenant_id:
shared_networks = [net for net in shared_networks
if net.tenant_id == tenant_id]
networks.extend(shared_networks)
return 'networks', networks
@threaded.promise
def get_subnets():
if 'subnet' not in disabled_quotas:
return 'subnets', neutron.subnet_list(request)
@threaded.promise
def get_routers():
if 'router' not in disabled_quotas:
routers = neutron.router_list(request)
if tenant_id:
routers = filter(lambda rou: rou.tenant_id == tenant_id,
routers)
return 'routers', routers
promises = [func() for func in
(get_floating_ips, get_sec_groups, get_networks, get_subnets,
get_routers)]
return {promise.result[0]: promise.result[1] for promise in promises
if promise.result is not None}
def _update_tenant_network_usages(usages, **data):
for key, value in data.items():
usages.tally(key, len(value))
@threaded.promise
def _get_tenant_volume_data(request, disabled_quotas, tenant_id):
data = {}
if 'volumes' not in disabled_quotas:
try:
opts = None
if tenant_id:
opts = {'all_tenants': 1, 'project_id': tenant_id}
@threaded.promise
def get_volumes():
return 'volumes', cinder.volume_list(request, opts)
@threaded.promise
def get_snapshots():
return 'snapshots', cinder.volume_snapshot_list(request, opts)
promises = [func() for func in (get_volumes, get_snapshots)]
data = {p.result[0]: p.result[1] for p in promises}
except cinder.cinder_exception.ClientException:
msg = _("Unable to retrieve volume limit information.")
exceptions.handle(request, msg)
return data
def _update_tenant_volume_usages(usages, volumes=None, snapshots=None):
if volumes is not None:
usages.tally('gigabytes', sum([int(v.size) for v in volumes]))
usages.tally('volumes', len(volumes))
if snapshots is not None:
usages.tally('snapshots', len(snapshots))
@memoized
[docs]def tenant_quota_usages(request, tenant_id=None):
"""Get our quotas and construct our usage object.
If no tenant_id is provided, a the request.user.project_id
is assumed to be used
"""
if not tenant_id:
tenant_id = request.user.project_id
disabled_quotas = get_disabled_quotas(request)
quota_data_promise, compute_promise, network_promise, volume_promise = [
func(request, disabled_quotas, tenant_id) for func in
(threaded.promise(get_tenant_quota_data), _get_tenant_compute_data,
_get_tenant_network_data, _get_tenant_volume_data)]
usages = QuotaUsage()
for quota in quota_data_promise.result:
usages.add_quota(quota)
_update_tenant_compute_usages(usages, **compute_promise.result)
_update_tenant_network_usages(usages, **network_promise.result)
_update_tenant_volume_usages(usages, **volume_promise.result)
return usages
[docs]def tenant_limit_usages(request):
# TODO(licostan): This method shall be removed from Quota module.
# ProjectUsage/BaseUsage maybe used instead on volume/image dashboards.
limits = {}
try:
limits.update(nova.tenant_absolute_limits(request))
except Exception:
msg = _("Unable to retrieve compute limit information.")
exceptions.handle(request, msg)
if cinder.is_volume_service_enabled(request):
try:
limits.update(cinder.tenant_absolute_limits(request))
volumes = cinder.volume_list(request)
snapshots = cinder.volume_snapshot_list(request)
# gigabytesUsed should be a total of volumes and snapshots
vol_size = sum([getattr(volume, 'size', 0) for volume
in volumes])
snap_size = sum([getattr(snap, 'size', 0) for snap
in snapshots])
limits['gigabytesUsed'] = vol_size + snap_size
limits['volumesUsed'] = len(volumes)
limits['snapshotsUsed'] = len(snapshots)
except cinder.cinder_exception.ClientException:
msg = _("Unable to retrieve volume limit information.")
exceptions.handle(request, msg)
return limits