use of org.apache.commons.httpclient.HttpMethod in project pinot by linkedin.
the class WebHdfsV1Client method uploadSegment.
// This method is based on:
// https://hadoop.apache.org/docs/r1.0.4/webhdfs.html#CREATE
public synchronized boolean uploadSegment(String webHdfsPath, String localFilePath) {
// Step 1: Submit a HTTP PUT request without automatically following
// redirects and without sending the file data.
String firstPutReqString = String.format(WEB_HDFS_UPLOAD_PATH_TEMPLATE, _protocol, _host, _port, webHdfsPath, _overwrite, _permission);
HttpMethod firstPutReq = new PutMethod(firstPutReqString);
try {
LOGGER.info("Trying to send request: {}.", firstPutReqString);
int firstResponseCode = _httpClient.executeMethod(firstPutReq);
if (firstResponseCode != 307) {
LOGGER.error(String.format("Failed to execute the first PUT request to upload segment to webhdfs: %s. " + "Expected response code 307, but get %s. Response body: %s", firstPutReqString, firstResponseCode, firstPutReq.getResponseBodyAsString()));
return false;
}
} catch (Exception e) {
LOGGER.error(String.format("Failed to execute the first request to upload segment to webhdfs: %s.", firstPutReqString), e);
return false;
} finally {
firstPutReq.releaseConnection();
}
// Step 2: Submit another HTTP PUT request using the URL in the Location
// header with the file data to be written.
String redirectedReqString = firstPutReq.getResponseHeader(LOCATION).getValue();
PutMethod redirectedReq = new PutMethod(redirectedReqString);
File localFile = new File(localFilePath);
RequestEntity requestEntity = new FileRequestEntity(localFile, "application/binary");
redirectedReq.setRequestEntity(requestEntity);
try {
LOGGER.info("Trying to send request: {}.", redirectedReqString);
int redirectedResponseCode = _httpClient.executeMethod(redirectedReq);
if (redirectedResponseCode != 201) {
LOGGER.error(String.format("Failed to execute the redirected PUT request to upload segment to webhdfs: %s. " + "Expected response code 201, but get %s. Response: %s", redirectedReqString, redirectedResponseCode, redirectedReq.getResponseBodyAsString()));
}
return true;
} catch (IOException e) {
LOGGER.error(String.format("Failed to execute the redirected request to upload segment to webhdfs: %s.", redirectedReqString), e);
return false;
} finally {
redirectedReq.releaseConnection();
}
}
use of org.apache.commons.httpclient.HttpMethod in project CloudStack-archive by CloudStack-extras.
the class StressTestDirectAttach method executeDeployment.
private static Integer executeDeployment(String server, String developerServer, String username) throws HttpException, IOException {
// test steps:
// - create user
// - deploy Windows VM
// - deploy Linux VM
// - associate IP address
// - create two IP forwarding rules
// - create load balancer rule
// - list IP forwarding rules
// - list load balancer rules
// -----------------------------
// CREATE USER
// -----------------------------
String encodedUsername = URLEncoder.encode(username, "UTF-8");
String encryptedPassword = createMD5Password(username);
String encodedPassword = URLEncoder.encode(encryptedPassword, "UTF-8");
String url = server + "?command=createUser&username=" + encodedUsername + "&password=" + encodedPassword + "&firstname=Test&lastname=Test&email=test@vmops.com&domainId=1&accounttype=0";
if (accountName != null) {
url = server + "?command=createUser&username=" + encodedUsername + "&password=" + encodedPassword + "&firstname=Test&lastname=Test&email=test@vmops.com&domainId=1&accounttype=0&account=" + accountName;
}
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
int responseCode = client.executeMethod(method);
long userId = -1;
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> userIdValues = getSingleValueFromXML(is, new String[] { "id", "account" });
String userIdStr = userIdValues.get("id");
s_logger.info("created user " + username + " with id " + userIdStr);
if (userIdStr != null) {
userId = Long.parseLong(userIdStr);
_userId.set(userId);
_account.set(userIdValues.get("account"));
if (userId == -1) {
s_logger.error("create user (" + username + ") failed to retrieve a valid user id, aborting depolyment test");
return -1;
}
}
} else {
s_logger.error("create user test failed for user " + username + " with error code :" + responseCode);
return responseCode;
}
_secretKey.set(executeRegistration(server, username, username));
if (_secretKey.get() == null) {
s_logger.error("FAILED to retrieve secret key during registration, skipping user: " + username);
return -1;
} else {
s_logger.info("got secret key: " + _secretKey.get());
s_logger.info("got api key: " + _apiKey.get());
}
// ---------------------------------
// CREATE NETWORK GROUP AND ADD INGRESS RULE TO IT
// ---------------------------------
String networkAccount = null;
if (accountName != null) {
networkAccount = accountName;
} else {
networkAccount = encodedUsername;
}
String encodedApiKey = URLEncoder.encode(_apiKey.get(), "UTF-8");
String requestToSign = "apikey=" + encodedApiKey + "&command=createSecurityGroup&name=" + encodedUsername;
requestToSign = requestToSign.toLowerCase();
String signature = signRequest(requestToSign, _secretKey.get());
String encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=createSecurityGroup&name=" + encodedUsername + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> values = getSingleValueFromXML(is, new String[] { "id" });
if (values.get("id") == null) {
s_logger.info("Create network rule response code: 401");
return 401;
} else {
s_logger.info("Create security group response code: " + responseCode);
}
} else {
s_logger.error("Create security group failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
String encodedCidr = URLEncoder.encode("192.168.1.143/32", "UTF-8");
url = server + "?command=authorizeSecurityGroupIngress&cidrlist=" + encodedCidr + "&endport=22&" + "securitygroupname=" + encodedUsername + "&protocol=tcp&startport=22&account=" + networkAccount + "&domainid=1";
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });
if (values.get("id") == null) {
s_logger.info("Authorise security group ingress response code: 401");
return 401;
} else {
s_logger.info("Authorise security group ingress response code: " + responseCode);
}
} else {
s_logger.error("Authorise security group ingress failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// ---------------------------------
// DEPLOY LINUX VM
// ---------------------------------
{
long templateId = 2;
String encodedZoneId = URLEncoder.encode("" + zoneId, "UTF-8");
String encodedServiceOfferingId = URLEncoder.encode("" + serviceOfferingId, "UTF-8");
String encodedTemplateId = URLEncoder.encode("" + templateId, "UTF-8");
encodedApiKey = URLEncoder.encode(_apiKey.get(), "UTF-8");
requestToSign = "apikey=" + encodedApiKey + "&command=deployVirtualMachine&securitygrouplist=" + encodedUsername + "&serviceofferingid=" + encodedServiceOfferingId + "&templateid=" + encodedTemplateId + "&zoneid=" + encodedZoneId;
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=deployVirtualMachine&securitygrouplist=" + encodedUsername + "&zoneid=" + encodedZoneId + "&serviceofferingid=" + encodedServiceOfferingId + "&templateid=" + encodedTemplateId + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id", "ipaddress" });
if ((values.get("ipaddress") == null) || (values.get("id") == null)) {
s_logger.info("deploy linux vm response code: 401");
return 401;
} else {
s_logger.info("deploy linux vm response code: " + responseCode);
long linuxVMId = Long.parseLong(values.get("id"));
s_logger.info("got linux virtual machine id: " + linuxVMId);
_linuxVmId.set(values.get("id"));
_linuxIP.set(values.get("ipaddress"));
_linuxPassword.set("rs-ccb35ea5");
}
} else {
s_logger.error("deploy linux vm failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
//Create a new volume
{
url = server + "?command=createVolume&diskofferingid=" + diskOfferingId + "&zoneid=" + zoneId + "&name=newvolume&account=" + _account.get() + "&domainid=1";
s_logger.info("Creating volume....");
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });
if (values.get("id") == null) {
s_logger.info("create volume response code: 401");
return 401;
} else {
s_logger.info("create volume response code: " + responseCode);
String volumeId = values.get("id");
s_logger.info("got volume id: " + volumeId);
_newVolume.set(volumeId);
}
} else {
s_logger.error("create volume failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
//attach a new volume to the vm
{
url = server + "?command=attachVolume&id=" + _newVolume.get() + "&virtualmachineid=" + _linuxVmId.get();
s_logger.info("Attaching volume with id " + _newVolume.get() + " to the vm " + _linuxVmId.get());
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Attach data volume response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });
if (values.get("id") == null) {
s_logger.info("Attach volume response code: 401");
return 401;
} else {
s_logger.info("Attach volume response code: " + responseCode);
}
} else {
s_logger.error("Attach volume failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
//DEPLOY SECOND VM, ADD VOLUME TO IT
// ---------------------------------
// DEPLOY another linux vm
// ---------------------------------
{
long templateId = 2;
String encodedZoneId = URLEncoder.encode("" + zoneId, "UTF-8");
String encodedServiceOfferingId = URLEncoder.encode("" + serviceOfferingId, "UTF-8");
String encodedTemplateId = URLEncoder.encode("" + templateId, "UTF-8");
encodedApiKey = URLEncoder.encode(_apiKey.get(), "UTF-8");
requestToSign = "apikey=" + encodedApiKey + "&command=deployVirtualMachine&securitygrouplist=" + encodedUsername + "&serviceofferingid=" + encodedServiceOfferingId + "&templateid=" + encodedTemplateId + "&zoneid=" + encodedZoneId;
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=deployVirtualMachine&securitygrouplist=" + encodedUsername + "&zoneid=" + encodedZoneId + "&serviceofferingid=" + encodedServiceOfferingId + "&templateid=" + encodedTemplateId + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id", "ipaddress" });
if ((values.get("ipaddress") == null) || (values.get("id") == null)) {
s_logger.info("deploy linux vm response code: 401");
return 401;
} else {
s_logger.info("deploy linux vm response code: " + responseCode);
long linuxVMId = Long.parseLong(values.get("id"));
s_logger.info("got linux virtual machine id: " + linuxVMId);
_linuxVmId1.set(values.get("id"));
}
} else {
s_logger.error("deploy linux vm failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
//Create a new volume
{
url = server + "?command=createVolume&diskofferingid=" + diskOfferingId1 + "&zoneid=" + zoneId + "&name=newvolume1&account=" + _account.get() + "&domainid=1";
s_logger.info("Creating volume....");
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });
if (values.get("id") == null) {
s_logger.info("create volume response code: 401");
return 401;
} else {
s_logger.info("create volume response code: " + responseCode);
String volumeId = values.get("id");
s_logger.info("got volume id: " + volumeId);
_newVolume1.set(volumeId);
}
} else {
s_logger.error("create volume failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
//attach a new volume to the vm
{
url = server + "?command=attachVolume&id=" + _newVolume1.get() + "&virtualmachineid=" + _linuxVmId1.get();
s_logger.info("Attaching volume with id " + _newVolume1.get() + " to the vm " + _linuxVmId1.get());
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Attach data volume response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });
if (values.get("id") == null) {
s_logger.info("Attach volume response code: 401");
return 401;
} else {
s_logger.info("Attach volume response code: " + responseCode);
}
} else {
s_logger.error("Attach volume failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
return 200;
}
use of org.apache.commons.httpclient.HttpMethod in project CloudStack-archive by CloudStack-extras.
the class TestClientWithAPI method getNetworkStat.
private static boolean getNetworkStat(String server) {
try {
String url = server + "?command=listAccountStatistics&account=" + _account.get();
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
int responseCode = client.executeMethod(method);
s_logger.info("listAccountStatistics response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> requestKeyValues = getSingleValueFromXML(is, new String[] { "receivedbytes", "sentbytes" });
int bytesReceived = Integer.parseInt(requestKeyValues.get("receivedbytes"));
int bytesSent = Integer.parseInt(requestKeyValues.get("sentbytes"));
if ((bytesReceived > 100000000) && (bytesSent > 0)) {
s_logger.info("Network stat is correct for account" + _account.get() + "; bytest received is " + bytesReceived + " and bytes sent is " + bytesSent);
return true;
} else {
s_logger.error("Incorrect value for bytes received/sent for the account " + _account.get() + ". We got " + bytesReceived + " bytes received; " + " and " + bytesSent + " bytes sent");
return false;
}
} else {
s_logger.error("listAccountStatistics failed with error code: " + responseCode + ". Following URL was sent: " + url);
return false;
}
} catch (Exception ex) {
s_logger.error("Exception while sending command listAccountStatistics");
return false;
}
}
use of org.apache.commons.httpclient.HttpMethod in project CloudStack-archive by CloudStack-extras.
the class TestClientWithAPI method executeCleanup.
private static int executeCleanup(String server, String developerServer, String username) throws HttpException, IOException {
// test steps:
// - get user
// - delete user
// -----------------------------
// GET USER
// -----------------------------
String userId = _userId.get().toString();
String encodedUserId = URLEncoder.encode(userId, "UTF-8");
String url = server + "?command=listUsers&id=" + encodedUserId;
s_logger.info("Cleaning up resources for user: " + userId + " with url " + url);
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
int responseCode = client.executeMethod(method);
s_logger.info("get user response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> userInfo = getSingleValueFromXML(is, new String[] { "username", "id", "account" });
if (!username.equals(userInfo.get("username"))) {
s_logger.error("get user failed to retrieve requested user, aborting cleanup test" + ". Following URL was sent: " + url);
return -1;
}
} else {
s_logger.error("get user failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// -----------------------------
// UPDATE USER
// -----------------------------
{
url = server + "?command=updateUser&id=" + userId + "&firstname=delete&lastname=me";
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("update user response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> success = getSingleValueFromXML(is, new String[] { "success" });
s_logger.info("update user..success? " + success.get("success"));
} else {
s_logger.error("update user failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
// -----------------------------
// Detach existin dataVolume, create a new volume, attach it to the vm
// -----------------------------
{
url = server + "?command=listVolumes&virtualMachineId=" + _linuxVmId.get() + "&type=dataDisk";
s_logger.info("Getting dataDisk id of Centos vm");
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("List volumes response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> success = getSingleValueFromXML(is, new String[] { "id" });
s_logger.info("Got dataDiskVolume with id " + success.get("id"));
_dataVolume.set(success.get("id"));
} else {
s_logger.error("List volumes failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
// Detach volume
{
url = server + "?command=detachVolume&id=" + _dataVolume.get();
s_logger.info("Detaching volume with id " + _dataVolume.get());
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Detach data volume response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
s_logger.info("The volume was detached successfully");
} else {
s_logger.error("Detach data disk failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
// Delete a volume
{
url = server + "?command=deleteVolume&id=" + _dataVolume.get();
s_logger.info("Deleting volume with id " + _dataVolume.get());
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Delete data volume response code: " + responseCode);
if (responseCode == 200) {
s_logger.info("The volume was deleted successfully");
} else {
s_logger.error("Delete volume failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
// Create a new volume
{
url = server + "?command=createVolume&diskofferingid=" + diskOfferingId + "&zoneid=" + zoneId + "&name=newvolume&account=" + _account.get() + "&domainid=1";
s_logger.info("Creating volume....");
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });
if (values.get("id") == null) {
s_logger.info("create volume response code: 401");
return 401;
} else {
s_logger.info("create volume response code: " + responseCode);
long volumeId = Long.parseLong(values.get("id"));
s_logger.info("got volume id: " + volumeId);
_newVolume.set(values.get("id"));
}
} else {
s_logger.error("create volume failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
// attach a new volume to the vm
{
url = server + "?command=attachVolume&id=" + _newVolume.get() + "&virtualmachineid=" + _linuxVmId.get();
s_logger.info("Attaching volume with id " + _newVolume.get() + " to the vm " + _linuxVmId.get());
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Attach data volume response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
s_logger.info("The volume was attached successfully");
} else {
s_logger.error("Attach volume failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
// Create a snapshot
// list volumes
{
url = server + "?command=listVolumes&virtualMachineId=" + _linuxVmId.get() + "&type=root";
s_logger.info("Getting rootDisk id of Centos vm");
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("List volumes response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> success = getSingleValueFromXML(is, new String[] { "id" });
if (success.get("id") == null) {
s_logger.error("Unable to get root volume. Followin url was sent: " + url);
}
s_logger.info("Got rootVolume with id " + success.get("id"));
_rootVolume.set(success.get("id"));
} else {
s_logger.error("List volumes failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
// //Create snapshot from root disk volume
String encodedApiKey = URLEncoder.encode(_apiKey.get(), "UTF-8");
String requestToSign = "apikey=" + encodedApiKey + "&command=createSnapshot&volumeid=" + _rootVolume.get();
requestToSign = requestToSign.toLowerCase();
String signature = signRequest(requestToSign, _secretKey.get());
String encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=createSnapshot&volumeid=" + _rootVolume.get() + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Create snapshot response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });
if (values.get("id") == null) {
s_logger.info("create snapshot response code: 401");
return 401;
} else {
s_logger.info("create snapshot response code: " + responseCode + ". Got snapshot with id " + values.get("id"));
_snapshot.set(values.get("id"));
}
} else {
s_logger.error("create snapshot failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// Create volume from the snapshot created on the previous step and attach it to the running vm
/* encodedApiKey = URLEncoder.encode(_apiKey.get(), "UTF-8");
requestToSign = "apikey=" + encodedApiKey + "&command=createVolume&name=" + _account.get() + "&snapshotid=" + _snapshot.get();
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=createVolume&name=" + _account.get() + "&snapshotid=" + _snapshot.get() + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Create volume from snapshot response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });
if (values.get("id") == null) {
s_logger.info("create volume from snapshot response code: 401");
return 401;
} else {
s_logger.info("create volume from snapshot response code: " + responseCode + ". Got volume with id " + values.get("id") + ". The command was sent with url " + url);
_volumeFromSnapshot.set(values.get("id"));
}
} else {
s_logger.error("create volume from snapshot failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
{
url = server + "?command=attachVolume&id=" + _volumeFromSnapshot.get() + "&virtualmachineid=" + _linuxVmId.get();
s_logger.info("Attaching volume with id " + _volumeFromSnapshot.get() + " to the vm " + _linuxVmId.get());
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Attach volume from snapshot to linux vm response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
s_logger.info("The volume created from snapshot was attached successfully to linux vm");
} else {
s_logger.error("Attach volume created from snapshot failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
*/
// -----------------------------
// Execute reboot/stop/start commands for the VMs before deleting the account - made to exercise xen
// -----------------------------
// Reboot windows VM
requestToSign = "apikey=" + encodedApiKey + "&command=rebootVirtualMachine&id=" + _windowsVmId.get();
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=rebootVirtualMachine&id=" + _windowsVmId.get() + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Reboot windows Vm response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> success = getSingleValueFromXML(el, new String[] { "success" });
s_logger.info("Windows VM was rebooted with the status: " + success.get("success"));
} else {
s_logger.error("Reboot windows VM test failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// Stop centos VM
requestToSign = "apikey=" + encodedApiKey + "&command=stopVirtualMachine&id=" + _linuxVmId.get();
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=stopVirtualMachine&id=" + _linuxVmId.get() + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Stop linux Vm response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> success = getSingleValueFromXML(el, new String[] { "success" });
s_logger.info("Linux VM was stopped with the status: " + success.get("success"));
} else {
s_logger.error("Stop linux VM test failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// Create private template from root disk volume
requestToSign = "apikey=" + encodedApiKey + "&command=createTemplate" + "&displaytext=" + _account.get() + "&name=" + _account.get() + "&ostypeid=11" + "&snapshotid=" + _snapshot.get();
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=createTemplate" + "&displaytext=" + _account.get() + "&name=" + _account.get() + "&ostypeid=11" + "&snapshotid=" + _snapshot.get() + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Create private template response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });
if (values.get("id") == null) {
s_logger.info("create private template response code: 401");
return 401;
} else {
s_logger.info("create private template response code: " + responseCode);
}
} else {
s_logger.error("create private template failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// Start centos VM
requestToSign = "apikey=" + encodedApiKey + "&command=startVirtualMachine&id=" + _windowsVmId.get();
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=startVirtualMachine&id=" + _windowsVmId.get() + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Start linux Vm response code: " + responseCode);
if (responseCode != 200) {
s_logger.error("Start linux VM test failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// get domainRouter id
{
url = server + "?command=listRouters&zoneid=" + zoneId + "&account=" + _account.get() + "&domainid=1";
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("List domain routers response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> success = getSingleValueFromXML(is, new String[] { "id" });
s_logger.info("Got the domR with id " + success.get("id"));
_domainRouterId.set(success.get("id"));
} else {
s_logger.error("List domain routers failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
// reboot the domain router
{
url = server + "?command=rebootRouter&id=" + _domainRouterId.get();
s_logger.info("Rebooting domR with id " + _domainRouterId.get());
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Reboot domain router response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
s_logger.info("Domain router was rebooted successfully");
} else {
s_logger.error("Reboot domain routers failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
// -----------------------------
// DELETE ACCOUNT
// -----------------------------
{
url = server + "?command=deleteAccount&id=" + _accountId.get();
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("delete account response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
s_logger.info("Deleted account successfully");
} else {
s_logger.error("delete account failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
return responseCode;
}
use of org.apache.commons.httpclient.HttpMethod in project CloudStack-archive by CloudStack-extras.
the class TestClientWithAPI method executeDeployment.
private static Integer executeDeployment(String server, String developerServer, String username, String snapshot_test) throws HttpException, IOException {
// test steps:
// - create user
// - deploy Windows VM
// - deploy Linux VM
// - associate IP address
// - create two IP forwarding rules
// - create load balancer rule
// - list IP forwarding rules
// - list load balancer rules
// -----------------------------
// CREATE ACCOUNT
// -----------------------------
String encodedUsername = URLEncoder.encode(username, "UTF-8");
String encryptedPassword = createMD5Password(username);
String encodedPassword = URLEncoder.encode(encryptedPassword, "UTF-8");
String url = server + "?command=createAccount&username=" + encodedUsername + "&account=" + encodedUsername + "&password=" + encodedPassword + "&firstname=Test&lastname=Test&email=test@vmops.com&domainId=1&accounttype=0";
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
int responseCode = client.executeMethod(method);
long accountId = -1;
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> accountValues = getSingleValueFromXML(is, new String[] { "id", "name" });
String accountIdStr = accountValues.get("id");
s_logger.info("created account " + username + " with id " + accountIdStr);
if (accountIdStr != null) {
accountId = Long.parseLong(accountIdStr);
_accountId.set(accountId);
_account.set(accountValues.get("name"));
if (accountId == -1) {
s_logger.error("create account (" + username + ") failed to retrieve a valid user id, aborting depolyment test");
return -1;
}
}
} else {
s_logger.error("create account test failed for account " + username + " with error code :" + responseCode + ", aborting deployment test. The command was sent with url " + url);
return -1;
}
// LIST JUST CREATED USER TO GET THE USER ID
url = server + "?command=listUsers&username=" + encodedUsername + "&account=" + encodedUsername + "&domainId=1";
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
long userId = -1;
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> userIdValues = getSingleValueFromXML(is, new String[] { "id" });
String userIdStr = userIdValues.get("id");
s_logger.info("listed user " + username + " with id " + userIdStr);
if (userIdStr != null) {
userId = Long.parseLong(userIdStr);
_userId.set(userId);
if (userId == -1) {
s_logger.error("list user by username " + username + ") failed to retrieve a valid user id, aborting depolyment test");
return -1;
}
}
} else {
s_logger.error("list user test failed for account " + username + " with error code :" + responseCode + ", aborting deployment test. The command was sent with url " + url);
return -1;
}
_secretKey.set(executeRegistration(server, username, username));
if (_secretKey.get() == null) {
s_logger.error("FAILED to retrieve secret key during registration, skipping user: " + username);
return -1;
} else {
s_logger.info("got secret key: " + _secretKey.get());
s_logger.info("got api key: " + _apiKey.get());
}
// ---------------------------------
// CREATE VIRTUAL NETWORK
// ---------------------------------
url = server + "?command=createNetwork&networkofferingid=" + networkOfferingId + "&account=" + encodedUsername + "&domainId=1" + "&zoneId=" + zoneId + "&name=virtualnetwork-" + encodedUsername + "&displaytext=virtualnetwork-" + encodedUsername;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> networkValues = getSingleValueFromXML(is, new String[] { "id" });
String networkIdStr = networkValues.get("id");
s_logger.info("Created virtual network with name virtualnetwork-" + encodedUsername + " and id " + networkIdStr);
if (networkIdStr != null) {
_networkId.set(networkIdStr);
}
} else {
s_logger.error("Create virtual network failed for account " + username + " with error code :" + responseCode + ", aborting deployment test. The command was sent with url " + url);
return -1;
}
/*
// ---------------------------------
// CREATE DIRECT NETWORK
// ---------------------------------
url = server + "?command=createNetwork&networkofferingid=" + networkOfferingId_dir + "&account=" + encodedUsername + "&domainId=1" + "&zoneId=" + zoneId + "&name=directnetwork-" + encodedUsername + "&displaytext=directnetwork-" + encodedUsername;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> networkValues = getSingleValueFromXML(is, new String[] { "id" });
String networkIdStr = networkValues.get("id");
s_logger.info("Created direct network with name directnetwork-" + encodedUsername + " and id " + networkIdStr);
if (networkIdStr != null) {
_networkId_dir.set(networkIdStr);
}
} else {
s_logger.error("Create direct network failed for account " + username + " with error code :" + responseCode + ", aborting deployment test. The command was sent with url " + url);
return -1;
}
*/
// ---------------------------------
// DEPLOY LINUX VM
// ---------------------------------
String linuxVMPrivateIP = null;
{
// long templateId = 3;
long templateId = 4;
String encodedZoneId = URLEncoder.encode("" + zoneId, "UTF-8");
String encodedServiceOfferingId = URLEncoder.encode("" + serviceOfferingId, "UTF-8");
String encodedTemplateId = URLEncoder.encode("" + templateId, "UTF-8");
String encodedApiKey = URLEncoder.encode(_apiKey.get(), "UTF-8");
String encodedNetworkIds = URLEncoder.encode(_networkId.get() + ",206", "UTF-8");
String requestToSign = "apikey=" + encodedApiKey + "&command=deployVirtualMachine&diskofferingid=" + diskOfferingId + "&networkids=" + encodedNetworkIds + "&serviceofferingid=" + encodedServiceOfferingId + "&templateid=" + encodedTemplateId + "&zoneid=" + encodedZoneId;
requestToSign = requestToSign.toLowerCase();
String signature = signRequest(requestToSign, _secretKey.get());
String encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=deployVirtualMachine" + "&zoneid=" + encodedZoneId + "&serviceofferingid=" + encodedServiceOfferingId + "&diskofferingid=" + diskOfferingId + "&networkids=" + encodedNetworkIds + "&templateid=" + encodedTemplateId + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id", "ipaddress" });
if ((values.get("ipaddress") == null) || (values.get("id") == null)) {
s_logger.info("deploy linux vm response code: 401, the command was sent with url " + url);
return 401;
} else {
s_logger.info("deploy linux vm response code: " + responseCode);
long linuxVMId = Long.parseLong(values.get("id"));
s_logger.info("got linux virtual machine id: " + linuxVMId);
_linuxVmId.set(values.get("id"));
linuxVMPrivateIP = values.get("ipaddress");
// _linuxPassword.set(values.get("password"));
_linuxPassword.set(vmPassword);
s_logger.info("got linux virtual machine password: " + _linuxPassword.get());
}
} else {
s_logger.error("deploy linux vm failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
{
// ---------------------------------
// ASSOCIATE IP for windows
// ---------------------------------
String ipAddr = null;
String encodedApiKey = URLEncoder.encode(_apiKey.get(), "UTF-8");
String requestToSign = "apikey=" + encodedApiKey + "&command=associateIpAddress" + "&zoneid=" + zoneId;
requestToSign = requestToSign.toLowerCase();
String signature = signRequest(requestToSign, _secretKey.get());
String encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=associateIpAddress" + "&apikey=" + encodedApiKey + "&zoneid=" + zoneId + "&signature=" + encodedSignature;
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
/*Asynchronous Job - Corresponding Changes Made*/
Element associpel = queryAsyncJobResult(server, is);
Map<String, String> values = getSingleValueFromXML(associpel, new String[] { "id", "ipaddress" });
if ((values.get("ipaddress") == null) || (values.get("id") == null)) {
s_logger.info("associate ip for Windows response code: 401, the command was sent with url " + url);
return 401;
} else {
s_logger.info("Associate IP Address response code: " + responseCode);
long publicIpId = Long.parseLong(values.get("id"));
s_logger.info("Associate IP's Id: " + publicIpId);
_publicIpId.set(values.get("id"));
}
} else {
s_logger.error("associate ip address for windows vm failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
String encodedPublicIpId = URLEncoder.encode(_publicIpId.get(), "UTF-8");
requestToSign = "apikey=" + encodedApiKey + "&command=listPublicIpAddresses" + "&id=" + encodedPublicIpId;
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=listPublicIpAddresses&apikey=" + encodedApiKey + "&id=" + encodedPublicIpId + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("url is " + url);
s_logger.info("list ip addresses for user " + userId + " response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
// InputStream ips = method.getResponseBodyAsStream();
List<String> ipAddressValues = getIPs(is, false);
// List<String> ipAddressVals = getIPs(is, false, true);
if ((ipAddressValues != null) && !ipAddressValues.isEmpty()) {
_windowsIpId.set(ipAddressValues.get(0));
_windowsIP.set(ipAddressValues.get(1));
s_logger.info("For Windows, using non-sourceNat IP address ID: " + ipAddressValues.get(0));
s_logger.info("For Windows, using non-sourceNat IP address: " + ipAddressValues.get(1));
}
} else {
s_logger.error("list ip addresses failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// ---------------------------------
// Use the SourceNat IP for linux
// ---------------------------------
{
requestToSign = "apikey=" + encodedApiKey + "&command=listPublicIpAddresses";
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=listPublicIpAddresses&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("url is " + url);
s_logger.info("list ip addresses for user " + userId + " response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
// InputStream ips = method.getResponseBodyAsStream();
List<String> ipAddressValues = getIPs(is, true);
// List<String> ipAddressVals = getIPs(is, true, true);
if ((ipAddressValues != null) && !ipAddressValues.isEmpty()) {
_linuxIpId.set(ipAddressValues.get(0));
_linuxIP.set(ipAddressValues.get(1));
s_logger.info("For linux, using sourceNat IP address ID: " + ipAddressValues.get(0));
s_logger.info("For linux, using sourceNat IP address: " + ipAddressValues.get(1));
}
} else {
s_logger.error("list ip addresses failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
//--------------------------------------------
// Enable Static NAT for the Source NAT Ip
//--------------------------------------------
String encodedSourceNatPublicIpId = URLEncoder.encode(_linuxIpId.get(), "UTF-8");
/* requestToSign = "apikey=" + encodedApiKey + "&command=enableStaticNat"+"&id=" + encodedSourceNatPublicIpId + "&virtualMachineId=" + encodedVmId;;
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=enableStaticNat&apikey=" + encodedApiKey + "&signature=" + encodedSignature + "&id=" + encodedSourceNatPublicIpId + "&virtualMachineId=" + encodedVmId;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("url is " + url);
s_logger.info("list ip addresses for user " + userId + " response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> success = getSingleValueFromXML(is, new String[] { "success" });
s_logger.info("Enable Static NAT..success? " + success.get("success"));
} else {
s_logger.error("Enable Static NAT failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
*/
// -------------------------------------------------------------
// CREATE IP FORWARDING RULE -- Linux VM
// -------------------------------------------------------------
String encodedVmId = URLEncoder.encode(_linuxVmId.get(), "UTF-8");
String encodedIpAddress = URLEncoder.encode(_linuxIpId.get(), "UTF-8");
requestToSign = "apikey=" + encodedApiKey + "&command=createPortForwardingRule&ipaddressid=" + encodedIpAddress + "&privateport=22&protocol=TCP&publicport=22" + "&virtualmachineid=" + encodedVmId;
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=createPortForwardingRule&apikey=" + encodedApiKey + "&ipaddressid=" + encodedIpAddress + "&privateport=22&protocol=TCP&publicport=22&virtualmachineid=" + encodedVmId + "&signature=" + encodedSignature;
s_logger.info("Created port forwarding rule with " + url);
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });
s_logger.info("Port forwarding rule was assigned successfully to Linux VM");
long ipfwdid = Long.parseLong(values.get("id"));
s_logger.info("got Port Forwarding Rule's Id:" + ipfwdid);
_linipfwdid.set(values.get("id"));
} else {
s_logger.error("Port forwarding rule creation failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// Create snapshot recurring policy if needed; otherwise create windows vm
if (snapshot_test.equals("yes")) {
// list volumes for linux vm
{
url = server + "?command=listVolumes&virtualMachineId=" + _linuxVmId.get() + "&type=root";
s_logger.info("Getting rootDisk id of Centos vm");
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("List volumes response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> success = getSingleValueFromXML(is, new String[] { "id" });
if (success.get("id") == null) {
s_logger.error("Unable to get root volume for linux vm. Followin url was sent: " + url);
}
s_logger.info("Got rootVolume for linux vm with id " + success.get("id"));
_rootVolume.set(success.get("id"));
} else {
s_logger.error("List volumes for linux vm failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
// Create recurring snapshot policy for linux vm
{
String encodedTimeZone = URLEncoder.encode("America/Los Angeles", "UTF-8");
url = server + "?command=createSnapshotPolicy&intervaltype=hourly&schedule=10&maxsnaps=4&volumeid=" + _rootVolume.get() + "&timezone=" + encodedTimeZone;
s_logger.info("Creating recurring snapshot policy for linux vm ROOT disk");
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Create recurring snapshot policy for linux vm ROOT disk: " + responseCode);
if (responseCode != 200) {
s_logger.error("Create recurring snapshot policy for linux vm ROOT disk failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
} else {
// ---------------------------------
// DEPLOY WINDOWS VM
// ---------------------------------
String windowsVMPrivateIP = null;
{
// long templateId = 6;
long templateId = 4;
String encodedZoneId = URLEncoder.encode("" + zoneId, "UTF-8");
String encodedServiceOfferingId = URLEncoder.encode("" + serviceOfferingId, "UTF-8");
String encodedTemplateId = URLEncoder.encode("" + templateId, "UTF-8");
encodedApiKey = URLEncoder.encode(_apiKey.get(), "UTF-8");
String encodedNetworkIds = URLEncoder.encode(_networkId.get() + ",206", "UTF-8");
requestToSign = "apikey=" + encodedApiKey + "&command=deployVirtualMachine&diskofferingid=" + diskOfferingId + "&networkids=" + encodedNetworkIds + "&serviceofferingid=" + encodedServiceOfferingId + "&templateid=" + encodedTemplateId + "&zoneid=" + encodedZoneId;
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=deployVirtualMachine" + "&zoneid=" + encodedZoneId + "&serviceofferingid=" + encodedServiceOfferingId + "&diskofferingid=" + diskOfferingId + "&networkids=" + encodedNetworkIds + "&templateid=" + encodedTemplateId + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id", "ipaddress" });
if ((values.get("ipaddress") == null) || (values.get("id") == null)) {
s_logger.info("deploy windows vm response code: 401, the command was sent with url " + url);
return 401;
} else {
s_logger.info("deploy windows vm response code: " + responseCode);
windowsVMPrivateIP = values.get("ipaddress");
long windowsVMId = Long.parseLong(values.get("id"));
s_logger.info("got windows virtual machine id: " + windowsVMId);
_windowsVmId.set(values.get("id"));
}
} else {
s_logger.error("deploy windows vm failes with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
//--------------------------------------------
// Enable Static NAT for the Non Source NAT Ip
//--------------------------------------------
encodedVmId = URLEncoder.encode(_windowsVmId.get(), "UTF-8");
encodedPublicIpId = URLEncoder.encode(_publicIpId.get(), "UTF-8");
requestToSign = "apikey=" + encodedApiKey + "&command=enableStaticNat" + "&ipaddressid=" + encodedPublicIpId + "&virtualMachineId=" + encodedVmId;
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=enableStaticNat&apikey=" + encodedApiKey + "&ipaddressid=" + encodedPublicIpId + "&signature=" + encodedSignature + "&virtualMachineId=" + encodedVmId;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("url is " + url);
s_logger.info("list ip addresses for user " + userId + " response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> success = getSingleValueFromXML(is, new String[] { "success" });
s_logger.info("Enable Static NAT..success? " + success.get("success"));
} else {
s_logger.error("Enable Static NAT failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// -------------------------------------------------------------
// CREATE IP FORWARDING RULE -- Windows VM
// -------------------------------------------------------------
// create port forwarding rule for window vm
encodedIpAddress = URLEncoder.encode(_windowsIpId.get(), "UTF-8");
//encodedVmId = URLEncoder.encode(_windowsVmId.get(), "UTF-8");
requestToSign = "apikey=" + encodedApiKey + "&command=createIpForwardingRule&endPort=22&ipaddressid=" + encodedIpAddress + "&protocol=TCP&startPort=22";
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, _secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=createIpForwardingRule&apikey=" + encodedApiKey + "&endPort=22&ipaddressid=" + encodedIpAddress + "&protocol=TCP&signature=" + encodedSignature + "&startPort=22";
s_logger.info("Created Ip forwarding rule with " + url);
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> values = getSingleValueFromXML(el, new String[] { "id" });
s_logger.info("Port forwarding rule was assigned successfully to Windows VM");
long ipfwdid = Long.parseLong(values.get("id"));
s_logger.info("got Ip Forwarding Rule's Id:" + ipfwdid);
_winipfwdid.set(values.get("id"));
} else {
s_logger.error("Port forwarding rule creation failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
}
return responseCode;
}
Aggregations