XenServer 5.5 API Cheat Sheet
The code snippets below does not include error handling and asynchronous API calls.
Logging into Xenserver
Connection connection =
new Connection(new
URL(" + xenserverHostname));
Session.loginWithPassword( connection,
xenserverUsername, xenserverPassword,
xenserverApiversion);
//
default is null
try
{
// xenserver api goes here
}
finally
{
Session.logout(connection);
}
Identify the machine to clone
// namelabelToClone = "CentOS_5.3"
// find a halted real virtual machine that match the specified name label.
Set<VM> refVMs = VM.getByNameLabel(connection, namelabelToClone);
if
(refVMs.size() == 1) {
VM vm = refVMs.iterator().next();
VM.Record record = vm.getRecord(connection);
if
(!record.isATemplate &&
!record.isControlDomain &&
record.powerState == Types.VmPowerState.HALTED) {
return
vm;
}
}
Clone a new machine
// clone machine by name lable
VM newVM = vm.createClone(getConnection(), namelabel);
newVM.setNameDescription(
getConnection(),
"Cloned " + namelabelToClone + " at " +
new
Date().toString());