Search in sources :

Example 6 with Template

use of org.ovirt.engine.sdk4.types.Template 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)

Example 7 with Template

use of org.ovirt.engine.sdk4.types.Template in project ovirt-engine-sdk-java by oVirt.

the class AddVm 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 "add" method to create a new virtual machine:
    vmsService.add().vm(vm().name("myvm").cluster(cluster().name("mycluster")).template(template().name("Blank"))).send();
    // Close the connection to the server:
    connection.close();
}
Also used : Connection(org.ovirt.engine.sdk4.Connection) VmsService(org.ovirt.engine.sdk4.services.VmsService)

Aggregations

Connection (org.ovirt.engine.sdk4.Connection)7 VmsService (org.ovirt.engine.sdk4.services.VmsService)6 SystemService (org.ovirt.engine.sdk4.services.SystemService)3 Template (org.ovirt.engine.sdk4.types.Template)3 Vm (org.ovirt.engine.sdk4.types.Vm)3 StorageDomainsService (org.ovirt.engine.sdk4.services.StorageDomainsService)2 TemplateService (org.ovirt.engine.sdk4.services.TemplateService)2 TemplatesService (org.ovirt.engine.sdk4.services.TemplatesService)2 DiskAttachment (org.ovirt.engine.sdk4.types.DiskAttachment)2 StorageDomain (org.ovirt.engine.sdk4.types.StorageDomain)2 ArrayList (java.util.ArrayList)1 ImageService (org.ovirt.engine.sdk4.services.ImageService)1 ImagesService (org.ovirt.engine.sdk4.services.ImagesService)1 StorageDomainService (org.ovirt.engine.sdk4.services.StorageDomainService)1 VmService (org.ovirt.engine.sdk4.services.VmService)1 Cluster (org.ovirt.engine.sdk4.types.Cluster)1 Disk (org.ovirt.engine.sdk4.types.Disk)1 Image (org.ovirt.engine.sdk4.types.Image)1 Permission (org.ovirt.engine.sdk4.types.Permission)1