Skip to content

Commit 11ba949

Browse files
committed
Get first cut of how many of each flavor where
1 parent d2b057e commit 11ba949

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

os_capacity/data/candidates.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55

66
def get_capacity(conn):
7-
capacity = {}
87
flavors = list(conn.compute.flavors())
8+
print("Flavor count: " + str(len(flavors)))
9+
10+
capacity_per_flavor = {}
911
for flavor in flavors:
1012
resources, traits = get_placement_request(flavor)
11-
candidates, total = get_candidates(conn, resources, traits, flavor.name)
12-
return capacity
13+
candidates = get_candidates(conn, resources, traits, flavor.name)
14+
# intentionally add empty responses into this
15+
capacity_per_flavor[flavor.name] = candidates
16+
return capacity_per_flavor
1317

1418

1519
def get_placement_request(flavor):
@@ -32,23 +36,38 @@ def get_candidates(conn, resources, required_traits, flavor_name):
3236
[key + ":" + str(value) for key, value in resources.items() if value]
3337
)
3438
required_str = ",".join(required_traits)
39+
# TODO(johngarbut): remove disabled!
3540
forbidden_str = "COMPUTE_STATUS_DISABLED"
3641

3742
response = conn.placement.get(
3843
"/allocation_candidates",
3944
params={"resources": resource_str, "required": required_str},
40-
headers={"OpenStack-API-Version": "placement 1.29"}
45+
headers={"OpenStack-API-Version": "placement 1.29"},
4146
)
4247
raw_data = response.json()
43-
print(raw_data)
44-
raise Exception("asdf")
45-
return [], 0
48+
count_per_rp = {}
49+
for rp_uuid, summary in raw_data.get("provider_summaries", {}).items():
50+
# per resource, get max possible number of instances
51+
max_counts = []
52+
for resource, amounts in summary["resources"].items():
53+
requested = resources.get(resource, 0)
54+
if requested:
55+
free = amounts["capacity"] - amounts["used"]
56+
amount = int(free / requested)
57+
max_counts.append(amount)
58+
# available count is the min of the max counts
59+
if max_counts:
60+
count_per_rp[rp_uuid] = min(max_counts)
61+
62+
print("Found " + str(len(count_per_rp.keys())) + " candidate hosts for " + flavor_name)
63+
return count_per_rp
4664

4765

4866
def main():
4967
conn = openstack.connect()
50-
capacity = get_capacity(conn)
51-
print(capacity)
68+
capacity_per_flavor = get_capacity(conn)
69+
import json
70+
print(json.dumps(capacity_per_flavor, indent=2))
5271

5372

5473
main()

0 commit comments

Comments
 (0)