Commit ec3cc4a8 authored by Thomas Thrainer's avatar Thomas Thrainer

Extract query related logical units from cmdlib

All LUQuery* classes are extracted to query.py.
Signed-off-by: default avatarThomas Thrainer <thomasth@google.com>
Reviewed-by: default avatarBernardo Dal Seno <bdalseno@google.com>
parent 7ecd5e87
......@@ -317,6 +317,7 @@ cmdlib_PYTHON = \
lib/cmdlib/instance.py \
lib/cmdlib/instance_utils.py \
lib/cmdlib/backup.py \
lib/cmdlib/query.py \
lib/cmdlib/tags.py \
lib/cmdlib/network.py \
lib/cmdlib/test.py
......
......@@ -62,31 +62,29 @@ from ganeti.cmdlib.instance_utils import _AssembleInstanceDisks, \
from ganeti.cmdlib.cluster import LUClusterActivateMasterIp, \
LUClusterDeactivateMasterIp, LUClusterConfigQuery, LUClusterDestroy, \
LUClusterPostInit, _ClusterQuery, LUClusterQuery, LUClusterRedistConf, \
LUClusterRename, LUClusterRepairDiskSizes, LUClusterSetParams, \
LUClusterVerify, LUClusterVerifyConfig, LUClusterVerifyGroup, \
LUClusterVerifyDisks
LUClusterPostInit, LUClusterQuery, LUClusterRedistConf, LUClusterRename, \
LUClusterRepairDiskSizes, LUClusterSetParams, LUClusterVerify, \
LUClusterVerifyConfig, LUClusterVerifyGroup, LUClusterVerifyDisks
from ganeti.cmdlib.group import LUGroupAdd, LUGroupAssignNodes, \
_GroupQuery, LUGroupQuery, LUGroupSetParams, LUGroupRemove, \
LUGroupRename, LUGroupEvacuate, LUGroupVerifyDisks
LUGroupQuery, LUGroupSetParams, LUGroupRemove, LUGroupRename, \
LUGroupEvacuate, LUGroupVerifyDisks
from ganeti.cmdlib.node import LUNodeAdd, LUNodeSetParams, \
LUNodePowercycle, LUNodeEvacuate, LUNodeMigrate, LUNodeModifyStorage, \
_NodeQuery, LUNodeQuery, LUNodeQueryvols, LUNodeQueryStorage, \
LUNodeRemove, LURepairNodeStorage
LUNodeQuery, LUNodeQueryvols, LUNodeQueryStorage, LUNodeRemove, \
LURepairNodeStorage
from ganeti.cmdlib.instance import LUInstanceCreate, LUInstanceRename, \
LUInstanceRemove, LUInstanceMove, _InstanceQuery, LUInstanceQuery, \
LUInstanceQueryData, LUInstanceRecreateDisks, LUInstanceGrowDisk, \
LUInstanceReplaceDisks, LUInstanceActivateDisks, \
LUInstanceDeactivateDisks, LUInstanceStartup, LUInstanceShutdown, \
LUInstanceReinstall, LUInstanceReboot, LUInstanceConsole, \
LUInstanceFailover, LUInstanceMigrate, LUInstanceMultiAlloc, \
LUInstanceSetParams, LUInstanceChangeGroup
from ganeti.cmdlib.backup import _ExportQuery, LUBackupQuery, \
LUBackupPrepare, LUBackupExport, LUBackupRemove
LUInstanceRemove, LUInstanceMove, LUInstanceQuery, LUInstanceQueryData, \
LUInstanceRecreateDisks, LUInstanceGrowDisk, LUInstanceReplaceDisks, \
LUInstanceActivateDisks, LUInstanceDeactivateDisks, LUInstanceStartup, \
LUInstanceShutdown, LUInstanceReinstall, LUInstanceReboot, \
LUInstanceConsole, LUInstanceFailover, LUInstanceMigrate, \
LUInstanceMultiAlloc, LUInstanceSetParams, LUInstanceChangeGroup
from ganeti.cmdlib.backup import LUBackupQuery, LUBackupPrepare, \
LUBackupExport, LUBackupRemove
from ganeti.cmdlib.query import LUQuery, LUQueryFields
from ganeti.cmdlib.tags import LUTagsGet, LUTagsSearch, LUTagsSet, LUTagsDel
from ganeti.cmdlib.network import LUNetworkAdd, LUNetworkRemove, \
LUNetworkSetParams, _NetworkQuery, LUNetworkQuery, LUNetworkConnect, \
LUNetworkDisconnect
LUNetworkSetParams, LUNetworkQuery, LUNetworkConnect, LUNetworkDisconnect
from ganeti.cmdlib.test import LUTestDelay, LUTestJqueue, LUTestAllocator
......@@ -590,45 +588,6 @@ class LUExtStorageDiagnose(NoHooksLU):
return self.eq.OldStyleQuery(self)
class LUQuery(NoHooksLU):
"""Query for resources/items of a certain kind.
"""
# pylint: disable=W0142
REQ_BGL = False
def CheckArguments(self):
qcls = _GetQueryImplementation(self.op.what)
self.impl = qcls(self.op.qfilter, self.op.fields, self.op.use_locking)
def ExpandNames(self):
self.impl.ExpandNames(self)
def DeclareLocks(self, level):
self.impl.DeclareLocks(self, level)
def Exec(self, feedback_fn):
return self.impl.NewStyleQuery(self)
class LUQueryFields(NoHooksLU):
"""Query for resources/items of a certain kind.
"""
# pylint: disable=W0142
REQ_BGL = False
def CheckArguments(self):
self.qcls = _GetQueryImplementation(self.op.what)
def ExpandNames(self):
self.needed_locks = {}
def Exec(self, feedback_fn):
return query.QueryFields(self.qcls.FIELDS, self.op.fields)
class LURestrictedCommand(NoHooksLU):
"""Logical unit for executing restricted commands.
......@@ -674,31 +633,3 @@ class LURestrictedCommand(NoHooksLU):
result.append((True, nres.payload))
return result
#: Query type implementations
_QUERY_IMPL = {
constants.QR_CLUSTER: _ClusterQuery,
constants.QR_INSTANCE: _InstanceQuery,
constants.QR_NODE: _NodeQuery,
constants.QR_GROUP: _GroupQuery,
constants.QR_NETWORK: _NetworkQuery,
constants.QR_OS: _OsQuery,
constants.QR_EXTSTORAGE: _ExtStorageQuery,
constants.QR_EXPORT: _ExportQuery,
}
assert set(_QUERY_IMPL.keys()) == constants.QR_VIA_OP
def _GetQueryImplementation(name):
"""Returns the implemtnation for a query type.
@param name: Query type, must be one of L{constants.QR_VIA_OP}
"""
try:
return _QUERY_IMPL[name]
except KeyError:
raise errors.OpPrereqError("Unknown query resource '%s'" % name,
errors.ECODE_INVAL)
#
#
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
"""Logical units for queries."""
from ganeti import constants
from ganeti import errors
from ganeti import query
from ganeti.cmdlib import _ExtStorageQuery
from ganeti.cmdlib import _OsQuery
from ganeti.cmdlib.backup import _ExportQuery
from ganeti.cmdlib.base import NoHooksLU
from ganeti.cmdlib.cluster import _ClusterQuery
from ganeti.cmdlib.group import _GroupQuery
from ganeti.cmdlib.instance import _InstanceQuery
from ganeti.cmdlib.network import _NetworkQuery
from ganeti.cmdlib.node import _NodeQuery
#: Query type implementations
_QUERY_IMPL = {
constants.QR_CLUSTER: _ClusterQuery,
constants.QR_INSTANCE: _InstanceQuery,
constants.QR_NODE: _NodeQuery,
constants.QR_GROUP: _GroupQuery,
constants.QR_NETWORK: _NetworkQuery,
constants.QR_OS: _OsQuery,
constants.QR_EXTSTORAGE: _ExtStorageQuery,
constants.QR_EXPORT: _ExportQuery,
}
assert set(_QUERY_IMPL.keys()) == constants.QR_VIA_OP
def _GetQueryImplementation(name):
"""Returns the implemtnation for a query type.
@param name: Query type, must be one of L{constants.QR_VIA_OP}
"""
try:
return _QUERY_IMPL[name]
except KeyError:
raise errors.OpPrereqError("Unknown query resource '%s'" % name,
errors.ECODE_INVAL)
class LUQuery(NoHooksLU):
"""Query for resources/items of a certain kind.
"""
# pylint: disable=W0142
REQ_BGL = False
def CheckArguments(self):
qcls = _GetQueryImplementation(self.op.what)
self.impl = qcls(self.op.qfilter, self.op.fields, self.op.use_locking)
def ExpandNames(self):
self.impl.ExpandNames(self)
def DeclareLocks(self, level):
self.impl.DeclareLocks(self, level)
def Exec(self, feedback_fn):
return self.impl.NewStyleQuery(self)
class LUQueryFields(NoHooksLU):
"""Query for resources/items of a certain kind.
"""
# pylint: disable=W0142
REQ_BGL = False
def CheckArguments(self):
self.qcls = _GetQueryImplementation(self.op.what)
def ExpandNames(self):
self.needed_locks = {}
def Exec(self, feedback_fn):
return query.QueryFields(self.qcls.FIELDS, self.op.fields)
......@@ -37,6 +37,7 @@ from ganeti.cmdlib import cluster
from ganeti.cmdlib import group
from ganeti.cmdlib import instance
from ganeti.cmdlib import common
from ganeti.cmdlib import query
from ganeti import opcodes
from ganeti import errors
from ganeti import utils
......@@ -164,18 +165,18 @@ class TestLUTestJqueue(unittest.TestCase):
class TestLUQuery(unittest.TestCase):
def test(self):
self.assertEqual(sorted(cmdlib._QUERY_IMPL.keys()),
self.assertEqual(sorted(query._QUERY_IMPL.keys()),
sorted(constants.QR_VIA_OP))
assert constants.QR_NODE in constants.QR_VIA_OP
assert constants.QR_INSTANCE in constants.QR_VIA_OP
for i in constants.QR_VIA_OP:
self.assert_(cmdlib._GetQueryImplementation(i))
self.assert_(query._GetQueryImplementation(i))
self.assertRaises(errors.OpPrereqError, cmdlib._GetQueryImplementation,
self.assertRaises(errors.OpPrereqError, query._GetQueryImplementation,
"")
self.assertRaises(errors.OpPrereqError, cmdlib._GetQueryImplementation,
self.assertRaises(errors.OpPrereqError, query._GetQueryImplementation,
"xyz")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment