Search in sources :

Example 1 with Template

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

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

the class AddVmWithSysprep 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();
    // Find the service that manages the collection of virtual machines:
    VmsService vmsService = connection.systemService().vmsService();
    // Create the virtual machine. Note that no Sysprep stuff is needed here, when creating it, it will be used
    // later, when starting it:
    Vm vm = vmsService.add().vm(vm().name("myvm").cluster(cluster().name("mycluster")).template(template().name("mytemplate"))).send().vm();
    // Find the service that manages the virtual machine:
    VmService vmService = vmsService.vmService(vm.id());
    // Wait till the virtual machine is down, which indicates that all the disks have been created:
    while (vm.status() != VmStatus.DOWN) {
        Thread.sleep(5 * 1000);
        vm = vmService.get().send().vm();
    }
    // The content of the Unattend.xml file. Note that this is an incomplete file, make sure to use a complete one,
    // maybe reading it from an external file:
    String unattendXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<unattend xmlns=\"urn:schemas-microsoft-com:unattend\">\n" + "  ...\n" + "</unattend>\n";
    // Start the virtual machine enabling Sysprep. Make sure to use a Windows operating system, either in the
    // template, or overriding it explicitly here. Without that the Sysprep logic won't be triggered.
    vmService.start().useSysprep(true).vm(vm().os(operatingSystem().type("windows_7x64"))).send();
    // Close the connection to the server:
    connection.close();
}
Also used : 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 3 with Template

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

the class FollowVmLinks 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 service that manages virtual machines:
    VmsService vmsService = connection.systemService().vmsService();
    // Find the virtual machine:
    Vm vm = vmsService.list().search("name=myvm").send().vms().get(0);
    // When the server returns a virtual machine it will return links to related objects, like the cluster,
    // template and permissions something like this:
    // 
    // <link href="/api/vms/123/permissions" rel="permissions"/>
    // ...
    // <cluster id="123" href="/api/clusters/123"/>
    // <template id="456" href="/api/templates/456"/>
    // 
    // The SDK provides a "followLink" method that can be used to retrieve the complete content of these related
    // objects.
    Cluster cluster = connection.followLink(vm.cluster());
    Template template = connection.followLink(vm.template());
    List<Permission> permissions = connection.followLink(vm.permissions());
    // Now we can use the details of the cluster, template and permissions:
    System.out.printf("cluster: %s\n", cluster.name());
    System.out.printf("template: %s\n", template.name());
    for (Permission permission : permissions) {
        System.out.printf("role: %s\n", permission.role().id());
    }
    // Close the connection to the server:
    connection.close();
}
Also used : Vm(org.ovirt.engine.sdk4.types.Vm) Connection(org.ovirt.engine.sdk4.Connection) Permission(org.ovirt.engine.sdk4.types.Permission) Cluster(org.ovirt.engine.sdk4.types.Cluster) VmsService(org.ovirt.engine.sdk4.services.VmsService) Template(org.ovirt.engine.sdk4.types.Template)

Example 4 with Template

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

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