Application Lifecycle Automation with Infobox Jelastic API

. At the end of the article, 300 rubles to the Infobox Jelastic account as a gift (you can receive it only once).
Jelastic API Request
All requests from the GET or POST API methods to Infobox Jelastic are made for the address:
https://app.jelasticloud.com/1.0/
In the documentation, this address is listed as [hoster-api-host].

Request data can be transferred in the query line after the "?" when using the GET method or in the body of a POST request. If using a GET request, parameters must use percent encoding (URL encoding).
Remember to limit the request length to 2048 characters. Therefore, we recommend:
- Use a GET request to obtain information that simply fits into the request length limit;
- Use a POST request to change data (create environments, change configuration files, etc.).
Therefore, you are not limited to a long request. This use is also more relevant to the specification of the HTTP protocol.
Each request must contain a set of required parameters. For some functions there is a set of additional parameters described in the documentation.
Each request includes a required appid parameter , outlining the requested environment. If the request is not related to environments, the system value 1dd8d191d38fff45e62564fcf67fdcd6 should be used . The textual parameter values must be represented in UTF-8 encoding. The sequence of parameters is not important.
Jelastic API Answer
The response to the request is presented in UTF-8 encoding. Responses to all API functions are given in JSON format . An example of the query result for each function is described in the documentation.

