Search in sources :

Example 11 with ServiceId

use of co.cask.cdap.proto.id.ServiceId in project cdap by caskdata.

the class CallServiceCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String method = arguments.get(ArgumentName.HTTP_METHOD.toString());
    String path = arguments.get(ArgumentName.ENDPOINT.toString());
    path = path.startsWith("/") ? path.substring(1) : path;
    String headers = arguments.getOptional(ArgumentName.HEADERS.toString(), "");
    String bodyString = arguments.getOptional(ArgumentName.HTTP_BODY.toString(), "");
    String bodyFile = arguments.getOptional(ArgumentName.LOCAL_FILE_PATH.toString(), "");
    Preconditions.checkNotNull(bodyString);
    Preconditions.checkNotNull(bodyFile);
    if (!bodyString.isEmpty() && !bodyFile.isEmpty()) {
        String message = String.format("Please provide either [body <%s>] or [body:file <%s>], " + "but not both", ArgumentName.HTTP_BODY.toString(), ArgumentName.LOCAL_FILE_PATH.toString());
        throw new CommandInputError(this, message);
    }
    Map<String, String> headerMap = GSON.fromJson(headers, new TypeToken<Map<String, String>>() {
    }.getType());
    ServiceId service = new ServiceId(parseProgramId(arguments, ElementType.SERVICE));
    URL url = arguments.hasArgument(ArgumentName.APP_VERSION.getName()) ? new URL(serviceClient.getVersionedServiceURL(service), path) : new URL(serviceClient.getServiceURL(service), path);
    HttpMethod httpMethod = HttpMethod.valueOf(method);
    HttpRequest.Builder builder = HttpRequest.builder(httpMethod, url).addHeaders(headerMap);
    if (httpMethod == HttpMethod.GET && (!bodyFile.isEmpty() || !bodyString.isEmpty())) {
        throw new UnsupportedOperationException("Sending body in a GET request is not supported");
    }
    if (!bodyFile.isEmpty()) {
        builder.withBody(filePathResolver.resolvePathToFile(bodyFile));
    } else if (!bodyString.isEmpty()) {
        builder.withBody(bodyString);
    }
    HttpResponse response = restClient.execute(builder.build(), clientConfig.getAccessToken());
    output.printf("< %s %s\n", response.getResponseCode(), response.getResponseMessage());
    for (Map.Entry<String, String> header : response.getHeaders().entries()) {
        output.printf("< %s: %s\n", header.getKey(), header.getValue());
    }
    output.print(response.getResponseBodyAsString());
}
Also used : CommandInputError(co.cask.cdap.cli.exception.CommandInputError) HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL) ServiceId(co.cask.cdap.proto.id.ServiceId) TypeToken(com.google.common.reflect.TypeToken) Map(java.util.Map) HttpMethod(co.cask.common.http.HttpMethod)

Example 12 with ServiceId

use of co.cask.cdap.proto.id.ServiceId in project cdap by caskdata.

the class CheckServiceAvailabilityCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    ServiceId serviceId = new ServiceId(parseProgramId(arguments, ElementType.SERVICE));
    serviceClient.checkAvailability(serviceId);
    output.println("Service is available to accept requests.");
}
Also used : ServiceId(co.cask.cdap.proto.id.ServiceId)

Example 13 with ServiceId

use of co.cask.cdap.proto.id.ServiceId in project cdap by caskdata.

the class GetRouteConfigCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    ServiceId serviceId = new ServiceId(parseProgramId(arguments, ElementType.SERVICE));
    Map<String, Integer> routeConfig = serviceClient.getRouteConfig(serviceId);
    output.printf(GSON.toJson(routeConfig));
}
Also used : ServiceId(co.cask.cdap.proto.id.ServiceId)

Example 14 with ServiceId

use of co.cask.cdap.proto.id.ServiceId in project cdap by caskdata.

