def tenant_quota_usages(request): # Get our quotas and construct our usage object. usages = QuotaUsage() for quota in get_tenant_quota_data(request): usages.add_quota(quota) # Get our usages. floating_ips = nova.tenant_floating_ip_list(request) flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) volumes = cinder.volume_list(request) instances = 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: flavors[missing] = {} exceptions.handle(request, ignore=True) usages.tally('instances', len(instances)) usages.tally('floating_ips', len(floating_ips)) usages.tally('volumes', len(volumes)) usages.tally('gigabytes', sum([int(v.size) for v in volumes])) # 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)) return usages
def __init__(self, request, context, *args, **kwargs): super(GeneralConfigurationAction, self).__init__(request, context, *args, **kwargs) templates = list_templates(request) jt_nn_templates = ((t.name, t.name) for t in templates if ("JT+NN" == t.node_type)) jt_templates = ((t.name, t.name) for t in templates if ("JT" == t.node_type)) nn_templates = ((t.name, t.name) for t in templates if ("NN" == t.node_type)) worker_templates = ((t.name, t.name) for t in templates if ("TT+DN" == t.node_type)) self.fields['jt_nn_template_choices'].choices = jt_nn_templates self.fields['jt_template_choices'].choices = jt_templates self.fields['nn_template_choices'].choices = nn_templates self.fields['worker_template_choices'].choices = worker_templates self.templates = templates self.template_infos = {} flavors = nova.flavor_list(request) for template in templates: flavor_name = template.flavor_name flavor = filter(lambda fl: fl.name == flavor_name, flavors)[0] self.template_infos[template.name] =\ "%s vcpu, %s Mb RAM, %s Gb disk" % ( flavor.vcpus, flavor.ram, flavor.disk)
def _get_tenant_compute_usages(request, usages, disabled_quotas, tenant_id, region=None): if tenant_id: instances, has_more = nova.server_list( request, search_opts={'tenant_id': tenant_id}, all_tenants=True, region=region) else: instances, has_more = nova.server_list(request, region=region) # Fetch deleted flavors if necessary. flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) 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)) # Sum our usage based on the flavors of the instances. # change by zhihao.ding 2015/7/17 for kill_flavor start #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)) for instance in instances: usages.tally('cores', instance.vcpus) usages.tally('ram', instance.memory_mb) # change by zhihao.ding 2015/7/17 for kill_flavor end # Initialise the tally if no instances have been launched yet if len(instances) == 0: usages.tally('cores', 0) usages.tally('ram', 0)
def _get_tenant_compute_usages(request, usages, disabled_quotas, tenant_id): 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. flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) 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)) # 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)
def tenant_quota_usages(request): cloud = None if 'cloud' in request.GET: cloud = request.GET['cloud'] elif 'cloud' in request.POST: cloud = request.POST['cloud'] # Get our quotas and construct our usage object. disabled_quotas = [] if not is_service_enabled(request, 'volume'): disabled_quotas.extend(['volumes', 'gigabytes']) usages = QuotaUsage() for quota in get_tenant_quota_data(request, disabled_quotas): usages.add_quota(quota) # Get our usages. floating_ips = nova.tenant_floating_ip_list(request) #flavors = dict([(f.id, f) for f in nova.flavor_list(request) if limit_by_cloud(f) ]) flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) instances = nova.server_list(request) if cloud is not None: instances = [ instance for instance in instances if get_cloud(instance) == cloud ] # 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: flavors[missing] = {} exceptions.handle(request, ignore=True) usages.tally('instances', len(instances)) usages.tally('floating_ips', len(floating_ips)) if 'volumes' not in disabled_quotas: volumes = cinder.volume_list(request) usages.tally('gigabytes', sum([int(v.size) for v in volumes])) usages.tally('volumes', len(volumes)) # 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_flavor_names(request): # TODO(lsmola) The flavors can be set per project, # so it should show only valid ones. try: flavors = nova.flavor_list(request, None) return [f.name for f in flavors] except Exception: return ['m1.tiny', 'm1.small', 'm1.medium', 'm1.large', 'm1.xlarge']
def get_flavor_names(request): # TODO(lsmola) The flavors can be set per project, # so it should show only valid ones. try: flavors = nova.flavor_list(request, None) return [f.name for f in flavors] except Exception: return ["m1.tiny", "m1.small", "m1.medium", "m1.large", "m1.xlarge"]
def tenant_quota_usages(request): cloud = None if 'cloud' in request.GET: cloud = request.GET['cloud'] elif 'cloud' in request.POST: cloud = request.POST['cloud'] # Get our quotas and construct our usage object. disabled_quotas = [] if not is_service_enabled(request, 'volume'): disabled_quotas.extend(['volumes', 'gigabytes']) usages = QuotaUsage() for quota in get_tenant_quota_data(request, disabled_quotas): usages.add_quota(quota) # Get our usages. floating_ips = nova.tenant_floating_ip_list(request) #flavors = dict([(f.id, f) for f in nova.flavor_list(request) if limit_by_cloud(f) ]) flavors = dict([(f.id, f) for f in nova.flavor_list(request) ]) instances = nova.server_list(request) if cloud is not None: instances = [instance for instance in instances if get_cloud(instance) == cloud] # 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: flavors[missing] = {} exceptions.handle(request, ignore=True) usages.tally('instances', len(instances)) usages.tally('floating_ips', len(floating_ips)) if 'volumes' not in disabled_quotas: volumes = cinder.volume_list(request) usages.tally('gigabytes', sum([int(v.size) for v in volumes])) usages.tally('volumes', len(volumes)) # 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 populate_flavor_choices(self, request, context): try: flavors = nova.flavor_list(request) flavor_list = [(flavor.id, "%s" % flavor.name) for flavor in flavors] except Exception: flavor_list = [] exceptions.handle(request, _('Unable to retrieve instance flavors.')) return sorted(flavor_list)
def _get_tenant_compute_usages(request, usages, disabled_quotas, tenant_id): enabled_compute_quotas = NOVA_COMPUTE_QUOTA_FIELDS - disabled_quotas if not enabled_compute_quotas: return # Unlike the other services it can be the case that nova is enabled but # doesn't support quotas, in which case we still want to get usage info, # so don't rely on '"instances" in disabled_quotas' as elsewhere if not base.is_service_enabled(request, 'compute'): return if tenant_id and tenant_id != request.user.project_id: # all_tenants is required when querying about any project the user is # not currently scoped to instances, has_more = nova.server_list(request, search_opts={ 'tenant_id': tenant_id, 'all_tenants': True }) else: instances, has_more = nova.server_list(request) _add_usage_if_quota_enabled(usages, 'instances', len(instances), disabled_quotas) if {'cores', 'ram'} - disabled_quotas: # Fetch deleted flavors if necessary. flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) 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) # Sum our usage based on the flavors of the instances. for flavor in [ flavors[instance.flavor['id']] for instance in instances ]: _add_usage_if_quota_enabled(usages, 'cores', getattr(flavor, 'vcpus', None), disabled_quotas) _add_usage_if_quota_enabled(usages, 'ram', getattr(flavor, 'ram', None), disabled_quotas) # Initialize the tally if no instances have been launched yet if len(instances) == 0: _add_usage_if_quota_enabled(usages, 'cores', 0, disabled_quotas) _add_usage_if_quota_enabled(usages, 'ram', 0, disabled_quotas)
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 '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 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. try: floating_ips = network.tenant_floating_ip_list(request) except neutronclient.NeutronClientException: floating_ips = [] 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 '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 get_flavors(self): try: flavors = nova.flavor_list(self.request) except nova_exceptions.ClientException: message = _("Failed to get list of flavors.") exceptions.handle(self.request, message) LOG.exception(message) flavors = [] def extract(flavor): info = flavor._info return {k: v for (k, v) in info.items() if k != 'links'} flavors = [extract(f) for f in flavors] self.storage.extra_data['flavors'] = flavors return json.dumps(flavors)
def _get_tenant_compute_usages(request, usages, disabled_quotas, tenant_id): enabled_compute_quotas = NOVA_COMPUTE_QUOTA_FIELDS - disabled_quotas if not enabled_compute_quotas: return # Unlike the other services it can be the case that nova is enabled but # doesn't support quotas, in which case we still want to get usage info, # so don't rely on '"instances" in disabled_quotas' as elsewhere if not base.is_service_enabled(request, 'compute'): return if tenant_id: instances, has_more = nova.server_list( request, search_opts={'tenant_id': tenant_id}) else: instances, has_more = nova.server_list(request) _add_usage_if_quota_enabled(usages, 'instances', len(instances), disabled_quotas) if {'cores', 'ram'} - disabled_quotas: # Fetch deleted flavors if necessary. flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) 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) # Sum our usage based on the flavors of the instances. for flavor in [flavors[instance.flavor['id']] for instance in instances]: _add_usage_if_quota_enabled( usages, 'cores', getattr(flavor, 'vcpus', None), disabled_quotas) _add_usage_if_quota_enabled( usages, 'ram', getattr(flavor, 'ram', None), disabled_quotas) # Initialize the tally if no instances have been launched yet if len(instances) == 0: _add_usage_if_quota_enabled(usages, 'cores', 0, disabled_quotas) _add_usage_if_quota_enabled(usages, 'ram', 0, disabled_quotas)
def _get_tenant_compute_usages(request, usages, disabled_quotas, tenant_id): # Unlike the other services it can be the case that nova is enabled but # doesn't support quotas, in which case we still want to get usage info, # so don't rely on '"instances" in disabled_quotas' as elsewhere if not base.is_service_enabled(request, 'compute'): return 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) # Fetch deleted flavors if necessary. flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) 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)) # 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)
def tenant_quota_usages(request): # Get our quotas and construct our usage object. disabled_quotas = [] if not is_service_enabled(request, "volume"): disabled_quotas.extend(["volumes", "gigabytes"]) usages = QuotaUsage() for quota in get_tenant_quota_data(request, disabled_quotas): usages.add_quota(quota) # Get our usages. floating_ips = network.tenant_floating_ip_list(request) flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) instances = 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: flavors[missing] = {} exceptions.handle(request, ignore=True) usages.tally("instances", len(instances)) usages.tally("floating_ips", len(floating_ips)) if "volumes" not in disabled_quotas: volumes = cinder.volume_list(request) usages.tally("gigabytes", sum([int(v.size) for v in volumes])) usages.tally("volumes", len(volumes)) # 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_tenant_compute_usages(request, usages, disabled_quotas, tenant_id): enabled_compute_quotas = NOVA_COMPUTE_QUOTA_FIELDS - disabled_quotas if not enabled_compute_quotas: return # Unlike the other services it can be the case that nova is enabled but # doesn't support quotas, in which case we still want to get usage info, # so don't rely on '"instances" in disabled_quotas' as elsewhere if not base.is_service_enabled(request, 'compute'): return if tenant_id: instances, has_more = nova.server_list( request, search_opts={'tenant_id': tenant_id}) else: instances, has_more = nova.server_list(request) # Fetch deleted flavors if necessary. flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) 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)) # 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)
def _get_tenant_compute_usages(request, usages, disabled_quotas, tenant_id): # Unlike the other services it can be the case that nova is enabled but # doesn't support quotas, in which case we still want to get usage info, # so don't rely on '"instances" in disabled_quotas' as elsewhere if not base.is_service_enabled(request, 'compute'): return 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) # Fetch deleted flavors if necessary. flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) 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)) # 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)
def tenant_quota_usages(request): # Get our quotas and construct our usage object. disabled_quotas = [] if not is_service_enabled(request, 'volume'): disabled_quotas.extend(['volumes', 'gigabytes']) usages = QuotaUsage() for quota in get_tenant_quota_data(request, disabled_quotas): usages.add_quota(quota) # Get our usages. floating_ips = nova.tenant_floating_ip_list(request) flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) instances = 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: flavors[missing] = {} exceptions.handle(request, ignore=True) usages.tally('instances', len(instances)) usages.tally('floating_ips', len(floating_ips)) if 'volumes' not in disabled_quotas: volumes = cinder.volume_list(request) usages.tally('gigabytes', sum([int(v.size) for v in volumes])) usages.tally('volumes', len(volumes)) # 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)) return usages
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 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
def list(cls, request): return [cls(item) for item in nova.flavor_list(request)]
def get_flavors(request): #nova = NovaClient('2', ADMIN_USERNAME, ADMIN_PASSWORD, ADMIN_TENANT, ADMIN_AUTH_URL) return nova.flavor_list(request)
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 = network.tenant_floating_ip_list(request) flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) instances = 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: flavors[missing] = {} exceptions.handle(request, ignore=True) # jt project_id = request.user.tenant_id resources = api.jt.get_used_resources(project_id) floating_ips = resources.get('floating_ips', 0) instances = resources.get('instances', 0) cores = resources.get('cores', 0) ram = resources.get('ram', 0) gigabytes = resources.get('gigabytes', 0) volumes = resources.get('volumes', 0) # jt #usages.tally('instances', len(instances)) #usages.tally('floating_ips', len(floating_ips)) usages.tally('instances', instances) usages.tally('floating_ips', floating_ips) usages.tally('cores', cores) usages.tally('ram', ram) if 'volumes' not in disabled_quotas: # jt #volumes = cinder.volume_list(request) #usages.tally('gigabytes', sum([int(v.size) for v in volumes])) #usages.tally('volumes', len(volumes)) usages.tally('gigabytes', gigabytes) usages.tally('volumes', volumes) # jt # 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)) # jt # Initialise the tally if no instances have been launched yet if instances == 0: #if len(instances) == 0: usages.tally('cores', 0) usages.tally('ram', 0) # Images owned_image_count = api.jt.get_image_count(project_id, request) image_limit = api.jt.get_image_quota(project_id) usages['images']['quota'] = image_limit usages['images']['used'] = owned_image_count usages['images']['available'] = image_limit - owned_image_count # Expiration expiration_date = api.jt.get_expiration_date(project_id) usages['expiration']['quota'] = -1 usages['expiration']['used'] = 0 usages['expiration']['available'] = 0 usages['expiration']['expiration_date'] = expiration_date # Start Date start_date = api.jt.get_start_date(project_id) usages['start']['quota'] = -1 usages['start']['used'] = 0 usages['start']['available'] = 0 usages['start']['start_date'] = start_date # DAIR Notice dair_notice = api.jt.get_dair_notice(project_id) usages['dair_notice']['quota'] = -1 usages['dair_notice']['used'] = 0 usages['dair_notice']['available'] = 0 usages['dair_notice']['dair_notice'] = dair_notice # Object Storage object_mb_usage = api.jt.get_object_mb_usage(project_id) object_mb_limit = api.jt.get_object_mb_quota(project_id) usages['object_mb']['quota'] = object_mb_limit usages['object_mb']['used'] = object_mb_usage usages['object_mb']['available'] = object_mb_limit - object_mb_usage return usages
def get_internalinstances_data(self): request = self.tab_group.request instances = [] marker = self.request.GET.get( project_tables.InstancesTable._meta.pagination_param, None) # # Gather our instances try: instances, self._more = nova.server_list( self.request, search_opts={'marker': marker, 'paginate': True}) except Exception: self._more = False instances = [] exceptions.handle(self.request, _('Unable to retrieve instances.')) # Gather our flavors and images and correlate our instances to them if instances: try: flavors = nova.flavor_list(self.request) except Exception: flavors = [] exceptions.handle(self.request, ignore=True) try: # TODO(gabriel): Handle pagination. images, more = glance.image_list_detailed(self.request) except Exception: images = [] exceptions.handle(self.request, ignore=True) full_flavors = SortedDict([(str(flavor.id), flavor) for flavor in flavors]) image_map = SortedDict([(str(image.id), image) for image in images]) # Loop through instances to get flavor info. for instance in instances: if hasattr(instance, 'image'): # Instance from image returns dict if isinstance(instance.image, dict): if instance.image.get('id') in image_map: instance.image = image_map[instance.image['id']] else: # Instance from volume returns a string instance.image = {'name': instance.image if instance.image else _("-")} try: flavor_id = instance.flavor["id"] if flavor_id in full_flavors: instance.full_flavor = full_flavors[flavor_id] else: # If the flavor_id is not in full_flavors list, # get it via nova api. instance.full_flavor = nova.flavor_get( self.request, flavor_id) except Exception: msg = _('Unable to retrieve instance size information.') exceptions.handle(self.request, msg) return instances
def _get_tenant_compute_usages(request, usages, disabled_quotas, tenant_id): 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) ''' if is_m1_user_admin(request): instances, has_more = server_list_nova( request, search_opts={'tenant_id': tenant_id}, all_tenants=all_tenants) else: 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) if is_m1_user_admin(request): instances, has_more = server_list_nova(request) else: instances, has_more = nova.server_list(request) # Fetch deleted flavors if necessary. #flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) if is_m1_user_admin(request): flavors = dict([(f.id, f) for f in flavor_list_nova(request)]) else: flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) 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) if is_m1_user_admin(request): flavors[missing] = flavor_get_nova(request, missing) else: flavors[missing] = nova.flavor_get(request, missing) except Exception: flavors[missing] = {} exceptions.handle(request, ignore=True) 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)) # Initialise the tally if no instances have been launched yet if len(instances) == 0: usages.tally('cores', 0) usages.tally('ram', 0)