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 _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(self, request, project_id, all_tenants): """Get instance list with current telemetry data Example GET: http://localhost/api/ceilometer/instancemonitor """ try: all_tenants = int(all_tenants) except: all_tenants = 0 if project_id == '-1': servers = nova.server_list(request, all_tenants=all_tenants)[0] else: servers = nova.server_list(request, search_opts={'project_id': project_id}, all_tenants=all_tenants)[0] items = [] for s in servers: server = s.to_dict() server['color'] = intToRGB(hashCode(server['id'])) server['host'] = server['OS-EXT-SRV-ATTR:host'] server['zone'] = server['OS-EXT-AZ:availability_zone'] server['full_flavor'] = nova.flavor_get( request, server['flavor']['id']).to_dict() items.append(server) return {'items': items}
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 list_targets(self): tenant_id = self.request.user.tenant_id ports = port_list(self.request, tenant_id=tenant_id) servers, has_more = nova.server_list(self.request) server_dict = collections.OrderedDict( [(s.id, s.name) for s in servers]) reachable_subnets = self._get_reachable_subnets(ports) targets = [] for p in ports: # Remove network ports from Floating IP targets if p.device_owner.startswith('network:'): continue port_id = p.id server_name = server_dict.get(p.device_id) for ip in p.fixed_ips: if ip['subnet_id'] not in reachable_subnets: continue target = {'name': '%s: %s' % (server_name, ip['ip_address']), 'id': '%s_%s' % (port_id, ip['ip_address']), 'port_id': port_id, 'instance_id': p.device_id} targets.append(FloatingIpTarget(target)) return targets
def list_targets(self): tenant_id = self.request.user.tenant_id ports = port_list(self.request, tenant_id=tenant_id) servers, has_more = nova.server_list(self.request) server_dict = SortedDict([(s.id, s.name) for s in servers]) reachable_subnets = self._get_reachable_subnets(ports) if is_service_enabled(self.request, config_name='enable_lb', ext_name='lbaas'): # Also get the loadbalancer VIPs vip_dict = { v['port_id']: v['name'] for v in self.client.list_vips().get('vips', []) } else: vip_dict = {} targets = [] for p in ports: # Remove network ports from Floating IP targets if p.device_owner.startswith('network:'): continue port_id = p.id server_name = server_dict.get(p.device_id) or vip_dict.get(port_id) for ip in p.fixed_ips: if ip['subnet_id'] not in reachable_subnets: continue target = { 'name': '%s: %s' % (server_name, ip['ip_address']), 'id': '%s_%s' % (port_id, ip['ip_address']), 'instance_id': p.device_id } targets.append(FloatingIpTarget(target)) return targets
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 list_targets(self): tenant_id = self.request.user.tenant_id ports = port_list(self.request, tenant_id=tenant_id) servers, has_more = nova.server_list(self.request) server_dict = SortedDict([(s.id, s.name) for s in servers]) reachable_subnets = self._get_reachable_subnets(ports) if is_service_enabled(self.request, config_name='enable_lb', ext_name='lbaas'): # Also get the loadbalancer VIPs vip_dict = {v['port_id']: v['name'] for v in self.client.list_vips().get('vips', [])} else: vip_dict = {} targets = [] for p in ports: # Remove network ports from Floating IP targets if p.device_owner.startswith('network:'): continue port_id = p.id server_name = server_dict.get(p.device_id) or vip_dict.get(port_id) for ip in p.fixed_ips: if ip['subnet_id'] not in reachable_subnets: continue target = {'name': '%s: %s' % (server_name, ip['ip_address']), 'id': '%s_%s' % (port_id, ip['ip_address']), 'instance_id': p.device_id} targets.append(FloatingIpTarget(target)) return targets
def get(cls, request, node_id): node = cls(baremetalclient(request).get(node_id)) node.request = request # FIXME ugly, fix after demo, make abstraction of instance details # this is realy not optimal, but i dont hve time do fix it now instances, more = nova.server_list( request, search_opts={'paginate': True}, all_tenants=True) instance_details = {} for instance in instances: id = (instance. _apiresource._info['OS-EXT-SRV-ATTR:hypervisor_hostname']) instance_details[id] = instance detail = instance_details.get(node_id) if detail: addresses = detail._apiresource.addresses.get('ctlplane') if addresses: node.ip_address_other = (", " .join([addr['addr'] for addr in addresses])) node.status = detail._apiresource._info['OS-EXT-STS:vm_state'] node.power_management = "" if node.pm_user: node.power_management = node.pm_user + "/********" else: node.status = 'unprovisioned' return node
def list_targets(self): tenant_id = self.request.user.tenant_id ports = port_list(self.request, tenant_id=tenant_id) servers, has_more = nova.server_list(self.request) server_dict = SortedDict([(s.id, s.name) for s in servers]) reachable_subnets = self._get_reachable_subnets(ports) if is_service_enabled(self.request, config_name="enable_lb", ext_name="lbaas"): # Also get the loadbalancer VIPs vip_dict = {v["port_id"]: v["name"] for v in self.client.list_vips().get("vips", [])} else: vip_dict = {} targets = [] for p in ports: # Remove network ports from Floating IP targets if p.device_owner.startswith("network:"): continue port_id = p.id server_name = server_dict.get(p.device_id) or vip_dict.get(port_id) for ip in p.fixed_ips: if ip["subnet_id"] not in reachable_subnets: continue target = { "name": "%s: %s" % (server_name, ip["ip_address"]), "id": "%s_%s" % (port_id, ip["ip_address"]), "instance_id": p.device_id, } targets.append(FloatingIpTarget(target)) return targets
def delete(self, request, server_id): """deletes a server :param request: The request object :param server_id: the server's id Example DELETE: http://localhost/api/extension/servers/<server_id> """ autonetwork = autonetwork_name(request) # todo: multiple try/catch blocks try: instances, _more = server_list(request) servers = [u.to_dict() for u in instances] server_to_delete = filter(lambda server: server['id'] == server_id, servers)[0] server_networks = server_to_delete['addresses'] response = server_delete(request, server_id) self._check_and_release_floating_ip(request, server_to_delete, server_networks, autonetwork) updated_servers = self._remove_server_from_serverlist(servers, server_id) if not self._servers_in_autonetwork(updated_servers, autonetwork): self._remove_autonetwork(server_networks, request) except Exception, e: LOG.debug("Server.delete(): {}".formant(e)) raise rest_utils.AjaxError(400, 'Unable to delete instance. ' "'%s'" % e.args[0])
def list(cls, request, associated=None, maintenance=None): """Return a list of Nodes :param request: request object :type request: django.http.HttpRequest :param associated: should we also retrieve all Nodes, only those associated with an Instance, or only those not associated with an Instance? :type associated: bool :param maintenance: should we also retrieve all Nodes, only those in maintenance mode, or those which are not in maintenance mode? :type maintenance: bool :return: list of Nodes, or an empty list if there are none :rtype: list of tuskar_ui.api.node.Node """ nodes = ironicclient(request).node.list(associated=associated, maintenance=maintenance) if associated is None or associated: servers = nova.server_list(request)[0] servers_dict = utils.list_to_dict(servers) nodes_with_instance = [] for n in nodes: server = servers_dict.get(n.instance_uuid, None) nodes_with_instance.append(cls(n, instance=server, request=request)) return [cls.get(request, node.uuid) for node in nodes_with_instance] return [cls.get(request, node.uuid) for node in nodes]
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 get_floating_ips_data(self): try: floating_ips = network.tenant_floating_ip_list(self.request) except Exception: floating_ips = [] exceptions.handle(self.request, _('Unable to retrieve floating IP addresses.')) try: floating_ip_pools = network.floating_ip_pools_list(self.request) except Exception: floating_ip_pools = [] messages.warning(self.request, _('Unable to retrieve floating IP pools.')) pool_dict = dict([(obj.id, obj.name) for obj in floating_ip_pools]) instances = [] try: instances, has_more = nova.server_list(self.request) except Exception: exceptions.handle(self.request, _('Unable to retrieve instance list.')) instances_dict = dict([(obj.id, obj) for obj in instances]) for ip in floating_ips: ip.instance_name = instances_dict[ip.instance_id].name \ if ip.instance_id in instances_dict else None ip.pool_name = pool_dict.get(ip.pool, ip.pool) return floating_ips
def list_deployed_ids(cls, request): """Get and memoize ID's of deployed flavors.""" servers = nova.server_list(request)[0] deployed_ids = set(server.flavor['id'] for server in servers) roles = tuskar.OvercloudRole.list(request) deployed_ids |= set(role.flavor_id for role in roles) return deployed_ids
def get_floating_ips_data(self): try: floating_ips = network.tenant_floating_ip_list(self.request) except: floating_ips = [] exceptions.handle(self.request, _('Unable to retrieve floating IP addresses.')) try: floating_ip_pools = network.floating_ip_pools_list(self.request) except: floating_ip_pools = [] messages.warning(self.request, _('Unable to retrieve floating IP pools.')) pool_dict = dict([(obj.id, obj.name) for obj in floating_ip_pools]) instances = [] try: instances, has_more = nova.server_list(self.request, all_tenants=True) except: exceptions.handle(self.request, _('Unable to retrieve instance list.')) instances_dict = dict([(obj.id, obj) for obj in instances]) for ip in floating_ips: ip.instance_name = instances_dict[ip.instance_id].name \ if ip.instance_id in instances_dict else None ip.pool_name = pool_dict.get(ip.pool, ip.pool) return floating_ips
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_initial(self): self.domain = self.get_domain() results = {'domain_id': self.domain.id, 'domain_name': self.domain.name, } if limit_records_to_fips(): results.update({'fips': tenant_floating_ip_list(self.request), 'instances': server_list(self.request)[0]}) return results
def get_initial(self): try: instances, has_more = nova.server_list(self.request) except Exception: instances = [] exceptions.handle(self.request, _("Unable to retrieve attachment information.")) return {'volume': self.get_object(), 'instances': instances}
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): 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 update(self, request, **kwargs): instances, has_more = nova.server_list(request, search_opts=self.filter) #if(self.filter['power_state']): # instances = [instance for instance in instances if getattr(instance, "OS-EXT-STS:power_state", 0) in self.filter['power_state']] insts = [(inst.id, '{0}({1})'.format(inst.name, inst.id)) for inst in instances] if insts: insts.insert(0, ("", _("Select Instance"))) else: insts.insert(0, ("", _("No instance available"))) self.choices = insts
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 _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 list_deployed_ids(cls, request): """Get and memoize ID's of deployed flavors.""" servers = nova.server_list(request)[0] deployed_ids = set(server.flavor['id'] for server in servers) deployed_names = [] for plan in tuskar_ui.api.tuskar.Plan.list(request): deployed_names.extend( [plan.parameter_value(role.flavor_parameter_name) for role in plan.role_list]) return [flavor.id for flavor in cls.list(request) if flavor.id in deployed_ids or flavor.name in deployed_names]
def _get_instances(self, search_opts=None): try: # TODO(tsufiev): we should pass attached_instance_ids to # nova.server_list as soon as Nova API allows for this instances, has_more = nova.server_list(self.request, search_opts=search_opts) return instances except Exception: exceptions.handle(self.request, _("Unable to retrieve volume/instance " "attachment information")) return []
def get_initial(self): self.domain = self.get_domain() results = { 'domain_id': self.domain.id, 'domain_name': self.domain.name, } if limit_records_to_fips(): results.update({ 'fips': tenant_floating_ip_list(self.request), 'instances': server_list(self.request)[0] }) return results
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 instance(self): """Return the Nova Instance associated with this Node :return: Nova Instance associated with this Node; or None if there is no Instance associated with this Node, or no matching Instance is found :rtype: Instance """ if self.instance_uuid: servers, _has_more_data = nova.server_list(self._request) for server in servers: if server.id == self.instance_uuid: return server
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_instances(self, search_opts=None, instance_ids=None): if not instance_ids: return [] try: # TODO(tsufiev): we should pass attached_instance_ids to # nova.server_list as soon as Nova API allows for this instances, has_more = nova.server_list(self.request, search_opts=search_opts) return instances except Exception: exceptions.handle(self.request, _("Unable to retrieve volume/instance " "attachment information")) return []
def list_deployed_ids(cls, request): """Get and memoize ID's of deployed flavors.""" servers = nova.server_list(request)[0] deployed_ids = set(server.flavor['id'] for server in servers) deployed_names = [] for plan in tuskar_ui.api.tuskar.Plan.list(request): deployed_names.extend([ plan.parameter_value(role.flavor_parameter_name) for role in plan.role_list ]) return [ flavor.id for flavor in cls.list(request) if flavor.id in deployed_ids or flavor.name in deployed_names ]
def list_targets(self): ports = port_list(self.request) servers, has_more = nova.server_list(self.request) server_dict = SortedDict([(s.id, s.name) for s in servers]) targets = [] for p in ports: # Remove network ports from Floating IP targets if p.device_owner.startswith('network:'): continue port_id = p.id server_name = server_dict.get(p.device_id) for ip in p.fixed_ips: target = {'name': '%s: %s' % (server_name, ip['ip_address']), 'id': '%s_%s' % (port_id, ip['ip_address'])} targets.append(FloatingIpTarget(target)) return targets
def list(cls, request, associated=None): nodes = NodeClient(request).node_class.list( request, associated=associated) if associated is None or associated: servers, has_more_data = nova.server_list(request) servers_dict = list_to_dict(servers) nodes_with_instance = [] for n in nodes: server = servers_dict.get(n.instance_uuid, None) nodes_with_instance.append(cls(n, instance=server, request=request)) return nodes_with_instance else: return [cls(node, request=request) for node in nodes]
def get_instance_and_stack(instance_data, request): instance_name = instance_data['name'] stack_name = '' instance_result_data = {} stack_result_data = {} instances, more = nova_api.server_list(request) for instance in instances: if instance_name in instance.name: instance_result_data = {'name': instance.name, 'id': instance.id} stack_name = instance.name.split('-' + instance_name)[0] break # Add link to stack details page if stack_name: stack_result_data = find_stack(stack_name) return instance_result_data, stack_result_data
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 list_targets(self): tenant_id = self.request.user.tenant_id ports = port_list(self.request, tenant_id=tenant_id) servers, has_more = nova.server_list(self.request) server_dict = SortedDict([(s.id, s.name) for s in servers]) targets = [] for p in ports: # Remove network ports from Floating IP targets if p.device_owner.startswith("network:"): continue port_id = p.id server_name = server_dict.get(p.device_id) for ip in p.fixed_ips: target = { "name": "%s: %s" % (server_name, ip["ip_address"]), "id": "%s_%s" % (port_id, ip["ip_address"]), } targets.append(FloatingIpTarget(target)) return targets
def get(self, request): """Provides a list with the user's servers Example GET: http://localhost/api/extension/servers """ try: instances, _more = server_list( request, search_opts=rest_utils.parse_filters_kwargs(request)[0]) except Exception as e: raise rest_utils.AjaxError(400, 'Unable to retrieve instances. ' "'%s'" % e.args[0]) all_servers = [u.to_dict() for u in instances] return { 'items': all_servers }
def get_floating_ips_data(self): try: floating_ips = network.tenant_floating_ip_list(self.request) except neutron_exc.ConnectionFailed: floating_ips = [] exceptions.handle(self.request) except Exception: floating_ips = [] exceptions.handle(self.request, _('Unable to retrieve floating IP addresses.')) try: floating_ip_pools = network.floating_ip_pools_list(self.request) except neutron_exc.ConnectionFailed: floating_ip_pools = [] exceptions.handle(self.request) except Exception: floating_ip_pools = [] exceptions.handle(self.request, _('Unable to retrieve floating IP pools.')) pool_dict = dict([(obj.id, obj.name) for obj in floating_ip_pools]) attached_instance_ids = [ ip.instance_id for ip in floating_ips if ip.instance_id is not None ] if attached_instance_ids: instances = [] try: # TODO(tsufiev): we should pass attached_instance_ids to # nova.server_list as soon as Nova API allows for this instances, has_more = nova.server_list(self.request) except Exception: exceptions.handle(self.request, _('Unable to retrieve instance list.')) instances_dict = dict([(obj.id, obj.name) for obj in instances]) for ip in floating_ips: ip.instance_name = instances_dict.get(ip.instance_id) ip.pool_name = pool_dict.get(ip.pool, ip.pool) return floating_ips
def list(cls, request, associated=None, maintenance=None): if NodeClient.ironic_enabled(request): nodes = NodeClient(request).node_class.list( request, associated=associated, maintenance=maintenance) else: nodes = NodeClient(request).node_class.list( request, associated=associated) if associated is None or associated: servers = nova.server_list(request)[0] servers_dict = utils.list_to_dict(servers) nodes_with_instance = [] for n in nodes: server = servers_dict.get(n.instance_uuid, None) nodes_with_instance.append(cls(n, instance=server, request=request)) return nodes_with_instance else: return [cls(node, request=request) for node in nodes]
def get_floating_ips_data(self): try: floating_ips = network.tenant_floating_ip_list(self.request) except neutron_exc.ConnectionFailed: floating_ips = [] exceptions.handle(self.request) except Exception: floating_ips = [] exceptions.handle(self.request, _('Unable to retrieve floating IP addresses.')) try: floating_ip_pools = network.floating_ip_pools_list(self.request) except neutron_exc.ConnectionFailed: floating_ip_pools = [] exceptions.handle(self.request) except Exception: floating_ip_pools = [] exceptions.handle(self.request, _('Unable to retrieve floating IP pools.')) pool_dict = dict([(obj.id, obj.name) for obj in floating_ip_pools]) attached_instance_ids = [ip.instance_id for ip in floating_ips if ip.instance_id is not None] if attached_instance_ids: instances = [] try: # TODO(tsufiev): we should pass attached_instance_ids to # nova.server_list as soon as Nova API allows for this instances, has_more = nova.server_list(self.request) except Exception: exceptions.handle(self.request, _('Unable to retrieve instance list.')) instances_dict = dict([(obj.id, obj.name) for obj in instances]) for ip in floating_ips: ip.instance_name = instances_dict.get(ip.instance_id) ip.pool_name = pool_dict.get(ip.pool, ip.pool) return floating_ips
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 list(cls, request, associated=None): """Return a list of Nodes in Ironic :param request: request object :type request: django.http.HttpRequest :param associated: should we also retrieve all Nodes, only those associated with an Instance, or only those not associated with an Instance? :type associated: bool :return: list of Nodes, or an empty list if there are none :rtype: list of tuskar_ui.api.Node """ # TODO(Tzu-Mainn Chen): remove test data when possible # nodes = ironicclient(request).nodes.list( # associated=associated) # nodes = test_data().ironicclient_nodes.list() nodes = baremetalclient(request).list() if associated is not None: if associated: nodes = [node for node in nodes if node.instance_uuid is not None] else: nodes = [node for node in nodes if node.instance_uuid is None] return [cls(node, request=request) for node in nodes] servers, has_more_data = nova.server_list(request) servers_dict = list_to_dict(servers) nodes_with_instance = [] for n in nodes: server = servers_dict.get(n.instance_uuid, None) nodes_with_instance.append(cls(n, instance=server, request=request)) return nodes_with_instance
def __init__(self, request, *args, **kwargs): super(SelectInstancesAction, self).__init__(request, *args, **kwargs) err_msg = _('Unable to retrieve members list. ' 'Please try again later.') default_role_field_name = self.get_default_role_field_name() self.fields[default_role_field_name] = forms.CharField(required=False, label='') self.fields[default_role_field_name].initial = 'member' role_member_field_name = self.get_member_field_name('member') self.fields[role_member_field_name] = forms.MultipleChoiceField( required=False, label='') # Get list of available instances all_instances = [] try: all_instances, has_more_data = nova.server_list(request) except Exception: exceptions.handle(request, err_msg) available_instances = [] for instance in all_instances: # skip shutoff instances # if instance.status == 'SHUTOFF': # continue instance_ip = self.get_ip(instance) # skip instances which has no network if not instance_ip: continue key = instance_ip value = instance.name + ' (' + self.get_ip(instance) + ')' available_instances.append((key, value)) self.instance_details[instance_ip] = (instance.name, instance.id) self.fields[self.get_member_field_name('member')].\ choices = available_instances
def get_floating_ips_data(self): try: floating_ips = network.tenant_floating_ip_list(self.request) except neutron_exc.ConnectionFailed: floating_ips = [] exceptions.handle(self.request) except Exception: floating_ips = [] exceptions.handle(self.request, _('Unable to retrieve floating IP addresses.')) try: floating_ip_pools = network.floating_ip_pools_list(self.request) except neutron_exc.ConnectionFailed: floating_ip_pools = [] exceptions.handle(self.request) except Exception: floating_ip_pools = [] exceptions.handle(self.request, _('Unable to retrieve floating IP pools.')) pool_dict = dict([(obj.id, obj.name) for obj in floating_ip_pools]) instances = [] try: instances, has_more = nova.server_list(self.request) except Exception: exceptions.handle(self.request, _('Unable to retrieve instance list.')) instances_dict = dict([(obj.id, obj.name) for obj in instances]) for ip in floating_ips: ip.instance_name = instances_dict.get(ip.instance_id) ip.pool_name = pool_dict.get(ip.pool, ip.pool) return floating_ips
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_context_data(self, request): """ :param request: :return: """ service_data = self.tab_group.kwargs['service'] environment_id = self.tab_group.kwargs['environment_id'] for id, name in STATUS_DISPLAY_CHOICES: if id == service_data.status: status_name = name detail_info = SortedDict([ ('Name', service_data.name), ('ID', service_data.id), ('Type', service_data.full_service_name), ('Status', status_name), ]) if hasattr(service_data, 'unitNamingPattern'): if service_data.unitNamingPattern: text = service_data.unitNamingPattern name_incrementation = True if '#' in text else False if name_incrementation: text += ' (# transforms into index number)' detail_info['Hostname template'] = text if not service_data.domain: detail_info['Domain'] = 'Not in domain' else: detail_info['Domain'] = service_data.domain if hasattr(service_data, 'repository'): detail_info['Application repository'] = service_data.repository if hasattr(service_data, 'uri'): detail_info['Load Balancer URI'] = service_data.uri #check for deployed services so additional information can be added units = [] instance_name = None for unit in service_data.units: if hasattr(unit, 'state'): # unit_detail = {'Name': unit.name} unit_detail = SortedDict() instance_hostname = unit.state.hostname if 'Hostname template' in detail_info: del detail_info['Hostname template'] unit_detail['Hostname'] = instance_hostname instances = nova_api.server_list(request) # HEAT always adds e before instance name instance_name = 'e' + environment_id + '.' + instance_hostname for instance in instances: if instance._apiresource.name == instance_name: unit_detail['instance'] = { 'id': instance._apiresource.id, 'name': instance_name } break if len(service_data.units) > 1: units.append(unit_detail) else: detail_info.update(unit_detail) return {'service': detail_info, 'units': units}
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_instances(request): return [(item.id, item.name) for item in server_list(request)[0]]
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)
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 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