Red_Hat_Enterprise_Virtualization-3.6-Java_SDK_Guide-en-US
Red_Hat_Enterprise_Virtualization-3.6-Java_SDK_Guide-en-US
This document is licensed by Red Hat under the Creative Commons Attribution-ShareAlike 3.0
Unported License. If you distribute this document, or a modified version of it, you must provide
attribution to Red Hat, Inc. and provide a link to the original. If the document is modified, all Red
Hat trademarks must be removed.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert,
Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, JBoss, OpenShift, Fedora, the Infinity
logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other
countries.
Linux ® is the registered trademark of Linus Torvalds in the United States and other countries.
XFS ® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United
States and/or other countries.
MySQL ® is a registered trademark of MySQL AB in the United States, the European Union and
other countries.
Node.js ® is an official trademark of Joyent. Red Hat Software Collections is not formally related
to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack ® Word Mark and OpenStack logo are either registered trademarks/service marks
or trademarks/service marks of the OpenStack Foundation, in the United States and other
countries and are used with the OpenStack Foundation's permission. We are not affiliated with,
endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
Abstract
This guide describes Red Hat Enterprise Virtualization's Java Software Development Kit.
Table of Contents
Table of Contents
.CHAPTER
. . . . . . . . . . 1.
. .OVERVIEW
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3. . . . . . . . . . . .
1.1. PREREQUISITES 3
.CHAPTER
. . . . . . . . . . 2.
. . CREATING
. . . . . . . . . . . .A. .BUILD
. . . . . . .ENVIRONMENT
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4. . . . . . . . . . . .
2.1. INSTALLING THE JAVA SOFTWARE DEVELOPMENT KIT 4
2.2. DEPENDENCIES 4
2.3. CONFIGURING SSL 4
.CHAPTER
. . . . . . . . . . 3.
. . USING
. . . . . . . .THE
. . . . SOFTWARE
. . . . . . . . . . . . .DEVELOPMENT
. . . . . . . . . . . . . . . . KIT
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6. . . . . . . . . . . .
3.1. CONNECTING TO THE RED HAT ENTERPRISE VIRTUALIZATION MANAGER 6
3.2. LISTING ENTITIES 7
3.3. MODIFYING THE ATTRIBUTES OF RESOURCES 7
3.4. GETTING A RESOURCE 8
3.5. ADDING RESOURCES 8
3.6. PERFORMING ACTIONS ON RESOURCES 9
3.7. LISTING SUB-RESOURCES 9
3.8. GETTING SUB-RESOURCES 10
3.9. ADDING SUB-RESOURCES TO A RESOURCE 10
3.10. MODIFYING SUB-RESOURCES 10
3.11. PERFORMING ACTIONS ON SUB-RESOURCES 11
. . . . . . . . . . . . A.
APPENDIX . . .APIBUILDER
. . . . . . . . . . . . . METHODS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
.............
. . . . . . . . . . . . B.
APPENDIX . . .REVISION
. . . . . . . . . . HISTORY
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
.............
1
Java SDK Guide
2
CHAPTER 1. OVERVIEW
CHAPTER 1. OVERVIEW
The Java software development kit is a collection of classes that allows you to interact with the Red
Hat Enterprise Virtualization Manager in Java-based projects. By downloading these classes and
adding them to your project, you can access a range of functionality for high-level automation of
administrative tasks.
The Java software development kit uses the rhevm-sdk-java package, which is available to systems
subscribed to a Red Hat Enterprise Virtualization entitlement pool in Red Hat Subscription
Manager.
1.1. PREREQUISITES
To install the Java software development kit, you must have:
A system where Red Hat Enterprise Linux 6.6 or 7 is installed. Both the Server and Workstation
variants are supported.
IMPORTANT
The rhevm-sdk-java package must be installed on each system where scripts that use
the software development kit will be run.
IMPORTANT
The software development kit is an interface for the Red Hat Enterprise Virtualization
REST API. As such, you must use the version of the software development kit that
corresponds to the version of your Red Hat Enterprise Virtualization environment. For
example, if you are using Red Hat Enterprise Virtualization 3.5, you must use the version
of the software development kit designed for 3.5.
3
Java SDK Guide
The Java software development kit and accompanying documentation are downloaded to the
/usr/share/java/rhevm-sdk-java directory, and can now be added to Java projects.
2.2. DEPENDENCIES
To use the Java software development kit in Java applications, you must add the following JAR files to
the class path of those applications:
commons-beanutils.jar
commons-codec.jar
httpclient.jar
httpcore.jar
jakarta-commons-logging.jar
log4j.jar
The packages that provide these JAR files are installed as dependencies to the rhevm-sdk-java
package. By default, they are available in the /usr/share/java directory on Red Hat Enterprise
Linux 6 and Red Hat Enterprise Linux 7 systems.
4
CHAPTER 2. CREATING A BUILD ENVIRONMENT
The Red Hat Enterprise Virtualization Manager Java SDK provides full support for HTTP over Secure
Socket Layer (SSL) and the IETF Transport Layer Security (TLS) protocol using the Java Secure
Socket Extension (JSSE). JSSE has been integrated into the Java 2 platform as of version 1.4 and
works with the Java SDK out of the box. On older Java 2 versions, JSSE must be manually installed and
configured.
1. Download the certificate for the Red Hat Enterprise Virtualization Manager:
2. Generate a keystore:
myBuilder.keyStorePath("/home/username/server.truststore")
myBuilder.keyStorePassword("p@ssw0rd")
myBuilder.noHostVerification(true)
IMPORTANT
This method should not be used for production systems due to security reasons, unless
it is a conscious decision and you are aware of the security implications of not verifying
host identity.
5
Java SDK Guide
The following is an example of a simple Java SE program that creates a connection with a Red Hat
Enterprise Virtualization environment, then gracefully shuts down and closes the connection:
package rhevm;
import org.ovirt.engine.sdk.Api;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ovirt.engine.sdk.ApiBuilder;
import org.ovirt.engine.sdk.exceptions.ServerException;
import org.ovirt.engine.sdk.exceptions.UnsecuredConnectionAttemptError;
try {
myBuilder.url("https://rhevm.example.com/api")
myBuilder.user("admin@internal")
myBuilder.password("p@ssw0rd")
myBuilder.keyStorePath("/home/username/server.truststore")
myBuilder.keyStorePassword("p@ssw0rd")
api = myBuilder.build();
api.shutdown();
6
CHAPTER 3. USING THE SOFTWARE DEVELOPMENT KIT
try {
api.close();
} catch (Exception ex) {
This example creates a connection using basic authentication, but other methods are also available.
For a list of the key arguments that can be passed to instances of the ApiBuilder class, see
Appendix A, ApiBuilder Methods.
NOTE
Note that the Api class does not implement the Autocloseable interface. As such, it is
recommended that you shut down instances of the Api class in a finally block as per
the above example to ensure the connection with the Red Hat Enterprise Virtualization
Manager is closed gracefully.
Declare a List of the type of entity to be listed and use the corresponding method to get the
list of entities:
VM vm = api.getVMs().get("test");
vm.setDescription("java_sdk");
7
Java SDK Guide
VM newVM = vm.update();
VM vm = api.getVMs().get("test");
VM vm = api.getVMs().get(UUID.fromString("5a89a1d2-32be-33f7-a0d1-
f8b5bc974ff6"));
Example 1
In this example, an instance of the VM class is declared to represent the new virtual machine to be
added. Next, the attributes of that virtual machine set to the preferred values. Finally, the new virtual
machine is added to the Manager.
vmParams.setName("myVm");
vmParams.setCluster(api.getClusters().get("myCluster"));
vmParams.setTemplate(api.getTemplates().get("myTemplate"));
...
VM vm = api.getVMs().add(vmParams);
Example 2
In this example, an instance of the VM class is declared in the same way as Example 1. However, rather
than using the get method to reference existing objects in the Manager, each attribute is referenced
by declaring an instance of that attribute. Finally, the new virtual machine is added to the Manager.
vmParams.setName("myVm");
org.ovirt.engine.sdk.entities.Cluster clusterParam = new Cluster();
clusterParam.setName("myCluster");
vmParams.setCluster(clusterParam);
org.ovirt.engine.sdk.entities.Template templateParam = new Template();
8
CHAPTER 3. USING THE SOFTWARE DEVELOPMENT KIT
templateParam.setName("myTemplate");
vmParams.setTemplate(templateParam);
...
VM vm = api.getVMs().add(vmParams);
VM vm = api.getVMs().get("test");
VM vm = api.getVMs().get("test");
9
Java SDK Guide
VM vm = api.getVMs().get("test");
VM vm = api.getVMs().get("test");
VM vm = api.getVMs().get("test");
10
CHAPTER 3. USING THE SOFTWARE DEVELOPMENT KIT
disk.setAlias("test_Disk1_updated");
1. Declare an instance of the resource containing the sub-resource on which the action is to be
performed:
VM vm = api.getVMs().get("test");
11
Java SDK Guide
user String The name of the user with which to connect to the
Manager. You must specify both the user name and
domain, such as admin@internal. This method
must be used together with the password method.
12
APPENDIX A. APIBUILDER METHODS
13
Java SDK Guide
14