Search in sources :

Example 11 with SystemService

use of org.ovirt.engine.sdk4.services.SystemService in project ovirt-engine-sdk-java by oVirt.

the class ImportGlanceImage 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 root of the services tree:
    SystemService systemService = connection.systemService();
    // Find the Glance storage domain that is available for default in any oVirt installation:
    StorageDomainsService sdsService = systemService.storageDomainsService();
    StorageDomain sd = sdsService.list().search("name=ovirt-image-repository").send().storageDomains().get(0);
    // Find the servie that manages the Glance storage domain:
    StorageDomainService sdService = sdsService.storageDomainService(sd.id());
    // Find the service that manages the images available in that storage domain:
    ImagesService imagesService = sdService.imagesService();
    // The images service doesn't support search, so in roder to find an image we need to retrieve all of them and
    // do the filtering explicitly:
    List<Image> images = imagesService.list().send().images();
    Image image = null;
    for (Image i : images) {
        if (Objects.equals(i.name(), "CirrOS 0.3.4 for x86_64")) {
            image = i;
            break;
        }
    }
    // Find the service that manages the image that we found in the previous step:
    ImageService imageService = imagesService.imageService(image.id());
    // Import the image:
    imageService.import_().importAsTemplate(true).template(template().name("mytemplate")).cluster(cluster().name("mycluster")).storageDomain(storageDomain().name("mydata")).send();
    // Close the connection to the server:
    connection.close();
}
Also used : StorageDomainsService(org.ovirt.engine.sdk4.services.StorageDomainsService) StorageDomainService(org.ovirt.engine.sdk4.services.StorageDomainService) StorageDomain(org.ovirt.engine.sdk4.types.StorageDomain) SystemService(org.ovirt.engine.sdk4.services.SystemService) Connection(org.ovirt.engine.sdk4.Connection) ImagesService(org.ovirt.engine.sdk4.services.ImagesService) Image(org.ovirt.engine.sdk4.types.Image) ImageService(org.ovirt.engine.sdk4.services.ImageService)

Example 12 with SystemService

use of org.ovirt.engine.sdk4.services.SystemService in project ovirt-engine-sdk-java by oVirt.

the class PinVm 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 tree of services:
    SystemService systemService = connection.systemService();
    // Find the virtual machine:
    VmsService vmsService = systemService.vmsService();
    Vm vm = vmsService.list().search("name=myvm").send().vms().get(0);
    // Update the placement policy of the virtual machine so that it is pinned to the host:
    VmService vmService = vmsService.vmService(vm.id());
    vmService.update().vm(vm().placementPolicy(vmPlacementPolicy().hosts(host().name("myhost")))).send();
    // Close the connection to the server:
    connection.close();
}
Also used : SystemService(org.ovirt.engine.sdk4.services.SystemService) Vm(org.ovirt.engine.sdk4.types.Vm) VmService(org.ovirt.engine.sdk4.services.VmService) Connection(org.ovirt.engine.sdk4.Connection) VmsService(org.ovirt.engine.sdk4.services.VmsService)

Example 13 with SystemService

use of org.ovirt.engine.sdk4.services.SystemService in project ovirt-engine-sdk-java by oVirt.

the class SetVmLeaseStorageDomain 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 tree of services:
    SystemService systemService = connection.systemService();
    // Find the virtual machine:
    VmsService vmsService = systemService.vmsService();
    Vm vm = vmsService.list().search("name=myvm").send().vms().get(0);
    // Find the storage domain:
    StorageDomainsService sdsService = systemService.storageDomainsService();
    StorageDomain sd = sdsService.list().search("name=mydata").send().storageDomains().get(0);
    // Update the virtual machine so that high availability is enabled and the lease is created in the selected
    // storage domain:
    VmService vmService = vmsService.vmService(vm.id());
    vmService.update().vm(vm().highAvailability(highAvailability().enabled(true)).lease(storageDomainLease().storageDomain(storageDomain().id(sd.id())))).send();
    // Close the connection to the server:
    connection.close();
}
Also used : StorageDomainsService(org.ovirt.engine.sdk4.services.StorageDomainsService) StorageDomain(org.ovirt.engine.sdk4.types.StorageDomain) SystemService(org.ovirt.engine.sdk4.services.SystemService) Vm(org.ovirt.engine.sdk4.types.Vm) VmService(org.ovirt.engine.sdk4.services.VmService) Connection(org.ovirt.engine.sdk4.Connection) VmsService(org.ovirt.engine.sdk4.services.VmsService)

