Search in sources :

Example 26 with Service

use of org.ovirt.engine.sdk4.Service 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 27 with Service

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

the class AddVmNic 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();
    // Locate the virtual machines service and use it to find the virtual machine:
    VmsService vmsService = connection.systemService().vmsService();
    Vm vm = vmsService.list().search("name=myvm").send().vms().get(0);
    // In order to specify the network that the new interface will be connected to, we need to specify the
    // identifier of the virtual network interface profile, so we need to find it:
    VnicProfilesService profilesService = connection.systemService().vnicProfilesService();
    String profileId = null;
    List<VnicProfile> profiles = profilesService.list().send().profiles();
    for (VnicProfile profile : profiles) {
        if (Objects.equals(profile.name(), "mynetwork")) {
            profileId = profile.id();
            break;
        }
    }
    // Locate the service that manages the NICs of the virtual machine:
    VmNicsService nicsService = vmsService.vmService(vm.id()).nicsService();
    // Use the "add" method of the disks service to add the disk:
    nicsService.add().nic(nic().name("mynic").description("My network interface card").vnicProfile(vnicProfile().id(profileId))).send();
    // Close the connection to the server:
    connection.close();
}
Also used : VnicProfilesService(org.ovirt.engine.sdk4.services.VnicProfilesService) Vm(org.ovirt.engine.sdk4.types.Vm) Connection(org.ovirt.engine.sdk4.Connection) VnicProfile(org.ovirt.engine.sdk4.types.VnicProfile) VmsService(org.ovirt.engine.sdk4.services.VmsService) VmNicsService(org.ovirt.engine.sdk4.services.VmNicsService)

Example 28 with Service

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

the class AddVmSnapshot 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();
    // Locate the virtual machines service and use it to find the virtual machine:
    VmsService vmsService = connection.systemService().vmsService();
    Vm vm = vmsService.list().search("name=myvm").send().vms().get(0);
    // Locate the service that manages the snapshots of the virtual machine:
    SnapshotsService snapshotsService = vmsService.vmService(vm.id()).snapshotsService();
    // Add the snapshot:
    snapshotsService.add().snapshot(snapshot().description("My snapshot")).send();
    // Close the connection to the server:
    connection.close();
}
Also used : SnapshotsService(org.ovirt.engine.sdk4.services.SnapshotsService) Vm(org.ovirt.engine.sdk4.types.Vm) Connection(org.ovirt.engine.sdk4.Connection) VmsService(org.ovirt.engine.sdk4.services.VmsService)

Example 29 with Service

use of org.ovirt.engine.sdk4.Service 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 30 with Service

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

the class AttachNfsDataStorageDomain 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();
    // Locate the service that manages the storage domains, and use it to search for the storage domain:
    StorageDomainsService sdsService = connection.systemService().storageDomainsService();
    StorageDomain sd = sdsService.list().search("name=mydata").send().storageDomains().get(0);
    // Locate the service that manages the data centers and use it to search for the data center:
    DataCentersService dcsService = connection.systemService().dataCentersService();
    DataCenter dc = dcsService.list().search("name=mydc").send().dataCenters().get(0);
    // Locate the service that manages the data center where we want to attach the storage domain:
    DataCenterService dcService = dcsService.dataCenterService(dc.id());
    // Locate the service that manages the storage domains that are attached to the data center:
    AttachedStorageDomainsService attachedSdsService = dcService.storageDomainsService();
    // Use the "add" method of the service that manages the attached storage domains to attach it:
    attachedSdsService.add().storageDomain(storageDomain().id(sd.id())).send();
    // Wait till the storage domain is active:
    AttachedStorageDomainService attachedSdService = attachedSdsService.storageDomainService(sd.id());
    for (; ; ) {
        Thread.sleep(5 * 1000);
        sd = attachedSdService.get().send().storageDomain();
        if (sd.status() == StorageDomainStatus.ACTIVE) {
            break;
        }
    }
    // Close the connection to the server:
    connection.close();
}
Also used : AttachedStorageDomainsService(org.ovirt.engine.sdk4.services.AttachedStorageDomainsService) StorageDomainsService(org.ovirt.engine.sdk4.services.StorageDomainsService) StorageDomain(org.ovirt.engine.sdk4.types.StorageDomain) DataCentersService(org.ovirt.engine.sdk4.services.DataCentersService) DataCenter(org.ovirt.engine.sdk4.types.DataCenter) AttachedStorageDomainService(org.ovirt.engine.sdk4.services.AttachedStorageDomainService) Connection(org.ovirt.engine.sdk4.Connection) DataCenterService(org.ovirt.engine.sdk4.services.DataCenterService) AttachedStorageDomainsService(org.ovirt.engine.sdk4.services.AttachedStorageDomainsService)

Aggregations

Connection (org.ovirt.engine.sdk4.Connection)55 Vm (org.ovirt.engine.sdk4.types.Vm)26 VmsService (org.ovirt.engine.sdk4.services.VmsService)25 Connector (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Connector)22 Service (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Service)22 CoreException (org.eclipse.core.runtime.CoreException)18 VmService (org.ovirt.engine.sdk4.services.VmService)15 ArrayList (java.util.ArrayList)12 StorageDomain (org.ovirt.engine.sdk4.types.StorageDomain)12 StorageDomainsService (org.ovirt.engine.sdk4.services.StorageDomainsService)11 SystemService (org.ovirt.engine.sdk4.services.SystemService)10 ServerPort (org.eclipse.wst.server.core.ServerPort)9 StorageDomainService (org.ovirt.engine.sdk4.services.StorageDomainService)7 HostService (org.ovirt.engine.sdk4.services.HostService)6 HostsService (org.ovirt.engine.sdk4.services.HostsService)6 Disk (org.ovirt.engine.sdk4.types.Disk)6 Test (org.junit.Test)5 DataCenterService (org.ovirt.engine.sdk4.services.DataCenterService)5 DataCentersService (org.ovirt.engine.sdk4.services.DataCentersService)5 Cluster (org.ovirt.engine.sdk4.types.Cluster)5