Search in sources :

Example 1 with TemplateService

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

the class AddVmFromTemplateVersion 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();
    // Get the reference to the service that manages the storage domains:
    StorageDomainsService storageDomainsService = systemService.storageDomainsService();
    // Find the storage domain we want to be used for virtual machine disks:
    StorageDomain storageDomain = storageDomainsService.list().search("name=mydata").send().storageDomains().get(0);
    // Get the reference to the service that manages the templates:
    TemplatesService templatesService = systemService.templatesService();
    // When a template has multiple versions they all have the same name, so we need to explicitly find the one that
    // has the version name or version number that we want to use. In this case we want to use version 3 of the
    // template.
    List<Template> templates = templatesService.list().search("name=mytemplate").send().templates();
    String templateId = null;
    for (Template template : templates) {
        if (template.version().versionNumber().equals(BigInteger.valueOf(3))) {
            templateId = template.id();
            break;
        }
    }
    // Find the template disk we want be created on specific storage domain
    // for our virtual machine:
    TemplateService templateService = templatesService.templateService(templateId);
    List<DiskAttachment> diskAttachments = connection.followLink(templateService.get().send().template().diskAttachments());
    Disk disk = diskAttachments.get(0).disk();
    // Get the reference to the service that manages the virtual machines:
    VmsService vmsService = connection.systemService().vmsService();
    // Add a new virtual machine explicitly indicating the identifier of the template version that we want to use:
    vmsService.add().vm(vm().name("myvm").cluster(cluster().name("mycluster")).template(template().id(templateId)).diskAttachments(diskAttachment().disk(disk().id(disk.id()).format(DiskFormat.COW).storageDomains(storageDomain().id(storageDomain.id()))))).send();
    // Close the connection to the server:
    connection.close();
}
Also used : Connection(org.ovirt.engine.sdk4.Connection) VmsService(org.ovirt.engine.sdk4.services.VmsService) TemplatesService(org.ovirt.engine.sdk4.services.TemplatesService) Template(org.ovirt.engine.sdk4.types.Template) StorageDomainsService(org.ovirt.engine.sdk4.services.StorageDomainsService) StorageDomain(org.ovirt.engine.sdk4.types.StorageDomain) SystemService(org.ovirt.engine.sdk4.services.SystemService) DiskAttachment(org.ovirt.engine.sdk4.types.DiskAttachment) TemplateService(org.ovirt.engine.sdk4.services.TemplateService) Disk(org.ovirt.engine.sdk4.types.Disk)

Example 2 with TemplateService

use of org.ovirt.engine.sdk4.services.TemplateService 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

Connection (org.ovirt.engine.sdk4.Connection)2 SystemService (org.ovirt.engine.sdk4.services.SystemService)2 TemplateService (org.ovirt.engine.sdk4.services.TemplateService)2 TemplatesService (org.ovirt.engine.sdk4.services.TemplatesService)2 VmsService (org.ovirt.engine.sdk4.services.VmsService)2 DiskAttachment (org.ovirt.engine.sdk4.types.DiskAttachment)2 Template (org.ovirt.engine.sdk4.types.Template)2 ArrayList (java.util.ArrayList)1 StorageDomainsService (org.ovirt.engine.sdk4.services.StorageDomainsService)1 Disk (org.ovirt.engine.sdk4.types.Disk)1 StorageDomain (org.ovirt.engine.sdk4.types.StorageDomain)1 Vm (org.ovirt.engine.sdk4.types.Vm)1