debuggers.hg

view tools/python/xen/xend/XendBase.py @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children
line source
1 #!/usr/bin/python
2 #============================================================================
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of version 2.1 of the GNU Lesser General Public
5 # License as published by the Free Software Foundation.
6 #
7 # This library is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
11 #
12 # You should have received a copy of the GNU Lesser General Public
13 # License along with this library; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 #============================================================================
16 # Copyright (C) 2007 Tom Wilkie <tom.wilkie@gmail.com>
17 #============================================================================
18 """
19 Base class for all XenAPI classes
20 """
22 from xen.xend.XendError import *
23 from xen.xend import XendAPIStore
25 class XendBase:
26 #
27 # These functions describe the object, and what is exposed via the API
28 #
29 def getClass(self):
30 return "Base"
32 def getAttrRO(self):
33 return ['uuid']
35 def getAttrRW(self):
36 return []
38 def getAttrInst(self):
39 return []
41 def getMethods(self):
42 return ["get_record"]
44 def getFuncs(self):
45 return ["get_all", "get_by_uuid", "get_all_records"]
47 getClass = classmethod(getClass)
48 getAttrRO = classmethod(getAttrRO)
49 getAttrRW = classmethod(getAttrRW)
50 getAttrInst = classmethod(getAttrInst)
51 getMethods = classmethod(getMethods)
52 getFuncs = classmethod(getFuncs)
54 def __init__(self, uuid, record):
55 self.__uuid = uuid
57 # First check this class implements all the correct methods:
58 for attr_ro in self.getAttrRO() + self.getAttrRW():
59 if not hasattr(self, "get_%s" % attr_ro):
60 raise ImplementationError(self.getClass(),
61 "get_%s" % attr_ro)
63 for attr_rw in self.getAttrRW():
64 if not hasattr(self, "set_%s" % attr_rw):
65 raise ImplementationError(self.getClass(),
66 "set_%s" % attr_rw)
68 for method in self.getMethods():
69 if not hasattr(self, method):
70 raise ImplementationError(self.getClass(),
71 method)
73 for func in self.getFuncs():
74 if not hasattr(self.__class__, func):
75 raise ImplementationError(self.getClass(),
76 func)
78 # Next check that the class is being created with the correct
79 # parameters
80 if not isinstance(record, dict):
81 raise CreateUnspecifiedAttributeError(
82 "record" , self.getClass())
84 for attr_inst in self.getAttrInst():
85 if attr_inst not in record:
86 raise CreateUnspecifiedAttributeError(
87 attr_inst, self.getClass())
88 setattr(self, attr_inst, record[attr_inst])
90 # Finally register it
91 XendAPIStore.register(uuid, self.getClass(), self)
93 def destroy(self):
94 XendAPIStore.deregister(self.get_uuid(), self.getClass())
96 def get_uuid(self):
97 return self.__uuid
99 def get_record(self):
100 keys = self.getAttrRO() + self.getAttrRW()
101 return dict([(key, getattr(self, "get_%s" % key)())
102 for key in keys])
104 #
105 # Class methods
106 #
108 def get_all(cls):
109 return XendAPIStore.get_all_uuid(cls.getClass())
111 def get_by_uuid(cls, uuid):
112 # Sanity check the uuid is one of us
113 me = XendAPIStore.get(uuid, cls.getClass())
114 if me is not None and me.getClass() == cls.getClass():
115 # In OSS, ref == uuid
116 return uuid
117 else:
118 raise "Big Error.. TODO!"
120 def get_all_records(cls):
121 return dict([(inst.get_uuid(), inst.get_record())
122 for inst in XendAPIStore.get_all(cls.getClass())])
124 get_all = classmethod(get_all)
125 get_by_uuid = classmethod(get_by_uuid)
126 get_all_records = classmethod(get_all_records)