57c7f2981d1ef05e99896d66fa4fe6d96314b9be
[ganeti-local] / lib / cmdlib / query.py
1 #
2 #
3
4 # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 """Logical units for queries."""
23
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_query 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
36
37
38 #: Query type implementations
39 _QUERY_IMPL = {
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,
48   }
49
50 assert set(_QUERY_IMPL.keys()) == constants.QR_VIA_OP
51
52
53 def _GetQueryImplementation(name):
54   """Returns the implemtnation for a query type.
55
56   @param name: Query type, must be one of L{constants.QR_VIA_OP}
57
58   """
59   try:
60     return _QUERY_IMPL[name]
61   except KeyError:
62     raise errors.OpPrereqError("Unknown query resource '%s'" % name,
63                                errors.ECODE_INVAL)
64
65
66 class LUQuery(NoHooksLU):
67   """Query for resources/items of a certain kind.
68
69   """
70   # pylint: disable=W0142
71   REQ_BGL = False
72
73   def CheckArguments(self):
74     qcls = _GetQueryImplementation(self.op.what)
75
76     self.impl = qcls(self.op.qfilter, self.op.fields, self.op.use_locking)
77
78   def ExpandNames(self):
79     self.impl.ExpandNames(self)
80
81   def DeclareLocks(self, level):
82     self.impl.DeclareLocks(self, level)
83
84   def Exec(self, feedback_fn):
85     return self.impl.NewStyleQuery(self)
86
87
88 class LUQueryFields(NoHooksLU):
89   """Query for resources/items of a certain kind.
90
91   """
92   # pylint: disable=W0142
93   REQ_BGL = False
94
95   def CheckArguments(self):
96     self.qcls = _GetQueryImplementation(self.op.what)
97
98   def ExpandNames(self):
99     self.needed_locks = {}
100
101   def Exec(self, feedback_fn):
102     return query.QueryFields(self.qcls.FIELDS, self.op.fields)