diff --git a/os_capacity/commands/commands.py b/os_capacity/commands/commands.py index 9969c75..84acf52 100644 --- a/os_capacity/commands/commands.py +++ b/os_capacity/commands/commands.py @@ -28,7 +28,8 @@ class FlavorList(Lister): def take_action(self, parsed_args): flavors = utils.get_flavors(self.app) - return (('UUID', 'Name', 'VCPUs', 'RAM MB', 'DISK GB'), flavors) + return (('UUID', 'Name', 'VCPUs', 'RAM MB', 'DISK GB', 'Extra Specs'), + flavors) class ListResourcesAll(Lister): diff --git a/os_capacity/data/flavors.py b/os_capacity/data/flavors.py index 6e45b6f..6d5f69a 100644 --- a/os_capacity/data/flavors.py +++ b/os_capacity/data/flavors.py @@ -16,12 +16,18 @@ Flavor = collections.namedtuple( - "Flavor", ("id", "name", "vcpus", "ram_mb", "disk_gb")) + "Flavor", ("id", "name", "vcpus", "ram_mb", "disk_gb", "extra_specs")) -def get_all(compute_client): - response = compute_client.get("/flavors/detail").json() +def get_all(compute_client, include_extra_specs=True): + response = compute_client.get('/flavors/detail').json() raw_flavors = response['flavors'] - # TODO(johngarbutt) disk should probably include ephemeral - return [Flavor(f['id'], f['name'], f['vcpus'], f['ram'], f['disk']) + if include_extra_specs: + for flavor in raw_flavors: + url = '/flavors/%s/os-extra_specs' % flavor['id'] + response = compute_client.get(url).json() + flavor['extra_specs'] = response['extra_specs'] + return [Flavor(f['id'], f['name'], f['vcpus'], f['ram'], + (f['disk'] + f['OS-FLV-EXT-DATA:ephemeral']), + f.get('extra_specs')) for f in raw_flavors] diff --git a/os_capacity/utils.py b/os_capacity/utils.py index 52187f5..94bfe65 100644 --- a/os_capacity/utils.py +++ b/os_capacity/utils.py @@ -24,8 +24,7 @@ def get_flavors(app): app.LOG.debug("Getting flavors") - raw_flavors = flavors.get_all(app.compute_client) - return [(f.id, f.name, f.vcpus, f.ram_mb, f.disk_gb) for f in raw_flavors] + return flavors.get_all(app.compute_client, include_extra_specs=True) def get_providers_with_resources_and_servers(app): @@ -54,7 +53,13 @@ def group_providers_by_type_with_capacity(app): all_flavors = flavors.get_all(app.compute_client) grouped_flavors = collections.defaultdict(list) for flavor in all_flavors: - key = (flavor.vcpus, flavor.ram_mb, flavor.disk_gb) + custom_rc = None + for extra_spec in flavor.extra_specs: + if extra_spec.startswith('resources:CUSTOM'): + custom_rc = extra_spec.replace('resources:', '') + break # Assuming a good Ironic setup here + + key = (flavor.vcpus, flavor.ram_mb, flavor.disk_gb, custom_rc) grouped_flavors[key] += [flavor.name] all_resource_providers = resource_provider.get_all(app.placement_client) @@ -69,6 +74,7 @@ def group_providers_by_type_with_capacity(app): vcpus = 0 ram_mb = 0 disk_gb = 0 + custom_rc = None for inventory in inventories: if "VCPU" in inventory.resource_class: vcpus += inventory.total @@ -76,7 +82,9 @@ def group_providers_by_type_with_capacity(app): ram_mb += inventory.total if "DISK" in inventory.resource_class: disk_gb += inventory.total - key = (vcpus, ram_mb, disk_gb) + if inventory.resource_class.startswith('CUSTOM_'): + custom_rc = inventory.resource_class # Ironic specific + key = (vcpus, ram_mb, disk_gb, custom_rc) inventory_counts[key] += 1 @@ -86,8 +94,9 @@ def group_providers_by_type_with_capacity(app): allocation_counts[key] += 1 for key, inventory_count in inventory_counts.items(): - resources = "VCPU:%s,MEMORY_MB:%s,DISK_GB:%s" % key + resources = "VCPU:%s,MEMORY_MB:%s,DISK_GB:%s,%s" % key matching_flavors = grouped_flavors[key] + matching_flavors.sort() matching_flavors = ", ".join(matching_flavors) total = inventory_count used = allocation_counts[key]