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());
}
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);
}
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);
}
}
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);
}
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);
}
}
Aggregations