Coverage for nova/scheduler/filters/utils.py: 100%
29 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-24 11:16 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-24 11:16 +0000
1# All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
15"""Bench of utility methods used by filters."""
17import collections
19from oslo_log import log as logging
21LOG = logging.getLogger(__name__)
24def aggregate_values_from_key(host_state, key_name):
25 """Returns a set of values based on a metadata key for a specific host."""
26 aggrlist = host_state.aggregates
27 return {aggr.metadata[key_name]
28 for aggr in aggrlist
29 if key_name in aggr.metadata
30 }
33def aggregate_metadata_get_by_host(host_state, key=None):
34 """Returns a dict of all metadata based on a metadata key for a specific
35 host. If the key is not provided, returns a dict of all metadata.
36 """
37 aggrlist = host_state.aggregates
38 metadata = collections.defaultdict(set)
39 for aggr in aggrlist:
40 if key is None or key in aggr.metadata:
41 for k, v in aggr.metadata.items():
42 metadata[k].update(x.strip() for x in v.split(','))
43 return metadata
46def validate_num_values(vals, default=None, cast_to=int, based_on=min):
47 """Returns a correctly casted value based on a set of values.
49 This method is useful to work with per-aggregate filters, It takes
50 a set of values then return the 'based_on'{min/max} converted to
51 'cast_to' of the set or the default value.
53 Note: The cast implies a possible ValueError
54 """
55 num_values = len(vals)
56 if num_values == 0:
57 return default
59 if num_values > 1:
60 if based_on == min:
61 LOG.info("%(num_values)d values found, "
62 "of which the minimum value will be used.",
63 {'num_values': num_values})
64 else:
65 LOG.info("%(num_values)d values found, "
66 "of which the maximum value will be used.",
67 {'num_values': num_values})
68 return based_on([cast_to(val) for val in vals])
71def instance_uuids_overlap(host_state, uuids):
72 """Tests for overlap between a host_state and a list of uuids.
74 Returns True if any of the supplied uuids match any of the instance.uuid
75 values in the host_state.
76 """
77 if isinstance(uuids, str):
78 uuids = [uuids]
79 set_uuids = set(uuids)
80 # host_state.instances is a dict whose keys are the instance uuids
81 host_uuids = set(host_state.instances.keys())
82 return bool(host_uuids.intersection(set_uuids))