<s> from collections import defaultdict <EOL> import itertools <EOL> import logging <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from horizon import exceptions <EOL> from horizon . utils . memoized import memoized <EOL> from openstack_dashboard . api import base <EOL> from openstack_dashboard . api import cinder <EOL> from openstack_dashboard . api import network <EOL> from openstack_dashboard . api import neutron <EOL> from openstack_dashboard . api import nova <EOL> from openstack_dashboard import policy <EOL> LOG = logging . getLogger ( __name__ ) <EOL> NOVA_QUOTA_FIELDS = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , ) <EOL> MISSING_QUOTA_FIELDS = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , ) <EOL> CINDER_QUOTA_FIELDS = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , ) <EOL> NEUTRON_QUOTA_FIELDS = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:port>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> QUOTA_FIELDS = NOVA_QUOTA_FIELDS + CINDER_QUOTA_FIELDS + NEUTRON_QUOTA_FIELDS <EOL> QUOTA_NAMES = { <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : _ ( "<STR_LIT>" ) , <EOL> "<STR_LIT:port>" : _ ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : _ ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : _ ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : _ ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : _ ( "<STR_LIT>" ) <EOL> } <EOL> class QuotaUsage ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . usages = defaultdict ( dict ) <EOL> def __contains__ ( self , key ) : <EOL> return key in self . usages <EOL> def __getitem__ ( self , key ) : <EOL> return self . usages [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def __repr__ ( self ) : <EOL> return repr ( dict ( self . usages ) ) <EOL> def get ( self , key , default = None ) : <EOL> return self . usages . get ( key , default ) <EOL> def add_quota ( self , quota ) : <EOL> """<STR_LIT>""" <EOL> if quota . limit is None or quota . limit == - <NUM_LIT:1> : <EOL> self . usages [ quota . name ] [ '<STR_LIT>' ] = float ( "<STR_LIT>" ) <EOL> self . usages [ quota . name ] [ '<STR_LIT>' ] = float ( "<STR_LIT>" ) <EOL> else : <EOL> self . usages [ quota . name ] [ '<STR_LIT>' ] = int ( quota . limit ) <EOL> def tally ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> value = value or <NUM_LIT:0> <EOL> if '<STR_LIT>' not in self . usages [ name ] : <EOL> self . usages [ name ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> self . usages [ name ] [ '<STR_LIT>' ] += int ( value ) <EOL> self . update_available ( name ) <EOL> def update_available ( self , name ) : <EOL> """<STR_LIT>""" <EOL> available = self . usages [ name ] [ '<STR_LIT>' ] - self . usages [ name ] [ '<STR_LIT>' ] <EOL> if available < <NUM_LIT:0> : <EOL> available = <NUM_LIT:0> <EOL> self . usages [ name ] [ '<STR_LIT>' ] = available <EOL> def _get_quota_data ( request , method_name , disabled_quotas = None , <EOL> tenant_id = None ) : <EOL> quotasets = [ ] <EOL> if not tenant_id : <EOL> tenant_id = request . user . tenant_id <EOL> quotasets . append ( getattr ( nova , method_name ) ( request , tenant_id ) ) <EOL> qs = base . QuotaSet ( ) <EOL> if disabled_quotas is None : <EOL> disabled_quotas = get_disabled_quotas ( request ) <EOL> if '<STR_LIT>' not in disabled_quotas : <EOL> try : <EOL> quotasets . append ( getattr ( cinder , method_name ) ( request , tenant_id ) ) <EOL> except cinder . cinder_exception . ClientException : <EOL> disabled_quotas . extend ( CINDER_QUOTA_FIELDS ) <EOL> msg = _ ( "<STR_LIT>" ) <EOL> exceptions . handle ( request , msg ) <EOL> for quota in itertools . chain ( * quotasets ) : <EOL> if quota . name not in disabled_quotas : <EOL> qs [ quota . name ] = quota . limit <EOL> return qs <EOL> def get_default_quota_data ( request , disabled_quotas = None , tenant_id = None ) : <EOL> return _get_quota_data ( request , <EOL> "<STR_LIT>" , <EOL> disabled_quotas = disabled_quotas , <EOL> tenant_id = tenant_id ) <EOL> def get_tenant_quota_data ( request , disabled_quotas = None , tenant_id = None ) : <EOL> qs = _get_quota_data ( request , <EOL> "<STR_LIT>" , <EOL> disabled_quotas = disabled_quotas , <EOL> tenant_id = tenant_id ) <EOL> if not disabled_quotas : <EOL> return qs <EOL> if '<STR_LIT>' and '<STR_LIT>' not in disabled_quotas : <EOL> tenant_id = tenant_id or request . user . tenant_id <EOL> neutron_quotas = neutron . tenant_quota_get ( request , tenant_id ) <EOL> if '<STR_LIT>' in disabled_quotas : <EOL> if '<STR_LIT>' in disabled_quotas : <EOL> qs . add ( base . QuotaSet ( { '<STR_LIT>' : - <NUM_LIT:1> } ) ) <EOL> else : <EOL> fips_quota = neutron_quotas . get ( '<STR_LIT>' ) . limit <EOL> qs . add ( base . QuotaSet ( { '<STR_LIT>' : fips_quota } ) ) <EOL> if '<STR_LIT>' in disabled_quotas : <EOL> if '<STR_LIT>' in disabled_quotas : <EOL> qs . add ( base . QuotaSet ( { '<STR_LIT>' : - <NUM_LIT:1> } ) ) <EOL> else : <EOL> sec_quota = neutron_quotas . get ( '<STR_LIT>' ) . limit <EOL> qs . add ( base . QuotaSet ( { '<STR_LIT>' : sec_quota } ) ) <EOL> if '<STR_LIT>' in disabled_quotas : <EOL> for item in qs . items : <EOL> if item . name == '<STR_LIT>' : <EOL> qs . items . remove ( item ) <EOL> break <EOL> else : <EOL> net_quota = neutron_quotas . get ( '<STR_LIT>' ) . limit <EOL> qs . add ( base . QuotaSet ( { '<STR_LIT>' : net_quota } ) ) <EOL> if '<STR_LIT>' in disabled_quotas : <EOL> for item in qs . items : <EOL> if item . name == '<STR_LIT>' : <EOL> qs . items . remove ( item ) <EOL> break <EOL> else : <EOL> net_quota = neutron_quotas . get ( '<STR_LIT>' ) . limit <EOL> qs . add ( base . QuotaSet ( { '<STR_LIT>' : net_quota } ) ) <EOL> if '<STR_LIT>' in disabled_quotas : <EOL> for item in qs . items : <EOL> if item . name == '<STR_LIT>' : <EOL> qs . items . remove ( item ) <EOL> break <EOL> else : <EOL> router_quota = neutron_quotas . get ( '<STR_LIT>' ) . limit <EOL> qs . add ( base . QuotaSet ( { '<STR_LIT>' : router_quota } ) ) <EOL> return qs <EOL> def get_disabled_quotas ( request ) : <EOL> disabled_quotas = [ ] <EOL> if not cinder . is_volume_service_enabled ( request ) : <EOL> disabled_quotas . extend ( CINDER_QUOTA_FIELDS ) <EOL> if not base . is_service_enabled ( request , '<STR_LIT>' ) : <EOL> disabled_quotas . extend ( NEUTRON_QUOTA_FIELDS ) <EOL> else : <EOL> disabled_quotas . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if neutron . is_extension_supported ( request , '<STR_LIT>' ) : <EOL> disabled_quotas . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> else : <EOL> disabled_quotas . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> try : <EOL> if not neutron . is_quotas_extension_supported ( request ) : <EOL> disabled_quotas . extend ( NEUTRON_QUOTA_FIELDS ) <EOL> except Exception : <EOL> LOG . exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return disabled_quotas <EOL> def _get_tenant_compute_usages ( request , usages , disabled_quotas , tenant_id ) : <EOL> if tenant_id : <EOL> all_tenants = policy . check ( ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) , <EOL> request ) <EOL> instances , has_more = nova . server_list ( <EOL> request , search_opts = { '<STR_LIT>' : tenant_id } , <EOL> all_tenants = all_tenants ) <EOL> else : <EOL> instances , has_more = nova . server_list ( request ) <EOL> flavors = dict ( [ ( f . id , f ) for f in nova . flavor_list ( request ) ] ) <EOL> missing_flavors = [ instance . flavor [ '<STR_LIT:id>' ] for instance in instances <EOL> if instance . flavor [ '<STR_LIT:id>' ] not in flavors ] <EOL> for missing in missing_flavors : <EOL> if missing not in flavors : <EOL> try : <EOL> flavors [ missing ] = nova . flavor_get ( request , missing ) <EOL> except Exception : <EOL> flavors [ missing ] = { } <EOL> exceptions . handle ( request , ignore = True ) <EOL> usages . tally ( '<STR_LIT>' , len ( instances ) ) <EOL> for flavor in [ flavors [ instance . flavor [ '<STR_LIT:id>' ] ] for instance in instances ] : <EOL> usages . tally ( '<STR_LIT>' , getattr ( flavor , '<STR_LIT>' , None ) ) <EOL> usages . tally ( '<STR_LIT>' , getattr ( flavor , '<STR_LIT>' , None ) ) <EOL> if len ( instances ) == <NUM_LIT:0> : <EOL> usages . tally ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> usages . tally ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> def _get_tenant_network_usages ( request , usages , disabled_quotas , tenant_id ) : <EOL> floating_ips = [ ] <EOL> try : <EOL> if network . floating_ip_supported ( request ) : <EOL> floating_ips = network . tenant_floating_ip_list ( request ) <EOL> except Exception : <EOL> pass <EOL> usages . tally ( '<STR_LIT>' , len ( floating_ips ) ) <EOL> if '<STR_LIT>' not in disabled_quotas : <EOL> security_groups = [ ] <EOL> security_groups = network . security_group_list ( request ) <EOL> usages . tally ( '<STR_LIT>' , len ( security_groups ) ) <EOL> if '<STR_LIT>' not in disabled_quotas : <EOL> networks = [ ] <EOL> networks = neutron . network_list ( request , shared = False ) <EOL> if tenant_id : <EOL> networks = [ net for net in networks if net . tenant_id == tenant_id ] <EOL> usages . tally ( '<STR_LIT>' , len ( networks ) ) <EOL> shared_networks = neutron . network_list ( request , shared = True ) <EOL> if tenant_id : <EOL> shared_networks = [ net for net in shared_networks <EOL> if net . tenant_id == tenant_id ] <EOL> usages . tally ( '<STR_LIT>' , len ( shared_networks ) ) <EOL> if '<STR_LIT>' not in disabled_quotas : <EOL> subnets = [ ] <EOL> subnets = neutron . subnet_list ( request ) <EOL> usages . tally ( '<STR_LIT>' , len ( subnets ) ) <EOL> if '<STR_LIT>' not in disabled_quotas : <EOL> routers = [ ] <EOL> routers = neutron . router_list ( request ) <EOL> if tenant_id : <EOL> routers = [ rou for rou in routers if rou . tenant_id == tenant_id ] <EOL> usages . tally ( '<STR_LIT>' , len ( routers ) ) <EOL> def _get_tenant_volume_usages ( request , usages , disabled_quotas , tenant_id ) : <EOL> if '<STR_LIT>' not in disabled_quotas : <EOL> try : <EOL> if tenant_id : <EOL> opts = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : tenant_id } <EOL> volumes = cinder . volume_list ( request , opts ) <EOL> snapshots = cinder . volume_snapshot_list ( request , opts ) <EOL> else : <EOL> volumes = cinder . volume_list ( request ) <EOL> snapshots = cinder . volume_snapshot_list ( request ) <EOL> usages . tally ( '<STR_LIT>' , sum ( [ int ( v . size ) for v in volumes ] ) ) <EOL> usages . tally ( '<STR_LIT>' , len ( volumes ) ) <EOL> usages . tally ( '<STR_LIT>' , len ( snapshots ) ) <EOL> except cinder . cinder_exception . ClientException : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> exceptions . handle ( request , msg ) <EOL> @ memoized <EOL> def tenant_quota_usages ( request , tenant_id = None ) : <EOL> """<STR_LIT>""" <EOL> if not tenant_id : <EOL> tenant_id = request . user . project_id <EOL> disabled_quotas = get_disabled_quotas ( request ) <EOL> usages = QuotaUsage ( ) <EOL> for quota in get_tenant_quota_data ( request , <EOL> disabled_quotas = disabled_quotas , <EOL> tenant_id = tenant_id ) : <EOL> usages . add_quota ( quota ) <EOL> _get_tenant_compute_usages ( request , usages , disabled_quotas , tenant_id ) <EOL> _get_tenant_network_usages ( request , usages , disabled_quotas , tenant_id ) <EOL> _get_tenant_volume_usages ( request , usages , disabled_quotas , tenant_id ) <EOL> return usages <EOL> def tenant_limit_usages ( request ) : <EOL> limits = { } <EOL> try : <EOL> limits . update ( nova . tenant_absolute_limits ( request ) ) <EOL> except Exception : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> exceptions . handle ( request , msg ) <EOL> if cinder . is_volume_service_enabled ( request ) : <EOL> try : <EOL> limits . update ( cinder . tenant_absolute_limits ( request ) ) <EOL> volumes = cinder . volume_list ( request ) <EOL> snapshots = cinder . volume_snapshot_list ( request ) <EOL> vol_size = sum ( [ getattr ( volume , '<STR_LIT:size>' , <NUM_LIT:0> ) for volume <EOL> in volumes ] ) <EOL> snap_size = sum ( [ getattr ( snap , '<STR_LIT:size>' , <NUM_LIT:0> ) for snap <EOL> in snapshots ] ) <EOL> limits [ '<STR_LIT>' ] = vol_size + snap_size <EOL> limits [ '<STR_LIT>' ] = len ( volumes ) <EOL> limits [ '<STR_LIT>' ] = len ( snapshots ) <EOL> except cinder . cinder_exception . ClientException : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> exceptions . handle ( request , msg ) <EOL> return limits </s>
