Files
xen-orchestra/packages/xo-lib
2015-02-23 12:25:17 +01:00
..
2014-07-26 10:15:29 +02:00
2014-07-26 10:15:29 +02:00
2015-02-05 11:55:00 +01:00
2015-02-05 13:51:50 +01:00
2015-02-20 17:14:44 +01:00
2015-02-23 12:10:45 +01:00
2015-02-20 17:14:44 +01:00

xo-lib

Build Status Dependency Status devDependency Status

Library to connect to XO-Server.

Installation

Node & Browserify

Installation of the npm package:

npm install --save xo-lib

Then require the package:

var xoLib = require('xo-lib');

High level API

This high-level interface handles session sign-in and a cache of remote XO objects. It also automatically reconnect and retry method calls when necessary.

var xo = new xoLib.Xo({
  url: 'https://xo.company.tld',
  auth: {
    email: 'admin@admin.net',
    password: 'admin',
  },
});

If the URL is not provided and the current environment is a web browser, the location of the current page will be used.

Method call

xo.call('token.create').then(function (token) {
  console.log('Token created', token);
});

Status

The connection status is available through the status property which is disconnected, connecting or connected.

console.log('%s to xo-server', xo.status);

Current user

Information about the user account used to sign in is available through the user property.

console.log('Current user is', xo.user);

This property is null when the status is not connected.

XO Objects

XO objects are cached locally in the objects collection.

// Read-only dictionary of all objects.
var allObjects = xo.objects.all;

// Looks up a given object by its identifier.
var object = allObjects[id];

// Read-only dictionary of all indexes.
var indexes = xo.objects.indexes;

// Read-only dictionary of types.
var byTypes = indexes.type;

// Read-only view of all VMs.
var vms = byTypes.VM;

Available indexes are: ref, type and UUID.

Low level

var api = new xoLib.Api('https://xo.company.tld');

If the URL is not provided and the current environment is a web browser, the location of the current page will be used.

Connection

api.connect().then(function () {
  console.log('connected');
});

Disconnection

api.close();

Method call

api.call('session.signInWithPassword', {
  email: 'admin@admin.net',
  password: 'admin',
}).then(function (user) {
  console.log('Connected as', user);
});

A method call automatically trigger a connection if necessary.

Events

api.on('connected', function () {
  console.log('connected');
});
api.on('disconnected', function () {
  console.log('disconnected');
});
api.on('notification', function (notif) {
  console.log('notification:', notif.method, notif.params);
});

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Vates SAS