diff options
author | Martin Polden <mpolden@mpolden.no> | 2018-03-06 10:24:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-06 10:24:08 +0100 |
commit | 208480c8c395c8cff621723ce78bf19880f4fec9 (patch) | |
tree | 469589050f7c118ff83b4699fd2ace1b3ef984e5 | |
parent | 868ffa919376734fecbe2032a018ec89d0571206 (diff) | |
parent | f578d34f70bf844a1ae032a8eed7a0f658512891 (diff) |
Merge pull request #5218 from vespa-engine/mpolden/set-service-view-user-agent
Set user agent for client used in service view
6 files changed, 99 insertions, 92 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/serviceview/StateResource.java b/configserver/src/main/java/com/yahoo/vespa/serviceview/StateResource.java index cd7559e775a..a6d4c229500 100644 --- a/configserver/src/main/java/com/yahoo/vespa/serviceview/StateResource.java +++ b/configserver/src/main/java/com/yahoo/vespa/serviceview/StateResource.java @@ -1,12 +1,13 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.serviceview; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.HashMap; -import java.util.List; -import java.util.ListIterator; -import java.util.Map; +import com.yahoo.container.jaxrs.annotation.Component; +import com.yahoo.vespa.serviceview.bindings.ApplicationView; +import com.yahoo.vespa.serviceview.bindings.ConfigClient; +import com.yahoo.vespa.serviceview.bindings.HealthClient; +import com.yahoo.vespa.serviceview.bindings.ModelResponse; +import com.yahoo.vespa.serviceview.bindings.StateClient; +import org.glassfish.jersey.client.proxy.WebResourceFactory; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -14,19 +15,20 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.ClientRequestFilter; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriInfo; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; -import com.yahoo.container.jaxrs.annotation.Component; -import com.yahoo.vespa.serviceview.bindings.ApplicationView; -import com.yahoo.vespa.serviceview.bindings.ConfigClient; -import com.yahoo.vespa.serviceview.bindings.HealthClient; -import com.yahoo.vespa.serviceview.bindings.ModelResponse; -import com.yahoo.vespa.serviceview.bindings.StateClient; - -import org.glassfish.jersey.client.proxy.WebResourceFactory; +import static java.util.Collections.singletonList; /** @@ -37,7 +39,9 @@ import org.glassfish.jersey.client.proxy.WebResourceFactory; @Path("/") public class StateResource implements StateClient { + private static final String USER_AGENT = "service-view-config-server-client"; private static final String SINGLE_API_LINK = "url"; + private final int restApiPort; private final String host; private final UriInfo uriInfo; @@ -55,7 +59,7 @@ public class StateResource implements StateClient { public StateResource(@Component ConfigServerLocation configServer, @Context UriInfo ui) { this.restApiPort = configServer.restApiPort; - host = "localhost"; + this.host = "localhost"; this.uriInfo = ui; } @@ -87,7 +91,7 @@ public class StateResource implements StateClient { @Produces(MediaType.TEXT_HTML) public interface HtmlProxyHack { @GET - public String proxy(); + String proxy(); } @GET @@ -103,7 +107,7 @@ public class StateResource implements StateClient { ServiceModel model = new ServiceModel(getModelConfig(tenantName, applicationName, environmentName, regionName, instanceName)); Service s = model.getService(identifier); int requestedPort = s.matchIdentifierWithPort(identifier); - Client client = ClientBuilder.newClient(); + Client client = client(); try { final StringBuilder uriBuffer = new StringBuilder("http://").append(s.host).append(':').append(requestedPort).append('/') .append(apiParams); @@ -113,9 +117,7 @@ public class StateResource implements StateClient { HtmlProxyHack resource = WebResourceFactory.newResource(HtmlProxyHack.class, target); return resource.proxy(); } finally { - if (client != null) { - client.close(); - } + client.close(); } } @@ -129,17 +131,13 @@ public class StateResource implements StateClient { } protected ModelResponse getModelConfig(String tenant, String application, String environment, String region, String instance) { - Client client = ClientBuilder.newClient(); + Client client = client(); try { WebTarget target = client.target("http://" + host + ":" + restApiPort + "/"); - ConfigClient resource = WebResourceFactory.newResource(ConfigClient.class, target); - return resource.getServiceModel(tenant, application, environment, region, instance); } finally { - if (client != null) { - client.close(); - } + client.close(); } } @@ -158,16 +156,14 @@ public class StateResource implements StateClient { ServiceModel model = new ServiceModel(getModelConfig(tenantName, applicationName, environmentName, regionName, instanceName)); Service s = model.getService(identifier); int requestedPort = s.matchIdentifierWithPort(identifier); - Client client = ClientBuilder.newClient(); + Client client = client(); try { HealthClient resource = getHealthClient(apiParams, s, requestedPort, client); HashMap<?, ?> apiResult = resource.getHealthInfo(); rewriteResourceLinks(apiResult, model, s, applicationIdentifier(tenantName, applicationName, environmentName, regionName, instanceName), identifier); return apiResult; } finally { - if (client != null) { - client.close(); - } + client.close(); } } @@ -180,8 +176,11 @@ public class StateResource implements StateClient { } private String applicationIdentifier(String tenant, String application, String environment, String region, String instance) { - return new StringBuilder("tenant/").append(tenant).append("/application/").append(application).append("/environment/") - .append(environment).append("/region/").append(region).append("/instance/").append(instance).toString(); + return "tenant/" + tenant + + "/application/" + application + + "/environment/" + environment + + "/region/" + region + + "/instance/" + instance; } private void rewriteResourceLinks(Object apiResult, @@ -279,4 +278,11 @@ public class StateResource implements StateClient { newUri.append(link.getRawPath()); } + private static Client client() { + return ClientBuilder.newBuilder() + .register((ClientRequestFilter) ctx -> ctx.getHeaders().put(HttpHeaders.USER_AGENT, + singletonList(USER_AGENT))) + .build(); + } + } diff --git a/configserver/src/test/java/com/yahoo/vespa/serviceview/ServiceModelTest.java b/configserver/src/test/java/com/yahoo/vespa/serviceview/ServiceModelTest.java index 6371ee25a8c..c7b08c08387 100644 --- a/configserver/src/test/java/com/yahoo/vespa/serviceview/ServiceModelTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/serviceview/ServiceModelTest.java @@ -1,32 +1,33 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.serviceview; -import static org.junit.Assert.*; - -import java.util.Arrays; - import com.yahoo.vespa.defaults.Defaults; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - import com.yahoo.vespa.serviceview.bindings.ApplicationView; import com.yahoo.vespa.serviceview.bindings.HostService; import com.yahoo.vespa.serviceview.bindings.ModelResponse; import com.yahoo.vespa.serviceview.bindings.ServicePort; import com.yahoo.vespa.serviceview.bindings.ServiceView; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; /** * Functional tests for the programmatic view of cloud.config.model. * - * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a> + * @author Steinar Knutsen */ public class ServiceModelTest { - ServiceModel model; + private ServiceModel model; @Before - public void setUp() throws Exception { + public void setUp() { ModelResponse model = syntheticModelResponse(); this.model = new ServiceModel(model); } @@ -46,7 +47,7 @@ public class ServiceModelTest { ServicePort port = new ServicePort(); port.number = Defaults.getDefaults().vespaWebServicePort(); port.tags = "state http"; - service0.ports = Arrays.asList(new ServicePort[] { port }); + service0.ports = Collections.singletonList(port); } com.yahoo.vespa.serviceview.bindings.Service service1 = new com.yahoo.vespa.serviceview.bindings.Service(); { @@ -59,7 +60,7 @@ public class ServiceModelTest { ServicePort port = new ServicePort(); port.number = 4090; port.tags = "state http"; - service1.ports = Arrays.asList(new ServicePort[] { port }); + service1.ports = Collections.singletonList(port); } com.yahoo.vespa.serviceview.bindings.Service service2 = new com.yahoo.vespa.serviceview.bindings.Service(); { @@ -72,15 +73,15 @@ public class ServiceModelTest { ServicePort port = new ServicePort(); port.number = 5000; port.tags = "state http"; - service2.ports = Arrays.asList(new ServicePort[] { port }); + service2.ports = Collections.singletonList(port); } - h.services = Arrays.asList(new com.yahoo.vespa.serviceview.bindings.Service[] { service0, service1, service2 }); - model.hosts = Arrays.asList(new HostService[] { h }); + h.services = Arrays.asList(service0, service1, service2); + model.hosts = Collections.singletonList(h); return model; } @After - public void tearDown() throws Exception { + public void tearDown() { model = null; } diff --git a/configserver/src/test/java/com/yahoo/vespa/serviceview/StateResourceTest.java b/configserver/src/test/java/com/yahoo/vespa/serviceview/StateResourceTest.java index 5e4cdd1a6c9..794b825db6e 100644 --- a/configserver/src/test/java/com/yahoo/vespa/serviceview/StateResourceTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/serviceview/StateResourceTest.java @@ -1,33 +1,31 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.serviceview; -import static org.junit.Assert.*; +import com.yahoo.cloud.config.ConfigserverConfig; +import com.yahoo.container.jaxrs.annotation.Component; +import com.yahoo.vespa.serviceview.bindings.ApplicationView; +import com.yahoo.vespa.serviceview.bindings.HealthClient; +import com.yahoo.vespa.serviceview.bindings.ModelResponse; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import javax.ws.rs.client.Client; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.UriInfo; import java.net.URI; -import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.ws.rs.client.Client; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.UriInfo; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; - -import com.yahoo.cloud.config.ConfigserverConfig; -import com.yahoo.container.jaxrs.annotation.Component; -import com.yahoo.vespa.serviceview.bindings.ApplicationView; -import com.yahoo.vespa.serviceview.bindings.HealthClient; -import com.yahoo.vespa.serviceview.bindings.ModelResponse; +import static org.junit.Assert.assertEquals; /** * Functional test for {@link StateResource}. * - * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a> + * @author Steinar Knutsen */ public class StateResourceTest { @@ -51,14 +49,14 @@ public class StateResourceTest { HashMap<Object, Object> dummyHealthData = new HashMap<>(); HashMap<String, String> dummyLink = new HashMap<>(); dummyLink.put("url", BASE_URI); - dummyHealthData.put("resources", Arrays.asList(dummyLink)); + dummyHealthData.put("resources", Collections.singletonList(dummyLink)); Mockito.when(healthClient.getHealthInfo()).thenReturn(dummyHealthData); return healthClient; } } - StateResource testResource; - ServiceModel correspondingModel; + private StateResource testResource; + private ServiceModel correspondingModel; @Before public void setUp() throws Exception { @@ -70,7 +68,7 @@ public class StateResourceTest { } @After - public void tearDown() throws Exception { + public void tearDown() { testResource = null; correspondingModel = null; } diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/restapi/v2/Authorizer.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/restapi/v2/Authorizer.java index 9541364a5e8..ec49560ebd9 100644 --- a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/restapi/v2/Authorizer.java +++ b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/restapi/v2/Authorizer.java @@ -167,7 +167,7 @@ public class Authorizer implements BiPredicate<Principal, URI> { path = path.substring(0, path.length() - 1); } int lastSeparator = path.lastIndexOf("/"); - if (lastSeparator == - 1) { + if (lastSeparator == -1) { return path; } return path.substring(lastSeparator + 1, path.length()); diff --git a/serviceview/src/main/java/com/yahoo/vespa/serviceview/bindings/ConfigClient.java b/serviceview/src/main/java/com/yahoo/vespa/serviceview/bindings/ConfigClient.java index b31afcd4df1..bc40586b473 100644 --- a/serviceview/src/main/java/com/yahoo/vespa/serviceview/bindings/ConfigClient.java +++ b/serviceview/src/main/java/com/yahoo/vespa/serviceview/bindings/ConfigClient.java @@ -10,16 +10,16 @@ import javax.ws.rs.core.MediaType; /** * Client to fetch the model config from the configserver. * - * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a> + * @author Steinar Knutsen */ public interface ConfigClient { @GET @Path("/config/v2/tenant/{tenantName}/application/{applicationName}/environment/{environmentName}/region/{regionName}/instance/{instanceName}/cloud.config.model") @Produces(MediaType.APPLICATION_JSON) - public ModelResponse getServiceModel(@PathParam("tenantName") String tenantName, - @PathParam("applicationName") String applicationName, - @PathParam("environmentName") String environmentName, - @PathParam("regionName") String regionName, - @PathParam("instanceName") String instanceName); + ModelResponse getServiceModel(@PathParam("tenantName") String tenantName, + @PathParam("applicationName") String applicationName, + @PathParam("environmentName") String environmentName, + @PathParam("regionName") String regionName, + @PathParam("instanceName") String instanceName); } diff --git a/serviceview/src/main/java/com/yahoo/vespa/serviceview/bindings/StateClient.java b/serviceview/src/main/java/com/yahoo/vespa/serviceview/bindings/StateClient.java index fcb5058920a..5e5ee023d84 100644 --- a/serviceview/src/main/java/com/yahoo/vespa/serviceview/bindings/StateClient.java +++ b/serviceview/src/main/java/com/yahoo/vespa/serviceview/bindings/StateClient.java @@ -1,39 +1,41 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.serviceview.bindings; -import java.util.HashMap; - import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; +import java.util.HashMap; +/** + * @author Steinar Knutsen + */ public interface StateClient { @GET @Path("v1/") @Produces(MediaType.APPLICATION_JSON) - public ApplicationView getDefaultUserInfo(); + ApplicationView getDefaultUserInfo(); @GET @Path("v1/tenant/{tenantName}/application/{applicationName}/environment/{environmentName}/region/{regionName}/instance/{instanceName}") @Produces(MediaType.APPLICATION_JSON) - public ApplicationView getUserInfo(@PathParam("tenantName") String tenantName, - @PathParam("applicationName") String applicationName, - @PathParam("environmentName") String environmentName, - @PathParam("regionName") String regionName, - @PathParam("instanceName") String instanceName); + ApplicationView getUserInfo(@PathParam("tenantName") String tenantName, + @PathParam("applicationName") String applicationName, + @PathParam("environmentName") String environmentName, + @PathParam("regionName") String regionName, + @PathParam("instanceName") String instanceName); @SuppressWarnings("rawtypes") @GET @Path("v1/tenant/{tenantName}/application/{applicationName}/environment/{environmentName}/region/{regionName}/instance/{instanceName}/service/{serviceIdentifier}/{apiParams: .*}") @Produces(MediaType.APPLICATION_JSON) - public HashMap singleService(@PathParam("tenantName") String tenantName, - @PathParam("applicationName") String applicationName, - @PathParam("environmentName") String environmentName, - @PathParam("regionName") String regionName, - @PathParam("instanceName") String instanceName, - @PathParam("serviceIdentifier") String identifier, - @PathParam("apiParams") String apiParams); + HashMap singleService(@PathParam("tenantName") String tenantName, + @PathParam("applicationName") String applicationName, + @PathParam("environmentName") String environmentName, + @PathParam("regionName") String regionName, + @PathParam("instanceName") String instanceName, + @PathParam("serviceIdentifier") String identifier, + @PathParam("apiParams") String apiParams); } |