def show_by_host(self, req, tenant_id): """ list platform usage """ def get_zone_by_host(zones, host): for zone in zones: if host in zone["hosts"]: return zone["zoneName"] auth_token = req.headers.get("x-auth-token") m = manager.Manager() usages = m.request_usages(models.HOST_LEVEL) hosts_capacity = ops_api.get_hosts_capacity(tenant_id, auth_token) zones = ops_api.get_hosts_az(tenant_id, auth_token) if "availability_zones" not in zones: LOG.warn("availability_zones does not exists in zones(%s)" % zones) zones = zones.get("availability_zones", []) for v in usages: # NOTE(hzzhoushaoyu): usages is a iterative value and here is only # one item. items = self._construct_usage(v) result = [] for hostname in items: value = items[hostname] value.update(hostname=hostname) value.update(hosts_capacity.get(hostname, {})) zone_name = get_zone_by_host(zones, hostname) value.update(availability_zone=zone_name) # FIXME : This is a little ugly. when instances in the host # doesn't use any floating ip, we need to set the # ips_used number to 0. Currently I can't find a much # proper place to do this thing. if "private_ips_used" not in value.keys(): value.update(private_ips_used=0) if "public_ips_used" not in value.keys(): value.update(public_ips_used=0) result.append(value) # Add statistics for hosts with no vms. for hostname in hosts_capacity.keys(): already_counted = False for value in result: if value["hostname"] == hostname: already_counted = True break if not already_counted: value = {} value.update(hostname=hostname) value.update(hosts_capacity.get(hostname, {})) value.update(private_ips_used=0) value.update(public_ips_used=0) zone_name = get_zone_by_host(zones, hostname) value.update(availability_zone=zone_name) result.append(value) result = list_filter.filter_host_usages(req, result) result = list_sort.sort_host_usage(req, result) return dict(hosts=result)