You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InfoAction.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.ce.ws;
  21. import org.sonar.api.server.ws.Request;
  22. import org.sonar.api.server.ws.Response;
  23. import org.sonar.api.server.ws.WebService;
  24. import org.sonar.ce.queue.CeQueue;
  25. import org.sonar.process.ProcessProperties;
  26. import org.sonar.server.user.AbstractUserSession;
  27. import org.sonar.server.user.SystemPasscode;
  28. import org.sonar.server.user.UserSession;
  29. import org.sonar.server.ws.WsUtils;
  30. import org.sonarqube.ws.Ce;
  31. public class InfoAction implements CeWsAction {
  32. private final UserSession userSession;
  33. private final SystemPasscode systemPasscode;
  34. private final CeQueue ceQueue;
  35. public InfoAction(UserSession userSession, SystemPasscode systemPasscode, CeQueue ceQueue) {
  36. this.userSession = userSession;
  37. this.systemPasscode = systemPasscode;
  38. this.ceQueue = ceQueue;
  39. }
  40. @Override
  41. public void define(WebService.NewController controller) {
  42. controller.createAction("info")
  43. .setDescription("Gets information about Compute Engine. Requires the system administration permission or " +
  44. "system passcode (see " + ProcessProperties.Property.WEB_SYSTEM_PASS_CODE + " in sonar.properties).")
  45. .setSince("7.2")
  46. .setInternal(true)
  47. .setHandler(this)
  48. .setResponseExample(getClass().getResource("info-example.json"));
  49. }
  50. @Override
  51. public void handle(Request request, Response response) throws Exception {
  52. if (!systemPasscode.isValid(request) && !userSession.isSystemAdministrator()) {
  53. throw AbstractUserSession.insufficientPrivilegesException();
  54. }
  55. Ce.InfoWsResponse.Builder builder = Ce.InfoWsResponse.newBuilder();
  56. CeQueue.WorkersPauseStatus status = ceQueue.getWorkersPauseStatus();
  57. builder.setWorkersPauseStatus(convert(status));
  58. WsUtils.writeProtobuf(builder.build(), request, response);
  59. }
  60. private static Ce.WorkersPauseStatus convert(CeQueue.WorkersPauseStatus status) {
  61. switch (status) {
  62. case PAUSING:
  63. return Ce.WorkersPauseStatus.PAUSING;
  64. case PAUSED:
  65. return Ce.WorkersPauseStatus.PAUSED;
  66. case RESUMED:
  67. return Ce.WorkersPauseStatus.RESUMED;
  68. default:
  69. throw new IllegalStateException("Unsupported WorkersPauseStatus: " + status);
  70. }
  71. }
  72. }