diff options
author | Valerij Fredriksen <valerijf@verizonmedia.com> | 2021-06-14 14:03:07 +0200 |
---|---|---|
committer | Valerij Fredriksen <valerijf@verizonmedia.com> | 2021-06-14 14:03:07 +0200 |
commit | 33e23de1c0df684628aeeb4a18971325713dbe57 (patch) | |
tree | 74191279e00b29a548a06c089fea7c8b884dca6b | |
parent | 291e527fb70897742f16caa4b3c7062cc6e47708 (diff) |
Proxy Horzion HTTP response code
4 files changed, 66 insertions, 40 deletions
diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/HorizonClient.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/HorizonClient.java index 77a36610dc7..554d3e5b7fa 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/HorizonClient.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/HorizonClient.java @@ -1,27 +1,25 @@ // Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.controller.api.integration.horizon; -import java.io.InputStream; - /** * @author olaa */ public interface HorizonClient { - InputStream getMetrics(byte[] query); + HorizonResponse getMetrics(byte[] query); - InputStream getUser(); + HorizonResponse getUser(); - InputStream getDashboard(String dashboardId) ; + HorizonResponse getDashboard(String dashboardId); - InputStream getFavorite(String userId); + HorizonResponse getFavorite(String userId); - InputStream getTopFolders(); + HorizonResponse getTopFolders(); - InputStream getRecent(String userId); + HorizonResponse getRecent(String userId); - InputStream getClipboard(String dashboardId); + HorizonResponse getClipboard(String dashboardId); - InputStream getMetaData(byte[] query); + HorizonResponse getMetaData(byte[] query); } diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/HorizonResponse.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/HorizonResponse.java new file mode 100644 index 00000000000..ba7540b2708 --- /dev/null +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/HorizonResponse.java @@ -0,0 +1,30 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.controller.api.integration.horizon; + +import java.io.InputStream; + +/** + * @author valerijf + */ +public class HorizonResponse { + + private final int code; + private final InputStream inputStream; + + public HorizonResponse(int code, InputStream inputStream) { + this.code = code; + this.inputStream = inputStream; + } + + public int code() { + return code; + } + + public InputStream inputStream() { + return inputStream; + } + + public static HorizonResponse empty() { + return new HorizonResponse(200, InputStream.nullInputStream()); + } +} diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/MockHorizonClient.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/MockHorizonClient.java index 0ecad973cff..13a8c2ec079 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/MockHorizonClient.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/horizon/MockHorizonClient.java @@ -1,50 +1,48 @@ // Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.controller.api.integration.horizon; -import java.io.InputStream; - /** * @author olaa */ public class MockHorizonClient implements HorizonClient { @Override - public InputStream getMetrics(byte[] query) { - return null; + public HorizonResponse getMetrics(byte[] query) { + return HorizonResponse.empty(); } @Override - public InputStream getUser() { - return null; + public HorizonResponse getUser() { + return HorizonResponse.empty(); } @Override - public InputStream getDashboard(String dashboardId) { - return null; + public HorizonResponse getDashboard(String dashboardId) { + return HorizonResponse.empty(); } @Override - public InputStream getFavorite(String userId) { - return null; + public HorizonResponse getFavorite(String userId) { + return HorizonResponse.empty(); } @Override - public InputStream getTopFolders() { - return null; + public HorizonResponse getTopFolders() { + return HorizonResponse.empty(); } @Override - public InputStream getRecent(String userId) { - return null; + public HorizonResponse getRecent(String userId) { + return HorizonResponse.empty(); } @Override - public InputStream getClipboard(String dashboardId) { - return null; + public HorizonResponse getClipboard(String dashboardId) { + return HorizonResponse.empty(); } @Override - public InputStream getMetaData(byte[] query) { - return null; + public HorizonResponse getMetaData(byte[] query) { + return HorizonResponse.empty(); } } diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/horizon/HorizonApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/horizon/HorizonApiHandler.java index a7875b0d1ae..6f5b1f30592 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/horizon/HorizonApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/horizon/HorizonApiHandler.java @@ -10,6 +10,7 @@ import com.yahoo.restapi.ErrorResponse; import com.yahoo.restapi.Path; import com.yahoo.vespa.hosted.controller.Controller; import com.yahoo.vespa.hosted.controller.api.integration.horizon.HorizonClient; +import com.yahoo.vespa.hosted.controller.api.integration.horizon.HorizonResponse; import com.yahoo.vespa.hosted.controller.api.role.SecurityContext; import com.yahoo.yolean.Exceptions; @@ -17,7 +18,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Optional; -import java.util.function.Supplier; import java.util.logging.Level; /** @@ -58,10 +58,10 @@ public class HorizonApiHandler extends LoggingRequestHandler { private HttpResponse get(HttpRequest request) { Path path = new Path(request.getUri()); - if (path.matches("/horizon/v1/config/dashboard/topFolders")) return new JsonInputStreamResponse(client::getTopFolders); - if (path.matches("/horizon/v1/config/dashboard/file/{id}")) return new JsonInputStreamResponse(() -> client.getDashboard(path.get("id"))); - if (path.matches("/horizon/v1/config/dashboard/favorite")) return new JsonInputStreamResponse(() -> client.getFavorite(request.getProperty("user"))); - if (path.matches("/horizon/v1/config/dashboard/recent")) return new JsonInputStreamResponse(() -> client.getRecent(request.getProperty("user"))); + if (path.matches("/horizon/v1/config/dashboard/topFolders")) return new JsonInputStreamResponse(client.getTopFolders()); + if (path.matches("/horizon/v1/config/dashboard/file/{id}")) return new JsonInputStreamResponse(client.getDashboard(path.get("id"))); + if (path.matches("/horizon/v1/config/dashboard/favorite")) return new JsonInputStreamResponse(client.getFavorite(request.getProperty("user"))); + if (path.matches("/horizon/v1/config/dashboard/recent")) return new JsonInputStreamResponse(client.getRecent(request.getProperty("user"))); return ErrorResponse.notFoundError("Nothing at " + path); } @@ -74,7 +74,7 @@ public class HorizonApiHandler extends LoggingRequestHandler { private HttpResponse put(HttpRequest request) { Path path = new Path(request.getUri()); - if (path.matches("/horizon/v1/config/user")) return new JsonInputStreamResponse(client::getUser); + if (path.matches("/horizon/v1/config/user")) return new JsonInputStreamResponse(client.getUser()); return ErrorResponse.notFoundError("Nothing at " + path); } @@ -82,7 +82,7 @@ public class HorizonApiHandler extends LoggingRequestHandler { SecurityContext securityContext = getAttribute(request, SecurityContext.ATTRIBUTE_NAME, SecurityContext.class); try { byte[] data = TsdbQueryRewriter.rewrite(request.getData().readAllBytes(), securityContext.roles(), systemName); - return new JsonInputStreamResponse(() -> isMetricQuery ? client.getMetrics(data) : client.getMetaData(data)); + return new JsonInputStreamResponse(isMetricQuery ? client.getMetrics(data) : client.getMetaData(data)); } catch (TsdbQueryRewriter.UnauthorizedException e) { return ErrorResponse.forbidden("Access denied"); } catch (IOException e) { @@ -99,11 +99,11 @@ public class HorizonApiHandler extends LoggingRequestHandler { private static class JsonInputStreamResponse extends HttpResponse { - private final Supplier<InputStream> inputStreamSupplier; + private final HorizonResponse response; - public JsonInputStreamResponse(Supplier<InputStream> inputStreamSupplier) { - super(200); - this.inputStreamSupplier = inputStreamSupplier; + public JsonInputStreamResponse(HorizonResponse response) { + super(response.code()); + this.response = response; } @Override @@ -113,7 +113,7 @@ public class HorizonApiHandler extends LoggingRequestHandler { @Override public void render(OutputStream outputStream) throws IOException { - try (InputStream inputStream = inputStreamSupplier.get()) { + try (InputStream inputStream = response.inputStream()) { inputStream.transferTo(outputStream); } } |