the class GetServiceEndpointsCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    ServiceId serviceId = new ServiceId(parseProgramId(arguments, ElementType.SERVICE));
    List<ServiceHttpEndpoint> endpoints = serviceClient.getEndpoints(serviceId);
    Table table = Table.builder().setHeader("method", "path").setRows(endpoints, new RowMaker<ServiceHttpEndpoint>() {

        @Override
        public List<?> makeRow(ServiceHttpEndpoint endpoint) {
            return Lists.newArrayList(endpoint.getMethod(), endpoint.getPath());
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, output, table);
}
Also used : ServiceHttpEndpoint(co.cask.cdap.api.service.http.ServiceHttpEndpoint) Table(co.cask.cdap.cli.util.table.Table) RowMaker(co.cask.cdap.cli.util.RowMaker) ServiceId(co.cask.cdap.proto.id.ServiceId)

Example 15 with ServiceId

use of co.cask.cdap.proto.id.ServiceId in project cdap by caskdata.

the class CLIMainTest method testRouteConfig.

@Test
public void testRouteConfig() throws Exception {
    ServiceId service = FAKE_APP_ID.service(PrefixedEchoHandler.NAME);
    ServiceId serviceV1 = FAKE_APP_ID_V_1.service(PrefixedEchoHandler.NAME);
    String serviceName = String.format("%s.%s", FakeApp.NAME, PrefixedEchoHandler.NAME);
    String serviceArgument = String.format("%s version %s", serviceName, ApplicationId.DEFAULT_VERSION);
    String serviceV1Argument = String.format("%s version %s", serviceName, V1_SNAPSHOT);
    try {
        // Start service with default version
        Map<String, String> runtimeArgs = ImmutableMap.of("sdf", ApplicationId.DEFAULT_VERSION);
        String runtimeArgsKV = SPACE_JOINER.withKeyValueSeparator("=").join(runtimeArgs);
        testCommandOutputContains(cli, "start service " + serviceArgument + " '" + runtimeArgsKV + "'", "Successfully started service");
        assertProgramStatus(programClient, service, "RUNNING");
        // Verify that RouteConfig is empty initially
        testCommandOutputContains(cli, "get route-config for service " + serviceName, GSON.toJson(ImmutableMap.of()));
        // Call non-version service and get response from service with Default version
        testCommandOutputContains(cli, "call service " + serviceName + " POST /echo body \"testBody\"", String.format("%s:testBody", ApplicationId.DEFAULT_VERSION));
        // Start serviceV1
        Map<String, String> runtimeArgs1 = ImmutableMap.of("sdf", V1_SNAPSHOT);
        String runtimeArgs1KV = SPACE_JOINER.withKeyValueSeparator("=").join(runtimeArgs1);
        testCommandOutputContains(cli, "start service " + serviceV1Argument + " '" + runtimeArgs1KV + "'", "Successfully started service");
        assertProgramStatus(programClient, serviceV1, "RUNNING");
        // Set RouteConfig to route all traffic to service with default version
        Map<String, Integer> routeConfig = ImmutableMap.of(ApplicationId.DEFAULT_VERSION, 100, V1_SNAPSHOT, 0);
        String routeConfigKV = SPACE_JOINER.withKeyValueSeparator("=").join(routeConfig);
        testCommandOutputContains(cli, "set route-config for service " + serviceName + " '" + routeConfigKV + "'", "Successfully set route configuration");
        for (int i = 0; i < 10; i++) {
            testCommandOutputContains(cli, "call service " + serviceName + " POST /echo body \"testBody\"", String.format("%s:testBody", ApplicationId.DEFAULT_VERSION));
        }
        // Get the RouteConfig set previously
        testCommandOutputContains(cli, "get route-config for service " + serviceName, GSON.toJson(routeConfig));
        // Set RouteConfig to route all traffic to serviceV1
        routeConfig = ImmutableMap.of(ApplicationId.DEFAULT_VERSION, 0, V1_SNAPSHOT, 100);
        routeConfigKV = SPACE_JOINER.withKeyValueSeparator("=").join(routeConfig);
        testCommandOutputContains(cli, "set route-config for service " + serviceName + " '" + routeConfigKV + "'", "Successfully set route configuration");
        for (int i = 0; i < 10; i++) {
            testCommandOutputContains(cli, "call service " + serviceName + " POST /echo body \"testBody\"", String.format("%s:testBody", V1_SNAPSHOT));
        }
        // Get the RouteConfig set previously
        testCommandOutputContains(cli, "get route-config for service " + serviceName, GSON.toJson(routeConfig));
        // Delete the RouteConfig and verify the RouteConfig is empty
        testCommandOutputContains(cli, "delete route-config for service " + serviceName, "Successfully deleted route configuration");
        testCommandOutputContains(cli, "get route-config for service " + serviceName, GSON.toJson(ImmutableMap.of()));
    } finally {
        // Stop all running services
        programClient.stopAll(NamespaceId.DEFAULT);
    }
}
Also used : ServiceId(co.cask.cdap.proto.id.ServiceId) Test(org.junit.Test)

Aggregations

ServiceId (co.cask.cdap.proto.id.ServiceId)17 Test (org.junit.Test)5 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3 ProgramIdArgument (co.cask.cdap.cli.ProgramIdArgument)2 CommandInputError (co.cask.cdap.cli.exception.CommandInputError)2 HttpResponse (co.cask.common.http.HttpResponse)2 URL (java.net.URL)2 ServiceHttpEndpoint (co.cask.cdap.api.service.http.ServiceHttpEndpoint)1 RowMaker (co.cask.cdap.cli.util.RowMaker)1 Table (co.cask.cdap.cli.util.table.Table)1 EndpointStrategy (co.cask.cdap.common.discovery.EndpointStrategy)1 RandomEndpointStrategy (co.cask.cdap.common.discovery.RandomEndpointStrategy)1 BatchProgramStatus (co.cask.cdap.proto.BatchProgramStatus)1 ProgramStatus (co.cask.cdap.proto.ProgramStatus)1 FlowletId (co.cask.cdap.proto.id.FlowletId)1 ProgramId (co.cask.cdap.proto.id.ProgramId)1 HttpMethod (co.cask.common.http.HttpMethod)1 HttpRequest (co.cask.common.http.HttpRequest)1 TypeToken (com.google.common.reflect.TypeToken)1 File (java.io.File)1