Commit b73350c3 authored by Abmar Barros's avatar Abmar Barros
Browse files

first bits on FogbowClient

parent 1707c41c
/target
.project
.settings/
.classpath
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fogbow4j</groupId>
<artifactId>fogbow4j</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.fogbowcloud.cli.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.fogbowcloud</groupId>
<artifactId>manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>
package org.fogbowcloud.cli;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.fogbowcloud.manager.occi.core.OCCIHeaders;
import org.fogbowcloud.manager.occi.request.RequestAttribute;
import org.fogbowcloud.manager.occi.request.RequestConstants;
import org.fogbowcloud.manager.occi.request.RequestType;
public class FogbowClient {
private static final String FOGBOW_PUBLIC_ADDRESS_TERM = "org.fogbowcloud.request.ssh-public-address";
private static final String FOGBOW_INSTANCE_ID_TERM = "org.fogbowcloud.request.instance-id";
private static final String X_OCCI_ATTRIBUTE = "X-OCCI-Attribute: ";
private String managerHost;
private Integer managerPort;
private HttpWrapper httpWrapper;
public FogbowClient(String managerHost, Integer managerPort) {
this.managerHost = managerHost;
this.managerPort = managerPort;
this.httpWrapper = new HttpWrapper();
}
private String getManagerURL() {
return managerHost + ":" + managerPort;
}
public String createRequest(int minCPU, int minRAM,
String imageName, String pubKeyFilePath,
String federationAuthToken, String localAuthToken) throws Exception {
return createRequests(1, true, null, "Glue2vCPU >= " + minCPU + " Glue2RAM >= " + minRAM,
imageName, pubKeyFilePath, federationAuthToken, localAuthToken);
}
public String createRequest(String requirements,
String imageName, String pubKeyFilePath,
String federationAuthToken, String localAuthToken) throws Exception {
return createRequests(1, true, null, requirements,
imageName, pubKeyFilePath, federationAuthToken, localAuthToken);
}
public String createRequests(int instanceCount, boolean isOneTime, String flavour,
String requirements, String imageName, String pubKeyFilePath,
String federationAuthToken, String localAuthToken) throws Exception {
String pubKey = null;
if (pubKeyFilePath != null) {
pubKey = IOUtils.toString(new FileInputStream(pubKeyFilePath));
}
List<Header> headers = new LinkedList<Header>();
headers.add(new BasicHeader("Category", RequestConstants.TERM
+ "; scheme=\"" + RequestConstants.SCHEME + "\"; class=\""
+ RequestConstants.KIND_CLASS + "\""));
headers.add(new BasicHeader("X-OCCI-Attribute",
RequestAttribute.INSTANCE_COUNT.getValue() + "=" + instanceCount));
headers.add(new BasicHeader("X-OCCI-Attribute", RequestAttribute.TYPE.getValue() + "="
+ (isOneTime ? RequestType.ONE_TIME.getValue() : RequestType.PERSISTENT.getValue() )));
if (requirements != null) {
headers.add(new BasicHeader("X-OCCI-Attribute",
RequestAttribute.REQUIREMENTS + "=" + requirements));
}
if (flavour != null) {
headers.add(new BasicHeader("Category", flavour + "; "
+ "scheme=\"" + RequestConstants.TEMPLATE_RESOURCE_SCHEME + "\"; "
+ "class=\"" + RequestConstants.MIXIN_CLASS + "\""));
}
headers.add(new BasicHeader("Category", imageName + "; "
+ "scheme=\"" + RequestConstants.TEMPLATE_OS_SCHEME + "\"; "
+ "class=\"" + RequestConstants.MIXIN_CLASS + "\""));
if (pubKey != null && !pubKey.isEmpty()) {
headers.add(new BasicHeader("Category",
RequestConstants.PUBLIC_KEY_TERM + "; scheme=\""
+ RequestConstants.CREDENTIALS_RESOURCE_SCHEME
+ "\"; class=\"" + RequestConstants.MIXIN_CLASS
+ "\""));
headers.add(new BasicHeader("X-OCCI-Attribute",
RequestAttribute.DATA_PUBLIC_KEY.getValue() + "=" + pubKey));
}
headers.add(new BasicHeader(OCCIHeaders.X_LOCAL_AUTH_TOKEN, localAuthToken));
String response = httpWrapper.doRequest("post",
getManagerURL() + "/" + RequestConstants.TERM,
federationAuthToken, headers);
return getRequestId(response);
}
public Request getRequest(String requestId, String federationAuthToken) throws Exception {
String getRequestResponse = httpWrapper.doRequest("get",
getManagerURL() + "/" + RequestConstants.TERM + "/" + requestId,
federationAuthToken, new LinkedList<Header>());
Map<String, String> attrs = parseAttributes(getRequestResponse);
String instanceId = attrs.get(FOGBOW_INSTANCE_ID_TERM);
return new Request(instanceId);
}
public Instance getInstance(String instanceId, String federationAuthToken) throws Exception {
String instanceInfoStr = httpWrapper.doRequest(
"get", getManagerURL() + "/compute/" + instanceId,
federationAuthToken, new LinkedList<Header>());
Map<String, String> attrs = parseAttributes(instanceInfoStr);
String publicAddress = attrs.get(FOGBOW_PUBLIC_ADDRESS_TERM);
if (publicAddress == null) {
return new Instance(null, null);
}
String[] publicAddressSplit = publicAddress.split(":");
return new Instance(publicAddressSplit[0], Integer.parseInt(publicAddressSplit[1]));
}
private static String getRequestId(String createRequestResponse) {
String[] requestRes = createRequestResponse.split(":");
String[] requestId = requestRes[requestRes.length - 1].split("/");
return requestId[requestId.length - 1];
}
private static Map<String, String> parseAttributes(String response) {
Map<String, String> atts = new HashMap<String, String>();
for (String responseLine : response.split("\n")) {
if (responseLine.contains(X_OCCI_ATTRIBUTE)) {
String[] responseLineSplit = responseLine.substring(
X_OCCI_ATTRIBUTE.length()).split("=");
String value = responseLineSplit[1]
.trim().replace("\"", "");
if (value.equals("null")) {
continue;
}
atts.put(responseLineSplit[0].trim(), value);
}
}
return atts;
}
}
package org.fogbowcloud.cli;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.fogbowcloud.manager.occi.core.HeaderUtils;
import org.fogbowcloud.manager.occi.core.OCCIHeaders;
import org.fogbowcloud.manager.occi.request.RequestConstants;
public class HttpWrapper {
private static HttpClient createHttpClient() {
return HttpClients.createMinimal();
}
public String doRequest(String method, String endpoint, String federationAuthToken,
List<Header> additionalHeaders) throws Exception {
HttpUriRequest request = null;
if (method.equals("get")) {
request = new HttpGet(endpoint);
} else if (method.equals("delete")) {
request = new HttpDelete(endpoint);
} else if (method.equals("post")) {
request = new HttpPost(endpoint);
}
request.addHeader(OCCIHeaders.CONTENT_TYPE, OCCIHeaders.OCCI_CONTENT_TYPE);
request.addHeader(OCCIHeaders.X_FEDERATION_AUTH_TOKEN, federationAuthToken);
for (Header header : additionalHeaders) {
request.addHeader(header);
}
HttpResponse response = createHttpClient().execute(request);
HttpEntity entity = null;
try {
entity = response.getEntity();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK
|| response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
Header locationHeader = getLocationHeader(response
.getAllHeaders());
if (locationHeader != null
&& locationHeader.getValue().contains(
RequestConstants.TERM)) {
return generateLocationHeaderResponse(locationHeader);
} else {
return EntityUtils.toString(response.getEntity());
}
} else {
return response.getStatusLine().toString();
}
} finally {
try {
if (entity != null) {
EntityUtils.toString(entity);
}
} catch (Exception e) {
// Do nothing
}
}
}
protected static Header getLocationHeader(Header[] headers) {
Header locationHeader = null;
for (Header header : headers) {
if (header.getName().equals("Location")) {
locationHeader = header;
}
}
return locationHeader;
}
protected static String generateLocationHeaderResponse(Header header) {
String[] locations = header.getValue().split(",");
String response = "";
for (String location : locations) {
response += HeaderUtils.X_OCCI_LOCATION_PREFIX + location + "\n";
}
return response.trim();
}
}
package org.fogbowcloud.cli;
public class Instance {
private String sshHost;
private Integer sshPort;
public Instance(String sshHost, Integer sshPort) {
this.sshHost = sshHost;
this.sshPort = sshPort;
}
public String getSshHost() {
return sshHost;
}
public Integer getSshPort() {
return sshPort;
}
}
package org.fogbowcloud.cli;
public class Request {
private String instanceId;
public Request(String instanceId) {
this.instanceId = instanceId;
}
public String getInstanceId() {
return instanceId;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment