root / lib / cmdlib / query.py @ 31d3b918
History | View | Annotate | Download (2.4 kB)
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.base import NoHooksLU |
28 |
from ganeti.cmdlib.cluster import ClusterQuery |
29 |
from ganeti.cmdlib.misc import ExtStorageQuery |
30 |
from ganeti.cmdlib.operating_system import OsQuery |
31 |
|
32 |
|
33 |
#: Query type implementations
|
34 |
_QUERY_IMPL = { |
35 |
constants.QR_CLUSTER: ClusterQuery, |
36 |
constants.QR_OS: OsQuery, |
37 |
constants.QR_EXTSTORAGE: ExtStorageQuery, |
38 |
} |
39 |
|
40 |
assert set(_QUERY_IMPL.keys()) == constants.QR_VIA_OP |
41 |
|
42 |
|
43 |
def _GetQueryImplementation(name): |
44 |
"""Returns the implementation for a query type.
|
45 |
|
46 |
@param name: Query type, must be one of L{constants.QR_VIA_OP}
|
47 |
|
48 |
"""
|
49 |
try:
|
50 |
return _QUERY_IMPL[name]
|
51 |
except KeyError: |
52 |
raise errors.OpPrereqError("Unknown query resource '%s'" % name, |
53 |
errors.ECODE_INVAL) |
54 |
|
55 |
|
56 |
class LUQuery(NoHooksLU): |
57 |
"""Query for resources/items of a certain kind.
|
58 |
|
59 |
"""
|
60 |
# pylint: disable=W0142
|
61 |
REQ_BGL = False
|
62 |
|
63 |
def CheckArguments(self): |
64 |
qcls = _GetQueryImplementation(self.op.what)
|
65 |
|
66 |
self.impl = qcls(self.op.qfilter, self.op.fields, self.op.use_locking) |
67 |
|
68 |
def ExpandNames(self): |
69 |
self.impl.ExpandNames(self) |
70 |
|
71 |
def DeclareLocks(self, level): |
72 |
self.impl.DeclareLocks(self, level) |
73 |
|
74 |
def Exec(self, feedback_fn): |
75 |
return self.impl.NewStyleQuery(self) |
76 |
|
77 |
|
78 |
class LUQueryFields(NoHooksLU): |
79 |
"""Query for resources/items of a certain kind.
|
80 |
|
81 |
"""
|
82 |
# pylint: disable=W0142
|
83 |
REQ_BGL = False
|
84 |
|
85 |
def CheckArguments(self): |
86 |
self.qcls = _GetQueryImplementation(self.op.what) |
87 |
|
88 |
def ExpandNames(self): |
89 |
self.needed_locks = {}
|
90 |
|
91 |
def Exec(self, feedback_fn): |
92 |
return query.QueryFields(self.qcls.FIELDS, self.op.fields) |