Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib / query.py @ fbeb41e6

History | View | Annotate | Download (2.6 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.group import GroupQuery
30
from ganeti.cmdlib.instance_query import InstanceQuery
31
from ganeti.cmdlib.misc import ExtStorageQuery
32
from ganeti.cmdlib.operating_system import OsQuery
33

    
34

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

    
44
assert set(_QUERY_IMPL.keys()) == constants.QR_VIA_OP
45

    
46

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

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

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

    
59

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

63
  """
64
  # pylint: disable=W0142
65
  REQ_BGL = False
66

    
67
  def CheckArguments(self):
68
    qcls = _GetQueryImplementation(self.op.what)
69

    
70
    self.impl = qcls(self.op.qfilter, self.op.fields, self.op.use_locking)
71

    
72
  def ExpandNames(self):
73
    self.impl.ExpandNames(self)
74

    
75
  def DeclareLocks(self, level):
76
    self.impl.DeclareLocks(self, level)
77

    
78
  def Exec(self, feedback_fn):
79
    return self.impl.NewStyleQuery(self)
80

    
81

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

85
  """
86
  # pylint: disable=W0142
87
  REQ_BGL = False
88

    
89
  def CheckArguments(self):
90
    self.qcls = _GetQueryImplementation(self.op.what)
91

    
92
  def ExpandNames(self):
93
    self.needed_locks = {}
94

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