Advertisement
Guest User

JS-CSA

a guest
Mar 4th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. // Input Parameters
  2.  
  3. var BaseUrl = "https://atlas.budcca-demo.lab";
  4. var User = "budcca\jelarabi";
  5. var Password = "Constell@tion";
  6.  
  7. /**
  8. * Configure first API call to find sessionID
  9. * SessionId is our credential for other Api call
  10. * POST request
  11. */
  12. var operationUrl = baseUrl + "/rest/com/vmware/cis/session";
  13. var req = restHost.createRequest("POST", operationUrl, "");
  14. var resp;
  15. if (User == null || User == "" || Password == null || Password == "") {
  16. console.log("User or Password is empty");
  17. exit()
  18. }
  19. resp = req.executeWithCredentials(User, Password);
  20. statusCode = resp.statusCode;
  21. if (statusCode == 401) {
  22. console.log("Bad credential");
  23. exit();
  24. } else if (statusCode != 200) {
  25. console.log("Error");
  26. exit();
  27. }
  28.  
  29. // Take json body in response parse it to get this sessionId
  30. var sessionid = JSON.parse(resp.contentAsString).value;
  31. /**
  32. * Second Api call to get all folder in vCenter
  33. * Put the sessionId in header
  34. * GET request
  35. */
  36. var operationUrl = baseUrl + "/rest/vcenter/folder";
  37. var request = restHost.createRequest("GET", operationUrl, "");
  38. request.setHeader("Accept", "application/json");
  39. request.setHeader("Content-Type", "application/json");
  40. request.setHeader("vmware-api-session-id", sessionid);
  41. resp = request.execute();
  42. statusCode = resp.statusCode;
  43. if (statusCode == 401) {
  44. console.log("Bad sessionId");
  45. exit();
  46. } else if (statusCode != 200) {
  47. console.log("Error");
  48. exit();
  49. }
  50. // Parse Json Body to find all folder in one json object and stock them in folder array variable
  51. var jsonBody = JSON.parse(resp.contentAsString).value;
  52. var availableValues = [];
  53. for (var i in jsonBody) {
  54. var testVal = jsonBody[i].name;
  55. availableValues.push({'value': testVal, 'displayName': testVal, 'description': ''});
  56. }
  57. // Display all folder
  58. //console.log(folder);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement