Search in sources :

Example 31 with User

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

the class ConnectionBuilder method build.

/**
 * Checks the values of the parameters given so far, checks that they are valid, and builds a connection using
 * them.
 *
 * @return the new connection
 */
public Connection build() {
    try {
        // Check the parameters:
        if (url == null) {
            throw new IllegalArgumentException("The 'url' parameter is mandatory");
        }
        if (trustStoreFile != null && !new File(trustStoreFile).exists()) {
            throw new IllegalArgumentException(String.format("The truststore file '%s' doesn't exist'", trustStoreFile));
        }
        urlobj = new URL(url);
        // If all the checks pass, then create the connection:
        HttpConnection connection = new HttpConnection();
        connection.setClient(createHttpClient());
        connection.setUrl(url);
        connection.setUser(user);
        connection.setPassword(password);
        connection.setSsoToken(token);
        connection.setKerberos(kerberos);
        connection.setSsoUrl(ssoUrl);
        connection.setSsoTokenName(ssoTokenName);
        connection.setSsoRevokeUrl(ssoRevokeUrl);
        connection.setHeaders(headers);
        return connection;
    } catch (Exception e) {
        throw new Error("Failed to build connection", e);
    }
}
Also used : HttpConnection(org.ovirt.engine.sdk4.internal.HttpConnection) File(java.io.File) URL(java.net.URL)

Example 32 with User

use of org.ovirt.engine.sdk4.types.User 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();
}
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 33 with User

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

the class UnassignTagToVm 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=myvm0").send().vms().get(0);
    // Find the service that manages the vm:
    VmService vmService = vmsService.vmService(vm.id());
    // Locate the service that manages the tags of the vm:
    AssignedTagsService tagsService = vmService.tagsService();
    // Find the tag id:
    String tagId = null;
    for (Tag tag : tagsService.list().send().tags()) {
        if (tag.name().equals("mytag")) {
            tagId = tag.id();
            break;
        }
    }
    // Locate the service that manages the tag:
    AssignedTagService tagService = tagsService.tagService(tagId);
    // Unassign tag from virtual machine:
    tagService.remove().send();
    // Close the connection to the server:
    connection.close();
}
Also used : AssignedTagService(org.ovirt.engine.sdk4.services.AssignedTagService) 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) Tag(org.ovirt.engine.sdk4.types.Tag) AssignedTagsService(org.ovirt.engine.sdk4.services.AssignedTagsService)

Example 34 with User

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

the class VmBackup method run.

/**
 * This is the non-static method equivalent to {@code main}. Its purpose is to create a connection to the server and
 * make sure that it is always closed, regardless of what happens during the execution of the actual work.
 */
private void run() throws Exception {
    // Connect to the server:
    ConnectionBuilder builder = ConnectionBuilder.connection().url(API_URL).user(API_USER).password(API_PASSWORD).trustStoreFile(API_TRUSTSTORE);
    // Do the rest of the work and always remember to close the connection:
    try (Connection connection = builder.build()) {
        log.info("Connected to the server.");
        run(connection.systemService());
    }
}
Also used : Connection(org.ovirt.engine.sdk4.Connection) ConnectionBuilder(org.ovirt.engine.sdk4.ConnectionBuilder)

Example 35 with User

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

the class HttpConnection method getSsoResponse.

private JsonNode getSsoResponse(URI uri, List<NameValuePair> params) {
    HttpResponse response = null;
    try {
        // Send request and parse token:
        HttpPost requestToken = new HttpPost(uri);
        requestToken.addHeader("User-Agent", "JavaSDK");
        requestToken.addHeader("Accept", "application/json");
        requestToken.setEntity(new UrlEncodedFormEntity(params));
        response = client.execute(requestToken);
        checkContentType(JSON_CONTENT_TYPE_RE, "JSON", response.getFirstHeader("content-type").getValue());
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readTree(response.getEntity().getContent());
    } catch (IOException ex) {
        throw new Error("Failed to parse JSON response", ex);
    } catch (Exception ex) {
        throw new Error("Failed to send SSO request", ex);
    } finally {
        if (response != null) {
            EntityUtils.consumeQuietly(response.getEntity());
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpResponse(org.apache.http.HttpResponse) Error(org.ovirt.engine.sdk4.Error) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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