Example 14 with SystemService

use of org.ovirt.engine.sdk4.services.SystemService 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();
}
Also used : SnapshotService(org.ovirt.engine.sdk4.services.SnapshotService) SnapshotsService(org.ovirt.engine.sdk4.services.SnapshotsService) HashMap(java.util.HashMap) VmService(org.ovirt.engine.sdk4.services.VmService) Connection(org.ovirt.engine.sdk4.Connection) VmsService(org.ovirt.engine.sdk4.services.VmsService) StorageDomainsService(org.ovirt.engine.sdk4.services.StorageDomainsService) Snapshot(org.ovirt.engine.sdk4.types.Snapshot) SnapshotDisksService(org.ovirt.engine.sdk4.services.SnapshotDisksService) StorageDomain(org.ovirt.engine.sdk4.types.StorageDomain) SystemService(org.ovirt.engine.sdk4.services.SystemService) Vm(org.ovirt.engine.sdk4.types.Vm) HashMap(java.util.HashMap) Map(java.util.Map) Disk(org.ovirt.engine.sdk4.types.Disk)

Example 15 with SystemService

use of org.ovirt.engine.sdk4.services.SystemService 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();
}
Also used : SystemService(org.ovirt.engine.sdk4.services.SystemService) DiskAttachment(org.ovirt.engine.sdk4.types.DiskAttachment) Vm(org.ovirt.engine.sdk4.types.Vm) Connection(org.ovirt.engine.sdk4.Connection) ArrayList(java.util.ArrayList) VmsService(org.ovirt.engine.sdk4.services.VmsService) TemplateService(org.ovirt.engine.sdk4.services.TemplateService) TemplatesService(org.ovirt.engine.sdk4.services.TemplatesService) Template(org.ovirt.engine.sdk4.types.Template)

Aggregations

SystemService (org.ovirt.engine.sdk4.services.SystemService)14 Connection (org.ovirt.engine.sdk4.Connection)13 VmsService (org.ovirt.engine.sdk4.services.VmsService)9 Vm (org.ovirt.engine.sdk4.types.Vm)8 VmService (org.ovirt.engine.sdk4.services.VmService)7 StorageDomainsService (org.ovirt.engine.sdk4.services.StorageDomainsService)6 StorageDomain (org.ovirt.engine.sdk4.types.StorageDomain)6 SnapshotsService (org.ovirt.engine.sdk4.services.SnapshotsService)3 StorageDomainService (org.ovirt.engine.sdk4.services.StorageDomainService)3 Disk (org.ovirt.engine.sdk4.types.Disk)3 DiskAttachment (org.ovirt.engine.sdk4.types.DiskAttachment)3 Snapshot (org.ovirt.engine.sdk4.types.Snapshot)3 ArrayList (java.util.ArrayList)2 ImagesService (org.ovirt.engine.sdk4.services.ImagesService)2 SnapshotDisksService (org.ovirt.engine.sdk4.services.SnapshotDisksService)2 SnapshotService (org.ovirt.engine.sdk4.services.SnapshotService)2 TemplateService (org.ovirt.engine.sdk4.services.TemplateService)2 TemplatesService (org.ovirt.engine.sdk4.services.TemplatesService)2 Image (org.ovirt.engine.sdk4.types.Image)2 Template (org.ovirt.engine.sdk4.types.Template)2