use of org.apache.commons.httpclient.HttpMethod in project cloudstack by apache.
the class StressTestDirectAttach 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 = s_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;
}
}
// -----------------------------
// Execute reboot/stop/start commands for the VMs before deleting the account - made to exercise xen
// -----------------------------
//Reboot centos VM
String encodedApiKey = URLEncoder.encode(s_apiKey.get(), "UTF-8");
String requestToSign = "apikey=" + encodedApiKey + "&command=rebootVirtualMachine&id=" + s_linuxVmId.get();
requestToSign = requestToSign.toLowerCase();
String signature = signRequest(requestToSign, s_secretKey.get());
String encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=rebootVirtualMachine&id=" + s_linuxVmId.get() + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Reboot 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("VM was rebooted with the status: " + success.get("success"));
} else {
s_logger.error(" VM test failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
//Stop centos VM
requestToSign = "apikey=" + encodedApiKey + "&command=stopVirtualMachine&id=" + s_linuxVmId.get();
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, s_secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=stopVirtualMachine&id=" + s_linuxVmId.get() + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Stop 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("VM was stopped with the status: " + success.get("success"));
} else {
s_logger.error("Stop VM test failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
//Start centos VM
requestToSign = "apikey=" + encodedApiKey + "&command=startVirtualMachine&id=" + s_linuxVmId.get();
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, s_secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=startVirtualMachine&id=" + s_linuxVmId.get() + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("Start VM response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
Map<String, String> success = getSingleValueFromXML(el, new String[] { "id" });
if (success.get("id") == null) {
s_logger.info("Start linux vm response code: 401");
return 401;
} else {
s_logger.info("Start vm response code: " + responseCode);
}
s_logger.info("VM was started with the status: " + success.get("success"));
} else {
s_logger.error("Start VM test failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
//// // -----------------------------
//// // DISABLE USER
//// // -----------------------------
// {
// url = server + "?command=disableUser&id=" + userId;
// client = new HttpClient();
// method = new GetMethod(url);
// responseCode = client.executeMethod(method);
// s_logger.info("disable user response code: " + responseCode);
// if (responseCode == 200) {
// InputStream input = method.getResponseBodyAsStream();
// Element el = queryAsyncJobResult(server, input);
// s_logger
// .info("Disabled user successfully");
// } else {
// s_logger.error("disable user failed with error code: " + responseCode + ". Following URL was sent: " + url);
// return responseCode;
// }
// }
// -----------------------------
// DELETE USER
// -----------------------------
{
url = server + "?command=deleteUser&id=" + userId;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("delete user response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element el = queryAsyncJobResult(server, input);
s_logger.info("Deleted user successfully");
} else {
s_logger.error("delete user failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
return responseCode;
}
use of org.apache.commons.httpclient.HttpMethod in project cloudstack by apache.
the class StressTestDirectAttach method executeStop.
private static int executeStop(String server, String developerServer, String username) throws HttpException, IOException {
// test steps:
// - get userId for the given username
// - list virtual machines for the user
// - stop all virtual machines
// - get ip addresses for the user
// - release ip addresses
// -----------------------------
// GET USER
// -----------------------------
String userId = s_userId.get().toString();
String encodedUserId = URLEncoder.encode(userId, "UTF-8");
String url = server + "?command=listUsers&id=" + encodedUserId;
s_logger.info("Stopping resources for user: " + username);
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> userIdValues = getSingleValueFromXML(is, new String[] { "id" });
String userIdStr = userIdValues.get("id");
if (userIdStr != null) {
userId = userIdStr;
if (userId == null) {
s_logger.error("get user failed to retrieve a valid user id, aborting depolyment 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;
}
{
// ----------------------------------
// LIST VIRTUAL MACHINES
// ----------------------------------
String encodedApiKey = URLEncoder.encode(s_apiKey.get(), "UTF-8");
String requestToSign = "apikey=" + encodedApiKey + "&command=listVirtualMachines";
requestToSign = requestToSign.toLowerCase();
String signature = signRequest(requestToSign, s_secretKey.get());
String encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=listVirtualMachines&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
s_logger.info("Listing all virtual machines for the user with url " + url);
String[] vmIds = null;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("list virtual machines response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, List<String>> vmIdValues = getMultipleValuesFromXML(is, new String[] { "id" });
if (vmIdValues.containsKey("id")) {
List<String> vmIdList = vmIdValues.get("id");
if (vmIdList != null) {
vmIds = new String[vmIdList.size()];
vmIdList.toArray(vmIds);
String vmIdLogStr = "";
if ((vmIds != null) && (vmIds.length > 0)) {
vmIdLogStr = vmIds[0];
for (int i = 1; i < vmIds.length; i++) {
vmIdLogStr = vmIdLogStr + "," + vmIds[i];
}
}
s_logger.info("got virtual machine ids: " + vmIdLogStr);
}
}
} else {
s_logger.error("list virtual machines test failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// ----------------------------------
if (vmIds != null) {
for (String vmId : vmIds) {
requestToSign = "apikey=" + encodedApiKey + "&command=stopVirtualMachine&id=" + vmId;
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, s_secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=stopVirtualMachine&id=" + vmId + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("StopVirtualMachine" + " [" + vmId + "] 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("StopVirtualMachine..success? " + success.get("success"));
} else {
s_logger.error("Stop virtual machine test failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
}
// {
// url = server + "?command=deleteUser&id=" + userId;
// client = new HttpClient();
// method = new GetMethod(url);
// responseCode = client.executeMethod(method);
// s_logger.info("delete user response code: " + responseCode);
// if (responseCode == 200) {
// InputStream input = method.getResponseBodyAsStream();
// Element el = queryAsyncJobResult(server, input);
// s_logger
// .info("Deleted user successfully");
// } else {
// s_logger.error("delete user failed with error code: " + responseCode + ". Following URL was sent: " + url);
// return responseCode;
// }
// }
}
s_linuxIP.set("");
s_linuxVmId.set("");
s_linuxPassword.set("");
s_windowsIP.set("");
s_secretKey.set("");
s_apiKey.set("");
s_userId.set(Long.parseLong("0"));
s_account.set("");
s_domainRouterId.set("");
return responseCode;
}
use of org.apache.commons.httpclient.HttpMethod in project cloudstack by apache.
the class StressTestDirectAttach method executeRegistration.
private static String executeRegistration(String server, String username, String password) throws HttpException, IOException {
String url = server + "?command=registerUserKeys&id=" + s_userId.get().toString();
s_logger.info("registering: " + username);
String returnValue = null;
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
int responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, String> requestKeyValues = getSingleValueFromXML(is, new String[] { "apikey", "secretkey" });
s_apiKey.set(requestKeyValues.get("apikey"));
returnValue = requestKeyValues.get("secretkey");
} else {
s_logger.error("registration failed with error code: " + responseCode);
}
return returnValue;
}
use of org.apache.commons.httpclient.HttpMethod in project cloudstack by apache.
the class TestClientWithAPI method executeStop.
private static int executeStop(String server, String developerServer, String username, boolean destroy) throws HttpException, IOException {
// test steps:
// - get userId for the given username
// - list virtual machines for the user
// - stop all virtual machines
// - get ip addresses for the user
// - release ip addresses
// -----------------------------
// GET USER
// -----------------------------
String userId = s_userId.get().toString();
String encodedUserId = URLEncoder.encode(userId, "UTF-8");
String url = server + "?command=listUsers&id=" + encodedUserId;
s_logger.info("Stopping resources for user: " + username);
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> userIdValues = getSingleValueFromXML(is, new String[] { "id" });
String userIdStr = userIdValues.get("id");
if (userIdStr != null) {
userId = userIdStr;
} else {
s_logger.error("get user failed to retrieve a valid user id, aborting depolyment 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;
}
{
// ----------------------------------
// LIST VIRTUAL MACHINES
// ----------------------------------
String encodedApiKey = URLEncoder.encode(s_apiKey.get(), "UTF-8");
String requestToSign = "apikey=" + encodedApiKey + "&command=listVirtualMachines";
requestToSign = requestToSign.toLowerCase();
String signature = signRequest(requestToSign, s_secretKey.get());
String encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=listVirtualMachines&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
s_logger.info("Listing all virtual machines for the user with url " + url);
String[] vmIds = null;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("list virtual machines response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, List<String>> vmIdValues = getMultipleValuesFromXML(is, new String[] { "id" });
if (vmIdValues.containsKey("id")) {
List<String> vmIdList = vmIdValues.get("id");
if (vmIdList != null) {
vmIds = new String[vmIdList.size()];
vmIdList.toArray(vmIds);
String vmIdLogStr = "";
if ((vmIds != null) && (vmIds.length > 0)) {
vmIdLogStr = vmIds[0];
for (int i = 1; i < vmIds.length; i++) {
vmIdLogStr = vmIdLogStr + "," + vmIds[i];
}
}
s_logger.info("got virtual machine ids: " + vmIdLogStr);
}
}
} else {
s_logger.error("list virtual machines test failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// ----------------------------------
// LIST USER IP ADDRESSES
// ----------------------------------
requestToSign = "apikey=" + encodedApiKey + "&command=listPublicIpAddresses";
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, s_secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=listPublicIpAddresses&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
String[] ipAddresses = null;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("list ip addresses for user " + userId + " response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, List<String>> ipAddressValues = getMultipleValuesFromXML(is, new String[] { "ipaddress" });
if (ipAddressValues.containsKey("ipaddress")) {
List<String> ipAddressList = ipAddressValues.get("ipaddress");
if (ipAddressList != null) {
ipAddresses = new String[ipAddressList.size()];
ipAddressList.toArray(ipAddresses);
String ipAddressLogStr = "";
if ((ipAddresses != null) && (ipAddresses.length > 0)) {
ipAddressLogStr = ipAddresses[0];
for (int i = 1; i < ipAddresses.length; i++) {
ipAddressLogStr = ipAddressLogStr + "," + ipAddresses[i];
}
}
s_logger.info("got IP addresses: " + ipAddressLogStr);
}
}
} else {
s_logger.error("list user ip addresses failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// ----------------------------------
// LIST ZONES
// ----------------------------------
requestToSign = "apikey=" + encodedApiKey + "&command=listZones";
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, s_secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=listZones&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
String[] zoneNames = null;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("list zones response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, List<String>> zoneNameValues = getMultipleValuesFromXML(is, new String[] { "name" });
if (zoneNameValues.containsKey("name")) {
List<String> zoneNameList = zoneNameValues.get("name");
if (zoneNameList != null) {
zoneNames = new String[zoneNameList.size()];
zoneNameList.toArray(zoneNames);
String zoneNameLogStr = "\n\n";
if ((zoneNames != null) && (zoneNames.length > 0)) {
zoneNameLogStr += zoneNames[0];
for (int i = 1; i < zoneNames.length; i++) {
zoneNameLogStr = zoneNameLogStr + "\n" + zoneNames[i];
}
}
zoneNameLogStr += "\n\n";
s_logger.info("got zones names: " + zoneNameLogStr);
}
}
} else {
s_logger.error("list zones failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// ----------------------------------
// LIST ACCOUNT STATISTICS
// ----------------------------------
requestToSign = "apikey=" + encodedApiKey + "&command=listAccounts";
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, s_secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=listAccounts&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
String[] statNames = null;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("listAccountStatistics response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, List<String>> statValues = getMultipleValuesFromXML(is, new String[] { "receivedbytes" });
if (statValues.containsKey("receivedbytes")) {
List<String> statList = statValues.get("receivedbytes");
if (statList != null) {
statNames = new String[statList.size()];
statList.toArray(statNames);
String statLogStr = "\n\n";
if ((statNames != null) && (zoneNames.length > 0)) {
statLogStr += statNames[0];
for (int i = 1; i < statNames.length; i++) {
statLogStr = statLogStr + "\n" + zoneNames[i];
}
}
statLogStr += "\n\n";
s_logger.info("got accountstatistics: " + statLogStr);
}
}
} else {
s_logger.error("listAccountStatistics failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// ----------------------------------
// LIST TEMPLATES
// ----------------------------------
requestToSign = "apikey=" + encodedApiKey + "&command=listTemplates@templatefilter=self";
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, s_secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=listTemplates&apikey=" + encodedApiKey + "&templatefilter=self&signature=" + encodedSignature;
String[] templateNames = null;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("list templates response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, List<String>> templateNameValues = getMultipleValuesFromXML(is, new String[] { "name" });
if (templateNameValues.containsKey("name")) {
List<String> templateNameList = templateNameValues.get("name");
if (templateNameList != null) {
templateNames = new String[templateNameList.size()];
templateNameList.toArray(templateNames);
String templateNameLogStr = "\n\n";
if ((templateNames != null) && (templateNames.length > 0)) {
templateNameLogStr += templateNames[0];
for (int i = 1; i < templateNames.length; i++) {
templateNameLogStr = templateNameLogStr + "\n" + templateNames[i];
}
}
templateNameLogStr += "\n\n";
s_logger.info("got template names: " + templateNameLogStr);
}
}
} else {
s_logger.error("list templates failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// ----------------------------------
// LIST SERVICE OFFERINGS
// ----------------------------------
requestToSign = "apikey=" + encodedApiKey + "&command=listServiceOfferings";
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, s_secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=listServiceOfferings&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
String[] serviceOfferingNames = null;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("list service offerings response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, List<String>> serviceOfferingNameValues = getMultipleValuesFromXML(is, new String[] { "name" });
if (serviceOfferingNameValues.containsKey("name")) {
List<String> serviceOfferingNameList = serviceOfferingNameValues.get("name");
if (serviceOfferingNameList != null) {
serviceOfferingNames = new String[serviceOfferingNameList.size()];
serviceOfferingNameList.toArray(serviceOfferingNames);
String serviceOfferingNameLogStr = "";
if ((serviceOfferingNames != null) && (serviceOfferingNames.length > 0)) {
serviceOfferingNameLogStr = serviceOfferingNames[0];
for (int i = 1; i < serviceOfferingNames.length; i++) {
serviceOfferingNameLogStr = serviceOfferingNameLogStr + ", " + serviceOfferingNames[i];
}
}
s_logger.info("got service offering names: " + serviceOfferingNameLogStr);
}
}
} else {
s_logger.error("list service offerings failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// ----------------------------------
// LIST EVENTS
// ---------------------------------
url = server + "?command=listEvents&page=1&pagesize=100&&account=" + s_account.get();
String[] eventDescriptions = null;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("list events response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
Map<String, List<String>> eventNameValues = getMultipleValuesFromXML(is, new String[] { "description" });
if (eventNameValues.containsKey("description")) {
List<String> eventNameList = eventNameValues.get("description");
if (eventNameList != null) {
eventDescriptions = new String[eventNameList.size()];
eventNameList.toArray(eventDescriptions);
String eventNameLogStr = "\n\n";
if ((eventDescriptions != null) && (eventDescriptions.length > 0)) {
eventNameLogStr += eventDescriptions[0];
for (int i = 1; i < eventDescriptions.length; i++) {
eventNameLogStr = eventNameLogStr + "\n" + eventDescriptions[i];
}
}
eventNameLogStr += "\n\n";
s_logger.info("got event descriptions: " + eventNameLogStr);
}
}
} else {
s_logger.error("list events failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// ----------------------------------
if (vmIds != null) {
String cmdName = (destroy ? "destroyVirtualMachine" : "stopVirtualMachine");
for (String vmId : vmIds) {
requestToSign = "apikey=" + encodedApiKey + "&command=" + cmdName + "&id=" + vmId;
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, s_secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=" + cmdName + "&id=" + vmId + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info(cmdName + " [" + vmId + "] 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(cmdName + "..success? " + success.get("success"));
} else {
s_logger.error(cmdName + "test failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
}
}
{
String[] ipAddresses = null;
// -----------------------------------------
// LIST NAT IP ADDRESSES
// -----------------------------------------
String encodedApiKey = URLEncoder.encode(s_apiKey.get(), "UTF-8");
String requestToSign = "apikey=" + encodedApiKey + "&command=listPublicIpAddresses";
requestToSign = requestToSign.toLowerCase();
String signature = signRequest(requestToSign, s_secretKey.get());
String 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("list ip addresses for user " + userId + " response code: " + responseCode);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
List<String> ipAddressList = getNonSourceNatIPs(is);
ipAddresses = new String[ipAddressList.size()];
ipAddressList.toArray(ipAddresses);
String ipAddrLogStr = "";
if ((ipAddresses != null) && (ipAddresses.length > 0)) {
ipAddrLogStr = ipAddresses[0];
for (int i = 1; i < ipAddresses.length; i++) {
ipAddrLogStr = ipAddrLogStr + "," + ipAddresses[i];
}
}
s_logger.info("got ip addresses: " + ipAddrLogStr);
} else {
s_logger.error("list nat ip addresses failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// -------------------------------------------------------------
// Delete IP FORWARDING RULE -- Windows VM
// -------------------------------------------------------------
String encodedIpFwdId = URLEncoder.encode(s_winipfwdid.get(), "UTF-8");
requestToSign = "apikey=" + encodedApiKey + "&command=deleteIpForwardingRule&id=" + encodedIpFwdId;
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, s_secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=deleteIpForwardingRule&apikey=" + encodedApiKey + "&id=" + encodedIpFwdId + "&signature=" + encodedSignature;
s_logger.info("Delete 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);
s_logger.info("IP forwarding rule was successfully deleted");
} else {
s_logger.error("IP forwarding rule creation failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
//--------------------------------------------
// Disable Static NAT for the Source NAT Ip
//--------------------------------------------
encodedApiKey = URLEncoder.encode(s_apiKey.get(), "UTF-8");
String encodedPublicIpId = URLEncoder.encode(s_publicIpId.get(), "UTF-8");
requestToSign = "apikey=" + encodedApiKey + "&command=disableStaticNat" + "&id=" + encodedPublicIpId;
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, s_secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=disableStaticNat&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();
Map<String, String> success = getSingleValueFromXML(is, new String[] { "success" });
s_logger.info("Disable Static NAT..success? " + success.get("success"));
} else {
s_logger.error("Disable Static NAT failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
// -----------------------------------------
if (ipAddresses != null) {
for (String ipAddress : ipAddresses) {
requestToSign = "apikey=" + encodedApiKey + "&command=disassociateIpAddress&id=" + ipAddress;
requestToSign = requestToSign.toLowerCase();
signature = signRequest(requestToSign, s_secretKey.get());
encodedSignature = URLEncoder.encode(signature, "UTF-8");
url = developerServer + "?command=disassociateIpAddress&apikey=" + encodedApiKey + "&id=" + ipAddress + "&signature=" + encodedSignature;
client = new HttpClient();
method = new GetMethod(url);
responseCode = client.executeMethod(method);
s_logger.info("disassociate ip address [" + userId + "/" + ipAddress + "] response code: " + responseCode);
if (responseCode == 200) {
InputStream input = method.getResponseBodyAsStream();
Element disassocipel = queryAsyncJobResult(server, input);
Map<String, String> success = getSingleValueFromXML(disassocipel, new String[] { "success" });
// Map<String, String> success = getSingleValueFromXML(input, new String[] { "success" });
s_logger.info("disassociate ip address..success? " + success.get("success"));
} else {
s_logger.error("disassociate ip address failed with error code: " + responseCode + ". Following URL was sent: " + url);
return responseCode;
}
}
}
}
s_linuxIP.set("");
s_linuxIpId.set("");
s_linuxVmId.set("");
s_linuxPassword.set("");
s_windowsIP.set("");
s_windowsIpId.set("");
s_windowsVmId.set("");
s_secretKey.set("");
s_apiKey.set("");
s_userId.set(Long.parseLong("0"));
s_account.set("");
s_domainRouterId.set("");
return responseCode;
}
use of org.apache.commons.httpclient.HttpMethod in project cloudstack by apache.
the class TestClient method main.
public static void main(String[] args) {
String host = "http://localhost";
String port = "8080";
String testUrl = "/client/test";
int numThreads = 1;
try {
// Parameters
List<String> argsList = Arrays.asList(args);
Iterator<String> iter = argsList.iterator();
while (iter.hasNext()) {
String arg = iter.next();
// host
if (arg.equals("-h")) {
host = "http://" + iter.next();
}
if (arg.equals("-p")) {
port = iter.next();
}
if (arg.equals("-t")) {
numThreads = Integer.parseInt(iter.next());
}
if (arg.equals("-s")) {
sleepTime = Long.parseLong(iter.next());
}
if (arg.equals("-c")) {
cleanUp = Boolean.parseBoolean(iter.next());
if (!cleanUp)
// no need to wait if we don't ever cleanup
sleepTime = 0L;
}
if (arg.equals("-r")) {
repeat = Boolean.parseBoolean(iter.next());
}
if (arg.equals("-u")) {
numOfUsers = Integer.parseInt(iter.next());
}
if (arg.equals("-i")) {
internet = Boolean.parseBoolean(iter.next());
}
}
final String server = host + ":" + port + testUrl;
s_logger.info("Starting test against server: " + server + " with " + numThreads + " thread(s)");
if (cleanUp)
s_logger.info("Clean up is enabled, each test will wait " + sleepTime + " ms before cleaning up");
if (numOfUsers > 0) {
s_logger.info("Pre-generating users for test of size : " + numOfUsers);
users = new String[numOfUsers];
Random ran = new Random();
for (int i = 0; i < numOfUsers; i++) {
users[i] = Math.abs(ran.nextInt()) + "-user";
}
}
for (int i = 0; i < numThreads; i++) {
new Thread(new Runnable() {
@Override
public void run() {
do {
String username = null;
try {
long now = System.currentTimeMillis();
Random ran = new Random();
if (users != null) {
username = users[Math.abs(ran.nextInt()) % numOfUsers];
} else {
username = Math.abs(ran.nextInt()) + "-user";
}
NDC.push(username);
String url = server + "?email=" + username + "&password=" + username + "&command=deploy";
s_logger.info("Launching test for user: " + username + " with url: " + url);
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
int responseCode = client.executeMethod(method);
boolean success = false;
String reason = null;
if (responseCode == 200) {
if (internet) {
s_logger.info("Deploy successful...waiting 5 minute before SSH tests");
// Wait 60 seconds so the linux VM can boot up.
Thread.sleep(300000L);
s_logger.info("Begin Linux SSH test");
reason = sshTest(method.getResponseHeader("linuxIP").getValue());
if (reason == null) {
s_logger.info("Linux SSH test successful");
s_logger.info("Begin Windows SSH test");
reason = sshWinTest(method.getResponseHeader("windowsIP").getValue());
}
}
if (reason == null) {
if (internet) {
s_logger.info("Windows SSH test successful");
} else {
s_logger.info("deploy test successful....now cleaning up");
if (cleanUp) {
s_logger.info("Waiting " + sleepTime + " ms before cleaning up vms");
Thread.sleep(sleepTime);
} else {
success = true;
}
}
if (users == null) {
s_logger.info("Sending cleanup command");
url = server + "?email=" + username + "&password=" + username + "&command=cleanup";
} else {
s_logger.info("Sending stop DomR / destroy VM command");
url = server + "?email=" + username + "&password=" + username + "&command=stopDomR";
}
method = new GetMethod(url);
responseCode = client.executeMethod(method);
if (responseCode == 200) {
success = true;
} else {
reason = method.getStatusText();
}
} else {
// Just stop but don't destroy the VMs/Routers
s_logger.info("SSH test failed with reason '" + reason + "', stopping VMs");
url = server + "?email=" + username + "&password=" + username + "&command=stop";
responseCode = client.executeMethod(new GetMethod(url));
}
} else {
// Just stop but don't destroy the VMs/Routers
reason = method.getStatusText();
s_logger.info("Deploy test failed with reason '" + reason + "', stopping VMs");
url = server + "?email=" + username + "&password=" + username + "&command=stop";
client.executeMethod(new GetMethod(url));
}
if (success) {
s_logger.info("***** Completed test for user : " + username + " in " + ((System.currentTimeMillis() - now) / 1000L) + " seconds");
} else {
s_logger.info("##### FAILED test for user : " + username + " in " + ((System.currentTimeMillis() - now) / 1000L) + " seconds with reason : " + reason);
}
} catch (Exception e) {
s_logger.warn("Error in thread", e);
try {
HttpClient client = new HttpClient();
String url = server + "?email=" + username + "&password=" + username + "&command=stop";
client.executeMethod(new GetMethod(url));
} catch (Exception e1) {
s_logger.info("[ignored]" + "error while executing last resort stop attampt: " + e1.getLocalizedMessage());
}
} finally {
NDC.clear();
}
} while (repeat);
}
}).start();
}
} catch (Exception e) {
s_logger.error(e);
}
}
Aggregations