Search in sources :

Example 1 with User

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

the class UpdateDataCenter 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 data centers service:
    DataCentersService dcsService = connection.systemService().dataCentersService();
    // Retrieve the description of the data center:
    DataCenter dc = dcsService.list().search("name=mydc").send().dataCenters().get(0);
    // In order to update the data center we need a reference to the service that manages it, then we can call the
    // "update" method passing the update:
    DataCenterService dcService = dcsService.dataCenterService(dc.id());
    dc = dcService.update().dataCenter(dataCenter().description("Updated description")).send().dataCenter();
    // Print the description of the result of the update:
    System.out.printf("%s: %s", dc.name(), dc.description());
    // Close the connection to the server:
    connection.close();
}
Also used : DataCentersService(org.ovirt.engine.sdk4.services.DataCentersService) DataCenter(org.ovirt.engine.sdk4.types.DataCenter) Connection(org.ovirt.engine.sdk4.Connection) DataCenterService(org.ovirt.engine.sdk4.services.DataCenterService)

Example 2 with User

use of org.ovirt.engine.sdk4.types.User 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();
}
Also used : HostService(org.ovirt.engine.sdk4.services.HostService) Agent(org.ovirt.engine.sdk4.types.Agent) FenceAgentService(org.ovirt.engine.sdk4.services.FenceAgentService) Connection(org.ovirt.engine.sdk4.Connection) ArrayList(java.util.ArrayList) HostsService(org.ovirt.engine.sdk4.services.HostsService) Host(org.ovirt.engine.sdk4.types.Host) Option(org.ovirt.engine.sdk4.types.Option) FenceAgentsService(org.ovirt.engine.sdk4.services.FenceAgentsService)

Example 3 with User

use of org.ovirt.engine.sdk4.types.User 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();
}
Also used : StorageDomainService(org.ovirt.engine.sdk4.services.StorageDomainService) DataCentersService(org.ovirt.engine.sdk4.services.DataCentersService) Connection(org.ovirt.engine.sdk4.Connection) QuotaStorageLimitService(org.ovirt.engine.sdk4.services.QuotaStorageLimitService) QuotaStorageLimitsService(org.ovirt.engine.sdk4.services.QuotaStorageLimitsService) StorageDomainsService(org.ovirt.engine.sdk4.services.StorageDomainsService) StorageDomain(org.ovirt.engine.sdk4.types.StorageDomain) QuotaStorageLimit(org.ovirt.engine.sdk4.types.QuotaStorageLimit) DataCenter(org.ovirt.engine.sdk4.types.DataCenter) SystemService(org.ovirt.engine.sdk4.services.SystemService) Quota(org.ovirt.engine.sdk4.types.Quota) QuotasService(org.ovirt.engine.sdk4.services.QuotasService) DataCenterService(org.ovirt.engine.sdk4.services.DataCenterService) QuotaService(org.ovirt.engine.sdk4.services.QuotaService)

Example 4 with User

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

the class HttpConnection method getAccessToken.

private String getAccessToken() {
    if (ssoToken == null) {
        // Build SSO URL if necessary:
        URI ssoURI = ssoUrl != null ? SsoUtils.buildUrl(ssoUrl) : kerberos ? SsoUtils.buildSsoUrlKerberos(url) : SsoUtils.buildSsoUrlBasic(url);
        // Construct POST body:
        List<NameValuePair> params = new ArrayList<>(4);
        params.add(new BasicNameValuePair("scope", "ovirt-app-api"));
        if (kerberos) {
            params.add(new BasicNameValuePair("grant_type", "urn:ovirt:params:oauth:grant-type:http"));
        } else {
            params.add(new BasicNameValuePair("username", user));
            params.add(new BasicNameValuePair("password", password));
            params.add(new BasicNameValuePair("grant_type", "password"));
        }
        // Send request to obtain SSO token:
        JsonNode node = getSsoResponse(ssoURI, params);
        if (node.isArray()) {
            node = node.get(0);
        }
        if (node.get("error") != null) {
            throw new Error(String.format("Error during SSO authentication %1$s : %2$s", node.get("error_code"), node.get("error")));
        }
        ssoToken = node.get(ssoTokenName).getTextValue();
    }
    return ssoToken;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) Error(org.ovirt.engine.sdk4.Error) JsonNode(org.codehaus.jackson.JsonNode) URI(java.net.URI)

Example 5 with User

use of org.ovirt.engine.sdk4.types.User 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();
}
Also used : UsersService(org.ovirt.engine.sdk4.services.UsersService) User(org.ovirt.engine.sdk4.types.User) SystemService(org.ovirt.engine.sdk4.services.SystemService) UserService(org.ovirt.engine.sdk4.services.UserService) Connection(org.ovirt.engine.sdk4.Connection) SshPublicKeysService(org.ovirt.engine.sdk4.services.SshPublicKeysService)

Aggregations

Connection (org.ovirt.engine.sdk4.Connection)63 VmsService (org.ovirt.engine.sdk4.services.VmsService)30 Vm (org.ovirt.engine.sdk4.types.Vm)30 VmService (org.ovirt.engine.sdk4.services.VmService)18 SystemService (org.ovirt.engine.sdk4.services.SystemService)13 StorageDomainsService (org.ovirt.engine.sdk4.services.StorageDomainsService)12 StorageDomain (org.ovirt.engine.sdk4.types.StorageDomain)12 HostsService (org.ovirt.engine.sdk4.services.HostsService)6 StorageDomainService (org.ovirt.engine.sdk4.services.StorageDomainService)6 Host (org.ovirt.engine.sdk4.types.Host)6 DataCentersService (org.ovirt.engine.sdk4.services.DataCentersService)5 HostService (org.ovirt.engine.sdk4.services.HostService)5 Disk (org.ovirt.engine.sdk4.types.Disk)5 ClustersService (org.ovirt.engine.sdk4.services.ClustersService)4 Cluster (org.ovirt.engine.sdk4.types.Cluster)4 ArrayList (java.util.ArrayList)3 AffinityLabelsService (org.ovirt.engine.sdk4.services.AffinityLabelsService)3 AssignedTagsService (org.ovirt.engine.sdk4.services.AssignedTagsService)3 DataCenterService (org.ovirt.engine.sdk4.services.DataCenterService)3 DiskAttachmentsService (org.ovirt.engine.sdk4.services.DiskAttachmentsService)3