Validate entire job, not just op
[ganeti_webmgr.git] / ganeti_web / util / proxy / rapi_proxy.py
blob1b3abca520b1fadba012cd57b645528a37327d7b
1 # Copyright (C) 2010 Oregon State University et al.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
16 # USA.
19 from ganeti_web.util import client
20 from ganeti_web.util.proxy import CallProxy
21 from ganeti_web.util.proxy.constants import *
24 class RapiProxy(client.GanetiRapiClient):
25 """
26 Proxy class for testing RAPI interface without a cluster present.
27 This class has methods replaced that will return dummy info
28 """
29 error = None
31 def __new__(cls, *args, **kwargs):
32 """
33 Each time the RapiProxy is created, monkey patch
34 the GanetiRapiClient methods to return static data.
35 """
36 instance = object.__new__(cls)
37 instance.__init__(*args, **kwargs)
38 CallProxy.patch(instance, 'GetInstances', False, INSTANCES)
39 CallProxy.patch(instance, 'GetInstance', False, INSTANCE)
40 CallProxy.patch(instance, 'GetNodes', False, NODES_MAP)
41 CallProxy.patch(instance, 'GetNode', False, NODE)
42 CallProxy.patch(instance, 'GetInfo', False, INFO)
43 CallProxy.patch(instance, 'GetOperatingSystems', False,
44 OPERATING_SYSTEMS)
45 CallProxy.patch(instance, 'GetJobStatus', False, JOB_RUNNING)
46 CallProxy.patch(instance, 'StartupInstance', False, 1)
47 CallProxy.patch(instance, 'ShutdownInstance', False, 1)
48 CallProxy.patch(instance, 'RebootInstance', False, 1)
49 CallProxy.patch(instance, 'ReinstallInstance', False, 1)
50 CallProxy.patch(instance, 'AddInstanceTags', False)
51 CallProxy.patch(instance, 'DeleteInstanceTags', False)
52 CallProxy.patch(instance, 'CreateInstance', False, 1)
53 CallProxy.patch(instance, 'DeleteInstance', False, 1)
54 CallProxy.patch(instance, 'ModifyInstance', False, 1)
55 CallProxy.patch(instance, 'MigrateInstance', False, 1)
56 CallProxy.patch(instance, 'RenameInstance', False, 1)
57 CallProxy.patch(instance, 'RedistributeConfig', False, 1)
58 CallProxy.patch(instance, 'ReplaceInstanceDisks', False, 1)
59 CallProxy.patch(instance, 'SetNodeRole', False, 1)
60 CallProxy.patch(instance, 'EvacuateNode', False, 1)
61 CallProxy.patch(instance, 'MigrateNode', False, 1)
63 return instance
65 def fail(self, *args, **kwargs):
66 """
67 Raise the error set on this object.
68 """
69 raise self.error
71 def __setattr__(self, name, value):
72 return super(RapiProxy, self).__setattr__(name, value)
74 def __getattribute__(self, key):
75 if key in ['GetInstances', 'GetInstance', 'GetNodes', 'GetNode',
76 'GetInfo', 'StartupInstance', 'ShutdownInstance',
77 'RebootInstance', 'AddInstanceTags', 'DeleteInstanceTags',
78 'GetOperatingSystems', 'GetJobStatus', 'CreateInstance',
79 'ReinstallInstance'] \
80 and self.error:
81 return self.fail
82 return super(RapiProxy, self).__getattribute__(key)
85 class XenRapiProxy(RapiProxy):
86 def __new__(cls, *args, **kwargs):
87 """
88 Inherits from the RapiProxy and extends it to return
89 information for Xen clusters instead of Kvm clusters.
90 """
91 instance = RapiProxy.__new__(cls, *args, **kwargs)
92 # Unbind functions that are to be patched
93 instance.GetInstances = None
94 instance.GetInstance = None
95 instance.GetInfo = None
96 instance.GetOperatingSystems = None
97 CallProxy.patch(instance, 'GetInstances', False, INSTANCES)
98 CallProxy.patch(instance, 'GetInstance', False, XEN_PVM_INSTANCE)
99 CallProxy.patch(instance, 'GetInfo', False, XEN_INFO)
100 CallProxy.patch(instance, 'GetOperatingSystems', False,
101 XEN_OPERATING_SYSTEMS)
103 return instance