Search in sources :

Example 26 with ServiceSpecification

use of io.cdap.cdap.api.service.ServiceSpecification in project cdap by caskdata.

the class ProgramLifecycleHttpHandlerTest method testServiceSpecification.

@Test
public void testServiceSpecification() throws Exception {
    deploy(AppWithServices.class, 200);
    HttpResponse response = doGet("/v3/namespaces/default/apps/AppWithServices/services/NoOpService");
    Assert.assertEquals(200, response.getResponseCode());
    Set<ServiceHttpEndpoint> expectedEndpoints = ImmutableSet.of(new ServiceHttpEndpoint("GET", "/ping"), new ServiceHttpEndpoint("POST", "/multi"), new ServiceHttpEndpoint("GET", "/multi"), new ServiceHttpEndpoint("GET", "/multi/ping"));
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(ServiceSpecification.class, new ServiceSpecificationCodec());
    Gson gson = gsonBuilder.create();
    ServiceSpecification specification = readResponse(response, ServiceSpecification.class, gson);
    Set<ServiceHttpEndpoint> returnedEndpoints = new HashSet<>();
    for (HttpServiceHandlerSpecification httpServiceHandlerSpecification : specification.getHandlers().values()) {
        returnedEndpoints.addAll(httpServiceHandlerSpecification.getEndpoints());
    }
    Assert.assertEquals("NoOpService", specification.getName());
    Assert.assertEquals(returnedEndpoints, expectedEndpoints);
}
Also used : ServiceHttpEndpoint(io.cdap.cdap.api.service.http.ServiceHttpEndpoint) ServiceSpecificationCodec(io.cdap.cdap.internal.app.ServiceSpecificationCodec) ServiceSpecification(io.cdap.cdap.api.service.ServiceSpecification) GsonBuilder(com.google.gson.GsonBuilder) HttpResponse(io.cdap.common.http.HttpResponse) Gson(com.google.gson.Gson) HttpServiceHandlerSpecification(io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with ServiceSpecification

use of io.cdap.cdap.api.service.ServiceSpecification in project cdap by caskdata.

the class ServiceClient method getEndpoints.

/**
 * Gets a list of {@link ServiceHttpEndpoint} that a {@link Service} exposes.
 *
 * @param service ID of the service
 * @return A list of {@link ServiceHttpEndpoint}
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws NotFoundException if the app or service could not be found
 */
public List<ServiceHttpEndpoint> getEndpoints(ServiceId service) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
    ServiceSpecification specification = get(service);
    ImmutableList.Builder<ServiceHttpEndpoint> builder = new ImmutableList.Builder<>();
    for (HttpServiceHandlerSpecification handlerSpecification : specification.getHandlers().values()) {
        builder.addAll(handlerSpecification.getEndpoints());
    }
    return builder.build();
}
Also used : ServiceHttpEndpoint(io.cdap.cdap.api.service.http.ServiceHttpEndpoint) ServiceSpecification(io.cdap.cdap.api.service.ServiceSpecification) ImmutableList(com.google.common.collect.ImmutableList) HttpServiceHandlerSpecification(io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification)

Example 28 with ServiceSpecification

use of io.cdap.cdap.api.service.ServiceSpecification in project cdap by caskdata.

the class ApplicationRegistrationStage method registerDatasets.

// Register dataset usage, based upon the program specifications.
// Note that worker specifications' datasets are not registered upon app deploy because the useDataset of the
// WorkerConfigurer is deprecated. Workers' access to datasets is aimed to be completely dynamic. Other programs are
// moving in this direction.
// Also, SparkSpecifications are the same in that a Spark program's dataset access is completely dynamic.
private void registerDatasets(ApplicationWithPrograms input) {
    ApplicationSpecification appSpec = input.getSpecification();
    ApplicationId appId = input.getApplicationId();
    NamespaceId namespaceId = appId.getParent();
    for (MapReduceSpecification program : appSpec.getMapReduce().values()) {
        ProgramId programId = appId.mr(program.getName());
        for (String dataset : program.getDataSets()) {
            usageRegistry.register(programId, namespaceId.dataset(dataset));
        }
    }
    for (SparkSpecification sparkSpec : appSpec.getSpark().values()) {
        ProgramId programId = appId.spark(sparkSpec.getName());
        for (String dataset : sparkSpec.getDatasets()) {
            usageRegistry.register(programId, namespaceId.dataset(dataset));
        }
    }
    for (ServiceSpecification serviceSpecification : appSpec.getServices().values()) {
        ProgramId programId = appId.service(serviceSpecification.getName());
        for (HttpServiceHandlerSpecification handlerSpecification : serviceSpecification.getHandlers().values()) {
            for (String dataset : handlerSpecification.getDatasets()) {
                usageRegistry.register(programId, namespaceId.dataset(dataset));
            }
        }
    }
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) SparkSpecification(io.cdap.cdap.api.spark.SparkSpecification) ServiceSpecification(io.cdap.cdap.api.service.ServiceSpecification) MapReduceSpecification(io.cdap.cdap.api.mapreduce.MapReduceSpecification) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) HttpServiceHandlerSpecification(io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification)

Example 29 with ServiceSpecification

