HP BL680c XenServer Software Development Kit Guide 4.1.0 - Page 21

Finding references to useful objects, 4.1.4. Invoking synchronous operations on objects

Page 21 highlights

Using the API • Remember to log out of active sessions to avoid leaking them; and • Be prepared to log in again to the server if a SESSION_INVALID error is caught. In the following fragment a connection via the Unix domain socket is established and a session created: import XenAPI session = XenAPI.xapi_local() try: session.xenapi.login_with_password("root", "") ... finally: session.xenapi.session.logout() 4.1.3. Finding references to useful objects Once an application has authenticated the next step is to acquire references to objects in order to query their state or invoke operations on them. All objects have a set of "implicit" messages which include the following: • get_by_name_label: return a list of all objects of a particular class with a particular label; • get_by_uuid: return a single object named by its UUID; • get_all: return a set of references to all objects of a particular class; and • get_all_records: return a map of reference to records for each object of a particular class. For example, to list all hosts: hosts = session.xenapi.host.get_all() To find all VMs with the name "my first VM": vms = session.xenapi.VM.get_by_name_label('my first VM') Note Object name_label fields are not guaranteed to be unique and so the get_by_name_label API call returns a set of references rather than a single reference. In addition to the methods of finding objects described above, most objects also contain references to other objects within fields. For example it is possible to find the set of VMs running on a particular host by calling: vms = session.xenapi.host.get_resident_VMs(host) 4.1.4. Invoking synchronous operations on objects Once object references have been acquired, operations may be invoked on them. For example to start a VM: 15

  • 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

Using the API
15
Remember to log out of active sessions to avoid leaking them; and
Be prepared to log in again to the server if a
SESSION_INVALID
error is caught.
In the following fragment a connection via the Unix domain socket is established and a session created:
import XenAPI
session = XenAPI.xapi_local()
try:
session.xenapi.login_with_password("root", "")
...
finally:
session.xenapi.session.logout()
4.1.3. Finding references to useful objects
Once an application has authenticated the next step is to acquire references to objects in order to query their
state or invoke operations on them. All objects have a set of "implicit" messages which include the following:
get_by_name_label
: return a list of all objects of a particular class with a particular label;
get_by_uuid
: return a single object named by its UUID;
get_all
: return a set of references to all objects of a particular class; and
get_all_records
: return a map of reference to records for each object of a particular class.
For example, to list all hosts:
hosts = session.xenapi.host.get_all()
To find all VMs with the name "my first VM":
vms = session.xenapi.VM.get_by_name_label('my first VM')
Note
Object
name_label
fields are not guaranteed to be unique and so the
get_by_name_label
API
call returns a set of references rather than a single reference.
In addition to the methods of finding objects described above, most objects also contain references to other
objects within fields. For example it is possible to find the set of VMs running on a particular host by calling:
vms = session.xenapi.host.get_resident_VMs(host)
4.1.4. Invoking synchronous operations on objects
Once object references have been acquired, operations may be invoked on them. For example to start a VM: