ia64/xen-unstable

changeset 14933:ca5bfcd32e13

Add new base class and instance store for new autoplugged api classes

signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
author Tom Wilkie <tom.wilkie@gmail.com>
date Wed Apr 25 15:38:04 2007 +0100 (2007-04-25)
parents 2b994e23f96b
children 26b80f4523e5
files tools/python/xen/xend/XendAPIStore.py tools/python/xen/xend/XendBase.py
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/python/xen/xend/XendAPIStore.py	Wed Apr 25 15:38:04 2007 +0100
     1.3 @@ -0,0 +1,59 @@
     1.4 +#============================================================================
     1.5 +# This library is free software; you can redistribute it and/or
     1.6 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
     1.7 +# License as published by the Free Software Foundation.
     1.8 +#
     1.9 +# This library is distributed in the hope that it will be useful,
    1.10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.12 +# Lesser General Public License for more details.
    1.13 +#
    1.14 +# You should have received a copy of the GNU Lesser General Public
    1.15 +# License along with this library; if not, write to the Free Software
    1.16 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.17 +#============================================================================
    1.18 +# Copyright (C) 2007 Tom Wilkie <tom.wilkie@gmail.com>
    1.19 +#============================================================================
    1.20 +"""
    1.21 +This is a place to put instances of XenAPI objects,
    1.22 +instead of just holding them in arbitrary places.
    1.23 +
    1.24 +All objects which subclass XendBase should use this
    1.25 +mechanism.
    1.26 +
    1.27 +You must register both the uuid and type, and get objects
    1.28 +by type, to ensure safety
    1.29 +"""
    1.30 +
    1.31 +__classes = {}
    1.32 +
    1.33 +def register(uuid, type, inst):
    1.34 +    __classes[(uuid, type)] = inst
    1.35 +    return inst
    1.36 +
    1.37 +def deregister(uuid, type):
    1.38 +    old = get(uuid, type)
    1.39 +    del __classes[(uuid, type)]
    1.40 +    return old
    1.41 +
    1.42 +def get(uuid, type):
    1.43 +    """
    1.44 +    Get the instances by uuid and type
    1.45 +    """
    1.46 +    return __classes.get((uuid, type), None)
    1.47 +
    1.48 +def get_all(all_type):
    1.49 +    """
    1.50 +    Get all instances by type
    1.51 +    """
    1.52 +    return [inst
    1.53 +            for ((uuid, t), inst) in __classes.items()
    1.54 +            if t == all_type]        
    1.55 +
    1.56 +def get_all_uuid(all_type):
    1.57 +    """
    1.58 +    Get all uuids by type
    1.59 +    """
    1.60 +    return [uuid
    1.61 +            for (uuid, t) in __classes.keys()
    1.62 +            if t == all_type]
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/tools/python/xen/xend/XendBase.py	Wed Apr 25 15:38:04 2007 +0100
     2.3 @@ -0,0 +1,123 @@
     2.4 +#!/usr/bin/python
     2.5 +#============================================================================
     2.6 +# This library is free software; you can redistribute it and/or
     2.7 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
     2.8 +# License as published by the Free Software Foundation.
     2.9 +#
    2.10 +# This library is distributed in the hope that it will be useful,
    2.11 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.12 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.13 +# Lesser General Public License for more details.
    2.14 +#
    2.15 +# You should have received a copy of the GNU Lesser General Public
    2.16 +# License along with this library; if not, write to the Free Software
    2.17 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    2.18 +#============================================================================
    2.19 +# Copyright (C) 2007 Tom Wilkie <tom.wilkie@gmail.com>
    2.20 +#============================================================================
    2.21 +"""
    2.22 +Base class for all XenAPI classes
    2.23 +"""
    2.24 +
    2.25 +from xen.xend.XendError import *
    2.26 +from xen.xend import XendAPIStore
    2.27 +
    2.28 +class XendBase:
    2.29 +    #
    2.30 +    # These functions describe the object, and what is exposed via the API
    2.31 +    #
    2.32 +    def getClass(self):
    2.33 +        return "Base"
    2.34 +    
    2.35 +    def getAttrRO(self):
    2.36 +        return ['uuid']
    2.37 +
    2.38 +    def getAttrRW(self):
    2.39 +        return []
    2.40 +
    2.41 +    def getAttrInst(self):
    2.42 +        return []
    2.43 +
    2.44 +    def getMethods(self):
    2.45 +        return ["get_record"]
    2.46 +
    2.47 +    def getFuncs(self):
    2.48 +        return ["get_all", "get_by_uuid", "get_all_records"]
    2.49 +
    2.50 +    getClass    = classmethod(getClass)
    2.51 +    getAttrRO   = classmethod(getAttrRO)
    2.52 +    getAttrRW   = classmethod(getAttrRW)
    2.53 +    getAttrInst = classmethod(getAttrInst)
    2.54 +    getMethods  = classmethod(getMethods)
    2.55 +    getFuncs    = classmethod(getFuncs)
    2.56 +    
    2.57 +    def __init__(self, uuid, record):
    2.58 +        self.__uuid = uuid
    2.59 +        
    2.60 +        # First check this class implements all the correct methods:
    2.61 +        for attr_ro in self.getAttrRO() + self.getAttrRW():
    2.62 +            if not hasattr(self, "get_%s" % attr_ro):
    2.63 +                raise ImplementationError(self.getClass(),
    2.64 +                                          "get_%s" % attr_ro)
    2.65 +
    2.66 +        for attr_rw in self.getAttrRW():
    2.67 +            if not hasattr(self, "set_%s" % attr_rw):
    2.68 +                raise ImplementationError(self.getClass(),
    2.69 +                                          "set_%s" % attr_rw)
    2.70 +
    2.71 +        for method in self.getMethods():
    2.72 +            if not hasattr(self, method):
    2.73 +                raise ImplementationError(self.getClass(),
    2.74 +                                          method)
    2.75 +
    2.76 +        for func in self.getFuncs():
    2.77 +            if not hasattr(self.__class__, func):
    2.78 +                raise ImplementationError(self.getClass(),
    2.79 +                                          func)
    2.80 +
    2.81 +        # Next check that the class is being created with the correct
    2.82 +        # parameters
    2.83 +        if not isinstance(record, dict):
    2.84 +            raise CreateUnspecifiedAttributeError(
    2.85 +                    "record" , self.getClass())
    2.86 +        
    2.87 +        for attr_inst in self.getAttrInst():
    2.88 +            if attr_inst not in record:
    2.89 +                raise CreateUnspecifiedAttributeError(
    2.90 +                    attr_inst, self.getClass())
    2.91 +            setattr(self, attr_inst, record[attr_inst])
    2.92 +
    2.93 +        # Finally register it
    2.94 +        XendAPIStore.register(uuid, self.getClass(), self)
    2.95 +
    2.96 +    def get_uuid(self):
    2.97 +        return self.__uuid
    2.98 +
    2.99 +    def get_record(self):
   2.100 +        keys = self.getAttrRO() + self.getAttrRW()
   2.101 +        return dict([(key, getattr(self, "get_%s" % key)())
   2.102 +                     for key in keys])
   2.103 +
   2.104 +    #
   2.105 +    # Class methods
   2.106 +    #
   2.107 +
   2.108 +    def get_all(cls):
   2.109 +        return XendAPIStore.get_all_uuid(cls.getClass())
   2.110 +
   2.111 +    def get_by_uuid(cls, uuid):
   2.112 +        # Sanity check the uuid is one of us
   2.113 +        me = XendAPIStore.get(uuid, cls.getClass())
   2.114 +        if me is not None and me.getClass() == cls.getClass():
   2.115 +            # In OSS, ref == uuid
   2.116 +            return uuid
   2.117 +        else:
   2.118 +            raise "Big Error.. TODO!"
   2.119 +
   2.120 +    def get_all_records(cls):
   2.121 +        return [inst.get_record()
   2.122 +                for inst in XendAPIStore.get_all(cls.getClass())]
   2.123 +
   2.124 +    get_all = classmethod(get_all)
   2.125 +    get_by_uuid = classmethod(get_by_uuid)
   2.126 +    get_all_records = classmethod(get_all_records)