Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib / query.py @ 44ffd981

History | View | Annotate | Download (2.7 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.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.operating_system import OsQuery
34

    
35

    
36
#: Query type implementations
37
_QUERY_IMPL = {
38
  constants.QR_CLUSTER: ClusterQuery,
39
  constants.QR_INSTANCE: InstanceQuery,
40
  constants.QR_GROUP: GroupQuery,
41
  constants.QR_OS: OsQuery,
42
  constants.QR_EXTSTORAGE: ExtStorageQuery,
43
  constants.QR_EXPORT: ExportQuery,
44
  }
45

    
46
assert set(_QUERY_IMPL.keys()) == constants.QR_VIA_OP
47

    
48

    
49
def _GetQueryImplementation(name):
50
  """Returns the implementation for a query type.
51

52
  @param name: Query type, must be one of L{constants.QR_VIA_OP}
53

54
  """
55
  try:
56
    return _QUERY_IMPL[name]
57
  except KeyError:
58
    raise errors.OpPrereqError("Unknown query resource '%s'" % name,
59
                               errors.ECODE_INVAL)
60

    
61

    
62
class LUQuery(NoHooksLU):
63
  """Query for resources/items of a certain kind.
64

65
  """
66
  # pylint: disable=W0142
67
  REQ_BGL = False
68

    
69
  def CheckArguments(self):
70
    qcls = _GetQueryImplementation(self.op.what)
71

    
72
    self.impl = qcls(self.op.qfilter, self.op.fields, self.op.use_locking)
73

    
74
  def ExpandNames(self):
75
    self.impl.ExpandNames(self)
76

    
77
  def DeclareLocks(self, level):
78
    self.impl.DeclareLocks(self, level)
79

    
80
  def Exec(self, feedback_fn):
81
    return self.impl.NewStyleQuery(self)
82

    
83

    
84
class LUQueryFields(NoHooksLU):
85
  """Query for resources/items of a certain kind.
86

87
  """
88
  # pylint: disable=W0142
89
  REQ_BGL = False
90

    
91
  def CheckArguments(self):
92
    self.qcls = _GetQueryImplementation(self.op.what)
93

    
94
  def ExpandNames(self):
95
    self.needed_locks = {}
96

    
97
  def Exec(self, feedback_fn):
98
    return query.QueryFields(self.qcls.FIELDS, self.op.fields)