Sample Code

Here is some sample code to help you get going using this library in your own projects.

Connecting to the OpenShift server

Create a connection to the OpenShift server

IOpenShiftConnection connection = new OpenShiftConnectionFactory().getConnection("my_user_application", "user", "password");

Interacting with User objects

Get the user information

IUser user = connection.getUser();
System.out.println("RHLogin:\t" + user.getRhlogin());

Interacting with Domain objects

Create a new domain

IUser user = connection.getUser();
IDomain domain = user.createDomain("myDomain");

Get the default domain

IDomain domain = user.getDefaultDomain();
System.out.println("Namespace:\t" + domain.getId());

Interacting with Application objects

Create a new application within a domain

IApplication as7Application = domain.createApplication("myApplication", LatestVersionOf.jbossAs().get(user));

Get application information

for (IApplication application : domain.getApplications()) {
    System.out.println(application.getName());
    System.out.println("\tFramework:\t" + application.getCartridge().getName());
    System.out.println("\tCreation:\t" + application.getCreationTime());
    System.out.println("\tUUID:\t\t" + application.getUUID());
    System.out.println("\tGit URL:\t" + application.getGitUrl());
    System.out.println("\tPublic URL:\t" + application.getApplicationUrl() + "\n");
}

Interacting with Cartridge objects

Add the latest version of of the MySQL cartridge

IEmbeddedCartridge mySqlCartridge = as7Application.addEmbeddableCartridge(LatestVersionOf.mySQL().get(user));

Get embedded cartridge information

for(IEmbeddedCartridge cartridge : application.getEmbeddedCartridges()) {
    System.out.println("\t" + cartridge.getName() + " - URL:" + cartridge.getUrl());
}

Get the MySQL cartridge's connection URL

String mySqlConnectionUrl = mySqlCartridge.getUrl();

Interacting with SSHPublicKey objects

Add a public SSH key

ISSHPublicKey key = new SSHPublicKey(SSH_PUBLIC_KEY);
IOpenShiftSSHKey addedKey = user.getSSHKeyByPublicKey(key.getPublicKey());
if (addedKey == null) {
    user.putSSHKey(String.valueOf(System.currentTimeMillis()), key);
}

Special thanks to the following articles for supplying some of the above sample code:

enable-openshift-ci: full example using openshift-java-client

show-domain-info: openshift-java-client in a nutshell