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);
}
}
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();
}
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();
}
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());
}
}
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());
}
}
}
Aggregations