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