Skip to main content

an object-oriented Xen API for python

Project description

XEN Bridge

An object-oriented Xen API for python

Tested on XCP-ng, but should also work on XenServer

Install from pypi using pip install xen-bridge

Usage

from xenbridge import XenConnection

xen = XenConnection('http://XEN_HOSTNAME', 'root', 'password')

xoa_vm = xen.VM.get_by_uuid('UUID_OF_VM')
# Or by name-label:
# xoa_vm = xen.VM.get_by_name_label('XOA')[0]

print(f'{xoa_vm.name_label} ({xoa_vm.uuid})')
print(f'VM is a template: {xoa_vm.is_a_template}')
xoa_vm.name_description = 'This is a VM'
xoa_vm.start()      # Spin up the VM

Exceptions

While calling API methods, XEN might return an error. When this happens, a XenError is raised. When catching the exception, the error code can be accessed through the error_code attribute

# Assuming VM is already running:
try:
    xoa_vm.start()      # Should throw an error
except xenbridge.XenError as e:
    print(e.error_code)     # Prints 'VM_BAD_POWER_STATE'

How it works

Firstly, xenboject.py defines some helper functions and baseclasses for the endpoints that do the actual work of calling the XMLRPC API and casting the data to the corresponding types.

For each class, there is a file corresponding to that class - for example, vm.py. In here, a class that defines the methods and properties can be found. All methods are wrapped using the @XenMethod decorator that copies the function's signature and replaces its functionality.

class VM(XenObject):
    @XenMethod
    def start(self, start_paused: bool, force: bool) -> None:
        """Start the specified VM.  This function can only be called with the VM is in the
        Halted State."""
    power_state: VmPowerState = XenProperty(XenProperty.READONLY)

As the API responds with a string for numbers, enums and Xen objects, the return type annotations are used to cast the objects to the correct type.

The XenConnection class is the object that is used to interact with the API

Missing methods

All API methods are generated from the XenAPI documentation using docscraper.py. If there is a method that is missing, you can either:

  • Add it to the corresponding class in the module yourself
  • Call it using the class's call(methodname, *args) method and manually cast it to the correct data type

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xen-bridge-0.2.tar.gz (56.1 kB view details)

Uploaded Source

Built Distribution

xen_bridge-0.2-py3-none-any.whl (101.0 kB view details)

Uploaded Python 3

File details

Details for the file xen-bridge-0.2.tar.gz.

File metadata

  • Download URL: xen-bridge-0.2.tar.gz
  • Upload date:
  • Size: 56.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for xen-bridge-0.2.tar.gz
Algorithm Hash digest
SHA256 26d9a3fa5303ef9a89e8225d761fd26eb28ff00790b4a1812528d6fc63f8fd33
MD5 c4e89dc65e241b782d6baa7974d1039e
BLAKE2b-256 2247d0acfb389438bde3ae1d416b7a07e2e8aae0934118561582cbc5161e73c7

See more details on using hashes here.

File details

Details for the file xen_bridge-0.2-py3-none-any.whl.

File metadata

  • Download URL: xen_bridge-0.2-py3-none-any.whl
  • Upload date:
  • Size: 101.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for xen_bridge-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 37ea4b952f8e9447224d3143e5b1a5d1a9d1dc4aceea73ebb704635ea2f07587
MD5 2b86eb61655ab4ea82590be937666070
BLAKE2b-256 43c7f49d1f5681bf80c09ccacb1d5593bf6b154b054e12b9111525581ea6c286

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page