Search in sources :

Example 1 with ServiceId

use of io.cdap.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(io.cdap.cdap.cli.exception.CommandInputError) HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) ServiceId(io.cdap.cdap.proto.id.ServiceId) TypeToken(com.google.common.reflect.TypeToken) Map(java.util.Map) HttpMethod(io.cdap.common.http.HttpMethod)

Example 2 with ServiceId

use of io.cdap.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(io.cdap.cdap.api.service.http.ServiceHttpEndpoint) Table(io.cdap.cdap.cli.util.table.Table) RowMaker(io.cdap.cdap.cli.util.RowMaker) ServiceId(io.cdap.cdap.proto.id.ServiceId)

Example 3 with ServiceId

use of io.cdap.cdap.proto.id.ServiceId in project cdap by caskdata.

the class SetProgramInstancesCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String[] programIdParts = arguments.get(elementType.getArgumentName().toString()).split("\\.");
    ApplicationId appId = cliConfig.getCurrentNamespace().app(programIdParts[0]);
    int numInstances = arguments.getInt(ArgumentName.NUM_INSTANCES.toString());
    switch(elementType) {
        case WORKER:
            if (programIdParts.length < 2) {
                throw new CommandInputError(this);
            }
            String workerName = programIdParts[1];
            ProgramId workerId = appId.worker(workerName);
            programClient.setWorkerInstances(workerId, numInstances);
            output.printf("Successfully set worker '%s' of app '%s' to %d instances\n", workerName, appId.getEntityName(), numInstances);
            break;
        case SERVICE:
            if (programIdParts.length < 2) {
                throw new CommandInputError(this);
            }
            String serviceName = programIdParts[1];
            ServiceId service = appId.service(serviceName);
            programClient.setServiceInstances(service, numInstances);
            output.printf("Successfully set service '%s' of app '%s' to %d instances\n", serviceName, appId.getEntityName(), numInstances);
            break;
        default:
            // TODO: remove this
            throw new IllegalArgumentException("Unrecognized program element type for scaling: " + elementType);
    }
}
Also used : CommandInputError(io.cdap.cdap.cli.exception.CommandInputError) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) ServiceId(io.cdap.cdap.proto.id.ServiceId)

Example 4 with ServiceId

use of io.cdap.cdap.proto.id.ServiceId in project cdap by caskdata.

the class CLITestBase method testRuntimeArgs.

@Test
public void testRuntimeArgs() throws Exception {
    String qualifiedServiceId = String.format("%s.%s", FakeApp.NAME, PrefixedEchoHandler.NAME);
    ServiceId service = NamespaceId.DEFAULT.app(FakeApp.NAME).service(PrefixedEchoHandler.NAME);
    testServiceRuntimeArgs(qualifiedServiceId, service);
}
Also used : ServiceId(io.cdap.cdap.proto.id.ServiceId) Test(org.junit.Test)

Example 5 with ServiceId

use of io.cdap.cdap.proto.id.ServiceId in project cdap by caskdata.

the class CLITestBase method testService.

@Test
public void testService() throws Exception {
    ProgramClient programClient = getProgramClient();
    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 {
        // Test service commands with no optional version argument
        testCommandOutputContains("start service " + serviceName, "Successfully started service");
        assertProgramStatus(programClient, service, ProgramStatus.RUNNING.name());
        testCommandOutputContains("get endpoints service " + serviceName, "POST");
        testCommandOutputContains("get endpoints service " + serviceName, "/echo");
        testCommandOutputContains("check service availability " + serviceName, "Service is available");
        testCommandOutputContains("call service " + serviceName + " POST /echo body \"testBody\"", ":testBody");
        testCommandOutputContains("stop service " + serviceName, "Successfully stopped service");
        assertProgramStatus(programClient, service, ProgramStatus.STOPPED.name());
        // Test service commands with version argument when two versions of the service are running
        testCommandOutputContains("start service " + serviceArgument, "Successfully started service");
        testCommandOutputContains("start service " + serviceV1Argument, "Successfully started service");
        assertProgramStatus(programClient, service, ProgramStatus.RUNNING.name());
        assertProgramStatus(programClient, serviceV1, ProgramStatus.RUNNING.name());
        testCommandOutputContains("get endpoints service " + serviceArgument, "POST");
        testCommandOutputContains("get endpoints service " + serviceV1Argument, "POST");
        testCommandOutputContains("get endpoints service " + serviceArgument, "/echo");
        testCommandOutputContains("get endpoints service " + serviceV1Argument, "/echo");
        testCommandOutputContains("check service availability " + serviceArgument, "Service is available");
        testCommandOutputContains("check service availability " + serviceV1Argument, "Service is available");
        testCommandOutputContains("call service " + serviceArgument + " POST /echo body \"testBody\"", ":testBody");
        testCommandOutputContains("call service " + serviceV1Argument + " POST /echo body \"testBody\"", ":testBody");
        testCommandOutputContains("get service logs " + serviceName, "Starting HTTP server for Service " + service);
    } finally {
        // Stop all running services
        programClient.stopAll(NamespaceId.DEFAULT);
    }
}
Also used : ProgramClient(io.cdap.cdap.client.ProgramClient) ServiceId(io.cdap.cdap.proto.id.ServiceId) Test(org.junit.Test)

Aggregations

ServiceId (io.cdap.cdap.proto.id.ServiceId)16 Test (org.junit.Test)10 HttpResponse (io.cdap.common.http.HttpResponse)7 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)5 URL (java.net.URL)4 Gson (com.google.gson.Gson)3 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 JsonObject (com.google.gson.JsonObject)2 ProgramIdArgument (io.cdap.cdap.cli.ProgramIdArgument)2 CommandInputError (io.cdap.cdap.cli.exception.CommandInputError)2 FakeApp (io.cdap.cdap.client.app.FakeApp)2 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)2 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)2 ProgramId (io.cdap.cdap.proto.id.ProgramId)2 File (java.io.File)2 TypeToken (com.google.common.reflect.TypeToken)1 ServiceHttpEndpoint (io.cdap.cdap.api.service.http.ServiceHttpEndpoint)1