Java SDK Guide
Using the Red Hat Enterprise Virtualization Java SDK
Red Hat Enterprise Virtualization Documentation Team
rhev-docs@redhat.com
Abstract
Chapter 1. Overview
Red Hat Enterprise Virtualization
entitlement pool in Red Hat Subscription Manager.
1.1. Prerequisites
- A system where Red Hat Enterprise Linux 6.6 or 7 is installed. Both the Server and Workstation variants are supported.
- A subscription to Red Hat Enterprise Virtualization entitlements.
Important
Important
Chapter 2. Creating a Build Environment
2.1. Installing the Java Software Development Kit
Procedure 2.1. Installing the Java Software Development Kit
- Ensure your system is subscribed to the
Red Hat Enterprise Virtualization
entitlement in Red Hat Subscription Manager:# subscription-manager list --available | grep -A8 "Red Hat Enterprise Virtualization" # subscription-manager attach --pool=pool_id # subscription-manager repos --enable=rhel-6-server-rpms # subscription-manager repos --enable=rhel-6-server-rhevm-3.6-rpms # subscription-manager repos --enable=jb-eap-6-for-rhel-6-server-rpms
- Install the required packages:
# yum install rhevm-sdk-java # yum install rhevm-sdk-java-javadoc
/usr/share/java/rhevm-sdk-java
directory, and can now be added to Java projects.
2.2. Dependencies
commons-beanutils.jar
commons-codec.jar
httpclient.jar
httpcore.jar
jakarta-commons-logging.jar
log4j.jar
/usr/share/java
directory on Red Hat Enterprise Linux 6 and Red Hat Enterprise Linux 7 systems.
2.3. Configuring SSL
2.3.1. Configuring SSL
Procedure 2.2. Configuring SSL
- Download the certificate for the Red Hat Enterprise Virtualization Manager:
https://[your manager's address]:[port]/ca.crt
- Generate a keystore:
keytool -import -alias "server.crt truststore" -file ca.crt -keystore server.truststore
- Specify the
keyStorePath
andkeyStorePassword
arguments when constructing an instance of theApi
object as described in Section 3.1, “Connecting to the Red Hat Enterprise Virtualization Manager”:myBuilder.keyStorePath("/home/username/server.truststore") myBuilder.keyStorePassword("p@ssw0rd")
2.3.2. Host Verification
Api
class:
myBuilder.noHostVerification(true)
Important
Chapter 3. Using the Software Development Kit
3.1. Connecting to the Red Hat Enterprise Virtualization Manager
Api
class is the main class you use to connect to and manipulate objects in a Red Hat Enterprise Virtualization environment. To declare an instance of this class, you must declare an instance of the ApiBuilder
class, pass the required arguments to this instance using builder methods, then call the build
method on the instance. The build
method returns an instance of the Api
class that you can then assign to a variable and use to perform subsequent actions.
Example 3.1. Connecting to the Red Hat Enterprise Virtualization Manager
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; public class rhevm { public static void main(String[] args) { Api api = null; try { ApiBuilder myBuilder = new ApiBuilder(); 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(); } catch (ServerException | UnsecuredConnectionAttemptError | IOException ex) { Logger.getLogger(Ovirt.class.getName()).log(Level.SEVERE, null, ex); } finally { if (api != null) { try { api.close(); } catch (Exception ex) { Logger.getLogger(Ovirt.class.getName()).log(Level.SEVERE, null, ex); } } } } }
ApiBuilder
class, see Appendix A, ApiBuilder Methods.
Note
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.
3.2. Listing Entities
getVMs()
method of the Api
class.
Procedure 3.1. Listing Entities
- Declare a
List
of the type of entity to be listed and use the corresponding method to get the list of entities:List<VM> vms = api.getVMs().list();
3.3. Modifying the Attributes of Resources
Procedure 3.2. Modifying the Attributes of a Resource
- Declare an instance of the resource whose attributes are to be modified:
VM vm = api.getVMs().get("test");
- Set the new value of the attribute:
vm.setDescription("java_sdk");
- Update the virtual machine to apply the change:
VM newVM = vm.update();
3.4. Getting a Resource
name
, and UUID
. Both return an object with the specified attribute if that object exists.
name
attribute:
VM vm = api.getVMs().get("test");
UUID
attribute:
VM vm = api.getVMs().get(UUID.fromString("5a89a1d2-32be-33f7-a0d1-f8b5bc974ff6"));
3.5. Adding Resources
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.
org.ovirt.engine.sdk.entities.VM vmParams = new org.ovirt.engine.sdk.entities.VM(); vmParams.setName("myVm"); vmParams.setCluster(api.getClusters().get("myCluster")); vmParams.setTemplate(api.getTemplates().get("myTemplate")); ...
VM vm = api.getVMs().add(vmParams);
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.
org.ovirt.engine.sdk.entities.VM vmParams = new org.ovirt.engine.sdk.entities.VM(); 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(); templateParam.setName("myTemplate"); vmParams.setTemplate(templateParam); ...
VM vm = api.getVMs().add(vmParams);
3.6. Performing Actions on Resources
Procedure 3.3. Performing an Action on a Resource
- Declare an instance of the resource:
VM vm = api.getVMs().get("test");
- Declare action parameters to send to the resource:
Action actionParam = new Action(); org.ovirt.engine.sdk.entities.VM vmParam = new org.ovirt.engine.sdk.entities.VM(); actionParam.setVm(vmParam);
- Perform the action:
Action res = vm.start(actionParam);
Action res = vm.start(new Action() { { setVm(new org.ovirt.engine.sdk.entities.VM()); } });
3.7. Listing Sub-Resources
Procedure 3.4. Listing Sub-Resources
- Declare an instance of the resource whose sub-resources are to be listed:
VM vm = api.getVMs().get("test");
- List the sub-resources:
List<VMDisk> disks = vm.getDisks().list();
3.8. Getting Sub-Resources
Procedure 3.5. Getting the Sub-Resources of a Resource
- Declare an instance of the resource whose sub-resources are to be referenced:
VM vm = api.getVMs().get("test");
- Declare an instance of the sub-resource to be referenced:
VMDisk disk = vm.getDisks().get("my disk");
3.9. Adding Sub-Resources to a Resource
Procedure 3.6. Adding a Sub-Resource to a Resource
- Declare an instance of the resource to which sub-resources are to be added:
VM vm = api.getVMs().get("test");
- Create parameters to define the attributes of the resource:
Disk diskParam = new Disk(); diskParam.setProvisionedSize(1073741824L); diskParam.setInterface("virtio"); diskParam.setFormat("cow");
- Add the sub-resource:
Disk disk = vm.getDisks().add(diskParam);
3.10. Modifying Sub-Resources
Procedure 3.7. Updating a Sub-Resource
- Declare an instance of the resource whose sub-resource is to be modified:
VM vm = api.getVMs().get("test");
- Declare an instance of the sub-resource to be modified:
VMDisk disk = vm.getDisks().get("test_Disk1");
- Set the new value of the attribute:
disk.setAlias("test_Disk1_updated");
- Update the sub-resource:
VMDisk updateDisk = disk.update();
3.11. Performing Actions on Sub-Resources
Procedure 3.8. Performing an Action on a Sub-Resource
- Declare an instance of the resource containing the sub-resource on which the action is to be performed:
VM vm = api.getVMs().get("test");
- Declare an instance of the sub-resource:
VMDisk disk = vm.getDisks().get("test_Disk1");
- Declare action parameters to send to the sub-resource:
Action actionParam = new Action();
- Perform the action:
Action result = disk.activate(actionParam);
Appendix A. ApiBuilder Methods
ApiBuilder
class.
Table A.1. ApiBuilder Methods
Method | Argument Type | Description |
---|---|---|
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. |
password | String | The password of the user with which to connect to the Manager. |
sessionID | String | The identifier of a session with which to connect to the Manager. If you have already authenticated with the Manager and a session is available, you can specify this argument instead of specifying a user name and password. |
requestTimeout | Integer | The timeout, in seconds, to wait for responses to requests. If a request takes longer than this value to respond, the request is cancelled, and an exception is thrown. This argument is optional. |
sessionTimeout | Integer | The timeout, in minutes, after which an active session is destroyed if no requests are made to the Manager. This argument is optional. |
persistentAuth | Boolean | Enables or disables persistent authentication using cookies. This option is enabled by default, so this method is only required to disable this option. |
noHostVerification | Boolean | Enables or disables verification of the host name in the SSL certificate presented by the server where the Manager is hosted. By default, the identity of host names is verified, and the connection is rejected if the host name is not correct, so this method is only required to disable this option. |
keyStorePath | String | Specifies the location of a file containing the CA certificate used to verify the certificate presented by the server where the Manager is hosted. This method must be used together with the keyStorePassword method. |
keyStorePassword | String | The password used to access the keystore file specified in the keyStorePath method. |
filter | Boolean | Enables or disables filtering of objects based on the permissions of the user making the request. By default, this option is disabled, which allows any user to see all objects in the environment. This method is only required to restrict the objects in the environment to those visible to the user making the request. |
debug | Boolean | Enables or disables debug output. By default, this option is disabled. |
Appendix B. Revision History
Revision History | ||||
---|---|---|---|---|
Revision 3.6-4 | Wed 25 May 2016 | Red Hat Enterprise Virtualization Documentation Team | ||
| ||||
Revision 3.6-3 | Mon 22 Feb 2016 | Red Hat Enterprise Virtualization Documentation Team | ||
| ||||
Revision 3.6-2 | Wed 18 Nov 2015 | Red Hat Enterprise Virtualization Documentation Team | ||
| ||||
Revision 3.6-1 | Mon 10 Aug 2015 | Red Hat Enterprise Virtualization Documentation Team | ||
|