use of org.ovirt.engine.sdk4.Service in project ovirt-engine-sdk-java by oVirt.
the class UpdateFencingOptions 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();
// The name and value of the option that we want to add or update:
String name = "lanplus";
String value = "1";
// Get the reference to the service that manages the hosts:
HostsService hostsService = connection.systemService().hostsService();
// Find the host:
Host host = hostsService.list().search("name=myhost").send().hosts().get(0);
// Get the reference to the service that manages the fencing agents used by the host that we found in the
// previous step:
HostService hostService = hostsService.hostService(host.id());
FenceAgentsService agentsService = hostService.fenceAgentsService();
// The host may have multiple fencing agents, so we need to locate the first of type 'ipmilan':
List<Agent> agents = agentsService.list().send().agents();
Agent agent = null;
for (Agent x : agents) {
if ("ipmlan".equals(x.type())) {
agent = x;
break;
}
}
// Get the options of the fencing agent. There may be no options, in that case we need to use an empty list.
List<Option> original = agent.options();
if (original == null) {
original = Collections.emptyList();
}
// Create a list of modified options, containing all the original options except the one with the name we want
// to modify, as we will add that with the right value later:
List<Option> modified = new ArrayList<>();
for (Option option : original) {
if (!name.equals(option.name())) {
modified.add(option);
}
}
// Add the modified option to the list of modified options:
Option option = option().name(name).value(value).build();
modified.add(option);
// Find the service that manages the fence agent:
FenceAgentService agentService = agentsService.agentService(agent.id());
// Send the update request containing the modified list of options:
agentService.update().agent(agent().options(modified)).send();
// Close the connection to the server:
connection.close();
}
use of org.ovirt.engine.sdk4.Service in project ovirt-engine-sdk-java by oVirt.
the class UpdateQuotaLimits 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 reference to the root of the tree of services:
SystemService systemService = connection.systemService();
// Find the data center and the service that manages it:
DataCentersService dcsService = systemService.dataCentersService();
DataCenter dc = dcsService.list().search("name=mydc").send().dataCenters().get(0);
DataCenterService dcService = dcsService.dataCenterService(dc.id());
// Find the storage domain and the service that manages it:
StorageDomainsService sdsService = systemService.storageDomainsService();
StorageDomain sd = sdsService.list().search("name=mydata").send().storageDomains().get(0);
StorageDomainService sdService = sdsService.storageDomainService(sd.id());
// Find the quota and the service that manages it. Note that the service that manages the quota doesn't support
// search, so we need to retrieve all the quotas and filter explicitly. If the quota doesn't exist, create it.
QuotasService quotasService = dcService.quotasService();
List<Quota> quotas = quotasService.list().send().quotas();
Quota quota = null;
for (Quota q : quotas) {
if (Objects.equals(q.id(), "myquota")) {
quota = q;
break;
}
}
if (quota == null) {
quota = quotasService.add().quota(quota().name("myquota").description("My quota").clusterHardLimitPct(20).clusterSoftLimitPct(80).storageHardLimitPct(20).storageSoftLimitPct(80)).send().quota();
}
QuotaService quotaService = quotasService.quotaService(quota.id());
// Find the quota limits for the storage domain that we are interested on:
QuotaStorageLimitsService limitsService = quotaService.quotaStorageLimitsService();
List<QuotaStorageLimit> limits = limitsService.list().send().limits();
QuotaStorageLimit limit = null;
for (QuotaStorageLimit l : limits) {
if (Objects.equals(l.id(), sd.id())) {
limit = l;
break;
}
}
// If that limit exists we will delete it:
if (limit != null) {
QuotaStorageLimitService limitService = limitsService.limitService(limit.id());
limitService.remove();
}
// Create the limit again with the desired values, in this example it will be 100 GiB:
limitsService.add().limit(quotaStorageLimit().name("mydatalimit").description("My storage domain limit").limit(100).storageDomain(storageDomain().id(sd.id()))).send();
// Close the connection to the server:
connection.close();
}
use of org.ovirt.engine.sdk4.Service in project ovirt-engine-sdk-java by oVirt.
the class HttpConnection method followLink.
@Override
public <TYPE> TYPE followLink(TYPE object) {
if (!isLink(object)) {
throw new Error("Can't follow link because object don't have any");
}
String href = getHref(object);
if (href == null) {
throw new Error("Can't follow link because the 'href' attribute does't have a value");
}
try {
URL url = new URL(getUrl());
String prefix = url.getPath();
if (!prefix.endsWith("/")) {
prefix += "/";
}
if (!href.startsWith(prefix)) {
throw new Error("The URL '" + href + "' isn't compatible with the base URL of the connection");
}
// Get service based on path
String path = href.substring(prefix.length());
Service service = systemService().service(path);
// Obtain method which provides result object and invoke it:
Method get;
if (object instanceof ListWithHref) {
get = service.getClass().getMethod("list");
} else {
get = service.getClass().getMethod("get");
}
Object getRequest = get.invoke(service);
Method send = getRequest.getClass().getMethod("send");
send.setAccessible(true);
Object getResponse = send.invoke(getRequest);
Method obtainObject = getResponse.getClass().getDeclaredMethods()[0];
obtainObject.setAccessible(true);
return (TYPE) obtainObject.invoke(getResponse);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
throw new Error(String.format("Unexpected error while following link \"%1$s\"", href), ex);
} catch (MalformedURLException ex) {
throw new Error(String.format("Error while creating URL \"%1$s\"", getUrl()), ex);
}
}
use of org.ovirt.engine.sdk4.Service in project ovirt-engine-sdk-java by oVirt.
the class AddUserPublicSshKey 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 users:
UsersService usersService = systemService.usersService();
// Find the user:
User user = usersService.list().search("name=myuser").send().users().get(0);
// Get the reference to the service that manages the user that we found in the previous step:
UserService userService = usersService.userService(user.id());
// Get a reference to the service that manages the public SSH keys of the user:
SshPublicKeysService keysService = userService.sshPublicKeysService();
// Add a new SSH public key:
keysService.add().key(sshPublicKey().content("ssh-rsa AAA...mu9 myuser@example.com")).send();
// Note that the above operation will fail because the example SSH public key is not valid, make sure to use a
// valid key.
// Close the connection to the server:
connection.close();
}
use of org.ovirt.engine.sdk4.Service in project ovirt-engine-sdk-java by oVirt.
the class AddVmDisk 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 disk attachments of the virtual machine:
DiskAttachmentsService diskAttachmentsService = vmsService.vmService(vm.id()).diskAttachmentsService();
// Use the `add` method of the disk attachments service to add the disk. Note that the size of the disk,
// the `provionedSize` attribute, is specified in bytes, so to create a disk of 10 GiB the value should
// be 10 * 2^30.
DiskAttachment diskAttachment = diskAttachmentsService.add().attachment(diskAttachment().disk(disk().name("mydisk").description("My disk").format(DiskFormat.COW).provisionedSize(BigInteger.valueOf(10).multiply(BigInteger.valueOf(2).pow(30))).storageDomains(storageDomain().name("mydata"))).interface_(DiskInterface.VIRTIO).bootable(false).active(true)).send().attachment();
// Wait till the disk is OK:
DisksService disksService = connection.systemService().disksService();
DiskService diskService = disksService.diskService(diskAttachment.disk().id());
for (; ; ) {
Thread.sleep(5 * 1000);
Disk disk = diskService.get().send().disk();
if (disk.status() == DiskStatus.OK) {
break;
}
}
// Close the connection to the server:
connection.close();
}
Aggregations