Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib / query.py @ 1c4910f7

History | View | Annotate | Download (2.4 kB)

1 ec3cc4a8 Thomas Thrainer
#
2 ec3cc4a8 Thomas Thrainer
#
3 ec3cc4a8 Thomas Thrainer
4 ec3cc4a8 Thomas Thrainer
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
5 ec3cc4a8 Thomas Thrainer
#
6 ec3cc4a8 Thomas Thrainer
# This program is free software; you can redistribute it and/or modify
7 ec3cc4a8 Thomas Thrainer
# it under the terms of the GNU General Public License as published by
8 ec3cc4a8 Thomas Thrainer
# the Free Software Foundation; either version 2 of the License, or
9 ec3cc4a8 Thomas Thrainer
# (at your option) any later version.
10 ec3cc4a8 Thomas Thrainer
#
11 ec3cc4a8 Thomas Thrainer
# This program is distributed in the hope that it will be useful, but
12 ec3cc4a8 Thomas Thrainer
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 ec3cc4a8 Thomas Thrainer
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ec3cc4a8 Thomas Thrainer
# General Public License for more details.
15 ec3cc4a8 Thomas Thrainer
#
16 ec3cc4a8 Thomas Thrainer
# You should have received a copy of the GNU General Public License
17 ec3cc4a8 Thomas Thrainer
# along with this program; if not, write to the Free Software
18 ec3cc4a8 Thomas Thrainer
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 ec3cc4a8 Thomas Thrainer
# 02110-1301, USA.
20 ec3cc4a8 Thomas Thrainer
21 ec3cc4a8 Thomas Thrainer
22 ec3cc4a8 Thomas Thrainer
"""Logical units for queries."""
23 ec3cc4a8 Thomas Thrainer
24 ec3cc4a8 Thomas Thrainer
from ganeti import constants
25 ec3cc4a8 Thomas Thrainer
from ganeti import errors
26 ec3cc4a8 Thomas Thrainer
from ganeti import query
27 ec3cc4a8 Thomas Thrainer
from ganeti.cmdlib.base import NoHooksLU
28 5eacbcae Thomas Thrainer
from ganeti.cmdlib.cluster import ClusterQuery
29 5eacbcae Thomas Thrainer
from ganeti.cmdlib.misc import ExtStorageQuery
30 5eacbcae Thomas Thrainer
from ganeti.cmdlib.operating_system import OsQuery
31 ec3cc4a8 Thomas Thrainer
32 ec3cc4a8 Thomas Thrainer
33 ec3cc4a8 Thomas Thrainer
#: Query type implementations
34 ec3cc4a8 Thomas Thrainer
_QUERY_IMPL = {
35 5eacbcae Thomas Thrainer
  constants.QR_CLUSTER: ClusterQuery,
36 5eacbcae Thomas Thrainer
  constants.QR_OS: OsQuery,
37 5eacbcae Thomas Thrainer
  constants.QR_EXTSTORAGE: ExtStorageQuery,
38 ec3cc4a8 Thomas Thrainer
  }
39 ec3cc4a8 Thomas Thrainer
40 ec3cc4a8 Thomas Thrainer
assert set(_QUERY_IMPL.keys()) == constants.QR_VIA_OP
41 ec3cc4a8 Thomas Thrainer
42 ec3cc4a8 Thomas Thrainer
43 ec3cc4a8 Thomas Thrainer
def _GetQueryImplementation(name):
44 270df828 Jose A. Lopes
  """Returns the implementation for a query type.
45 ec3cc4a8 Thomas Thrainer

46 ec3cc4a8 Thomas Thrainer
  @param name: Query type, must be one of L{constants.QR_VIA_OP}
47 ec3cc4a8 Thomas Thrainer

48 ec3cc4a8 Thomas Thrainer
  """
49 ec3cc4a8 Thomas Thrainer
  try:
50 ec3cc4a8 Thomas Thrainer
    return _QUERY_IMPL[name]
51 ec3cc4a8 Thomas Thrainer
  except KeyError:
52 ec3cc4a8 Thomas Thrainer
    raise errors.OpPrereqError("Unknown query resource '%s'" % name,
53 ec3cc4a8 Thomas Thrainer
                               errors.ECODE_INVAL)
54 ec3cc4a8 Thomas Thrainer
55 ec3cc4a8 Thomas Thrainer
56 ec3cc4a8 Thomas Thrainer
class LUQuery(NoHooksLU):
57 ec3cc4a8 Thomas Thrainer
  """Query for resources/items of a certain kind.
58 ec3cc4a8 Thomas Thrainer

59 ec3cc4a8 Thomas Thrainer
  """
60 ec3cc4a8 Thomas Thrainer
  # pylint: disable=W0142
61 ec3cc4a8 Thomas Thrainer
  REQ_BGL = False
62 ec3cc4a8 Thomas Thrainer
63 ec3cc4a8 Thomas Thrainer
  def CheckArguments(self):
64 ec3cc4a8 Thomas Thrainer
    qcls = _GetQueryImplementation(self.op.what)
65 ec3cc4a8 Thomas Thrainer
66 ec3cc4a8 Thomas Thrainer
    self.impl = qcls(self.op.qfilter, self.op.fields, self.op.use_locking)
67 ec3cc4a8 Thomas Thrainer
68 ec3cc4a8 Thomas Thrainer
  def ExpandNames(self):
69 ec3cc4a8 Thomas Thrainer
    self.impl.ExpandNames(self)
70 ec3cc4a8 Thomas Thrainer
71 ec3cc4a8 Thomas Thrainer
  def DeclareLocks(self, level):
72 ec3cc4a8 Thomas Thrainer
    self.impl.DeclareLocks(self, level)
73 ec3cc4a8 Thomas Thrainer
74 ec3cc4a8 Thomas Thrainer
  def Exec(self, feedback_fn):
75 ec3cc4a8 Thomas Thrainer
    return self.impl.NewStyleQuery(self)
76 ec3cc4a8 Thomas Thrainer
77 ec3cc4a8 Thomas Thrainer
78 ec3cc4a8 Thomas Thrainer
class LUQueryFields(NoHooksLU):
79 ec3cc4a8 Thomas Thrainer
  """Query for resources/items of a certain kind.
80 ec3cc4a8 Thomas Thrainer

81 ec3cc4a8 Thomas Thrainer
  """
82 ec3cc4a8 Thomas Thrainer
  # pylint: disable=W0142
83 ec3cc4a8 Thomas Thrainer
  REQ_BGL = False
84 ec3cc4a8 Thomas Thrainer
85 ec3cc4a8 Thomas Thrainer
  def CheckArguments(self):
86 ec3cc4a8 Thomas Thrainer
    self.qcls = _GetQueryImplementation(self.op.what)
87 ec3cc4a8 Thomas Thrainer
88 ec3cc4a8 Thomas Thrainer
  def ExpandNames(self):
89 ec3cc4a8 Thomas Thrainer
    self.needed_locks = {}
90 ec3cc4a8 Thomas Thrainer
91 ec3cc4a8 Thomas Thrainer
  def Exec(self, feedback_fn):
92 ec3cc4a8 Thomas Thrainer
    return query.QueryFields(self.qcls.FIELDS, self.op.fields)