From a9369c6e9840b13a6582002b73d5e7ffc72a4800 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Tue, 6 May 2008 10:20:49 +0000 Subject: [PATCH] Use dict instead of if/elif map for hypervisor classes Reviewed-by: iustinp --- lib/hypervisor/__init__.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/hypervisor/__init__.py b/lib/hypervisor/__init__.py index fb6d39449..c3e5ece1f 100644 --- a/lib/hypervisor/__init__.py +++ b/lib/hypervisor/__init__.py @@ -31,6 +31,13 @@ from ganeti.hypervisor import hv_fake from ganeti.hypervisor import hv_xen +_HYPERVISOR_MAP = { + constants.HT_XEN_PVM30: hv_xen.XenPvmHypervisor, + constants.HT_XEN_HVM31: hv_xen.XenHvmHypervisor, + constants.HT_FAKE: hv_fake.FakeHypervisor, + } + + def GetHypervisor(): """Return a Hypervisor instance. @@ -39,12 +46,9 @@ def GetHypervisor(): """ ht_kind = ssconf.SimpleStore().GetHypervisorType() - if ht_kind == constants.HT_XEN_PVM30: - cls = hv_xen.XenPvmHypervisor - elif ht_kind == constants.HT_XEN_HVM31: - cls = hv_xen.XenHvmHypervisor - elif ht_kind == constants.HT_FAKE: - cls = hv_fake.FakeHypervisor - else: + + if ht_kind not in _HYPERVISOR_MAP: raise errors.HypervisorError("Unknown hypervisor type '%s'" % ht_kind) + + cls = _HYPERVISOR_MAP[ht_kind] return cls() -- GitLab