4 # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Logical units for queries."""
24 from ganeti import constants
25 from ganeti import errors
26 from ganeti import query
27 from ganeti.cmdlib.backup import _ExportQuery
28 from ganeti.cmdlib.base import NoHooksLU
29 from ganeti.cmdlib.cluster import _ClusterQuery
30 from ganeti.cmdlib.group import _GroupQuery
31 from ganeti.cmdlib.instance import _InstanceQuery
32 from ganeti.cmdlib.misc import _ExtStorageQuery
33 from ganeti.cmdlib.network import _NetworkQuery
34 from ganeti.cmdlib.node import _NodeQuery
35 from ganeti.cmdlib.operating_system import _OsQuery
38 #: Query type implementations
40 constants.QR_CLUSTER: _ClusterQuery,
41 constants.QR_INSTANCE: _InstanceQuery,
42 constants.QR_NODE: _NodeQuery,
43 constants.QR_GROUP: _GroupQuery,
44 constants.QR_NETWORK: _NetworkQuery,
45 constants.QR_OS: _OsQuery,
46 constants.QR_EXTSTORAGE: _ExtStorageQuery,
47 constants.QR_EXPORT: _ExportQuery,
50 assert set(_QUERY_IMPL.keys()) == constants.QR_VIA_OP
53 def _GetQueryImplementation(name):
54 """Returns the implemtnation for a query type.
56 @param name: Query type, must be one of L{constants.QR_VIA_OP}
60 return _QUERY_IMPL[name]
62 raise errors.OpPrereqError("Unknown query resource '%s'" % name,
66 class LUQuery(NoHooksLU):
67 """Query for resources/items of a certain kind.
70 # pylint: disable=W0142
73 def CheckArguments(self):
74 qcls = _GetQueryImplementation(self.op.what)
76 self.impl = qcls(self.op.qfilter, self.op.fields, self.op.use_locking)
78 def ExpandNames(self):
79 self.impl.ExpandNames(self)
81 def DeclareLocks(self, level):
82 self.impl.DeclareLocks(self, level)
84 def Exec(self, feedback_fn):
85 return self.impl.NewStyleQuery(self)
88 class LUQueryFields(NoHooksLU):
89 """Query for resources/items of a certain kind.
92 # pylint: disable=W0142
95 def CheckArguments(self):
96 self.qcls = _GetQueryImplementation(self.op.what)
98 def ExpandNames(self):
99 self.needed_locks = {}
101 def Exec(self, feedback_fn):
102 return query.QueryFields(self.qcls.FIELDS, self.op.fields)