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.

Elasticsearch.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.sonarqube.ws.tester;
  21. import java.io.IOException;
  22. import java.net.InetAddress;
  23. import okhttp3.MediaType;
  24. import okhttp3.OkHttpClient;
  25. import okhttp3.Request;
  26. import okhttp3.RequestBody;
  27. import okhttp3.Response;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. /**
  30. * Helper to directly access Elasticsearch. It requires the HTTP port
  31. * to be open.
  32. */
  33. public class Elasticsearch {
  34. private final int httpPort;
  35. public Elasticsearch(int httpPort) {
  36. this.httpPort = httpPort;
  37. }
  38. /**
  39. * Forbid indexing requests on the specified index. Index becomes read-only.
  40. */
  41. public void lockWrites(String index) throws IOException {
  42. putIndexSetting(httpPort, index, "blocks.write", "true");
  43. }
  44. /**
  45. * Enable indexing requests on the specified index.
  46. * @see #lockWrites(String)
  47. */
  48. public void unlockWrites(String index) throws IOException {
  49. putIndexSetting(httpPort, index, "blocks.write", "false");
  50. }
  51. public void makeYellow() throws IOException {
  52. putIndexSetting(httpPort, "issues", "number_of_replicas", "5");
  53. }
  54. public void makeGreen() throws IOException {
  55. putIndexSetting(httpPort, "issues", "number_of_replicas", "0");
  56. }
  57. private static void putIndexSetting(int searchHttpPort, String index, String key, String value) throws IOException {
  58. Request.Builder request = new Request.Builder()
  59. .url(baseUrl(searchHttpPort) + index + "/_settings")
  60. .put(RequestBody.create(MediaType.parse("application/json"), "{" +
  61. " \"index\" : {" +
  62. " \"" + key + "\" : \"" + value + "\"" +
  63. " }" +
  64. "}"));
  65. OkHttpClient okClient = new OkHttpClient.Builder().build();
  66. try (Response response = okClient.newCall(request.build()).execute()) {
  67. assertThat(response.isSuccessful()).isTrue();
  68. }
  69. }
  70. private static String baseUrl(int searchHttpPort) {
  71. return "http://" + InetAddress.getLoopbackAddress().getHostAddress() + ":" + searchHttpPort + "/";
  72. }
  73. }