HP BL680c XenServer Software Development Kit Guide 4.1.0 - Page 20

Using the API, 4.1. Anatomy of a typical application, 4.1.1. Choosing a low-level

Page 20 highlights

Chapter 4. Using the API This chapter describes how to use the XenServer Management API from real programs to manage XenServer Hosts and VMs. The chapter begins with a walk-through of a typical client application and demonstrates how the API can be used to perform common tasks. Example code fragments are given in python syntax but equivalent code in C and C# would look very similar. The language bindings themselves are discussed afterwards and the chapter finishes with walk-throughs of two complete examples included in the SDK. 4.1. Anatomy of a typical application This section describes the structure of a typical application using the XenServer Management API. Most client applications begin by connecting to a XenServer Host and authenticating (e.g. with a username and password). Assuming the authentication succeeds, the server will create a "session" object and return a reference to the client. This reference will be passed as an argument to all future API calls. Once authenticated, the client may search for references to other useful objects (e.g. XenServer Hosts, VMs, etc.) and invoke operations on them. Operations may be invoked either synchronously or asynchronously; special task objects represent the state and progress of asynchronous operations. These application elements are all described in detail in the following sections. 4.1.1. Choosing a low-level transport API calls can be issued over two transports: • SSL-encrypted TCP on port 443 (https) over an IP network • plaintext over a local Unix domain socket: /var/xapi/xapi The SSL-encrypted TCP transport is used for all off-host traffic while the Unix domain socket can be used from services running directly on the XenServer Host itself. In the SSL-encrypted TCP transport, all API calls should be directed at the Resource Pool master; failure to do so will result in the error HOST_IS_SLAVE, which includes the IP address of the master as an error parameter. Note As a special-case, all messages sent through the Unix domain socket are transparently forwarded to the correct node. 4.1.2. Authentication and session handling The vast majority of API calls take a session reference as their first parameter; failure to supply a valid reference will result in a SESSION_INVALID error being returned. A session reference is acquired by supplying a username and password to the login_with_password function. Note As a special-case, if this call is executed over the local Unix domain socket then the username and password are ignored and the call always succeeds. Every session has an associated "last active" timestamp which is updated on every API call. The server software currently has a built-in limit of 200 active sessions and will remove those with the oldest "last active" field if this limit is exceeded. In addition all sessions whose "last active" field is older than 24 hours are also removed. Therefore it is important to: 14

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

14
Chapter 4. Using the API
This chapter describes how to use the XenServer Management API from real programs to manage XenServ-
er Hosts and VMs. The chapter begins with a walk-through of a typical client application and demonstrates
how the API can be used to perform common tasks. Example code fragments are given in python syntax
but equivalent code in C and C# would look very similar. The language bindings themselves are discussed
afterwards and the chapter finishes with walk-throughs of two complete examples included in the SDK.
4.1. Anatomy of a typical application
This section describes the structure of a typical application using the XenServer Management API. Most
client applications begin by connecting to a XenServer Host and authenticating (e.g. with a username and
password). Assuming the authentication succeeds, the server will create a "session" object and return a
reference to the client. This reference will be passed as an argument to all future API calls. Once authenti-
cated, the client may search for references to other useful objects (e.g. XenServer Hosts, VMs, etc.) and
invoke operations on them. Operations may be invoked either synchronously or asynchronously; special
task objects represent the state and progress of asynchronous operations. These application elements are
all described in detail in the following sections.
4.1.1. Choosing a low-level transport
API calls can be issued over two transports:
SSL-encrypted TCP on port 443 (https) over an IP network
plaintext over a local Unix domain socket:
/var/xapi/xapi
The SSL-encrypted TCP transport is used for all off-host traffic while the Unix domain socket can be used
from services running directly on the XenServer Host itself. In the SSL-encrypted TCP transport, all API calls
should be directed at the Resource Pool master; failure to do so will result in the error
HOST_IS_SLAVE
,
which includes the IP address of the master as an error parameter.
Note
As a special-case, all messages sent through the Unix domain socket are transparently forwarded
to the correct node.
4.1.2. Authentication and session handling
The vast majority of API calls take a session reference as their first parameter; failure to supply a valid
reference will result in a
SESSION_INVALID
error being returned. A session reference is acquired by sup-
plying a username and password to the
login_with_password
function.
Note
As a special-case, if this call is executed over the local Unix domain socket then the username and
password are ignored and the call always succeeds.
Every session has an associated "last active" timestamp which is updated on every API call. The server
software currently has a built-in limit of 200 active sessions and will remove those with the oldest "last active"
field if this limit is exceeded. In addition all sessions whose "last active" field is older than 24 hours are also
removed. Therefore it is important to: