Creating Java code using the handy command-line and Java libraries

The IBM SmartCloud Enterprise provides a number of APIs, and I describe them in multiple articles on this web site (read: my blog posts).

These APIs allow you to interact with the IBM SmartCloud Enterprise to create, update, and delete resources; but, when you write automation you will need more. For example, you will need to wait until a resource is ready to pursue your automation, or imagine you are a teacher and you want to deploy, multiple times, the same image for your attendees. That’s the kind of functionality the “handy command-lines and Java Library” provide you. As it is mentioned in its name, the “handy command-line and Java library” provides you this functionality in two ways: DOS or Linux command-lines, and Java Library.

You can download the “handy command-lines and Java library” from the IBM SmartCloud Enterprise platform. Basically, log in, and open the asset catalog.

In the catalog, search for “handy command-lines” (don’t forget to remove the default filter).

In this asset, you find the documentation and a .zip file. The installation is simple and explained in the documentation. The .zip file contains a number of .jar’ files (Java library), which include the compiled code and also the source code, and thus provides you a source example for how to use the IBM SmartCloud Enterprise APIs.

The list shows available extra command lines:

Available Java classes (extracted from the available Javadoc) are:

In addition to the new functionality, the advantage of this package is that you can extend it. Because the Java code is written as a framework, you can use a number of classes to create your own functionality. I will not go into the details, which could bother non-Java fans, but I can say that there is a bench of classes you can take advantage of to create your own command-lines and classes, which you can use to extend the Java library.

The Java package follows the same structure as the IBM SmartCloud Enterprise Java API: You have a HandyDeveloperCloudClient interface that defines all available extra methods. This interface extends the DeveloperCloudClient provided by the IBM SmartCloud Enterprise Java API. This means, if you have a HandyDeveloperCloudClient reference (which can be created with the HandyDeveloperCloud.getClient() method), you can call either the methods provided by the DeveloperCloudClient and the HandyDeveloperCloudClient API.

If you don’t want to change your existing Java code and thus keep your DeveloperCloudClient object, you can use the various available managers to call the methods provided by the HandyDeveloperCloudClient. For example, you could instantiate InstanceManager and call the createInstances() method with an “s” to create multiple instances at once.

Next, I provide several usage examples for command-line and Java. The examples automate the deployment of one instance and wait until this instance is active.

Command-line example scripts

The handy command-lines exist for DOS and Linux. Creating scripts in DOS seems to be easy at a first, but the difficulty comes when you have to retrieve the result of one command line and use it in the remainder of your scripts. Therefore, I suggest to DOS-based users to use Cygwin, which allows you to create bash-like scripts on a DOS-based operating system. Linux users can easily parse a results using awk commands or any other parsers.

DOS Script example:

Same example using Linux or Cygwin:

./ic-create-instance.sh -u <user> -g mypasswordfile -w mypassphrase -n DVE_TEST -d “My Test” -c DVE_User -L 141 -k 20019758 -t COP32.1/2048/60

./handy-ic-wait-instances-status.sh -u <user> -w mypassphrase -g mypasswordfile -n DVE_TEST -s ACTIVE -t 30 > result.txt

RESULT=`grep Result result.txt| cut -d” ” -f3- -`

echo $RESULT

echo ${RESULT}

RESULT=”./handy-ic-get-result.sh -u <user> -w mypassphrase -g mypasswordfile -t KeyValues -i 3 -v -r \”$RESULT\”"

echo $RESULT

echo `$RESULT`

The result file looks like this:

20120327_170119 – Instance=DVE_TEST id=210580 has status=NEW

20120327_170220 – Instance=DVE_TEST id=210580 has status=NEW

20120327_170320 – Instance=DVE_TEST id=210580 has status=NEW

20120327_170421 – Instance=DVE_TEST id=210580 has status=PROVISIONING

20120327_170521 – Instance=DVE_TEST id=210580 has status=PROVISIONING

20120327_170622 – Instance=DVE_TEST id=210580 has status=PROVISIONING

20120327_170722 – Instance=DVE_TEST id=210580 has status=PROVISIONING

20120327_170822 – Instance=DVE_TEST id=210580 has status=PROVISIONING

20120327_170923 – Instance=DVE_TEST id=210580 has status=PROVISIONING

20120327_171023 – Instance=DVE_TEST id=210580 has status=PROVISIONING

20120327_171124 – Instance=DVE_TEST id=210580 has status=ACTIVE

20120327_171124 – Result:{Message:Instance Status reached,ID:210580,Name:DVE_TEST,Status:ACTIVE}

Java example code with the HandyDeveloperCloudClient:

Java example code with the manager:

Comments: 1
Dominique Vernier

About Dominique Vernier

Dominique is the Global IT Architect for IBM Cloud, in charge of deploying the IBM Cloud solution throughout the world. He has extensive IT experience and has previously worked for the IBM South-West Europe Cloud Center of Excellence and the SOA Center of Excellence. He is co-author of two patents and has filed (Patent Pending Process) two others as author. He is based in Brussels.
This entry was posted in Managing the Cloud and tagged , , , , , , , , , , . Bookmark the permalink.

One Response to Creating Java code using the handy command-line and Java libraries

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>