Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib / query.py @ 862beeab

History | View | Annotate | Download (2.8 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 5eacbcae Thomas Thrainer
from ganeti.cmdlib.backup import ExportQuery
28 ec3cc4a8 Thomas Thrainer
from ganeti.cmdlib.base import NoHooksLU
29 5eacbcae Thomas Thrainer
from ganeti.cmdlib.cluster import ClusterQuery
30 5eacbcae Thomas Thrainer
from ganeti.cmdlib.group import GroupQuery
31 5eacbcae Thomas Thrainer
from ganeti.cmdlib.instance_query import InstanceQuery
32 5eacbcae Thomas Thrainer
from ganeti.cmdlib.misc import ExtStorageQuery
33 5eacbcae Thomas Thrainer
from ganeti.cmdlib.network import NetworkQuery
34 5eacbcae Thomas Thrainer
from ganeti.cmdlib.operating_system import OsQuery
35 ec3cc4a8 Thomas Thrainer
36 ec3cc4a8 Thomas Thrainer
37 ec3cc4a8 Thomas Thrainer
#: Query type implementations
38 ec3cc4a8 Thomas Thrainer
_QUERY_IMPL = {
39 5eacbcae Thomas Thrainer
  constants.QR_CLUSTER: ClusterQuery,
40 5eacbcae Thomas Thrainer
  constants.QR_INSTANCE: InstanceQuery,
41 5eacbcae Thomas Thrainer
  constants.QR_GROUP: GroupQuery,
42 5eacbcae Thomas Thrainer
  constants.QR_NETWORK: NetworkQuery,
43 5eacbcae Thomas Thrainer
  constants.QR_OS: OsQuery,
44 5eacbcae Thomas Thrainer
  constants.QR_EXTSTORAGE: ExtStorageQuery,
45 5eacbcae Thomas Thrainer
  constants.QR_EXPORT: ExportQuery,
46 ec3cc4a8 Thomas Thrainer
  }
47 ec3cc4a8 Thomas Thrainer
48 ec3cc4a8 Thomas Thrainer
assert set(_QUERY_IMPL.keys()) == constants.QR_VIA_OP
49 ec3cc4a8 Thomas Thrainer
50 ec3cc4a8 Thomas Thrainer
51 ec3cc4a8 Thomas Thrainer
def _GetQueryImplementation(name):
52 270df828 Jose A. Lopes
  """Returns the implementation for a query type.
53 ec3cc4a8 Thomas Thrainer

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

56 ec3cc4a8 Thomas Thrainer
  """
57 ec3cc4a8 Thomas Thrainer
  try:
58 ec3cc4a8 Thomas Thrainer
    return _QUERY_IMPL[name]
59 ec3cc4a8 Thomas Thrainer
  except KeyError:
60 ec3cc4a8 Thomas Thrainer
    raise errors.OpPrereqError("Unknown query resource '%s'" % name,
61 ec3cc4a8 Thomas Thrainer
                               errors.ECODE_INVAL)
62 ec3cc4a8 Thomas Thrainer
63 ec3cc4a8 Thomas Thrainer
64 ec3cc4a8 Thomas Thrainer
class LUQuery(NoHooksLU):
65 ec3cc4a8 Thomas Thrainer
  """Query for resources/items of a certain kind.
66 ec3cc4a8 Thomas Thrainer

67 ec3cc4a8 Thomas Thrainer
  """
68 ec3cc4a8 Thomas Thrainer
  # pylint: disable=W0142
69 ec3cc4a8 Thomas Thrainer
  REQ_BGL = False
70 ec3cc4a8 Thomas Thrainer
71 ec3cc4a8 Thomas Thrainer
  def CheckArguments(self):
72 ec3cc4a8 Thomas Thrainer
    qcls = _GetQueryImplementation(self.op.what)
73 ec3cc4a8 Thomas Thrainer
74 ec3cc4a8 Thomas Thrainer
    self.impl = qcls(self.op.qfilter, self.op.fields, self.op.use_locking)
75 ec3cc4a8 Thomas Thrainer
76 ec3cc4a8 Thomas Thrainer
  def ExpandNames(self):
77 ec3cc4a8 Thomas Thrainer
    self.impl.ExpandNames(self)
78 ec3cc4a8 Thomas Thrainer
79 ec3cc4a8 Thomas Thrainer
  def DeclareLocks(self, level):
80 ec3cc4a8 Thomas Thrainer
    self.impl.DeclareLocks(self, level)
81 ec3cc4a8 Thomas Thrainer
82 ec3cc4a8 Thomas Thrainer
  def Exec(self, feedback_fn):
83 ec3cc4a8 Thomas Thrainer
    return self.impl.NewStyleQuery(self)
84 ec3cc4a8 Thomas Thrainer
85 ec3cc4a8 Thomas Thrainer
86 ec3cc4a8 Thomas Thrainer
class LUQueryFields(NoHooksLU):
87 ec3cc4a8 Thomas Thrainer
  """Query for resources/items of a certain kind.
88 ec3cc4a8 Thomas Thrainer

89 ec3cc4a8 Thomas Thrainer
  """
90 ec3cc4a8 Thomas Thrainer
  # pylint: disable=W0142
91 ec3cc4a8 Thomas Thrainer
  REQ_BGL = False
92 ec3cc4a8 Thomas Thrainer
93 ec3cc4a8 Thomas Thrainer
  def CheckArguments(self):
94 ec3cc4a8 Thomas Thrainer
    self.qcls = _GetQueryImplementation(self.op.what)
95 ec3cc4a8 Thomas Thrainer
96 ec3cc4a8 Thomas Thrainer
  def ExpandNames(self):
97 ec3cc4a8 Thomas Thrainer
    self.needed_locks = {}
98 ec3cc4a8 Thomas Thrainer
99 ec3cc4a8 Thomas Thrainer
  def Exec(self, feedback_fn):
100 ec3cc4a8 Thomas Thrainer
    return query.QueryFields(self.qcls.FIELDS, self.op.fields)