1
# Copyright (c) 2011 OpenStack, LLC.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
19
from cinder.openstack.common import jsonutils
20
from cinder.openstack.common.scheduler import filters
23
class JsonFilter(filters.BaseHostFilter):
24
"""Host Filter to allow simple JSON-based grammar for
27
def _op_compare(self, args, op):
28
"""Returns True if the specified operator can successfully
29
compare the first item in the args with all the rest. Will
30
return False if only one item is in the list.
34
if op is operator.contains:
35
bad = not args[0] in args[1:]
37
bad = [arg for arg in args[1:]
38
if not op(args[0], arg)]
41
def _equals(self, args):
42
"""First term is == all the other terms."""
43
return self._op_compare(args, operator.eq)
45
def _less_than(self, args):
46
"""First term is < all the other terms."""
47
return self._op_compare(args, operator.lt)
49
def _greater_than(self, args):
50
"""First term is > all the other terms."""
51
return self._op_compare(args, operator.gt)
54
"""First term is in set of remaining terms"""
55
return self._op_compare(args, operator.contains)
57
def _less_than_equal(self, args):
58
"""First term is <= all the other terms."""
59
return self._op_compare(args, operator.le)
61
def _greater_than_equal(self, args):
62
"""First term is >= all the other terms."""
63
return self._op_compare(args, operator.ge)
66
"""Flip each of the arguments."""
67
return [not arg for arg in args]
70
"""True if any arg is True."""
74
"""True if all args are True."""
82
'<=': _less_than_equal,
83
'>=': _greater_than_equal,
89
def _parse_string(self, string, host_state):
90
"""Strings prefixed with $ are capability lookups in the
91
form '$variable' where 'variable' is an attribute in the
92
HostState class. If $variable is a dictionary, you may
93
use: $variable.dictkey
97
if not string.startswith("$"):
100
path = string[1:].split(".")
101
obj = getattr(host_state, path[0], None)
104
for item in path[1:]:
105
obj = obj.get(item, None)
110
def _process_filter(self, query, host_state):
111
"""Recursively parse the query structure."""
115
method = self.commands[cmd]
117
for arg in query[1:]:
118
if isinstance(arg, list):
119
arg = self._process_filter(arg, host_state)
120
elif isinstance(arg, basestring):
121
arg = self._parse_string(arg, host_state)
123
cooked_args.append(arg)
124
result = method(self, cooked_args)
127
def host_passes(self, host_state, filter_properties):
128
"""Return a list of hosts that can fulfill the requirements
129
specified in the query.
131
# TODO(zhiteng) Add description for filter_properties structure
132
# and scheduler_hints.
134
query = filter_properties['scheduler_hints']['query']
140
# NOTE(comstud): Not checking capabilities or service for
141
# enabled/disabled so that a provided json filter can decide
143
result = self._process_filter(jsonutils.loads(query), host_state)
144
if isinstance(result, list):
145
# If any succeeded, include the host