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.
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.
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
#============================================================================
19
Base class for all XenAPI classes
22
from xen.xend.XendError import *
23
from xen.xend import XendAPIStore
27
# These functions describe the object, and what is exposed via the API
38
def getAttrInst(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):
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(),
63
for attr_rw in self.getAttrRW():
64
if not hasattr(self, "set_%s" % attr_rw):
65
raise ImplementationError(self.getClass(),
68
for method in self.getMethods():
69
if not hasattr(self, method):
70
raise ImplementationError(self.getClass(),
73
for func in self.getFuncs():
74
if not hasattr(self.__class__, func):
75
raise ImplementationError(self.getClass(),
78
# Next check that the class is being created with the correct
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])
91
XendAPIStore.register(uuid, self.getClass(), self)
94
XendAPIStore.deregister(self.get_uuid(), self.getClass())
100
keys = self.getAttrRO() + self.getAttrRW()
101
return dict([(key, getattr(self, "get_%s" % key)())
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
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)