Jelastic API in action (using the Java library as an example)
To start automating the required process with the Jelastic API, you need to register on the Infobox Jelastic cloud hosting . You also need to download the Jelastic Client Library and add it to the classpath.
If you are using Maven, add the following dependency to pom.xml
<dependency><groupId>com.jelastic</groupId><artifactId>jelastic-public-j2se</artifactId><version>2.2</version></dependency>To call any API function you need to authenticate. The session parameter is responsible for this, identifying the user with the request. The session begins by calling the Users > Authentication > Signin method .
https://app.jelasticloud.com/1.0/users/authentication/rest/signin?appid=[string]&login=[string]&password=[string]
where
- appid is the system value 1dd8d191d38fff45e62564fcf67fdcd6
- login and password are the account information in your Jelastic account
The following API function calls must be made with the received session value.
To end the session with the API, you must call the Users > Authentication > Signout method .
https://app.jelasticloud.com/1.0/users/authentication/rest/signout?appid=[string]&session=[string]
Using the Java Client Library, you can automate various actions of your application life cycle, for example: create environments, change their status, delete, reboot hosts, deploy applications, etc.
Let's see how to create an environment with the selected topology and configure it using the Jelastic Client Library.
Environment creation
The full version of the environment creation example is available in the Jelastic API documentation (section "Jelastic Java Samples"). Here we will take a look at the main points step by step:
1. Define a new public class CreateEnviroment, which will include all of the following blocks and parameters. The first block of parameters should contain the following lines:
privatefinalstatic String PLATFORM_APPID = "1dd8d191d38fff45e62564fcf67fdcd6";
privatefinalstatic String HOSTER_URL = "https://app.jelasticloud.com";
privatefinalstatic String USER_EMAIL = "<email>";
privatefinalstatic String USER_PASSWORD = "<password>";
privatefinalstatic String ENV_NAME = "test-api-environment-" + new Random().nextInt(100);
where:
- email - your Jelastic login;
- password - password from your Jelastic account;
2. Then configure authentication, which will use the login and password specified above:
publicstaticvoidmain(String[] args){
System.out.println("Authenticate user...");
AuthenticationResponse authenticationResponse = authenticationService.signin(USER_EMAIL, USER_PASSWORD);
System.out.println("Signin response: " + authenticationResponse);
if (!authenticationResponse.isOK()) {
System.exit(authenticationResponse.getResult());
}
final String session = authenticationResponse.getSession();
After authentication, a new unique session is created. It will be used to perform the necessary operations on the user account. All of the following API calls must be made through this session, which remains valid until the Signout method is called.
3. In the next step, we will get a list of engines available for the specified <engine_type> (maybe java, ruby, php, python, etc.).
System.out.println("Getting list of engines...");
ArrayResponse arrayResponse = environmentService.getEngineList(PLATFORM_APPID, session, "<engine_type>");
System.out.println("GetEngineList response: " + arrayResponse);
if (!arrayResponse.isOK()) {
System.exit(arrayResponse.getResult());
}
4. After that, we will get a list of available node templates in accordance with the specified <template_type> , which can be of the following types:
ALL - all available platform templates, including native and cartridges.
NATIVE - default Jelastic templates.
CARTRIDGE are custom templates added as cartridges by Infobox.
System.out.println("Getting list of templates...");
arrayResponse = environmentService.getTemplates(PLATFORM_APPID, session, "<templates_type>", false);
System.out.println("GetTemplates response: " + arrayResponse);
if (!arrayResponse.isOK()) {
System.exit(arrayResponse.getResult());
}
5. In the next block, we determine the necessary configuration and settings for your new environment and the server that it will contain. You can read more about JSON parameters used in Jelastic manifests to determine the topology of the environment here .
JSONObject env = new JSONObject()
.put("ishaenabled", false)
.put("engine", "php5.5")
.put("shortdomain", ENV_NAME);
JSONObject apacheNode = new JSONObject()
.put("nodeType", "apache2")
.put("extip", false)
.put("count", 1)
.put("fixedCloudlets", 1)
.put("flexibleCloudlets", 4);
JSONObject mysqlNode = new JSONObject()
.put("nodeType", "mysql5")
.put("extip", false)
.put("fixedCloudlets", 1)
.put("flexibleCloudlets", 4);
JSONObject memcachedNode = new JSONObject()
.put("nodeType", "memcached");
JSONArray nodes = new JSONArray()
.put(apacheNode)
.put(mysqlNode)
.put(memcachedNode);
6. And finally, we initiate the creation of a new environment:
System.out.println("Creating environment...");
ScriptEvalResponse scriptEvalResponse = environmentService.createEnvironment(PLATFORM_APPID, session,
"createenv", env.toString(), nodes.toString());
System.out.println("CreateEnvironment response: " + scriptEvalResponse);

Our environment has been successfully created.

This way you can automate the creation of various environments. You can also find other examples of using the Jelastic Java Client Library in Jelastic Java Samples .
package com.infobox.playground;
import com.jelastic.api.development.response.ScriptEvalResponse;
import com.jelastic.api.development.response.interfaces.ArrayResponse;
import com.jelastic.api.environment.Environment;
import com.jelastic.api.users.Authentication;
import com.jelastic.api.users.response.AuthenticationResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Random;
publicclassApp{
privatefinalstatic String PLATFORM_APPID = "1dd8d191d38fff45e62564fcf67fdcd6";
privatefinalstatic String HOSTER_URL = "https://app.jelasticloud.com";
privatefinalstatic String USER_EMAIL = "<login>";
privatefinalstatic String USER_PASSWORD = "<password>";
privatefinalstatic String ENV_NAME = "test-api-environment-" + new Random().nextInt(100);
privatestatic Authentication authenticationService;
privatestatic Environment environmentService;
static {
authenticationService = new Authentication(PLATFORM_APPID);
authenticationService.setServerUrl(HOSTER_URL + "/1.0/");
environmentService = new Environment(PLATFORM_APPID);
environmentService.setServerUrl(HOSTER_URL + "/1.0/");
}
publicstaticvoidmain(String[] args){
/**
* Authentication
* To get started you need to specify the login and password of your Jelastic account in order to authenticate.
* After this a unique session will be created for performing the required actions within user account.
* The further calling of the API functions should be performed with the received session value.
* This session is valid until the Signout method is called.
*/
System.out.println("Authenticate user...");
AuthenticationResponse authenticationResponse = authenticationService.signin(USER_EMAIL, USER_PASSWORD);
System.out.println("Signin response: " + authenticationResponse);
if (!authenticationResponse.isOK()) {
System.exit(authenticationResponse.getResult());
}
final String session = authenticationResponse.getSession();
/**
* Get the list of all available engines available for the required engine type (java, php, ruby, js etc.).
*/
System.out.println("Getting list of engines...");
ArrayResponse arrayResponse = environmentService.getEngineList(PLATFORM_APPID, session, "php");
System.out.println("GetEngineList response: " + arrayResponse);
if (!arrayResponse.isOK()) {
System.exit(arrayResponse.getResult());
}
/**
* Get the list of available node templates according to the specified type (ALL, NATIVE or CARTRIDGE).
*/
System.out.println("Getting list of templates...");
arrayResponse = environmentService.getTemplates(PLATFORM_APPID, session, "NATIVE", false);
System.out.println("GetTemplates response: " + arrayResponse);
if (!arrayResponse.isOK()) {
System.exit(arrayResponse.getResult());
}
JSONObject env = new JSONObject()
.put("ishaenabled", false)
.put("engine", "php5.5")
.put("shortdomain", ENV_NAME);
JSONObject apacheNode = new JSONObject()
.put("nodeType", "apache2")
.put("extip", false)
.put("count", 1)
.put("fixedCloudlets", 1)
.put("flexibleCloudlets", 4);
JSONObject mysqlNode = new JSONObject()
.put("nodeType", "mysql5")
.put("extip", false)
.put("fixedCloudlets", 1)
.put("flexibleCloudlets", 4);
JSONObject memcachedNode = new JSONObject()
.put("nodeType", "memcached");
JSONArray nodes = new JSONArray()
.put(apacheNode)
.put(mysqlNode)
.put(memcachedNode);
/**
* Create a new environment with all required settings.
*/
System.out.println("Creating environment...");
ScriptEvalResponse scriptEvalResponse = environmentService.createEnvironment(PLATFORM_APPID, session, "createenv", env.toString(), nodes.toString());
System.out.println("CreateEnvironment response: " + scriptEvalResponse);
}
}
Try the Jelastic Cloud API now and enjoy the ease of development with Infobox Jelastic ! Get 300 rubles to your Infobox Jelastic account for experiments (if you haven’t received them from our other articles). To receive the bonus, send your Jelastic login to [email protected] .