Search in sources :

Example 61 with Connection

use of org.openmuc.j60870.Connection 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 62 with Connection

use of org.openmuc.j60870.Connection in project ovirt-engine-sdk-java by oVirt.

the class CloneVmFromSnapshot 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 service that manages the virtual machine:
    VmService vmService = vmsService.vmService(vm.id());
    // Find the snapshot. Note that the snapshots collection doesn't support search, so we need to retrieve the
    // complete list and the look for the snapshot that has the description that we are looking for.
    SnapshotsService snapsService = vmService.snapshotsService();
    List<Snapshot> snaps = snapsService.list().send().snapshots();
    Snapshot snap = null;
    for (Snapshot s : snaps) {
        if (Objects.equals(s.description(), "mysnap")) {
            snap = s;
            break;
        }
    }
    // Create a new virtual machine, cloning it from the snapshot:
    Vm clonedVm = vmsService.add().vm(vm().name("myclonedvm").snapshots(snapshot().id(snap.id())).cluster(cluster().name("mycluster"))).send().vm();
    // Find the service that manages the cloned virtual machine:
    VmService clonedVmService = vmsService.vmService(clonedVm.id());
    // Wait till the virtual machine is down, as that means that the creation of the disks has been completed:
    for (; ; ) {
        Thread.sleep(5 * 1000);
        clonedVm = clonedVmService.get().send().vm();
        if (clonedVm.status() == VmStatus.DOWN) {
            break;
        }
    }
    // Close the connection to the server:
    connection.close();
}
Also used : Snapshot(org.ovirt.engine.sdk4.types.Snapshot) SystemService(org.ovirt.engine.sdk4.services.SystemService) SnapshotsService(org.ovirt.engine.sdk4.services.SnapshotsService) 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 63 with Connection

use of org.openmuc.j60870.Connection in project ovirt-engine-sdk-java by oVirt.

the class EnableSerialConsole 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 virtual machine. Note the use of the  `all_content` parameter, it is required in order to obtain
    // additional information that isn't retrieved by default, like the configuration of the serial console.
    VmsService vmsService = connection.systemService().vmsService();
    Vm vm = vmsService.list().search("name=myvm").allContent(true).send().vms().get(0);
    // Check if the serial console is enabled, and if it isn't then update the virtual machine to enable it:
    if (!vm.console().enabled()) {
        VmService vmService = vmsService.vmService(vm.id());
        vmService.update().vm(vm().console(console().enabled(true))).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 64 with Connection

use of org.openmuc.j60870.Connection in project ovirt-engine-sdk-java by oVirt.

the class SearchVms 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 "list" method of the "vms" service to search the virtual machines that match a search query:
    List<Vm> vms = vmsService.list().search("name=MYVM").caseSensitive(false).send().vms();
    // Print the virtual machine names and identifiers:
    for (Vm vm : vms) {
        System.out.printf("%s: %s", vm.name(), vm.id());
    }
    // Close the connection to the server:
    connection.close();
}
Also used : Vm(org.ovirt.engine.sdk4.types.Vm) Connection(org.ovirt.engine.sdk4.Connection) VmsService(org.ovirt.engine.sdk4.services.VmsService)

Example 65 with Connection

use of org.openmuc.j60870.Connection in project ovirt-engine-sdk-java by oVirt.

the class RemoveVm 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 VMs:
    VmsService vmsService = connection.systemService().vmsService();
    // Find the VM:
    Vm vm = vmsService.list().search("name=myvm").send().vms().get(0);
    // Note that the "vm" variable that we assigned above contains only the data of the VM, it doesn't have any
    // method like "remove". Methods are defined in the services. So now that we have the description of the VM
    // we can find the service that manages it, calling the locator method "vmService" defined in the "vms"
    // service. This locator method receives as parameter the identifier of the VM and returns a reference to the
    // service that manages that VM.
    VmService vmService = vmsService.vmService(vm.id());
    // Now that we have the reference to the service that manages the VM we can use it to remove the VM. Note that
    // this method doesn't need any parameter, as the identifier of the VM is already known by the service that we
    // located in the previous step.
    vmService.remove().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)

Aggregations

Connection (org.ovirt.engine.sdk4.Connection)64 Connection (com.trilead.ssh2.Connection)55 IOException (java.io.IOException)43 Session (com.trilead.ssh2.Session)32 VmsService (org.ovirt.engine.sdk4.services.VmsService)30 Vm (org.ovirt.engine.sdk4.types.Vm)30 InputStream (java.io.InputStream)25 VmService (org.ovirt.engine.sdk4.services.VmService)18 Connection (okhttp3.Connection)15 Connection (ch.ethz.ssh2.Connection)13 Request (okhttp3.Request)13 SystemService (org.ovirt.engine.sdk4.services.SystemService)13 Response (okhttp3.Response)12 StorageDomainsService (org.ovirt.engine.sdk4.services.StorageDomainsService)12 StorageDomain (org.ovirt.engine.sdk4.types.StorageDomain)12 MediaType (okhttp3.MediaType)11 ResponseBody (okhttp3.ResponseBody)11 RequestBody (okhttp3.RequestBody)10 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 Charset (java.nio.charset.Charset)9