main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "Rackspace.h"
  11. #include <stdio.h>
  12. #include "Gets.h"
  13. #include "Rackspace.h"
  14. #include "TCPInterface.h"
  15. #include "Kbhit.h"
  16. #include "RakSleep.h"
  17. struct CommandAndDescription
  18. {
  19. const char *command;
  20. const char *description;
  21. };
  22. CommandAndDescription commands[]=
  23. {
  24. {"Help","This screen"},
  25. {"Quit","Exit application"},
  26. {"Authenticate","desc"},
  27. {"ListServers","desc"},
  28. {"ListServersWithDetails","desc"},
  29. {"CreateServer","desc"},
  30. {"GetServerDetails","desc"},
  31. {"UpdateServerNameOrPassword","desc"},
  32. {"DeleteServer","desc"},
  33. {"ListServerAddresses","desc"},
  34. {"ShareServerAddress","desc"},
  35. {"DeleteServerAddress","desc"},
  36. {"RebootServer","desc"},
  37. {"RebuildServer","desc"},
  38. {"ResizeServer","desc"},
  39. {"ConfirmResizedServer","desc"},
  40. {"RevertResizedServer","desc"},
  41. {"ListFlavors","desc"},
  42. {"GetFlavorDetails","desc"},
  43. {"ListImages","desc"},
  44. {"CreateImage","desc"},
  45. {"GetImageDetails","desc"},
  46. {"DeleteImage","desc"},
  47. {"ListSharedIPGroups","desc"},
  48. {"ListSharedIPGroupsWithDetails","desc"},
  49. {"CreateSharedIPGroup","desc"},
  50. {"GetSharedIPGroupDetails","desc"},
  51. {"DeleteSharedIPGroup","desc"},
  52. };
  53. void PrintCommands(void)
  54. {
  55. printf("\n");
  56. unsigned int i;
  57. for (i=0; i < sizeof(commands) / sizeof(CommandAndDescription); i++)
  58. {
  59. printf("%s - %s\n", commands[i].command, commands[i].description);
  60. }
  61. printf("\n");
  62. }
  63. bool Authenticate(RakNet::Rackspace *rackspaceApi, RakNet::TCPInterface *tcpInterface)
  64. {
  65. RakNet::SystemAddress serverAddress;
  66. char authenticationURL[128];
  67. char rackspaceCloudUsername[128];
  68. char apiAccessKey[128];
  69. // See http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf
  70. // US-based accounts authenticate through auth.api.rackspacecloud.com
  71. // UK-based accounts authenticate through lon.auth.api.rackspacecloud.com
  72. printf("Enter authenticationURL: ");
  73. Gets(authenticationURL, sizeof(authenticationURL));
  74. if (authenticationURL[0]==0)
  75. {
  76. FILE *fp;
  77. fp = fopen("authenticationURL.txt", "rb");
  78. if (fp)
  79. {
  80. fgets(authenticationURL,128,fp);
  81. fclose(fp);
  82. printf("Using %s\n", authenticationURL);
  83. }
  84. else
  85. {
  86. printf("Can't open authenticationURL.txt");
  87. return false;
  88. }
  89. }
  90. printf("Enter rackspaceCloudUsername: ");
  91. Gets(rackspaceCloudUsername, sizeof(rackspaceCloudUsername));
  92. if (rackspaceCloudUsername[0]==0)
  93. {
  94. FILE *fp;
  95. fp = fopen("rackspaceCloudUsername.txt", "rb");
  96. if (fp)
  97. {
  98. fgets(rackspaceCloudUsername,128,fp);
  99. fclose(fp);
  100. printf("Using %s\n", rackspaceCloudUsername);
  101. }
  102. else
  103. {
  104. printf("Can't open rackspaceCloudUsername.txt");
  105. return false;
  106. }
  107. }
  108. // This is found when you log in, under Your Account, expanded to API Access. Then click Show Key under Enable API Access.
  109. printf("Enter apiAccessKey: ");
  110. Gets(apiAccessKey, sizeof(apiAccessKey));
  111. if (apiAccessKey[0]==0)
  112. {
  113. FILE *fp;
  114. fp = fopen("apiAccessKey.txt", "rb");
  115. if (fp)
  116. {
  117. fgets(apiAccessKey,128,fp);
  118. fclose(fp);
  119. printf("Using %s\n", apiAccessKey);
  120. }
  121. else
  122. {
  123. printf("Can't open apiAccessKey.txt");
  124. return false;
  125. }
  126. }
  127. serverAddress=rackspaceApi->Authenticate(tcpInterface, authenticationURL, rackspaceCloudUsername, apiAccessKey);
  128. if (serverAddress==RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  129. printf("Failed to connect to %s\n", authenticationURL);
  130. return true;
  131. }
  132. class DisplayHTMLPage : public RakNet::RackspaceEventCallback_Default
  133. {
  134. virtual void ExecuteDefault(const char *callbackName, RakNet::RackspaceEventType eventType, const char *htmlAdditionalInfo)
  135. {
  136. /*
  137. RakNet::RakString fileName("%s.html", callbackName);
  138. FILE *fp = fopen(fileName.C_String(), "wb");
  139. if (fp)
  140. {
  141. fwrite(htmlAdditionalInfo,1,strlen(htmlAdditionalInfo),fp);
  142. fclose(fp);
  143. TCHAR szDirectory[MAX_PATH] = "";
  144. GetCurrentDirectory(sizeof(szDirectory) - 1, szDirectory);
  145. ShellExecute(NULL, "open", fileName.C_String(), NULL, szDirectory, SW_SHOWNORMAL);
  146. }
  147. */
  148. printf("%s completed with result %s\n", callbackName, RakNet::Rackspace::EventTypeToString(eventType));
  149. printf(htmlAdditionalInfo);
  150. printf("\n");
  151. }
  152. virtual void OnConnectionAttemptFailure( RakNet::RackspaceOperationType operationType, const char *url) {printf("OnConnectionAttemptFailure\noperationType=%i\n%s\n", operationType,url);}
  153. };
  154. int main()
  155. {
  156. #if OPEN_SSL_CLIENT_SUPPORT!=1
  157. printf("RakNet must be built with OPEN_SSL_CLIENT_SUPPORT");
  158. return 1;
  159. #endif
  160. RakNet::Rackspace rackspaceApi;
  161. RakNet::TCPInterface tcpInterface;
  162. DisplayHTMLPage callback;
  163. rackspaceApi.AddEventCallback(&callback);
  164. tcpInterface.Start(0, 0, 1);
  165. if (!Authenticate(&rackspaceApi, &tcpInterface))
  166. {
  167. return 1;
  168. }
  169. //rackspaceApi.ListImages();
  170. PrintCommands();
  171. RakNet::SystemAddress systemAddress;
  172. RakNet::Packet *packet;
  173. while (1)
  174. {
  175. for (packet=tcpInterface.Receive(); packet; tcpInterface.DeallocatePacket(packet), packet=tcpInterface.Receive())
  176. {
  177. rackspaceApi.OnReceive(packet);
  178. }
  179. RakNet::SystemAddress lostConnectionAddress = tcpInterface.HasLostConnection();
  180. if (lostConnectionAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  181. rackspaceApi.OnClosedConnection(lostConnectionAddress);
  182. if (kbhit())
  183. {
  184. printf("Command: ");
  185. char command[128];
  186. Gets(command, sizeof(command));
  187. if (stricmp(command,"Help")==0)
  188. {
  189. PrintCommands();
  190. }
  191. else if (stricmp(command,"Quit")==0)
  192. {
  193. break;
  194. }
  195. else if (stricmp(command,"Authenticate")==0)
  196. {
  197. Authenticate(&rackspaceApi, &tcpInterface);
  198. }
  199. else if (stricmp(command,"ListServers")==0)
  200. {
  201. rackspaceApi.ListServers();
  202. }
  203. else if (stricmp(command,"ListServersWithDetails")==0)
  204. {
  205. rackspaceApi.ListServersWithDetails();
  206. }
  207. else if (stricmp(command,"CreateServer")==0)
  208. {
  209. RakNet::RakString name;
  210. printf("Enter server name: ");
  211. Gets(command, sizeof(command));
  212. name=command;
  213. RakNet::RakString imageId;
  214. printf("Enter imageId: ");
  215. Gets(command, sizeof(command));
  216. imageId=command;
  217. RakNet::RakString flavorId;
  218. printf("Enter flavorId: ");
  219. Gets(command, sizeof(command));
  220. flavorId=command;
  221. rackspaceApi.CreateServer(name, imageId, flavorId);
  222. }
  223. else if (stricmp(command,"GetServerDetails")==0)
  224. {
  225. printf("Enter server id: ");
  226. Gets(command, sizeof(command));
  227. rackspaceApi.GetServerDetails(command);
  228. }
  229. else if (stricmp(command,"UpdateServerNameOrPassword")==0)
  230. {
  231. RakNet::RakString serverId;
  232. printf("Enter server id: ");
  233. Gets(command, sizeof(command));
  234. serverId=command;
  235. RakNet::RakString newName;
  236. printf("Enter newName: ");
  237. Gets(command, sizeof(command));
  238. newName=command;
  239. RakNet::RakString newPassword;
  240. printf("Enter newPassword: ");
  241. Gets(command, sizeof(command));
  242. newPassword=command;
  243. rackspaceApi.UpdateServerNameOrPassword(serverId,newName,newPassword);
  244. }
  245. else if (stricmp(command,"DeleteServer")==0)
  246. {
  247. printf("Enter server id: ");
  248. Gets(command, sizeof(command));
  249. rackspaceApi.DeleteServer(command);
  250. }
  251. else if (stricmp(command,"ListServerAddresses")==0)
  252. {
  253. printf("Enter server id: ");
  254. Gets(command, sizeof(command));
  255. rackspaceApi.ListServerAddresses(command);
  256. }
  257. else if (stricmp(command,"ShareServerAddress")==0)
  258. {
  259. RakNet::RakString serverId;
  260. printf("Enter server id: ");
  261. Gets(command, sizeof(command));
  262. serverId=command;
  263. RakNet::RakString serverAddress;
  264. printf("Enter server serverAddress: ");
  265. Gets(command, sizeof(command));
  266. serverAddress=command;
  267. rackspaceApi.ShareServerAddress(serverId, serverAddress);
  268. }
  269. else if (stricmp(command,"DeleteServerAddress")==0)
  270. {
  271. RakNet::RakString serverId;
  272. printf("Enter server id: ");
  273. Gets(command, sizeof(command));
  274. serverId=command;
  275. RakNet::RakString serverAddress;
  276. printf("Enter server serverAddress: ");
  277. Gets(command, sizeof(command));
  278. serverAddress=command;
  279. rackspaceApi.DeleteServerAddress(serverId, serverAddress);
  280. }
  281. else if (stricmp(command,"RebootServer")==0)
  282. {
  283. RakNet::RakString serverId;
  284. printf("Enter server id: ");
  285. Gets(command, sizeof(command));
  286. serverId=command;
  287. RakNet::RakString rebootType;
  288. printf("Enter rebootType: ");
  289. Gets(command, sizeof(command));
  290. rebootType=command;
  291. rackspaceApi.RebootServer(serverId,rebootType);
  292. }
  293. else if (stricmp(command,"RebuildServer")==0)
  294. {
  295. RakNet::RakString serverId;
  296. printf("Enter server id: ");
  297. Gets(command, sizeof(command));
  298. serverId=command;
  299. RakNet::RakString imageId;
  300. printf("Enter imageId: ");
  301. Gets(command, sizeof(command));
  302. imageId=command;
  303. rackspaceApi.RebuildServer(serverId,imageId);
  304. }
  305. else if (stricmp(command,"ResizeServer")==0)
  306. {
  307. RakNet::RakString serverId;
  308. printf("Enter server id: ");
  309. Gets(command, sizeof(command));
  310. serverId=command;
  311. RakNet::RakString flavorId;
  312. printf("Enter flavorId: ");
  313. Gets(command, sizeof(command));
  314. flavorId=command;
  315. rackspaceApi.ResizeServer(serverId,flavorId);
  316. }
  317. else if (stricmp(command,"ConfirmResizedServer")==0)
  318. {
  319. printf("Enter server id: ");
  320. Gets(command, sizeof(command));
  321. rackspaceApi.ConfirmResizedServer(command);
  322. }
  323. else if (stricmp(command,"RevertResizedServer")==0)
  324. {
  325. printf("Enter server id: ");
  326. Gets(command, sizeof(command));
  327. rackspaceApi.RevertResizedServer(command);
  328. }
  329. else if (stricmp(command,"ListFlavors")==0)
  330. {
  331. rackspaceApi.ListFlavors();
  332. }
  333. else if (stricmp(command,"GetFlavorDetails")==0)
  334. {
  335. printf("Enter flavor id: ");
  336. Gets(command, sizeof(command));
  337. rackspaceApi.GetFlavorDetails(command);
  338. }
  339. else if (stricmp(command,"ListImages")==0)
  340. {
  341. rackspaceApi.ListImages();
  342. }
  343. else if (stricmp(command,"CreateImage")==0)
  344. {
  345. RakNet::RakString serverId;
  346. printf("Enter server id: ");
  347. Gets(command, sizeof(command));
  348. serverId=command;
  349. RakNet::RakString imageName;
  350. printf("Enter imageName: ");
  351. Gets(command, sizeof(command));
  352. imageName=command;
  353. rackspaceApi.CreateImage(serverId,imageName);
  354. }
  355. else if (stricmp(command,"GetImageDetails")==0)
  356. {
  357. printf("Enter image id: ");
  358. Gets(command, sizeof(command));
  359. rackspaceApi.GetImageDetails(command);
  360. }
  361. else if (stricmp(command,"DeleteImage")==0)
  362. {
  363. printf("Enter image id: ");
  364. Gets(command, sizeof(command));
  365. rackspaceApi.DeleteImage(command);
  366. }
  367. else if (stricmp(command,"ListSharedIPGroups")==0)
  368. {
  369. rackspaceApi.ListSharedIPGroups();
  370. }
  371. else if (stricmp(command,"ListSharedIPGroupsWithDetails")==0)
  372. {
  373. rackspaceApi.ListSharedIPGroupsWithDetails();
  374. }
  375. else if (stricmp(command,"CreateSharedIPGroup")==0)
  376. {
  377. rackspaceApi.CreateSharedIPGroup("testSharedIPGroup","");
  378. }
  379. else if (stricmp(command,"GetSharedIPGroupDetails")==0)
  380. {
  381. printf("Enter group id: ");
  382. Gets(command, sizeof(command));
  383. rackspaceApi.GetSharedIPGroupDetails(command);
  384. }
  385. else if (stricmp(command,"DeleteSharedIPGroup")==0)
  386. {
  387. printf("Enter group id: ");
  388. Gets(command, sizeof(command));
  389. rackspaceApi.DeleteSharedIPGroup(command);
  390. }
  391. else
  392. {
  393. printf("Unknown command. Type 'help' for help.\n");
  394. }
  395. }
  396. RakSleep(30);
  397. }
  398. return 0;
  399. }
粤ICP备19079148号