4
4
5
5
6
6
def get_capacity (conn ):
7
- capacity = {}
8
7
flavors = list (conn .compute .flavors ())
8
+ print ("Flavor count: " + str (len (flavors )))
9
+
10
+ capacity_per_flavor = {}
9
11
for flavor in flavors :
10
12
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
13
17
14
18
15
19
def get_placement_request (flavor ):
@@ -32,23 +36,38 @@ def get_candidates(conn, resources, required_traits, flavor_name):
32
36
[key + ":" + str (value ) for key , value in resources .items () if value ]
33
37
)
34
38
required_str = "," .join (required_traits )
39
+ # TODO(johngarbut): remove disabled!
35
40
forbidden_str = "COMPUTE_STATUS_DISABLED"
36
41
37
42
response = conn .placement .get (
38
43
"/allocation_candidates" ,
39
44
params = {"resources" : resource_str , "required" : required_str },
40
- headers = {"OpenStack-API-Version" : "placement 1.29" }
45
+ headers = {"OpenStack-API-Version" : "placement 1.29" },
41
46
)
42
47
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
46
64
47
65
48
66
def main ():
49
67
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 ))
52
71
53
72
54
73
main ()
0 commit comments