use of org.ovirt.engine.sdk4.types.Vm in project ovirt-engine-sdk-java by oVirt.
the class StopVm method main.
public static void main(String[] args) throws Exception {
// Create the connection to the server:
Connection connection = connection().url("https://engine40.example.com/ovirt-engine/api").user("admin@internal").password("redhat123").trustStoreFile("truststore.jks").build();
// Get the reference to the "vms" service:
VmsService vmsService = connection.systemService().vmsService();
// Find the virtual machine:
Vm vm = vmsService.list().search("name=myvm").send().vms().get(0);
// Locate the service that manages the virtual machine, as that is where the action methods are defined:
VmService vmService = vmsService.vmService(vm.id());
// Call the "stop" method of the service to stop it:
vmService.stop().send();
// What till the virtual machine is down:
for (; ; ) {
Thread.sleep(5 * 1000);
vm = vmService.get().send().vm();
if (vm.status() == VmStatus.DOWN) {
break;
}
}
// Close the connection to the server:
connection.close();
}
use of org.ovirt.engine.sdk4.types.Vm in project ovirt-engine-sdk-java by oVirt.
the class ListVmSnapshots method main.
public static void main(String[] args) throws Exception {
// Create the connection to the server:
Connection connection = connection().url("https://engine40.example.com/ovirt-engine/api").user("admin@internal").password("redhat123").trustStoreFile("truststore.jks").build();
// Get the reference to the root service:
SystemService systemService = connection.systemService();
// Find all the virtual machines and store the id and name in a
// map, so that looking them up later will be faster:
VmsService vmsService = systemService.vmsService();
Map<String, String> vmsMap = new HashMap<>();
for (Vm vm : vmsService.list().send().vms()) {
vmsMap.put(vm.id(), vm.name());
}
// Same for storage domains:
StorageDomainsService storageDomainsService = systemService.storageDomainsService();
Map<String, String> storageDomainsMap = new HashMap<>();
for (StorageDomain sd : storageDomainsService.list().send().storageDomains()) {
storageDomainsMap.put(sd.id(), sd.name());
}
// find its disks:
for (Map.Entry<String, String> vm : vmsMap.entrySet()) {
VmService vmService = vmsService.vmService(vm.getKey());
SnapshotsService snapshotsService = vmService.snapshotsService();
Map<String, String> snapshotsMap = new HashMap<>();
for (Snapshot snapshot : snapshotsService.list().send().snapshots()) {
snapshotsMap.put(snapshot.id(), snapshot.description());
}
for (Map.Entry<String, String> snapshot : snapshotsMap.entrySet()) {
SnapshotService snapshotService = snapshotsService.snapshotService(snapshot.getKey());
SnapshotDisksService snapshotDisksService = snapshotService.disksService();
for (Disk disk : snapshotDisksService.list().send().disks()) {
if (disk.storageDomains().size() > 0) {
String storageDomainId = disk.storageDomains().get(0).id();
String storageDomainName = storageDomainsMap.get(storageDomainId);
System.out.printf("%s:%s:%s:%s\n", vm.getValue(), snapshot.getValue(), disk.alias(), storageDomainName);
}
}
}
}
// Close the connection to the server:
connection.close();
}
use of org.ovirt.engine.sdk4.types.Vm in project ovirt-engine-sdk-java by oVirt.
the class ListVmTags method main.
public static void main(String[] args) throws Exception {
// Create the connection to the server:
Connection connection = connection().url("https://engine40.example.com/ovirt-engine/api").user("admin@internal").password("redhat123").trustStoreFile("truststore.jks").build();
// Get the reference to the "vms" service:
VmsService vmsService = connection.systemService().vmsService();
// Find the virtual machine:
Vm vm = vmsService.list().search("name=myvm0").send().vms().get(0);
// Find the service that manages the vm:
VmService vmService = vmsService.vmService(vm.id());
// Locate the service that manages the tags of the vm:
AssignedTagsService tagsService = vmService.tagsService();
// For each tag print its name and description:
for (Tag tag : tagsService.list().send().tags()) {
System.out.printf("%s: %s\n", tag.name(), tag.description());
}
// Close the connection to the server:
connection.close();
}
use of org.ovirt.engine.sdk4.types.Vm in project ovirt-engine-sdk-java by oVirt.
the class AddIndependentVm method main.
public static void main(String[] args) throws Exception {
// Create the connection to the server:
Connection connection = connection().url("https://engine40.example.com/ovirt-engine/api").user("admin@internal").password("redhat123").trustStoreFile("truststore.jks").build();
// Get the reference to the "vms" service:
VmsService vmsService = connection.systemService().vmsService();
// Use the "clone" parameter of the "add" method to request that the
// disks of the new virtual machine are independent of the template.
vmsService.add().vm(vm().name("myvm").cluster(cluster().name("mycluster")).template(template().name("mytemplate"))).clone_(true).send();
// Close the connection to the server:
connection.close();
}
use of org.ovirt.engine.sdk4.types.Vm in project ovirt-engine-sdk-java by oVirt.
the class AddTemplate method main.
public static void main(String[] args) throws Exception {
// Create the connection to the server:
Connection connection = connection().url("https://engine40.example.com/ovirt-engine/api").user("admin@internal").password("redhat123").trustStoreFile("truststore.jks").build();
// Get the reference to the root of the services tree:
SystemService systemService = connection.systemService();
// Find the original virtual machine:
VmsService vmsService = systemService.vmsService();
Vm vm = vmsService.list().search("name=myvm").send().vms().get(0);
// Get the identifiers of the disks attached to the virtual machine. We # need this because we want to tell the
// server to create the disks of the template using a format different to the format used by the original disks.
List<String> diskIds = new ArrayList<>();
for (DiskAttachment attachment : connection.followLink(vm.diskAttachments())) {
diskIds.add(attachment.disk().id());
}
// Create a customized list of disk attachments, explicitly indicating that we want COW disks, regardless of
// format the original disks had:
List<DiskAttachment> attachments = new ArrayList<>();
for (String diskId : diskIds) {
attachments.add(diskAttachment().disk(disk().id(diskId).format(DiskFormat.COW)).build());
}
// Send the request to create the template. Note that the way to specify the original virtual machine, and the
// customizations, is to use the 'vm' attribute of the 'Template' type.
TemplatesService templatesService = systemService.templatesService();
Template template = templatesService.add().template(template().name("mytemplate").vm(vm().id(vm.id()).diskAttachments(attachments))).send().template();
// Wait till the status of the template is OK, as that means that it is completely created and ready to use:
TemplateService templateService = templatesService.templateService(template.id());
for (; ; ) {
Thread.sleep(5 * 1000);
template = templateService.get().send().template();
if (template.status() == TemplateStatus.OK) {
break;
}
}
// Close the connection to the server:
connection.close();
}
Aggregations