~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/python/xen/xend/XendBase.py

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
"""
 
21
 
 
22
from xen.xend.XendError import *
 
23
from xen.xend import XendAPIStore
 
24
 
 
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"
 
31
    
 
32
    def getAttrRO(self):
 
33
        return ['uuid']
 
34
 
 
35
    def getAttrRW(self):
 
36
        return []
 
37
 
 
38
    def getAttrInst(self):
 
39
        return []
 
40
 
 
41
    def getMethods(self):
 
42
        return ["get_record"]
 
43
 
 
44
    def getFuncs(self):
 
45
        return ["get_all", "get_by_uuid", "get_all_records"]
 
46
 
 
47
    getClass    = classmethod(getClass)
 
48
    getAttrRO   = classmethod(getAttrRO)
 
49
    getAttrRW   = classmethod(getAttrRW)
 
50
    getAttrInst = classmethod(getAttrInst)
 
51
    getMethods  = classmethod(getMethods)
 
52
    getFuncs    = classmethod(getFuncs)
 
53
    
 
54
    def __init__(self, uuid, record):
 
55
        self.__uuid = uuid
 
56
        
 
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)
 
62
 
 
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)
 
67
 
 
68
        for method in self.getMethods():
 
69
            if not hasattr(self, method):
 
70
                raise ImplementationError(self.getClass(),
 
71
                                          method)
 
72
 
 
73
        for func in self.getFuncs():
 
74
            if not hasattr(self.__class__, func):
 
75
                raise ImplementationError(self.getClass(),
 
76
                                          func)
 
77
 
 
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())
 
83
        
 
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])
 
89
 
 
90
        # Finally register it
 
91
        XendAPIStore.register(uuid, self.getClass(), self)
 
92
 
 
93
    def destroy(self):
 
94
        XendAPIStore.deregister(self.get_uuid(), self.getClass())
 
95
 
 
96
    def get_uuid(self):
 
97
        return self.__uuid
 
98
 
 
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])
 
103
 
 
104
    #
 
105
    # Class methods
 
106
    #
 
107
 
 
108
    def get_all(cls):
 
109
        return XendAPIStore.get_all_uuid(cls.getClass())
 
110
 
 
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!"
 
119
 
 
120
    def get_all_records(cls):
 
121
        return dict([(inst.get_uuid(), inst.get_record())
 
122
                     for inst in XendAPIStore.get_all(cls.getClass())])
 
123
 
 
124
    get_all = classmethod(get_all)
 
125
    get_by_uuid = classmethod(get_by_uuid)
 
126
    get_all_records = classmethod(get_all_records)