ia64/xen-unstable

view tools/python/xen/xend/XendAPI.py @ 12147:808f5aa6dbb0

[XEND] Fix missing SR references in XendAPI

Signed-off-by: Alastair Tse <atse@xensource.com>
author Alastair Tse <atse@xensource.com>
date Fri Oct 27 16:44:15 2006 +0100 (2006-10-27)
parents df5431b2b519
children b6dc7eecff33
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2006 XenSource Ltd.
16 #============================================================================
18 from xen.xend import XendDomain, XendDomainInfo, XendNode
19 from xen.xend import XendLogging
21 from xen.xend.XendAuthSessions import instance as auth_manager
22 from xen.xend.XendAuthSessions import session_required
23 from xen.xend.XendError import *
24 from xen.xend.XendClient import ERROR_INVALID_DOMAIN
25 from xen.xend.XendLogging import log
27 from xen.xend.XendAPIConstants import *
28 from xen.util.xmlrpclib2 import stringify
30 def xen_api_success(value):
31 return {"Status": "Success", "Value": stringify(value)}
33 def xen_api_success_void():
34 """Return success, but caller expects no return value."""
35 return xen_api_success("")
36 def xen_api_error(error):
37 return {"Status": "Error", "ErrorDescription": error}
38 def xen_api_todo():
39 """Temporary method to make sure we track down all the TODOs"""
40 return {"Status": "Error", "ErrorDescription": XEND_ERROR_TODO}
42 def trace(func, api_name = ''):
43 """Decorator to trace XMLRPC Xen API methods."""
44 if hasattr(func, 'api'):
45 api_name = func.api
46 def trace_func(self, *args, **kwargs):
47 log.debug('%s: %s' % (api_name, args))
48 return func(self, *args, **kwargs)
49 trace_func.api = api_name
50 return trace_func
52 def valid_host(func):
53 """Decorator to verify if host_ref is valid before calling
54 method.
56 @param func: function with params: (self, session, host_ref)
57 @rtype: callable object
58 """
59 def check_host_ref(self, session, host_ref, *args, **kwargs):
60 xennode = XendNode.instance()
61 if type(host_ref) == type(str()) and xennode.is_valid_host(host_ref):
62 return func(self, session, host_ref, *args, **kwargs)
63 else:
64 return {'Status': 'Failure',
65 'ErrorDescription': XEND_ERROR_HOST_INVALID}
67 # make sure we keep the 'api' attribute
68 if hasattr(func, 'api'):
69 check_host_ref.api = func.api
71 return check_host_ref
73 def valid_host_cpu(func):
74 """Decorator to verify if host_cpu_ref is valid before calling
75 method.
77 @param func: function with params: (self, session, host_cpu_ref)
78 @rtype: callable object
79 """
80 def check_host_cpu_ref(self, session, host_cpu_ref, *args, **kwargs):
81 xennode = XendNode.instance()
82 if type(host_cpu_ref) == type(str()) and \
83 xennode.is_valid_cpu(host_cpu_ref):
84 return func(self, session, host_cpu_ref, *args, **kwargs)
85 else:
86 return {'Status': 'Failure',
87 'ErrorDescription': XEND_ERROR_HOST_CPU_INVALID}
89 # make sure we keep the 'api' attribute
90 if hasattr(func, 'api'):
91 check_host_cpu_ref.api = func.api
93 return check_host_cpu_ref
95 def valid_vm(func):
96 """Decorator to verify if vm_ref is valid before calling
97 method.
99 @param func: function with params: (self, session, vm_ref)
100 @rtype: callable object
101 """
102 def check_vm_ref(self, session, vm_ref, *args, **kwargs):
103 xendom = XendDomain.instance()
104 if type(vm_ref) == type(str()) and \
105 xendom.is_valid_vm(vm_ref):
106 return func(self, session, vm_ref, *args, **kwargs)
107 else:
108 return {'Status': 'Failure',
109 'ErrorDescription': XEND_ERROR_VM_INVALID}
111 # make sure we keep the 'api' attribute
112 if hasattr(func, 'api'):
113 check_vm_ref.api = func.api
115 return check_vm_ref
117 def valid_vbd(func):
118 """Decorator to verify if vbd_ref is valid before calling
119 method.
121 @param func: function with params: (self, session, vbd_ref)
122 @rtype: callable object
123 """
124 def check_vbd_ref(self, session, vbd_ref, *args, **kwargs):
125 xendom = XendDomain.instance()
126 if type(vbd_ref) == type(str()) and \
127 xendom.is_valid_dev('vbd', vbd_ref):
128 return func(self, session, vbd_ref, *args, **kwargs)
129 else:
130 return {'Status': 'Failure',
131 'ErrorDescription': XEND_ERROR_VBD_INVALID}
133 # make sure we keep the 'api' attribute
134 if hasattr(func, 'api'):
135 check_vbd_ref.api = func.api
137 return check_vbd_ref
139 def valid_vif(func):
140 """Decorator to verify if vif_ref is valid before calling
141 method.
143 @param func: function with params: (self, session, vif_ref)
144 @rtype: callable object
145 """
146 def check_vif_ref(self, session, vif_ref, *args, **kwargs):
147 xendom = XendDomain.instance()
148 if type(vif_ref) == type(str()) and \
149 xendom.is_valid_dev('vif', vif_ref):
150 return func(self, session, vif_ref, *args, **kwargs)
151 else:
152 return {'Status': 'Failure',
153 'ErrorDescription': XEND_ERROR_VIF_INVALID}
155 # make sure we keep the 'api' attribute
156 if hasattr(func, 'api'):
157 check_vif_ref.api = func.api
159 return check_vif_ref
162 def valid_vdi(func):
163 """Decorator to verify if vdi_ref is valid before calling
164 method.
166 @param func: function with params: (self, session, vdi_ref)
167 @rtype: callable object
168 """
169 def check_vdi_ref(self, session, vdi_ref, *args, **kwargs):
170 xennode = XendNode.instance()
171 if type(vdi_ref) == type(str()) and \
172 xennode.get_sr().is_valid_vdi(vdi_ref):
173 return func(self, session, vdi_ref, *args, **kwargs)
174 else:
175 return {'Status': 'Failure',
176 'ErrorDescription': XEND_ERROR_VDI_INVALID}
178 # make sure we keep the 'api' attribute
179 if hasattr(func, 'api'):
180 check_vdi_ref.api = func.api
182 return check_vdi_ref
184 def valid_sr(func):
185 """Decorator to verify if sr_ref is valid before calling
186 method.
188 @param func: function with params: (self, session, sr_ref)
189 @rtype: callable object
190 """
191 def check_sr_ref(self, session, sr_ref, *args, **kwargs):
192 xennode = XendNode.instance()
193 if type(sr_ref) == type(str()) and \
194 xennode.get_sr().uuid == sr_ref:
195 return func(self, session, sr_ref, *args, **kwargs)
196 else:
197 return {'Status': 'Failure',
198 'ErrorDescription': XEND_ERROR_SR_INVALID}
200 # make sure we keep the 'api' attribute
201 if hasattr(func, 'api'):
202 check_sr_ref.api = func.api
204 return check_sr_ref
206 def do_vm_func(fn_name, vm_ref, *args):
207 """Helper wrapper func to abstract away from repeative code.
209 @param fn_name: function name for XendDomain instance
210 @type fn_name: string
211 @param vm_ref: vm_ref
212 @type vm_ref: string
213 @param *args: more arguments
214 @type *args: tuple
215 """
216 xendom = XendDomain.instance()
217 fn = getattr(xendom, fn_name)
218 return xen_api_success(xendom.do_legacy_api_with_uuid(
219 fn, vm_ref, *args))
221 class XendAPI:
222 """Implementation of the Xen-API in Xend. Expects to be
223 used via XMLRPCServer.
225 All methods that need a valid session are marked with
226 a L{XendAuthManager.session_required} decorator that will
227 transparently perform the required session authentication.
229 We need to support Python <2.4, so we use the old decorator syntax.
231 All XMLRPC accessible methods require an 'api' attribute and
232 is set to the XMLRPC function name which the method implements.
233 """
235 def __init__(self):
236 """Initialised Xen API wrapper by making sure all functions
237 have the correct validation decorators such as L{valid_host}
238 and L{session_required}.
239 """
241 classes = {
242 'session': (session_required,),
243 'host': (valid_host, session_required),
244 'host_cpu': (valid_host_cpu, session_required),
245 'VM': (valid_vm, session_required),
246 'VBD': (valid_vbd, session_required),
247 'VIF': (valid_vif, session_required),
248 'VDI': (valid_vdi, session_required),
249 'SR': (valid_sr, session_required)}
251 # Cheat methods
252 # -------------
253 # Methods that have a trivial implementation for all classes.
254 # 1. get_by_uuid == getting by ref, so just return uuid.
256 for cls in classes.keys():
257 get_by_uuid = '%s_get_by_uuid' % cls.lower()
258 get_uuid = '%s_get_uuid' % cls.lower()
259 setattr(XendAPI, get_by_uuid,
260 lambda s, sess, obj_ref: xen_api_success(obj_ref))
261 setattr(XendAPI, get_uuid,
262 lambda s, sess, obj_ref: xen_api_success(obj_ref))
264 # 2. get_record is just getting all the attributes, so provide
265 # a fake template implementation.
266 #
267 # TODO: ...
270 # Wrapping validators around XMLRPC calls
271 # ---------------------------------------
273 for cls, validators in classes.items():
274 ro_attrs = getattr(self, '%s_attr_ro' % cls, [])
275 rw_attrs = getattr(self, '%s_attr_rw' % cls, [])
276 methods = getattr(self, '%s_methods' % cls, [])
277 funcs = getattr(self, '%s_funcs' % cls, [])
279 # wrap validators around readable class attributes
280 for attr_name in ro_attrs + rw_attrs + self.Base_attr_ro:
281 getter_name = '%s_get_%s' % (cls.lower(), attr_name.lower())
282 try:
283 getter = getattr(XendAPI, getter_name)
284 for validator in validators:
285 getter = validator(getter)
286 getter.api = '%s.get_%s' % (cls, attr_name)
287 setattr(XendAPI, getter_name, getter)
288 except AttributeError:
289 log.warn("API call: %s not found" % getter_name)
291 # wrap validators around writable class attrributes
292 for attr_name in rw_attrs + self.Base_attr_rw:
293 setter_name = '%s_set_%s' % (cls.lower(), attr_name.lower())
294 try:
295 setter = getattr(XendAPI, setter_name)
296 for validator in validators:
297 setter = validator(setter)
298 setter.api = '%s.set_%s' % (cls, attr_name)
299 setattr(XendAPI, setter_name, setter)
300 except AttributeError:
301 log.warn("API call: %s not found" % setter_name)
303 # wrap validators around methods
304 for method_name in methods + self.Base_methods:
305 method_full_name = '%s_%s' % (cls.lower(),method_name.lower())
306 try:
307 method = getattr(XendAPI, method_full_name)
308 for validator in validators:
309 method = validator(method)
310 method.api = '%s.%s' % (cls, method_name)
311 setattr(XendAPI, method_full_name, method)
312 except AttributeError:
313 log.warn('API call: %s not found' % method_full_name)
315 # wrap validators around class functions
316 for func_name in funcs + self.Base_funcs:
317 func_full_name = '%s_%s' % (cls.lower(), func_name.lower())
318 try:
319 method = getattr(XendAPI, func_full_name)
320 method = session_required(method)
321 method.api = '%s.%s' % (cls, func_name)
322 setattr(XendAPI, func_full_name, method)
323 except AttributeError:
324 log.warn('API call: %s not found' % func_full_name)
327 Base_attr_ro = ['uuid']
328 Base_attr_rw = []
329 Base_methods = ['destroy', 'to_XML', 'get_record']
330 Base_funcs = ['create', 'get_by_uuid', 'get_all']
332 # Xen API: Class Session
333 # ----------------------------------------------------------------
334 # NOTE: Left unwrapped by __init__
336 session_attr_ro = ['this_host', 'this_user']
337 session_methods = ['logout']
338 # session_funcs = ['login_with_password']
340 def session_login_with_password(self, username, password):
341 try:
342 session = auth_manager().login_with_password(username, password)
343 return xen_api_success(session)
344 except XendError, e:
345 return xen_api_error(XEND_ERROR_AUTHENTICATION_FAILED)
346 session_login_with_password.api = 'session.login_with_password'
349 # object methods
350 def session_logout(self, session):
351 auth_manager().logout(session)
352 return xen_api_success_void()
353 def session_destroy(self, session):
354 return xen_api_error(XEND_ERROR_UNSUPPORTED)
355 def session_get_record(self, session):
356 record = {'this_host': XendNode.instance().uuid,
357 'this_user': auth_manager().get_user(session)}
358 return xen_api_success(record)
359 def session_to_xml(self, session):
360 return xen_api_todo()
362 # attributes (ro)
363 def session_get_this_host(self, session):
364 return xen_api_success(XendNode.instance().uuid)
365 def session_get_this_user(self, session):
366 user = auth_manager().get_user(session)
367 if user:
368 return xen_api_success(user)
369 return xen_api_error(XEND_ERROR_SESSION_INVALID)
372 # Xen API: Class User
373 # ----------------------------------------------------------------
374 # TODO: NOT IMPLEMENTED YET
376 # Xen API: Class Tasks
377 # ----------------------------------------------------------------
378 # TODO: NOT IMPLEMENTED YET
380 # Xen API: Class Host
381 # ----------------------------------------------------------------
383 host_attr_ro = ['software_version',
384 'resident_VMs',
385 'host_CPUs']
387 host_attr_rw = ['name_label',
388 'name_description']
390 host_methods = ['disable',
391 'enable',
392 'reboot',
393 'shutdown']
395 host_funcs = ['get_by_name_label']
397 # attributes
398 def host_get_name_label(self, session, host_ref):
399 return xen_api_success(XendNode.instance().name)
400 def host_set_name_label(self, session, host_ref):
401 return xen_api_success(XendNode.instance().name)
402 def host_get_name_description(self, session, host_ref):
403 return xen_api_success(XendNode.instance().description)
404 def host_set_name_description(self, session, host_ref):
405 return xen_api_success(XendNode.instance().description)
406 def host_get_software_version(self, session, host_ref):
407 return xen_api_success(XendNode.instance().xen_version())
408 def host_get_resident_vms(self, session, host_ref):
409 return xen_api_success(XendDomain.instance().get_domain_refs())
410 def host_get_host_cpus(self, session, host_ref):
411 return xen_api_success(XendNode.instance().get_host_cpu_refs())
413 # object methods
414 def host_destroy(self, session, host_ref):
415 return xen_api_error(XEND_ERROR_UNSUPPORTED)
416 def host_disable(self, session, host_ref):
417 XendDomain.instance().set_allow_new_domains(False)
418 return xen_api_success_void()
419 def host_enable(self, session, host_ref):
420 XendDomain.instance().set_allow_new_domains(True)
421 return xen_api_success_void()
422 def host_reboot(self, session, host_ref):
423 if not XendDomain.instance().allow_new_domains():
424 return xen_api_error(XEND_ERROR_HOST_RUNNING)
425 return xen_api_error(XEND_ERROR_UNSUPPORTED)
426 def host_shutdown(self, session, host_ref):
427 if not XendDomain.instance().allow_new_domains():
428 return xen_api_error(XEND_ERROR_HOST_RUNNING)
429 return xen_api_error(XEND_ERROR_UNSUPPORTED)
430 def host_get_record(self, session, host_ref):
431 node = XendNode.instance()
432 dom = XendDomain.instance()
433 record = {'name_label': node.name,
434 'name_description': '',
435 'software_version': node.xen_version(),
436 'resident_VMs': dom.get_domain_refs(),
437 'host_CPUs': node.get_host_cpu_refs()}
438 return xen_api_success(record)
440 # class methods
441 def host_get_all(self, session):
442 return xen_api_success((XendNode.instance().uuid,))
443 def host_create(self, session, struct):
444 return xen_api_error(XEND_ERROR_UNSUPPORTED)
446 # Xen API: Class Host_CPU
447 # ----------------------------------------------------------------
449 host_cpu_attr_ro = ['host',
450 'number',
451 'features',
452 'utilisation']
454 # attributes
455 def host_cpu_get_uuid(self, session, host_cpu_ref):
456 uuid = XendNode.instance().get_host_cpu_uuid(host_cpu_ref)
457 return xen_api_success(uuid)
458 def host_cpu_get_host(self, session, host_cpu_ref):
459 return xen_api_success(XendNode.instance().uuid)
460 def host_cpu_get_features(self, session, host_cpu_ref):
461 features = XendNode.instance().get_host_cpu_features(host_cpu_ref)
462 return xen_api_success(features)
463 def host_cpu_get_utilisation(self, session, host_cpu_ref):
464 util = XendNode.instance().get_host_cpu_load(host_cpu_ref)
465 return xen_api_success(util)
466 def host_cpu_get_number(self, session, host_cpu_ref):
467 num = XendNode.instance().get_host_cpu_number(host_cpu_ref)
468 return xen_api_success(num)
470 # object methods
471 def host_cpu_destroy(self, session, host_cpu_ref):
472 return xen_api_error(XEND_ERROR_UNSUPPORTED)
473 def host_cpu_get_record(self, session, host_cpu_ref):
474 node = XendNode.instance()
475 record = {'uuid': host_cpu_ref,
476 'host': node.uuid,
477 'number': node.get_host_cpu_number(host_cpu_ref),
478 'features': node.get_host_cpu_features(host_cpu_ref),
479 'utilisation': node.get_host_cpu_load(host_cpu_ref)}
480 return xen_api_success(record)
481 def host_cpu_to_xml(self, session, host_cpu_ref):
482 return xen_api_todo()
484 # class methods
485 def host_cpu_get_all(self, session):
486 return xen_api_success(XendNode.instance().get_host_cpu_refs())
487 def host_cpu_create(self, session, struct):
488 return xen_api_error(XEND_ERROR_UNSUPPORTED)
491 # Xen API: Class Network
492 # ----------------------------------------------------------------
493 # TODO: NOT IMPLEMENTED
495 Network_attr_ro = ['VIFs']
496 Network_attr_rw = ['name_label',
497 'name_description',
498 'NIC',
499 'VLAN',
500 'default_gateway',
501 'default_netmask']
503 # Xen API: Class VM
504 # ----------------------------------------------------------------
506 VM_attr_ro = ['power_state',
507 'resident_on',
508 'memory_actual',
509 'memory_static_max',
510 'memory_static_min',
511 'VCPUs_number',
512 'VCPUs_utilisation',
513 'VCPUs_features_required',
514 'VCPUs_can_use',
515 'VIFs',
516 'VBDs',
517 'TPM_instance',
518 'TPM_backend',
519 'PCI_bus',
520 'tools_version',
521 ]
523 VM_attr_rw = ['name_label',
524 'name_description',
525 'user_version',
526 'is_a_template',
527 'memory_dynamic_max',
528 'memory_dynamic_min',
529 'VCPUs_policy',
530 'VCPUs_params',
531 'VCPUs_features_force_on',
532 'VCPUS_features_force_off',
533 'actions_after_shutdown',
534 'actions_after_reboot',
535 'actions_after_suspend',
536 'actions_after_crash',
537 'bios_boot',
538 'platform_std_VGA',
539 'platform_serial',
540 'platform_localtime',
541 'platform_clock_offset',
542 'platform_enable_audio',
543 'builder',
544 'boot_method',
545 'kernel_kernel',
546 'kernel_initrd',
547 'kernel_args',
548 'grub_cmdline',
549 'other_config']
551 VM_methods = ['clone',
552 'start',
553 'pause',
554 'unpause',
555 'clean_shutdown',
556 'clean_reboot',
557 'hard_shutdown',
558 'hard_reboot',
559 'suspend',
560 'resume']
562 VM_funcs = ['get_by_name_label']
564 # parameters required for _create()
565 VM_attr_inst = [
566 'name_label',
567 'name_description',
568 'user_version',
569 'is_a_template',
570 'memory_static_max',
571 'memory_dynamic_max',
572 'memory_dynamic_min',
573 'memory_static_min',
574 'VCPUs_policy',
575 'VCPUs_params',
576 'VCPUs_features_required',
577 'VCPUs_features_can_use',
578 'VCPUs_features_force_on',
579 'VCPUs_features_force_off',
580 'actions_after_shutdown',
581 'actions_after_reboot',
582 'actions_after_suspend',
583 'actions_after_crash',
584 'TPM_instance',
585 'TPM_backend',
586 'bios_boot',
587 'platform_std_VGA',
588 'platform_serial',
589 'platform_localtime',
590 'platform_clock_offset',
591 'platform_enable_audio',
592 'builder',
593 'boot_method',
594 'kernel_kernel',
595 'kernel_initrd',
596 'kernel_args',
597 'grub_cmdline',
598 'PCI_bus',
599 'other_config']
601 # attributes (ro)
602 def vm_get_power_state(self, session, vm_ref):
603 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
604 return xen_api_success(dom.state)
606 def vm_get_resident_on(self, session, vm_ref):
607 return xen_api_success(XendNode.instance().uuid)
609 def vm_get_memory_actual(self, session, vm_ref):
610 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
611 return xen_api_todo() # unsupported by xc
613 def vm_get_memory_static_max(self, session, vm_ref):
614 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
615 return xen_api_success(dom.get_memory_static_max())
617 def vm_get_memory_static_min(self, session, vm_ref):
618 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
619 return xen_api_success(dom.get_memory_static_min())
621 def vm_get_vcpus_number(self, session, vm_ref):
622 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
623 return xen_api_success(dom.getVCpuCount())
625 def vm_get_vcpus_utilisation(self, session, vm_ref):
626 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
627 return xen_api_success(dom.get_vcpus_util())
629 def vm_get_vcpus_features_required(self, session, vm_ref):
630 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
631 return xen_api_todo() # unsupported by xc
633 def vm_get_vcpus_can_use(self, session, vm_ref):
634 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
635 return xen_api_todo() # unsupported by xc
637 def vm_get_vifs(self, session, vm_ref):
638 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
639 return xen_api_success(dom.get_vifs())
641 def vm_get_vbds(self, session, vm_ref):
642 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
643 return xen_api_success(dom.get_vbds())
645 def vm_get_tpm_instance(self, session, vm_ref):
646 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
647 return xen_api_todo() # unsupported by xc
649 def vm_get_tpm_backend(self, session, vm_ref):
650 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
651 return xen_api_todo() # unsupported by xc
653 def vm_get_pci_bus(self, session, vm_ref):
654 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
655 return xen_api_todo() # unsupported by xc
657 def vm_get_tools_version(self, session, vm_ref):
658 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
659 return xen_api_todo()
661 # attributes (rw)
662 def vm_get_name_label(self, session, vm_ref):
663 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
664 return xen_api_success(dom.getName())
666 def vm_get_name_description(self, session, vm_ref):
667 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
668 return xen_api_todo()
670 def vm_get_user_version(self, session, vm_ref):
671 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
672 return xen_api_todo()
674 def vm_get_is_a_template(self, session, vm_ref):
675 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
676 return xen_api_todo()
678 def vm_get_memory_dynamic_max(self, session, vm_ref):
679 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
680 return xen_api_todo()
682 def vm_get_memory_dynamic_min(self, session, vm_ref):
683 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
684 return xen_api_todo()
686 def vm_get_vcpus_policy(self, session, vm_ref):
687 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
688 return xen_api_todo() # need to access scheduler
690 def vm_get_vcpus_params(self, session, vm_ref):
691 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
692 return xen_api_todo() # need access to scheduler
694 def vm_get_vcpus_features_force_on(self, session, vm_ref):
695 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
696 return xen_api_todo()
698 def vm_get_vcpus_features_force_off(self, session, vm_ref):
699 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
700 return xen_api_todo()
702 def vm_get_actions_after_shutdown(self, session, vm_ref):
703 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
704 return xen_api_success(dom.get_on_shutdown())
706 def vm_get_actions_after_reboot(self, session, vm_ref):
707 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
708 return xen_api_success(dom.get_on_reboot())
710 def vm_get_actions_after_suspend(self, session, vm_ref):
711 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
712 return xen_api_success(dom.get_on_suspend())
714 def vm_get_actions_after_crash(self, session, vm_ref):
715 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
716 return xen_api_success(dom.get_on_crash())
718 def vm_get_bios_boot(self, session, vm_ref):
719 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
720 return xen_api_success(dom.get_bios_boot())
722 def vm_get_platform_std_vga(self, session, vm_ref):
723 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
724 return xen_api_todo()
726 def vm_get_platform_serial(self, session, vm_ref):
727 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
728 return xen_api_todo()
730 def vm_get_platform_localtime(self, session, vm_ref):
731 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
732 return xen_api_todo()
734 def vm_get_platform_clock_offset(self, session, vm_ref):
735 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
736 return xen_api_todo()
738 def vm_get_platform_enable_audio(self, session, vm_ref):
739 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
740 return xen_api_todo()
742 def vm_get_builder(self, session, vm_ref):
743 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
744 return xen_api_todo()
746 def vm_get_boot_method(self, session, vm_ref):
747 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
748 return xen_api_success('')
750 def vm_get_kernel_kernel(self, session, vm_ref):
751 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
752 return xen_api_success('')
754 def vm_get_kernel_initrd(self, session, vm_ref):
755 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
756 return xen_api_success('')
758 def vm_get_kernel_args(self, session, vm_ref):
759 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
760 return xen_api_success('')
762 def vm_get_grub_cmdline(self, session, vm_ref):
763 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
764 return xen_api_success('')
766 def vm_get_other_config(self, session, vm_ref):
767 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
768 return xen_api_todo()
770 def vm_set_name_label(self, session, vm_ref):
771 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
772 return xen_api_success_void()
774 def vm_set_name_description(self, session, vm_ref):
775 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
776 return xen_api_success_void()
778 def vm_set_user_version(self, session, vm_ref):
779 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
780 return xen_api_success_void()
782 def vm_set_is_a_template(self, session, vm_ref):
783 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
784 return xen_api_success_void()
786 def vm_set_memory_dynamic_max(self, session, vm_ref):
787 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
788 return xen_api_success_void()
790 def vm_set_memory_dynamic_min(self, session, vm_ref):
791 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
792 return xen_api_success_void()
794 def vm_set_vcpus_policy(self, session, vm_ref):
795 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
796 return xen_api_success_void()
798 def vm_set_vcpus_params(self, session, vm_ref):
799 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
800 return xen_api_success_void()
802 def vm_set_vcpus_features_force_on(self, session, vm_ref):
803 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
804 return xen_api_success_void()
806 def vm_set_vcpus_features_force_off(self, session, vm_ref):
807 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
808 return xen_api_success_void()
810 def vm_set_actions_after_shutdown(self, session, vm_ref):
811 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
812 return xen_api_success_void()
814 def vm_set_actions_after_reboot(self, session, vm_ref):
815 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
816 return xen_api_success_void()
818 def vm_set_actions_after_suspend(self, session, vm_ref):
819 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
820 return xen_api_success_void()
822 def vm_set_actions_after_crash(self, session, vm_ref):
823 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
824 return xen_api_success_void()
826 def vm_set_bios_boot(self, session, vm_ref):
827 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
828 return xen_api_success_void()
830 def vm_set_platform_std_vga(self, session, vm_ref):
831 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
832 return xen_api_success_void()
834 def vm_set_platform_serial(self, session, vm_ref):
835 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
836 return xen_api_success_void()
838 def vm_set_platform_localtime(self, session, vm_ref):
839 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
840 return xen_api_success_void()
842 def vm_set_platform_clock_offset(self, session, vm_ref):
843 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
844 return xen_api_success_void()
846 def vm_set_platform_enable_audio(self, session, vm_ref):
847 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
848 return xen_api_success_void()
850 def vm_set_builder(self, session, vm_ref):
851 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
852 return xen_api_success_void()
854 def vm_set_boot_method(self, session, vm_ref):
855 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
856 return xen_api_success_void()
858 def vm_set_kernel_kernel(self, session, vm_ref):
859 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
860 return xen_api_success_void()
862 def vm_set_kernel_initrd(self, session, vm_ref):
863 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
864 return xen_api_success_void()
866 def vm_set_kernel_args(self, session, vm_ref):
867 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
868 return xen_api_success_void()
870 def vm_set_grub_cmdline(self, session, vm_ref):
871 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
872 return xen_api_success_void()
874 def vm_set_other_config(self, session, vm_ref):
875 dom = XendDomain.instance().get_vm_by_uuid(vm_ref)
876 return xen_api_success_void()
878 # class methods
879 def vm_get_all(self, session):
880 refs = [d.get_uuid() for d in XendDomain.instance().list()]
881 return xen_api_success(refs)
883 def vm_get_by_name_label(self, session, label):
884 xendom = XendDomain.instance()
885 dom = xendom.domain_lookup_nr(label)
886 if dom:
887 return xen_api_success([dom.get_uuid()])
888 return xen_api_error(XEND_ERROR_VM_INVALID)
890 def vm_create(self, session, vm_struct):
891 xendom = XendDomain.instance()
892 domuuid = xendom.create_domain(vm_struct)
893 return xen_api_success(domuuid)
895 # object methods
896 def vm_to_xml(self, session, vm_ref):
897 return xen_api_todo()
899 def vm_get_record(self, session, vm_ref):
900 xendom = XendDomain.instance()
901 xeninfo = xendom.get_vm_by_uuid(vm_ref)
902 if not xeninfo:
903 return xen_api_error(XEND_ERROR_VM_INVALID)
905 record = {
906 'uuid': xeninfo.get_uuid(),
907 'power_state': xeninfo.get_power_state(),
908 'name_label': xeninfo.getName(),
909 'name_description': xeninfo.getName(),
910 'user_version': 1,
911 'is_a_template': False,
912 'resident_on': XendNode.instance().uuid,
913 'memory_static_min': xeninfo.get_memory_static_min(),
914 'memory_static_max': xeninfo.get_memory_static_max(),
915 'memory_dynamic_min': xeninfo.get_memory_static_min(),
916 'memory_dynamic_max': xeninfo.get_memory_static_max(),
917 'memory_actual': xeninfo.get_memory_static_min(),
918 'vcpus_policy': xeninfo.get_vcpus_policy(),
919 'vcpus_params': xeninfo.get_vcpus_params(),
920 'vcpus_number': xeninfo.getVCpuCount(),
921 'vcpus_utilisation': xeninfo.get_vcpus_util(),
922 'vcpus_features_required': [],
923 'vcpus_features_can_use': [],
924 'vcpus_features_force_on': [],
925 'vcpus_features_force_off': [],
926 'actions_after_shutdown': xeninfo.get_on_shutdown(),
927 'actions_after_reboot': xeninfo.get_on_reboot(),
928 'actions_after_suspend': xeninfo.get_on_suspend(),
929 'actions_after_crash': xeninfo.get_on_crash(),
930 'vifs': xeninfo.get_vifs(),
931 'vbds': xeninfo.get_vbds(),
932 'tpm_instance': xeninfo.get_tpm_instance(),
933 'tpm_backend': xeninfo.get_tpm_backend(),
934 'bios_boot': xeninfo.get_bios_boot(),
935 'platform_std_vga': xeninfo.get_platform_std_vga(),
936 'platform_serial': xeninfo.get_platform_serial(),
937 'platform_localtime': xeninfo.get_platform_localtime(),
938 'platform_clock_offset': xeninfo.get_platform_clock_offset(),
939 'platform_enable_audio': xeninfo.get_platform_enable_audio(),
940 'builder': xeninfo.get_builder(),
941 'boot_method': xeninfo.get_boot_method(),
942 'kernel_kernel': xeninfo.get_kernel_image(),
943 'kernel_initrd': xeninfo.get_kernel_initrd(),
944 'kernel_args': xeninfo.get_kernel_args(),
945 'grub_cmdline': xeninfo.get_grub_cmdline(),
946 'pci_bus': xeninfo.get_pci_bus(),
947 'tools_version': xeninfo.get_tools_version(),
948 'otherconfig': xeninfo.get_other_config()
949 }
950 return xen_api_success(record)
952 def vm_clean_reboot(self, session, vm_ref):
953 xendom = XendDomain.instance()
954 xeninfo = xendom.get_vm_by_uuid(vm_ref)
955 xeninfo.shutdown("reboot")
956 return xen_api_success_void()
957 def vm_clean_shutdown(self, session, vm_ref):
958 xendom = XendDomain.instance()
959 xeninfo = xendom.get_vm_by_uuid(vm_ref)
960 xeninfo.shutdown("poweroff")
961 return xen_api_success_void()
962 def vm_clone(self, session, vm_ref):
963 return xen_api_error(XEND_ERROR_UNSUPPORTED)
964 def vm_destroy(self, session, vm_ref):
965 return do_vm_func("domain_delete", vm_ref)
966 def vm_hard_reboot(self, session, vm_ref):
967 return xen_api_error(XEND_ERROR_UNSUPPORTED)
968 def vm_hard_shutdown(self, session, vm_ref):
969 return do_vm_func("domain_destroy", vm_ref)
970 def vm_pause(self, session, vm_ref):
971 return do_vm_func("domain_pause", vm_ref)
972 def vm_resume(self, session, vm_ref, start_paused):
973 return do_vm_func("domain_resume", vm_ref)
974 def vm_start(self, session, vm_ref):
975 return do_vm_func("domain_start", vm_ref)
976 def vm_suspend(self, session, vm_ref):
977 return do_vm_func("domain_suspend", vm_ref)
978 def vm_unpause(self, session, vm_ref):
979 return do_vm_func("domain_unpause", vm_ref)
981 # Xen API: Class VDI
982 # ----------------------------------------------------------------
983 # TODO: NOT IMPLEMENTED.
985 # Xen API: Class VBD
986 # ----------------------------------------------------------------
988 VBD_attr_ro = ['image',
989 'IO_bandwidth_incoming_kbs',
990 'IO_bandwidth_outgoing_kbs']
991 VBD_attr_rw = ['VM',
992 'VDI',
993 'device',
994 'mode',
995 'driver']
997 VBD_attr_inst = VBD_attr_rw + ['image']
999 # object methods
1000 def vbd_get_record(self, session, vbd_ref):
1001 xendom = XendDomain.instance()
1002 vm = xendom.get_vm_with_dev_uuid('vbd', vbd_ref)
1003 if not vm:
1004 return xen_api_error(XEND_ERROR_VBD_INVALID)
1005 cfg = vm.get_dev_xenapi_config('vbd', vbd_ref)
1006 if not cfg:
1007 return xen_api_error(XEND_ERROR_VBD_INVALID)
1008 return xen_api_success(cfg)
1010 # class methods
1011 def vbd_create(self, session, vbd_struct):
1012 xendom = XendDomain.instance()
1013 if not xendom.is_valid_vm(vbd_struct['VM']):
1014 return xen_api_error(XEND_ERROR_DOMAIN_INVALID)
1016 dom = xendom.get_vm_by_uuid(vbd_struct['VM'])
1017 vbd_ref = ''
1018 try:
1019 if not vbd_struct.get('VDI', None):
1020 # this is a traditional VBD without VDI and SR
1021 vbd_ref = dom.create_vbd(vbd_struct)
1022 else:
1023 # new VBD via VDI/SR
1024 vdi_ref = vbd_struct.get('VDI')
1025 sr = XendNode.instance().get_sr()
1026 vdi_image = sr.xen_api_get_by_uuid(vdi_ref)
1027 if not vdi_image:
1028 return xen_api_error(XEND_ERROR_VDI_INVALID)
1029 vdi_image = vdi_image.qcow_path
1030 vbd_ref = dom.create_vbd_with_vdi(vbd_struct, vdi_image)
1031 except XendError:
1032 return xen_api_todo()
1034 xendom.managed_config_save(dom)
1035 return xen_api_success(vbd_ref)
1037 # attributes (rw)
1038 def vbd_get_vm(self, session, vbd_ref):
1039 xendom = XendDomain.instance()
1040 return xen_api_success(xendom.get_dev_property('vbd', vbd_ref, 'VM'))
1042 def vbd_get_vdi(self, session, vbd_ref):
1043 return xen_api_todo()
1045 def vbd_get_device(self, session, vbd_ref):
1046 xendom = XendDomain.instance()
1047 return xen_api_success(xendom.get_dev_property('vbd', vbd_ref,
1048 'device'))
1049 def vbd_get_mode(self, session, vbd_ref):
1050 xendom = XendDomain.instance()
1051 return xen_api_success(xendom.get_dev_property('vbd', vbd_ref,
1052 'mode'))
1053 def vbd_get_driver(self, session, vbd_ref):
1054 xendom = XendDomain.instance()
1055 return xen_api_success(xendom.get_dev_property('vbd', vbd_ref,
1056 'driver'))
1058 # Xen API: Class VIF
1059 # ----------------------------------------------------------------
1061 VIF_attr_ro = ['network_read_kbs',
1062 'network_write_kbs',
1063 'IO_bandwidth_incoming_kbs',
1064 'IO_bandwidth_outgoing_kbs']
1065 VIF_attr_rw = ['name',
1066 'type',
1067 'device',
1068 'network',
1069 'VM',
1070 'MAC',
1071 'MTU']
1073 VIF_attr_inst = VIF_attr_rw
1075 # object methods
1076 def vif_get_record(self, session, vif_ref):
1077 xendom = XendDomain.instance()
1078 vm = xendom.get_vm_with_dev_uuid('vif', vif_ref)
1079 if not vm:
1080 return xen_api_error(XEND_ERROR_VIF_INVALID)
1081 cfg = vm.get_dev_xenapi_config('vif', vif_ref)
1082 if not cfg:
1083 return xen_api_error(XEND_ERROR_VIF_INVALID)
1084 valid_vif_keys = self.VIF_attr_ro + self.VIF_attr_rw + \
1085 self.Base_attr_ro + self.Base_attr_rw
1086 for k in cfg.keys():
1087 if k not in valid_vif_keys:
1088 del cfg[k]
1090 return xen_api_success(cfg)
1092 # class methods
1093 def vif_create(self, session, vif_struct):
1094 xendom = XendDomain.instance()
1095 if xendom.is_valid_vm(vif_struct['VM']):
1096 dom = xendom.get_vm_by_uuid(vif_struct['VM'])
1097 try:
1098 vif_ref = dom.create_vif(vif_struct)
1099 xendom.managed_config_save(dom)
1100 return xen_api_success(vif_ref)
1101 except XendError:
1102 return xen_api_error(XEND_ERROR_TODO)
1103 else:
1104 return xen_api_error(XEND_ERROR_DOMAIN_INVALID)
1107 # Xen API: Class VDI
1108 # ----------------------------------------------------------------
1109 VDI_attr_ro = ['VBDs',
1110 'physical_utilisation',
1111 'sector_size',
1112 'type',
1113 'parent',
1114 'children']
1115 VDI_attr_rw = ['name_label',
1116 'name_description',
1117 'SR',
1118 'virtual_size',
1119 'sharable',
1120 'read_only']
1121 VDI_attr_inst = VDI_attr_ro + VDI_attr_rw
1123 VDI_methods = ['snapshot']
1124 VDI_funcs = ['get_by_name_label']
1126 def vdi_get_vbds(self, session, vdi_ref):
1127 return xen_api_todo()
1129 def vdi_get_physical_utilisation(self, session, vdi_ref):
1130 sr = XendNode.instance().get_sr()
1131 image = sr.xen_api_get_by_uuid(vdi_ref)
1132 return xen_api_success(image.get_physical_utilisation())
1134 def vdi_get_sector_size(self, session, vdi_ref):
1135 sr = XendNode.instance().get_sr()
1136 image = sr.xen_api_get_by_uuid(vdi_ref)
1137 return xen_api_success(image.sector_size)
1139 def vdi_get_type(self, session, vdi_ref):
1140 sr = XendNode.instance().get_sr()
1141 image = sr.xen_api_get_by_uuid(vdi_ref)
1142 return xen_api_success(image.type)
1144 def vdi_get_parent(self, session, vdi_ref):
1145 sr = XendNode.instance().get_sr()
1146 image = sr.xen_api_get_by_uuid(vdi_ref)
1147 return xen_api_success(image.parent)
1149 def vdi_get_children(self, session, vdi_ref):
1150 sr = XendNode.instance().get_sr()
1151 image = sr.xen_api_get_by_uuid(vdi_ref)
1152 return xen_api_success(image.children)
1154 def vdi_get_name_label(self, session, vdi_ref):
1155 sr = XendNode.instance().get_sr()
1156 image = sr.xen_api_get_by_uuid(vdi_ref)
1157 return xen_api_success(image.name_label)
1159 def vdi_get_name_description(self, session, vdi_ref):
1160 sr = XendNode.instance().get_sr()
1161 image = sr.xen_api_get_by_uuid(vdi_ref)
1162 return xen_api_success(image.name_description)
1164 def vdi_get_sr(self, session, vdi_ref):
1165 sr = XendNode.instance().get_sr()
1166 return xen_api_success(sr.uuid)
1168 def vdi_get_virtual_size(self, session, vdi_ref):
1169 sr = XendNode.instance().get_sr()
1170 image = sr.xen_api_get_by_uuid(vdi_ref)
1171 return xen_api_success(image.virtual_size)
1173 def vdi_get_sharable(self, session, vdi_ref):
1174 sr = XendNode.instance().get_sr()
1175 image = sr.xen_api_get_by_uuid(vdi_ref)
1176 return xen_api_success(image.sharable)
1178 def vdi_get_read_only(self, session, vdi_ref):
1179 sr = XendNode.instance().get_sr()
1180 image = sr.xen_api_get_by_uuid(vdi_ref)
1181 return xen_api_success(image.sharable)
1183 def vdi_set_name_label(self, session, vdi_ref, value):
1184 sr = XendNode.instance().get_sr()
1185 image = sr.xen_api_get_by_uuid(vdi_ref)
1186 image.name_label = value
1187 return xen_api_success_void()
1189 def vdi_set_name_description(self, session, vdi_ref, value):
1190 sr = XendNode.instance().get_sr()
1191 image = sr.xen_api_get_by_uuid(vdi_ref)
1192 image.name_description = value
1193 return xen_api_success_void()
1195 def vdi_set_sr(self, session, vdi_ref, value):
1196 return xen_api_error(XEND_ERROR_UNSUPPORTED)
1198 def vdi_set_virtual_size(self, session, vdi_ref, value):
1199 return xen_api_error(XEND_ERROR_UNSUPPORTED)
1201 def vdi_set_sharable(self, session, vdi_ref, value):
1202 return xen_api_todo()
1203 def vdi_set_read_only(self, session, vdi_ref, value):
1204 return xen_api_todo()
1206 # Object Methods
1207 def vdi_snapshot(self, session, vdi_ref):
1208 return xen_api_todo()
1210 def vdi_destroy(self, session, vdi_ref):
1211 sr = XendNode.instance().get_sr()
1212 sr.destroy_image(vdi_ref)
1213 return xen_api_success_void()
1215 def vdi_to_xml(self, session, vdi_ref):
1216 return xen_api_todo()
1218 def vdi_get_record(self, session, vdi_ref):
1219 sr = XendNode.instance().get_sr()
1220 image = sr.xen_api_get_by_uuid(vdi_ref)
1221 if image:
1222 return xen_api_success({
1223 'uuid': vdi_ref,
1224 'name_label': image.name_label,
1225 'name_description': image.name_description,
1226 'SR': sr.uuid,
1227 'VBDs': [], # TODO
1228 'virtual_size': image.virtual_size,
1229 'physical_utilisation': image.physical_utilisation,
1230 'sector_size': image.sector_size,
1231 'type': image.type,
1232 'parent': image.parent,
1233 'children': image.children,
1234 'sharable': image.sharable,
1235 'read_only': image.read_only,
1236 })
1238 return xen_api_error(XEND_ERROR_VDI_INVALID)
1240 # Class Functions
1241 def vdi_create(self, session, vdi_struct):
1242 sr = XendNode.instance().get_sr()
1243 sr_ref = vdi_struct['SR']
1244 if sr.uuid != sr_ref:
1245 return xen_api_error(XEND_ERROR_SR_INVALID)
1247 vdi_uuid = sr.create_image(vdi_struct)
1248 return xen_api_success(vdi_uuid)
1250 def vdi_get_all(self, session):
1251 sr = XendNode.instance().get_sr()
1252 return xen_api_success(sr.list_images())
1254 def vdi_get_by_name_label(self, session, name):
1255 sr = XendNode.instance().get_sr()
1256 image_uuid = sr.xen_api_get_by_name_label(name)
1257 if image_uuid:
1258 return xen_api_success(image_uuid)
1260 return xen_api_error(XEND_ERROR_VDI_INVALID)
1263 # Xen API: Class SR
1264 # ----------------------------------------------------------------
1265 SR_attr_ro = ['VDIs',
1266 'virtual_allocation',
1267 'physical_utilisation',
1268 'physical_size',
1269 'type',
1270 'location']
1272 SR_attr_rw = ['name_label',
1273 'name_description']
1275 SR_attr_inst = ['physical_size',
1276 'type',
1277 'location',
1278 'name_label',
1279 'name_description']
1281 SR_methods = ['clone']
1282 SR_funcs = ['get_by_name_label']
1284 # Class Functions
1285 def sr_get_all(self, session):
1286 sr = XendNode.instance().get_sr()
1287 return xen_api_success([sr.uuid])
1289 def sr_get_by_name_label(self, session, label):
1290 sr = XendNode.instance().get_sr()
1291 if sr.name_label != label:
1292 return xen_api_error(XEND_ERROR_SR_INVALID)
1293 return xen_api_success([sr.uuid])
1295 def sr_create(self, session):
1296 return xen_api_error(XEND_ERROR_UNSUPPORTED)
1298 def sr_get_by_uuid(self, session):
1299 return xen_api_success(XendNode.instance().get_sr().uuid)
1301 # Class Methods
1302 def sr_clone(self, session, sr_ref):
1303 return xen_api_error(XEND_ERROR_UNSUPPORTED)
1304 def sr_destroy(self, session, sr_ref):
1305 return xen_api_error(XEND_ERROR_UNSUPPORTED)
1307 def sr_to_xml(self, session, sr_ref):
1308 return xen_api_todo()
1310 def sr_get_record(self, session, sr_ref):
1311 sr = XendNode.instance().get_sr()
1312 return xen_api_success({
1313 'uuid': sr.uuid,
1314 'name_label': sr.name_label,
1315 'name_description': sr.name_description,
1316 'VDIs': sr.list_images(),
1317 'virtual_allocation': sr.used_space_bytes(),
1318 'physical_utilisation': sr.used_space_bytes(),
1319 'physical_size': sr.total_space_bytes(),
1320 'type': sr.type,
1321 'location': sr.location
1322 })
1324 # Attribute acceess
1325 def sr_get_vdis(self, session, sr_ref):
1326 sr = XendNode.instance().get_sr()
1327 return xen_api_success(sr.list_images())
1329 def sr_get_virtual_allocation(self, session, sr_ref):
1330 sr = XendNode.instance().get_sr()
1331 return sr.used_space_bytes()
1333 def sr_get_physical_utilisation(self, session, sr_ref):
1334 sr = XendNode.instance().get_sr()
1335 return sr.used_space_bytes()
1337 def sr_get_physical_size(self, session, sr_ref):
1338 sr = XendNode.instance().get_sr()
1339 return sr.total_space_bytes()
1341 def sr_get_type(self, session, sr_ref):
1342 sr = XendNode.instance().get_sr()
1343 return xen_api_success(sr.type)
1345 def sr_get_location(self, session, sr_ref):
1346 sr = XendNode.instance().get_sr()
1347 return xen_api_success(sr.location)
1349 def sr_get_name_label(self, session, sr_ref):
1350 sr = XendNode.instance().get_sr()
1351 return xen_api_success(sr.name_label)
1353 def sr_get_name_description(self, session, sr_ref):
1354 sr = XendNode.instance().get_sr()
1355 return xen_api_success(sr.name_description)
1357 def sr_set_name_label(self, session, sr_ref, value):
1358 sr = XendNode.instance().get_sr()
1359 sr.name_label = value
1360 return xen_api_success_void()
1362 def sr_set_name_description(self, session, sr_ref, value):
1363 sr = XendNode.instance().get_sr()
1364 sr.name_description = value
1365 return xen_api_success_void()
1368 # Auto generate some stubs based on XendAPI introspection
1370 if __name__ == "__main__":
1371 def output(line):
1372 print ' ' + line
1374 classes = ['VDI', 'SR']
1375 for cls in classes:
1376 ro_attrs = getattr(XendAPI, '%s_attr_ro' % cls, [])
1377 rw_attrs = getattr(XendAPI, '%s_attr_rw' % cls, [])
1378 methods = getattr(XendAPI, '%s_methods' % cls, [])
1379 funcs = getattr(XendAPI, '%s_funcs' % cls, [])
1381 ref = '%s_ref' % cls.lower()
1383 for attr_name in ro_attrs + rw_attrs + XendAPI.Base_attr_ro:
1384 getter_name = '%s_get_%s' % (cls.lower(), attr_name.lower())
1385 output('def %s(self, session, %s):' % (getter_name, ref))
1386 output(' return xen_api_todo()')
1388 for attr_name in rw_attrs + XendAPI.Base_attr_rw:
1389 setter_name = '%s_set_%s' % (cls.lower(), attr_name.lower())
1390 output('def %s(self, session, %s, value):' % (setter_name, ref))
1391 output(' return xen_api_todo()')
1393 for method_name in methods + XendAPI.Base_methods:
1394 method_full_name = '%s_%s' % (cls.lower(),method_name.lower())
1395 output('def %s(self, session, %s):' % (method_full_name, ref))
1396 output(' return xen_api_todo()')
1398 for func_name in funcs + XendAPI.Base_funcs:
1399 func_full_name = '%s_%s' % (cls.lower(), func_name.lower())
1400 output('def %s(self, session):' % func_full_name)
1401 output(' return xen_api_todo()')