use of io.cdap.cdap.api.service.ServiceSpecification in project cdap by caskdata.

the class ApplicationSpecificationCodec method deserialize.

@Override
public ApplicationSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    String name = jsonObj.get("name").getAsString();
    String appVersion = ApplicationId.DEFAULT_VERSION;
    if (jsonObj.has("appVersion")) {
        appVersion = jsonObj.get("appVersion").getAsString();
    }
    String appCDAPVersion = jsonObj.has("appCDAPVersion") ? jsonObj.get("appCDAPVersion").getAsString() : null;
    String description = jsonObj.get("description").getAsString();
    String configuration = null;
    if (jsonObj.has("configuration")) {
        configuration = jsonObj.get("configuration").getAsString();
    }
    ArtifactId artifactId = context.deserialize(jsonObj.get("artifactId"), ArtifactId.class);
    Map<String, String> datasetModules = deserializeMap(jsonObj.get("datasetModules"), context, String.class);
    Map<String, DatasetCreationSpec> datasetInstances = deserializeMap(jsonObj.get("datasetInstances"), context, DatasetCreationSpec.class);
    Map<String, MapReduceSpecification> mapReduces = deserializeMap(jsonObj.get("mapReduces"), context, MapReduceSpecification.class);
    Map<String, SparkSpecification> sparks = deserializeMap(jsonObj.get("sparks"), context, SparkSpecification.class);
    Map<String, WorkflowSpecification> workflows = deserializeMap(jsonObj.get("workflows"), context, WorkflowSpecification.class);
    Map<String, ServiceSpecification> services = deserializeMap(jsonObj.get("services"), context, ServiceSpecification.class);
    Map<String, ScheduleCreationSpec> programSchedules = deserializeMap(jsonObj.get("programSchedules"), context, ScheduleCreationSpec.class);
    Map<String, WorkerSpecification> workers = deserializeMap(jsonObj.get("workers"), context, WorkerSpecification.class);
    Map<String, Plugin> plugins = deserializeMap(jsonObj.get("plugins"), context, Plugin.class);
    return new DefaultApplicationSpecification(name, appVersion, appCDAPVersion, description, configuration, artifactId, datasetModules, datasetInstances, mapReduces, sparks, workflows, services, programSchedules, workers, plugins);
}
Also used : ServiceSpecification(io.cdap.cdap.api.service.ServiceSpecification) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) WorkerSpecification(io.cdap.cdap.api.worker.WorkerSpecification) MapReduceSpecification(io.cdap.cdap.api.mapreduce.MapReduceSpecification) JsonObject(com.google.gson.JsonObject) ScheduleCreationSpec(io.cdap.cdap.internal.schedule.ScheduleCreationSpec) SparkSpecification(io.cdap.cdap.api.spark.SparkSpecification) DatasetCreationSpec(io.cdap.cdap.internal.dataset.DatasetCreationSpec) WorkflowSpecification(io.cdap.cdap.api.workflow.WorkflowSpecification) Plugin(io.cdap.cdap.api.plugin.Plugin)

Example 30 with ServiceSpecification

use of io.cdap.cdap.api.service.ServiceSpecification in project cdap by cdapio.

the class ServiceClient method get.

/**
 * Gets a {@link ServiceSpecification} for a {@link Service}.
 *
 * @param service ID of the service
 * @return {@link ServiceSpecification} representing the service
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws NotFoundException if the app or service could not be found
 */
public ServiceSpecification get(ProgramId service) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(service.getNamespaceId(), String.format("apps/%s/versions/%s/services/%s", service.getApplication(), service.getVersion(), service.getProgram()));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(service);
    }
    return ObjectResponse.fromJsonBody(response, ServiceSpecification.class).getResponseObject();
}
Also used : ServiceSpecification(io.cdap.cdap.api.service.ServiceSpecification) HttpResponse(io.cdap.common.http.HttpResponse) NotFoundException(io.cdap.cdap.common.NotFoundException) URL(java.net.URL)

Aggregations

ServiceSpecification (io.cdap.cdap.api.service.ServiceSpecification)34 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)16 HttpServiceHandlerSpecification (io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification)10 JsonObject (com.google.gson.JsonObject)6 ProgramType (io.cdap.cdap.proto.ProgramType)6 ProgramId (io.cdap.cdap.proto.id.ProgramId)6 Test (org.junit.Test)6 Resources (io.cdap.cdap.api.Resources)4 MapReduceSpecification (io.cdap.cdap.api.mapreduce.MapReduceSpecification)4 Plugin (io.cdap.cdap.api.plugin.Plugin)4 ServiceHttpEndpoint (io.cdap.cdap.api.service.http.ServiceHttpEndpoint)4 SparkSpecification (io.cdap.cdap.api.spark.SparkSpecification)4 ForwardingApplicationSpecification (io.cdap.cdap.internal.app.ForwardingApplicationSpecification)4 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)4 HttpResponse (io.cdap.common.http.HttpResponse)4 ImmutableList (com.google.common.collect.ImmutableList)2 TypeToken (com.google.common.reflect.TypeToken)2 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 AppWithServices (io.cdap.cdap.AppWithServices)2