def _get_tenant_network_usages(request, usages, disabled_quotas, tenant_id): enabled_quotas = ((NOVA_NETWORK_QUOTA_FIELDS | NEUTRON_QUOTA_FIELDS) - disabled_quotas) if not enabled_quotas: return # NOTE(amotoki): floatingip is Neutron quota and floating_ips is # Nova quota. We need to check both. if {'floatingip', 'floating_ips'} & enabled_quotas: floating_ips = [] try: if neutron.floating_ip_supported(request): floating_ips = neutron.tenant_floating_ip_list(request) except Exception: pass usages.tally('floating_ips', len(floating_ips)) if 'security_group' not in disabled_quotas: security_groups = [] security_groups = neutron.security_group_list(request) usages.tally('security_groups', len(security_groups)) if 'network' not in disabled_quotas: networks = neutron.network_list(request, tenant_id=tenant_id) usages.tally('networks', len(networks)) if 'subnet' not in disabled_quotas: subnets = neutron.subnet_list(request, tenant_id=tenant_id) usages.tally('subnets', len(subnets)) if 'router' not in disabled_quotas: routers = neutron.router_list(request, tenant_id=tenant_id) usages.tally('routers', len(routers))
def _remove_autonetwork(self, server_networks, request): """ Removes the automatically created network that was used prior to v1.4. """ server_networks_keys = list(server_networks.keys()) neutron_enabled = is_enabled_by_config('network') # delete only if vm has one network if neutron_enabled and len(server_networks) == 1: networks = network_list_for_tenant(request, request.user.tenant_id) autonetwork = filter(lambda network: network.name in server_networks_keys, networks) auto_subnet_name = autonetwork_subnet_name(request) if len(autonetwork) != 1: return autonetwork_subnet = filter(lambda subnet: subnet.name == auto_subnet_name, autonetwork[0].subnets) if len(autonetwork_subnet) != 1: return router = router_list(request, network_id=autonetwork_subnet[0]) if len(router) != 1: return router_remove_interface(request, router[0].id, subnet_id=autonetwork_subnet[0].id) router_delete(request, router[0].id) subnet_delete(request, autonetwork_subnet[0].id) network_delete(request, autonetwork[0].id)
def _firewall_get(request, firewall_id, expand_policy=True, expand_router=True): firewall = neutronclient(request).show_firewall(firewall_id).get( 'firewall') if expand_policy: policy_id = firewall['firewall_policy_id'] if policy_id: firewall['policy'] = _policy_get(request, policy_id, expand_rule=False) else: firewall['policy'] = None if expand_router: if neutron.is_extension_supported(request, 'fwaasrouterinsertion'): router_ids = firewall['router_ids'] if router_ids: firewall['routers'] = neutron.router_list(request, id=router_ids) else: firewall['routers'] = [] else: firewall['routers'] = [] return Firewall(firewall)
def _firewall_list(request, expand_policy, expand_router, **kwargs): firewalls = neutronclient(request).list_firewalls( **kwargs).get('firewalls') if expand_policy and firewalls: policies = _policy_list(request, expand_rule=False) policy_dict = OrderedDict((p.id, p) for p in policies) for fw in firewalls: fw['policy'] = policy_dict.get(fw['firewall_policy_id']) if expand_router and firewalls: if neutron.is_extension_supported(request, 'fwaasrouterinsertion'): filter_params = {} if 'tenant_id' in kwargs: filter_params['tenant_id'] = kwargs['tenant_id'] routers = neutron.router_list(request, **filter_params) router_dict = dict((r.id, r) for r in routers) def _get_router(router_id): try: return router_dict[router_id] except KeyError: return neutron.Router({'id': router_id, 'name': ''}) for fw in firewalls: fw['routers'] = [ _get_router(router_id) for router_id in fw['router_ids'] ] else: for fw in firewalls: fw['routers'] = fw['router_ids'] return [Firewall(f) for f in firewalls]
def _get_tenant_network_usages(request, usages, disabled_quotas, tenant_id): enabled_quotas = ((NOVA_NETWORK_QUOTA_FIELDS | NEUTRON_QUOTA_FIELDS) - disabled_quotas) if not enabled_quotas: return # NOTE(amotoki): floatingip is Neutron quota and floating_ips is # Nova quota. We need to check both. if {'floatingip', 'floating_ips'} & enabled_quotas: floating_ips = [] try: if neutron.floating_ip_supported(request): floating_ips = neutron.tenant_floating_ip_list(request) except Exception: pass usages.tally('floating_ips', len(floating_ips)) if 'security_group' not in disabled_quotas: security_groups = [] security_groups = neutron.security_group_list(request) usages.tally('security_groups', len(security_groups)) if 'network' not in disabled_quotas: networks = neutron.network_list(request, tenant_id=tenant_id) usages.tally('networks', len(networks)) if 'subnet' not in disabled_quotas: subnets = neutron.subnet_list(request, tenant_id=tenant_id) usages.tally('subnets', len(subnets)) if 'router' not in disabled_quotas: routers = neutron.router_list(request, tenant_id=tenant_id) usages.tally('routers', len(routers))
def _get_tenant_network_usages(request, usages, disabled_quotas, tenant_id): floating_ips = [] try: if network.floating_ip_supported(request): floating_ips = network.tenant_floating_ip_list(request) except Exception: pass usages.tally('floating_ips', len(floating_ips)) if 'security_group' not in disabled_quotas: security_groups = [] security_groups = network.security_group_list(request) usages.tally('security_groups', len(security_groups)) if 'network' not in disabled_quotas: networks = [] networks = neutron.network_list(request, shared=False) if tenant_id: networks = filter(lambda net: net.tenant_id == tenant_id, networks) usages.tally('networks', len(networks)) if 'subnet' not in disabled_quotas: subnets = [] subnets = neutron.subnet_list(request) usages.tally('subnets', len(subnets)) if 'router' not in disabled_quotas: routers = [] routers = neutron.router_list(request) if tenant_id: routers = filter(lambda rou: rou.tenant_id == tenant_id, routers) usages.tally('routers', len(routers))
def _vpnservice_list(request, expand_subnet=False, expand_router=False, expand_conns=False, **kwargs): vpnservices = neutronclient(request).list_vpnservices( **kwargs).get('vpnservices') if expand_subnet: subnets = neutron.subnet_list(request) subnet_dict = SortedDict((s.id, s) for s in subnets) for s in vpnservices: s['subnet_name'] = subnet_dict.get(s['subnet_id']).cidr if expand_router: routers = neutron.router_list(request) router_dict = SortedDict((r.id, r) for r in routers) for s in vpnservices: s['router_name'] = router_dict.get(s['router_id']).name_or_id if expand_conns: ipsecsiteconns = _ipsecsiteconnection_list(request, **kwargs) sslvpnconns = _sslvpnconnection_list(request, **kwargs) for s in vpnservices: s['ipsecsiteconns'] = [ c.id for c in ipsecsiteconns if c.vpnservice_id == s['id'] ] s['sslvpnconns'] = [ c.id for c in sslvpnconns if c.vpnservice_id == s['id'] ] return [VPNService(v) for v in vpnservices]
def get_publicips_data(self): data = [] for router in neutron.router_list( self.request, tenant_id=self.request.user.tenant_id): router_info = neutron.router_get(self.request, router.id) for port in router_info.get('ports', []): if port.get('device_owner') != 'network:router_gateway': continue ips = [i['ip_address'] for i in port.get('fixed_ips', [])] data.append(PublicIP(None, router.get('name'), ips)) return data
def get_publicips_data(self): data = [] for router in neutron.router_list( self.request, tenant_id=self.request.user.tenant_id): router_info = neutron.router_get(self.request, router.id) for port in router_info.get('ports', []): if port.get('device_owner') != 'network:router_gateway': continue ips = [i['ip_address'] for i in port.get('fixed_ips', [])] data.append(PublicIP(None, router.get('name'), ips)) return data
def firewall_unassociated_routers_list(request, tenant_id): all_routers = neutron.router_list(request, tenant_id=tenant_id) tenant_firewalls = firewall_list_for_tenant(request, tenant_id=tenant_id) firewall_router_ids = [rid for fw in tenant_firewalls for rid in getattr(fw, 'router_ids', [])] available_routers = [r for r in all_routers if r.id not in firewall_router_ids] available_routers = sorted(available_routers, key=lambda router: router.name_or_id) return available_routers
def optimizer_unassociated_routers_list(request, tenant_id): all_routers = neutron.router_list(request, tenant_id=tenant_id) tenant_optimizers = optimizer_list_for_tenant(request, tenant_id=tenant_id) optimizer_router_ids = [rid for opt in tenant_optimizers for rid in getattr(opt, 'router_ids', [])] available_routers = [r for r in all_routers if r.id not in optimizer_router_ids] available_routers = sorted(available_routers, key=lambda router: router.name_or_id) return available_routers
def firewall_unassociated_routers_list(request, tenant_id): all_routers = neutron.router_list(request, tenant_id=tenant_id) tenant_firewalls = firewall_list_for_tenant(request, tenant_id=tenant_id) firewall_router_ids = [rid for fw in tenant_firewalls for rid in getattr(fw, 'router_ids', [])] available_routers = [r for r in all_routers if r.id not in firewall_router_ids] available_routers = sorted(available_routers, key=lambda router: router.name_or_id) return available_routers
def test_firewall_get(self): exp_firewall = self.firewalls.first() ret_dict = {'firewall': self.api_firewalls.first()} policy_dict = {'firewall_policy': self.api_fw_policies.first()} neutronclient.show_firewall(exp_firewall.id).AndReturn(ret_dict) neutronclient.show_firewall_policy( exp_firewall.firewall_policy_id).AndReturn(policy_dict) api_neutron.is_extension_supported(mox.IsA( http.HttpRequest), 'fwaasrouterinsertion').AndReturn(True) api_neutron.router_list(mox.IsA(http.HttpRequest), id=exp_firewall.router_ids).AndReturn( exp_firewall.routers) self.mox.ReplayAll() ret_val = api_fwaas.firewall_get(self.request, exp_firewall.id) self._assert_firewall_return_value(ret_val, exp_firewall) self.assertEqual(exp_firewall.router_ids, ret_val.router_ids) self.assertEqual(exp_firewall.router_ids, [r.id for r in ret_val.routers]) self.assertEqual([r.name for r in exp_firewall.routers], [r.name for r in ret_val.routers])
def _get_tenant_network_usages(request, usages, disabled_quotas, tenant_id, region=None): floating_ips = [] try: if network.floating_ip_supported(request): floating_ips = network.floating_ip_list(request, region=region, tenant_id=tenant_id) if tenant_id: floating_ips = filter(lambda fip: fip.tenant_id == tenant_id, floating_ips) except Exception: raise pass usages.tally('floating_ips', len(floating_ips)) if 'security_group' not in disabled_quotas: security_groups = [] security_groups = network.security_group_list(request) usages.tally('security_groups', len(security_groups)) if 'network' not in disabled_quotas: networks = [] networks = neutron.network_list(request, shared=False, region=region) if tenant_id: networks = filter(lambda net: net.tenant_id == tenant_id, networks) usages.tally('networks', len(networks)) if 'subnet' not in disabled_quotas: subnets = [] subnets = neutron.subnet_list(request, region=region) if tenant_id: subnets = filter(lambda subnet: subnet.tenant_id == tenant_id, subnets) usages.tally('subnets', len(subnets)) if 'router' not in disabled_quotas: routers = [] routers = neutron.router_list(request, region=region) if tenant_id: routers = filter(lambda rou: rou.tenant_id == tenant_id, routers) usages.tally('routers', len(routers)) if 'pool' not in disabled_quotas: pools = [] pools = lbaas.pool_list(request) if tenant_id: pools = filter(lambda p: p.tenant_id == tenant_id, pools) usages.tally('pool', len(pools))
def optimizer_unassociated_routers_list(request, tenant_id): all_routers = neutron.router_list(request, tenant_id=tenant_id) tenant_optimizers = optimizer_list_for_tenant(request, tenant_id=tenant_id) optimizer_router_ids = [ rid for opt in tenant_optimizers for rid in getattr(opt, 'router_ids', []) ] available_routers = [ r for r in all_routers if r.id not in optimizer_router_ids ] available_routers = sorted(available_routers, key=lambda router: router.name_or_id) return available_routers
def _get_tenant_network_usages(request, usages, disabled_quotas, tenant_id): floating_ips = [] try: if network.floating_ip_supported(request): floating_ips = network.tenant_floating_ip_list(request) except Exception: pass usages.tally('floating_ips', len(floating_ips)) if 'security_group' not in disabled_quotas: security_groups = [] security_groups = network.security_group_list(request) usages.tally('security_groups', len(security_groups)) if 'network' not in disabled_quotas: networks = [] networks = neutron.network_list(request, shared=False) if tenant_id: networks = [net for net in networks if net.tenant_id == tenant_id] usages.tally('networks', len(networks)) # 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 ] usages.tally('networks', len(shared_networks)) if 'subnet' not in disabled_quotas: subnets = neutron.subnet_list(request, shared=False) if tenant_id: subnets = [sub for sub in subnets if sub.tenant_id == tenant_id] # get shared subnets shared_subnets = neutron.subnet_list(request, shared=True) if tenant_id: shared_subnets = [ subnet for subnet in shared_subnets if subnet.tenant_id == tenant_id ] usages.tally('subnets', len(subnets) + len(shared_subnets)) if 'router' not in disabled_quotas: routers = [] routers = neutron.router_list(request) if tenant_id: routers = [rou for rou in routers if rou.tenant_id == tenant_id] usages.tally('routers', len(routers))
def _get_tenant_network_usages(request, usages, disabled_quotas, tenant_id): floating_ips = [] try: if network.floating_ip_supported(request): floating_ips = network.tenant_floating_ip_list(request) except Exception: pass usages.tally('floating_ips', len(floating_ips)) if 'security_group' not in disabled_quotas: security_groups = [] security_groups = network.security_group_list(request) usages.tally('security_groups', len(security_groups)) if 'network' not in disabled_quotas: networks = [] networks = neutron.network_list(request, shared=False) if tenant_id: networks = [net for net in networks if net.tenant_id == tenant_id] usages.tally('networks', len(networks)) # 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] usages.tally('networks', len(shared_networks)) if 'subnet' not in disabled_quotas: subnets = neutron.subnet_list(request, shared=False) if tenant_id: subnets = [sub for sub in subnets if sub.tenant_id == tenant_id] # get shared subnets shared_subnets = neutron.subnet_list(request, shared=True) if tenant_id: shared_subnets = [subnet for subnet in shared_subnets if subnet.tenant_id == tenant_id] usages.tally('subnets', len(subnets) + len(shared_subnets)) if 'router' not in disabled_quotas: routers = [] routers = neutron.router_list(request) if tenant_id: routers = [rou for rou in routers if rou.tenant_id == tenant_id] usages.tally('routers', len(routers))
def _get_tenant_network_usages(request, usages, disabled_quotas, tenant_id, region=None): floating_ips = [] try: if network.floating_ip_supported(request): floating_ips = network.floating_ip_list(request, region=region, tenant_id=tenant_id) if tenant_id: floating_ips = filter(lambda fip: fip.tenant_id == tenant_id, floating_ips) except Exception: raise pass usages.tally('floating_ips', len(floating_ips)) if 'security_group' not in disabled_quotas: security_groups = [] security_groups = network.security_group_list(request) usages.tally('security_groups', len(security_groups)) if 'network' not in disabled_quotas: networks = [] networks = neutron.network_list(request, shared=False, region=region) if tenant_id: networks = filter(lambda net: net.tenant_id == tenant_id, networks) usages.tally('networks', len(networks)) if 'subnet' not in disabled_quotas: subnets = [] subnets = neutron.subnet_list(request, region=region) if tenant_id: subnets = filter(lambda subnet: subnet.tenant_id == tenant_id, subnets) usages.tally('subnets', len(subnets)) if 'router' not in disabled_quotas: routers = [] routers = neutron.router_list(request, region=region) if tenant_id: routers = filter(lambda rou: rou.tenant_id == tenant_id, routers) usages.tally('routers', len(routers)) if 'pool' not in disabled_quotas: pools = [] pools = lbaas.pool_list(request) if tenant_id: pools = filter(lambda p: p.tenant_id == tenant_id, pools) usages.tally('pool', len(pools))
def _vpnservice_list(request, expand_subnet=False, expand_router=False, expand_conns=False, **kwargs): vpnservices = neutronclient(request).list_vpnservices( **kwargs).get('vpnservices') if expand_subnet: subnets = neutron.subnet_list(request) subnet_dict = OrderedDict((s.id, s) for s in subnets) for s in vpnservices: s['subnet_name'] = subnet_dict.get(s['subnet_id']).cidr if expand_router: routers = neutron.router_list(request) router_dict = OrderedDict((r.id, r) for r in routers) for s in vpnservices: s['router_name'] = router_dict.get(s['router_id']).name_or_id if expand_conns: ipsecsiteconns = _ipsecsiteconnection_list(request, **kwargs) for s in vpnservices: s['ipsecsiteconns'] = [c.id for c in ipsecsiteconns if c.vpnservice_id == s['id']] return [VPNService(v) for v in vpnservices]
def get(self, request): result = neutron.router_list(request)[0] return result
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) usages = QuotaUsage() for quota in get_tenant_quota_data(request, disabled_quotas=disabled_quotas, tenant_id=tenant_id): usages.add_quota(quota) # Get our usages. floating_ips = [] try: if network.floating_ip_supported(request): floating_ips = network.tenant_floating_ip_list(request) except Exception: pass flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) if tenant_id: instances, has_more = nova.server_list( request, search_opts={'tenant_id': tenant_id}, all_tenants=True) else: instances, has_more = nova.server_list(request) # 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) usages.tally('instances', len(instances)) usages.tally('floating_ips', len(floating_ips)) if 'security_group' not in disabled_quotas: security_groups = [] security_groups = network.security_group_list(request) usages.tally('security_groups', len(security_groups)) if 'network' not in disabled_quotas: networks = [] networks = neutron.network_list(request, shared=False) if tenant_id: networks = filter(lambda net: net.tenant_id == tenant_id, networks) usages.tally('networks', len(networks)) if 'subnet' not in disabled_quotas: subnets = [] subnets = neutron.subnet_list(request) usages.tally('subnets', len(subnets)) if 'router' not in disabled_quotas: routers = [] routers = neutron.router_list(request) if tenant_id: routers = filter(lambda rou: rou.tenant_id == tenant_id, routers) usages.tally('routers', len(routers)) if 'volumes' not in disabled_quotas: if tenant_id: opts = {'alltenants': 1, 'tenant_id': tenant_id} volumes = cinder.volume_list(request, opts) snapshots = cinder.volume_snapshot_list(request, opts) else: volumes = cinder.volume_list(request) snapshots = cinder.volume_snapshot_list(request) usages.tally('gigabytes', sum([int(v.size) for v in volumes])) usages.tally('volumes', len(volumes)) usages.tally('snapshots', len(snapshots)) # 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)) # Initialise the tally if no instances have been launched yet if len(instances) == 0: usages.tally('cores', 0) usages.tally('ram', 0) return usages
def get(self, request): result = neutron.router_list(request)[0] return result
def tenant_quota_usages(request): # Get our quotas and construct our usage object. disabled_quotas = get_disabled_quotas(request) usages = QuotaUsage() for quota in get_tenant_quota_data(request, disabled_quotas=disabled_quotas): usages.add_quota(quota) # Get our usages. floating_ips = [] try: if network.floating_ip_supported(request): floating_ips = network.tenant_floating_ip_list(request) except Exception: pass flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) instances, has_more = nova.server_list(request) # 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) usages.tally('instances', len(instances)) usages.tally('floating_ips', len(floating_ips)) if 'security_group' not in disabled_quotas: security_groups = [] security_groups = network.security_group_list(request) usages.tally('security_groups', len(security_groups)) if 'network' not in disabled_quotas: networks = [] networks = neutron.network_list(request, shared=False) usages.tally('networks', len(networks)) if 'router' not in disabled_quotas: routers = [] routers = neutron.router_list(request) usages.tally('routers', len(routers)) if 'volumes' not in disabled_quotas: volumes = cinder.volume_list(request) snapshots = cinder.volume_snapshot_list(request) usages.tally('gigabytes', sum([int(v.size) for v in volumes])) usages.tally('volumes', len(volumes)) usages.tally('snapshots', len(snapshots)) # 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)) # Initialise the tally if no instances have been launched yet if len(instances) == 0: usages.tally('cores', 0) usages.tally('ram', 0) return usages
def dispose_project(request, project_id): # TODO missing check for VMs, Volumes and images try: flow_step = 0 prj_subnets = set() for s_item in neutron_api.subnet_list(request, tenant_id=project_id, project_id=project_id ): prj_subnets.add(s_item.id) for r_item in neutron_api.router_list(request): for p_item in neutron_api.port_list(request, tenant_id=project_id, project_id=project_id, device_id=r_item.id ): for ip_item in p_item.fixed_ips: if ip_item.get('subnet_id') in prj_subnets: LOG.info('Removing port %s' % ip_item.get('ip_address')) #neutron_api.router_remove_interface(request, r_item.id, None, p_item.id) flow_step = 1 for s_item in prj_subnets: LOG.info('Removing subnet %s' % s_item) #neutron_api.subnet_delete(request, s_item) flow_step = 2 for n_item in neutron_api.network_list(request, tenant_id=project_id, project_id=project_id ): LOG.info('Removing network %s' % n_item.name) #neutron_api.network_delete(request, n_item.id) flow_step = 3 #TODO release FIPs except: if flow_step == 0: err_msg = _("Cannot remove router interfaces") elif flow_step == 1: err_msg = _("Cannot remove subnets") elif flow_step == 2: err_msg = _("Cannot remove networks") else: err_msg = _("Cannot release FIPs") LOG.error(err_msg, exc_info=True) messages.error(request, err_msg) try: for agg_item in nova_api.aggregate_details_list(request): if agg_item.metadata.get('filter_tenant_id', '') == project_id: for agg_host in agg_item.hosts: LOG.info('Removing host %s from %s' % (agg_host, agg_item.name)) #nova_api.remove_host_from_aggregate(request, # agg_item.id, # agg_host #) LOG.info('Removing aggregate %s' % agg_item.name) #nova_api.aggregate_delete(request, agg_item.id) except: err_msg = _("Cannot delete host aggregate") LOG.error(err_msg, exc_info=True) messages.error(request, err_msg) return True
def tenant_quota_usages(request, tenant_id=None): """Get our quotas and construct our usage object.""" disabled_quotas = get_disabled_quotas(request) usages = QuotaUsage() for quota in get_tenant_quota_data(request, disabled_quotas=disabled_quotas, tenant_id=tenant_id): usages.add_quota(quota) # Get our usages. floating_ips = [] try: if network.floating_ip_supported(request): floating_ips = network.tenant_floating_ip_list(request) except Exception: pass flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) if tenant_id: instances, has_more = nova.server_list( request, search_opts={'tenant_id': tenant_id}, all_tenants=True) else: instances, has_more = nova.server_list(request) # 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) usages.tally('instances', len(instances)) usages.tally('floating_ips', len(floating_ips)) if 'security_group' not in disabled_quotas: security_groups = [] security_groups = network.security_group_list(request) usages.tally('security_groups', len(security_groups)) if 'network' not in disabled_quotas: networks = [] networks = neutron.network_list(request, shared=False) if tenant_id: networks = filter(lambda net: net.tenant_id == tenant_id, networks) usages.tally('networks', len(networks)) if 'router' not in disabled_quotas: routers = [] routers = neutron.router_list(request) if tenant_id: routers = filter(lambda rou: rou.tenant_id == tenant_id, routers) usages.tally('routers', len(routers)) if 'volumes' not in disabled_quotas: if tenant_id: opts = {'alltenants': 1, 'tenant_id': tenant_id} volumes = cinder.volume_list(request, opts) snapshots = cinder.volume_snapshot_list(request, opts) else: volumes = cinder.volume_list(request) snapshots = cinder.volume_snapshot_list(request) usages.tally('gigabytes', sum([int(v.size) for v in volumes])) usages.tally('volumes', len(volumes)) usages.tally('snapshots', len(snapshots)) # 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)) # Initialise the tally if no instances have been launched yet if len(instances) == 0: usages.tally('cores', 0) usages.tally('ram', 0) return usages