Search in sources :

Example 6 with ServiceView

use of com.yahoo.vespa.serviceview.bindings.ServiceView in project vespa by vespa-engine.

the class ServiceApiResponse method rewriteIfUrl.

private String rewriteIfUrl(String urlOrAnyString, Uri requestUri) {
    if (urlOrAnyString == null)
        return null;
    String hostPattern = "(" + String.join("|", configServerURIs.stream().map(URI::toString).map(s -> s.substring(0, s.length() - 1)).map(Pattern::quote).toArray(String[]::new)) + ")";
    String remoteServicePath = "/serviceview/" + "v1/tenant/" + application.tenant().value() + "/application/" + application.application().value() + "/environment/" + zone.environment().value() + "/region/" + zone.region().value() + "/instance/" + application.instance() + "/service/";
    Pattern remoteServiceResourcePattern = Pattern.compile("^(" + hostPattern + Pattern.quote(remoteServicePath) + ")");
    Matcher matcher = remoteServiceResourcePattern.matcher(urlOrAnyString);
    if (matcher.find()) {
        String proxiedPath = urlOrAnyString.substring(matcher.group().length());
        return requestUri.append(proxiedPath).toString();
    } else {
        // not a service url
        return urlOrAnyString;
    }
}
Also used : OutputStream(java.io.OutputStream) Cursor(com.yahoo.slime.Cursor) ApplicationId(com.yahoo.config.provision.ApplicationId) Slime(com.yahoo.slime.Slime) IOException(java.io.IOException) ApplicationView(com.yahoo.vespa.serviceview.bindings.ApplicationView) List(java.util.List) ZoneId(com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneId) Matcher(java.util.regex.Matcher) ServiceView(com.yahoo.vespa.serviceview.bindings.ServiceView) JsonFormat(com.yahoo.slime.JsonFormat) Uri(com.yahoo.vespa.hosted.controller.restapi.Uri) Map(java.util.Map) ClusterView(com.yahoo.vespa.serviceview.bindings.ClusterView) URI(java.net.URI) Pattern(java.util.regex.Pattern) HttpResponse(com.yahoo.container.jdisc.HttpResponse) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) URI(java.net.URI)

Example 7 with ServiceView

use of com.yahoo.vespa.serviceview.bindings.ServiceView in project vespa by vespa-engine.

the class ServiceApiResponseTest method testServiceViewResponseWithURLs.

@Test
public void testServiceViewResponseWithURLs() throws URISyntaxException, IOException {
    ServiceApiResponse response = new ServiceApiResponse(ZoneId.from(Environment.prod, RegionName.from("us-west-1")), ApplicationId.from("tenant2", "application2", "default"), Collections.singletonList(new URI("http://cfg1.test/")), new URI("http://cfg1.test/serviceview/v1/tenant/tenant2/application/application2/environment/prod/region/us-west-1/instance/default/service/searchnode-9dujk1pa0vufxrj6n4yvmi8uc/state/v1"));
    ApplicationView applicationView = new ApplicationView();
    ClusterView clusterView = new ClusterView();
    clusterView.type = "container";
    clusterView.name = "cluster1";
    clusterView.url = "http://cfg1.test/serviceview/v1/tenant/tenant2/application/application2/environment/prod/region/us-west-1/instance/default/service/searchnode-9dujk1pa0vufxrj6n4yvmi8uc/state/v1/health";
    ServiceView serviceView = new ServiceView();
    serviceView.url = null;
    serviceView.serviceType = "container";
    serviceView.serviceName = "service1";
    serviceView.configId = "configId1";
    serviceView.host = "host1";
    serviceView.legacyStatusPages = "legacyPages";
    clusterView.services = Collections.singletonList(serviceView);
    applicationView.clusters = Collections.singletonList(clusterView);
    response.setResponse(applicationView);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    response.render(stream);
    Slime responseSlime = SlimeUtils.jsonToSlime(stream.toByteArray());
    Slime expectedSlime = SlimeUtils.jsonToSlime(IOUtils.readFile(new File(responseFiles + "service-api-response-with-urls.json")).getBytes(StandardCharsets.UTF_8));
    assertEquals("service-api-response.json", new String(SlimeUtils.toJsonBytes(expectedSlime), StandardCharsets.UTF_8), new String(SlimeUtils.toJsonBytes(responseSlime), StandardCharsets.UTF_8));
}
Also used : ClusterView(com.yahoo.vespa.serviceview.bindings.ClusterView) ServiceView(com.yahoo.vespa.serviceview.bindings.ServiceView) ApplicationView(com.yahoo.vespa.serviceview.bindings.ApplicationView) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Slime(com.yahoo.slime.Slime) URI(java.net.URI) File(java.io.File) Test(org.junit.Test)

Aggregations

ServiceView (com.yahoo.vespa.serviceview.bindings.ServiceView)7 ClusterView (com.yahoo.vespa.serviceview.bindings.ClusterView)6 ApplicationView (com.yahoo.vespa.serviceview.bindings.ApplicationView)5 Slime (com.yahoo.slime.Slime)3 URI (java.net.URI)3 Test (org.junit.Test)3 Cursor (com.yahoo.slime.Cursor)2 HostService (com.yahoo.vespa.serviceview.bindings.HostService)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 ApplicationId (com.yahoo.config.provision.ApplicationId)1 HttpResponse (com.yahoo.container.jdisc.HttpResponse)1 JsonFormat (com.yahoo.slime.JsonFormat)1 ZoneId (com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneId)1 Uri (com.yahoo.vespa.hosted.controller.restapi.Uri)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1