PageRenderTime 103ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/src/com/cloud/test/longrun/User.java

https://github.com/flavioruiz/CloudStack
Java | 241 lines | 168 code | 55 blank | 18 comment | 17 complexity | 3a1db52e5a0056b5097e95a808402c94 MD5 | raw file
  1. /**
  2. * * Copyright (C) 2011 Citrix Systems, Inc. All rights reserved
  3. *
  4. *
  5. * This software is licensed under the GNU General Public License v3 or later.
  6. *
  7. * It is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package com.cloud.test.longrun;
  20. import java.util.ArrayList;
  21. import java.util.Map;
  22. import java.io.InputStream;
  23. import java.io.IOException;
  24. import java.net.URLEncoder;
  25. import org.apache.commons.httpclient.HttpClient;
  26. import org.apache.commons.httpclient.HttpException;
  27. import org.apache.commons.httpclient.HttpMethod;
  28. import org.apache.commons.httpclient.methods.GetMethod;
  29. import org.apache.log4j.Logger;
  30. import com.cloud.test.stress.TestClientWithAPI;
  31. public class User {
  32. public static final Logger s_logger= Logger.getLogger(User.class.getClass());
  33. private ArrayList<VirtualMachine> virtualMachines;
  34. private ArrayList<String> publicIp;
  35. private String server;
  36. private String developerServer;
  37. private String userName;
  38. private String userId;
  39. private String apiKey;
  40. private String secretKey;
  41. private String password;
  42. private String encryptedPassword;
  43. public User(String userName, String password, String server, String developerServer)
  44. {
  45. this.server=server;
  46. this.developerServer=developerServer;
  47. this.userName=userName;
  48. this.password=password;
  49. this.virtualMachines = new ArrayList<VirtualMachine>();
  50. this.publicIp = new ArrayList<String>();
  51. }
  52. public ArrayList<VirtualMachine> getVirtualMachines() {
  53. return virtualMachines;
  54. }
  55. public void setVirtualMachines(ArrayList<VirtualMachine> virtualMachines) {
  56. this.virtualMachines = virtualMachines;
  57. }
  58. public String getUserId() {
  59. return userId;
  60. }
  61. public void setUserId(String userId) {
  62. this.userId = userId;
  63. }
  64. public ArrayList<String> getPublicIp() {
  65. return publicIp;
  66. }
  67. public void setPublicIp(ArrayList<String> publicIp) {
  68. this.publicIp = publicIp;
  69. }
  70. public String getServer() {
  71. return server;
  72. }
  73. public void setServer(String server) {
  74. this.server = server;
  75. }
  76. public String getUserName() {
  77. return userName;
  78. }
  79. public void setUserName(String userName) {
  80. this.userName = userName;
  81. }
  82. public String getApiKey() {
  83. return apiKey;
  84. }
  85. public void setApiKey(String apiKey) {
  86. this.apiKey = apiKey;
  87. }
  88. public String getPassword() {
  89. return password;
  90. }
  91. public void setPassword(String password) {
  92. this.password = password;
  93. }
  94. public String getSecretKey() {
  95. return secretKey;
  96. }
  97. public void setSecretKey(String secretKey) {
  98. this.secretKey = secretKey;
  99. }
  100. public String getDeveloperServer() {
  101. return developerServer;
  102. }
  103. public void setDeveloperServer(String developerServer) {
  104. this.developerServer = developerServer;
  105. }
  106. public void launchUser() throws IOException {
  107. String encodedUsername = URLEncoder.encode(this.getUserName(), "UTF-8");
  108. this.encryptedPassword=TestClientWithAPI.createMD5Password(this.getPassword());
  109. String encodedPassword = URLEncoder.encode(this.encryptedPassword, "UTF-8");
  110. String url = this.server + "?command=createUser&username=" + encodedUsername
  111. + "&password=" + encodedPassword
  112. + "&firstname=Test&lastname=Test&email=alena@vmops.com&domainId=1";
  113. String userIdStr=null;
  114. HttpClient client = new HttpClient();
  115. HttpMethod method = new GetMethod(url);
  116. int responseCode = client.executeMethod(method);
  117. if (responseCode == 200) {
  118. InputStream is = method.getResponseBodyAsStream();
  119. Map<String, String> userIdValues = TestClientWithAPI.getSingleValueFromXML(is,
  120. new String[] { "id" });
  121. userIdStr = userIdValues.get("id");
  122. if ((userIdStr != null) && (Long.parseLong(userIdStr)!=-1)) {
  123. this.setUserId(userIdStr);
  124. }
  125. }
  126. }
  127. public void retrievePublicIp(long zoneId) throws IOException{
  128. String encodedApiKey = URLEncoder.encode(this.apiKey, "UTF-8");
  129. String encodedZoneId=URLEncoder.encode(""+zoneId,"UTF-8");
  130. String requestToSign = "apiKey=" + encodedApiKey
  131. + "&command=associateIpAddress" + "&zoneId="+encodedZoneId;
  132. requestToSign = requestToSign.toLowerCase();
  133. String signature = TestClientWithAPI.signRequest(requestToSign, this.secretKey);
  134. String encodedSignature = URLEncoder.encode(signature, "UTF-8");
  135. String url = this.developerServer + "?command=associateIpAddress" + "&apiKey="
  136. + encodedApiKey + "&zoneId="+encodedZoneId+"&signature="
  137. + encodedSignature;
  138. HttpClient client = new HttpClient();
  139. HttpMethod method = new GetMethod(url);
  140. int responseCode=client.executeMethod(method);
  141. if (responseCode == 200) {
  142. InputStream is = method.getResponseBodyAsStream();
  143. Map<String, String> values = TestClientWithAPI.getSingleValueFromXML(is,
  144. new String[] { "ipaddress" });
  145. this.getPublicIp().add(values.get("ipaddress"));
  146. s_logger.info("Ip address is " + values.get("ipaddress"));
  147. } else if (responseCode == 500) {
  148. InputStream is = method.getResponseBodyAsStream();
  149. Map<String, String> errorInfo = TestClientWithAPI.getSingleValueFromXML(is,
  150. new String[] { "errorcode", "description" });
  151. s_logger.error("associate ip test failed with errorCode: "
  152. + errorInfo.get("errorCode") + " and description: "
  153. + errorInfo.get("description"));
  154. } else {
  155. s_logger.error("internal error processing request: "
  156. + method.getStatusText());
  157. }
  158. }
  159. public void registerUser()throws HttpException, IOException{
  160. String encodedUsername = URLEncoder.encode(this.userName, "UTF-8");
  161. String encodedPassword = URLEncoder.encode(this.password, "UTF-8");
  162. String url = server + "?command=register&username=" + encodedUsername
  163. + "&domainid=1";
  164. s_logger.info("registering: " + this.userName+" with url "+url);
  165. HttpClient client = new HttpClient();
  166. HttpMethod method = new GetMethod(url);
  167. int responseCode = client.executeMethod(method);
  168. if (responseCode == 200) {
  169. InputStream is = method.getResponseBodyAsStream();
  170. Map<String, String> requestKeyValues = TestClientWithAPI.getSingleValueFromXML(is,
  171. new String[] { "apikey", "secretkey" });
  172. this.setApiKey(requestKeyValues.get("apikey"));
  173. this.setSecretKey(requestKeyValues.get("secretkey"));
  174. } else if (responseCode == 500) {
  175. InputStream is = method.getResponseBodyAsStream();
  176. Map<String, String> errorInfo = TestClientWithAPI.getSingleValueFromXML(is,
  177. new String[] { "errorcode", "description" });
  178. s_logger.error("registration failed with errorCode: "
  179. + errorInfo.get("errorCode") + " and description: "
  180. + errorInfo.get("description"));
  181. } else {
  182. s_logger.error("internal error processing request: "
  183. + method.getStatusText());
  184. }
  185. }
  186. }