use of org.ovirt.engine.sdk4.services.VmService in project ovirt-engine-sdk-java by oVirt.
the class ListVmDisks 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();
// Find the virtual machine:
Vm vm = vmsService.list().search("name=myvm").send().vms().get(0);
// Locate the service that manages the virtual machine:
VmService vmService = vmsService.vmService(vm.id());
// Locate the service that manages the disk attachments of the virtual machine:
DiskAttachmentsService diskAttachmentsService = vmService.diskAttachmentsService();
// Retrieve the list of disks attachments, and print the disk details. Note that each attachment contains a link
// to the corresponding disk, but not the actual disk data. In order to retrieve the actual disk data we use the
// `follow_link` method.
List<DiskAttachment> diskAttachments = diskAttachmentsService.list().send().attachments();
for (DiskAttachment diskAttachment : diskAttachments) {
Disk disk = connection.followLink(diskAttachment.disk());
System.out.printf("name: %s\n", disk.name());
System.out.printf("id: %s\n", disk.id());
System.out.printf("status: %s\n", disk.status());
System.out.printf("provisioned_size: %s\n", disk.provisionedSize());
}
// Close the connection to the server:
connection.close();
}
use of org.ovirt.engine.sdk4.services.VmService in project ovirt-engine-sdk-java by oVirt.
the class VmsServiceTest method testNullObjectForNonExistingID.
/**
* Test we don get null VM object for non-existing VM id.
*/
@Test
public void testNullObjectForNonExistingID() {
boolean raised = false;
VmService vmService = vmsService.vmService("456");
try {
vmService.get().send().vm();
} catch (Error e) {
assertTrue(e.getMessage().contains("404"));
raised = true;
}
assertTrue(raised);
}
use of org.ovirt.engine.sdk4.services.VmService in project ovirt-engine-sdk-java by oVirt.
the class VmsServiceTest method testFaultReaderWithoutAction.
/**
* When the server returns an action containing a fault
* raises an error containing the information of the fault.
*/
@Test
public void testFaultReaderWithoutAction() {
boolean raised = false;
VmService vmService = vmsService.vmService("123");
try {
vmService.start().send();
} catch (Error e) {
assertTrue(e.getMessage().contains("myreason"));
raised = true;
}
assertTrue(raised);
}
use of org.ovirt.engine.sdk4.services.VmService 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();
}
use of org.ovirt.engine.sdk4.services.VmService in project ovirt-engine-sdk-java by oVirt.
the class StartVmWithCloudInit 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:
VmsService vmsService = connection.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());
// Create cloud-init script, which you want to execute in deployed virtual machine, please note that
// the script must be properly formatted and indented as it's using YAML.
String myScript = "write_files:\n" + " - content: |\n" + " Hello, world!\n" + " path: /tmp/greeting.txt\n" + " permissions: '0644'\n";
// Start the virtual machine enabling cloud-init and providing the password for the `root` user and the network
// configuration:
vmService.start().useCloudInit(true).vm(vm().initialization(initialization().userName("root").rootPassword("redhat123").hostName("myvm.example.com").nicConfigurations(nicConfiguration().name("eth0").onBoot(true).bootProtocol(BootProtocol.STATIC).ip(ip().version(IpVersion.V4).address("192.168.0.100").netmask("255.255.255.0").gateway("192.168.0.1"))).dnsServers("192.168.0.1 192.168.0.2 192.168.0.3").dnsSearch("example.com").customScript(myScript))).send();
// Close the connection to the server:
connection.close();
}
Aggregations