Search in sources :

Example 1 with ApplicationRecord

use of io.cdap.cdap.proto.ApplicationRecord in project cdap by caskdata.

the class AppLifecycleHttpHandler method getAllApps.

/**
 * Returns a list of applications associated with a namespace.
 */
@GET
@Path("/apps")
public void getAllApps(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @QueryParam("artifactName") String artifactName, @QueryParam("artifactVersion") String artifactVersion, @QueryParam("pageToken") String pageToken, @QueryParam("pageSize") Integer pageSize, @QueryParam("orderBy") SortOrder orderBy, @QueryParam("nameFilter") String nameFilter) throws Exception {
    NamespaceId namespace = validateNamespace(namespaceId);
    Set<String> names = new HashSet<>();
    if (!Strings.isNullOrEmpty(artifactName)) {
        for (String name : Splitter.on(',').split(artifactName)) {
            names.add(name);
        }
    }
    if (Optional.ofNullable(pageSize).orElse(0) != 0) {
        JsonPaginatedListResponder.respond(GSON, responder, APP_LIST_PAGINATED_KEY, jsonListResponder -> {
            AtomicReference<ApplicationRecord> lastRecord = new AtomicReference<>(null);
            ScanApplicationsRequest scanRequest = getScanRequest(namespaceId, artifactVersion, pageToken, pageSize, orderBy, nameFilter, names);
            boolean pageLimitReached = applicationLifecycleService.scanApplications(scanRequest, appDetail -> {
                ApplicationRecord record = new ApplicationRecord(appDetail);
                jsonListResponder.send(record);
                lastRecord.set(record);
            });
            ApplicationRecord record = lastRecord.get();
            return !pageLimitReached || record == null ? null : record.getName() + EntityId.IDSTRING_PART_SEPARATOR + record.getAppVersion();
        });
    } else {
        ScanApplicationsRequest scanRequest = getScanRequest(namespaceId, artifactVersion, pageToken, null, orderBy, nameFilter, names);
        JsonWholeListResponder.respond(GSON, responder, jsonListResponder -> applicationLifecycleService.scanApplications(scanRequest, d -> jsonListResponder.send(new ApplicationRecord(d))));
    }
}
Also used : ScanApplicationsRequest(io.cdap.cdap.app.store.ScanApplicationsRequest) AuditDetail(io.cdap.cdap.common.security.AuditDetail) ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) Arrays(java.util.Arrays) GsonBuilder(com.google.gson.GsonBuilder) ScanApplicationsRequest(io.cdap.cdap.app.store.ScanApplicationsRequest) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) Map(java.util.Map) HeaderParam(javax.ws.rs.HeaderParam) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) ProgramTerminator(io.cdap.cdap.internal.app.deploy.ProgramTerminator) EnumSet(java.util.EnumSet) HttpRequest(io.netty.handler.codec.http.HttpRequest) BodyConsumer(io.cdap.http.BodyConsumer) Set(java.util.Set) Reader(java.io.Reader) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) StandardCharsets(java.nio.charset.StandardCharsets) Id(io.cdap.cdap.common.id.Id) JsonArray(com.google.gson.JsonArray) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Singleton(com.google.inject.Singleton) Iterables(com.google.common.collect.Iterables) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Location(org.apache.twill.filesystem.Location) GET(javax.ws.rs.GET) AccessEnforcer(io.cdap.cdap.security.spi.authorization.AccessEnforcer) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) WriteConflictException(io.cdap.cdap.internal.app.runtime.artifact.WriteConflictException) JsonWriter(com.google.gson.stream.JsonWriter) Nullable(javax.annotation.Nullable) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) AbstractBodyConsumer(io.cdap.cdap.common.http.AbstractBodyConsumer) Throwables(com.google.common.base.Throwables) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) ConflictException(io.cdap.cdap.common.ConflictException) NotImplementedException(io.cdap.cdap.common.NotImplementedException) InputStreamReader(java.io.InputStreamReader) ProgramRuntimeService(io.cdap.cdap.app.runtime.ProgramRuntimeService) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) JsonObject(com.google.gson.JsonObject) NamespaceQueryAdmin(io.cdap.cdap.common.namespace.NamespaceQueryAdmin) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) AccessException(io.cdap.cdap.api.security.AccessException) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) Unpooled(io.netty.buffer.Unpooled) QueryParam(javax.ws.rs.QueryParam) Gson(com.google.gson.Gson) AuthenticationContext(io.cdap.cdap.security.spi.authentication.AuthenticationContext) DefaultValue(javax.ws.rs.DefaultValue) Splitter(com.google.common.base.Splitter) DELETE(javax.ws.rs.DELETE) SortOrder(io.cdap.cdap.spi.data.SortOrder) ApplicationUpdateDetail(io.cdap.cdap.proto.ApplicationUpdateDetail) BatchApplicationDetail(io.cdap.cdap.proto.BatchApplicationDetail) Collection(java.util.Collection) ApplicationLifecycleService(io.cdap.cdap.internal.app.services.ApplicationLifecycleService) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) List(java.util.List) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) CaseInsensitiveEnumTypeAdapterFactory(io.cdap.cdap.common.io.CaseInsensitiveEnumTypeAdapterFactory) Optional(java.util.Optional) Constants(io.cdap.cdap.common.conf.Constants) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) DirUtils(io.cdap.cdap.common.utils.DirUtils) ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) ArtifactAlreadyExistsException(io.cdap.cdap.common.ArtifactAlreadyExistsException) NotFoundException(io.cdap.cdap.common.NotFoundException) StandardPermission(io.cdap.cdap.proto.security.StandardPermission) PathParam(javax.ws.rs.PathParam) TypeToken(com.google.common.reflect.TypeToken) ApplicationWithPrograms(io.cdap.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms) EntityId(io.cdap.cdap.proto.id.EntityId) ServiceException(io.cdap.cdap.common.ServiceException) AtomicReference(java.util.concurrent.atomic.AtomicReference) JsonElement(com.google.gson.JsonElement) HashSet(java.util.HashSet) OutputStreamWriter(java.io.OutputStreamWriter) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) AbstractAppFabricHttpHandler(io.cdap.cdap.gateway.handlers.util.AbstractAppFabricHttpHandler) ProgramController(io.cdap.cdap.app.runtime.ProgramController) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) HttpResponder(io.cdap.http.HttpResponder) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) JsonSyntaxException(com.google.gson.JsonSyntaxException) ChunkResponder(io.cdap.http.ChunkResponder) ApplicationFilter(io.cdap.cdap.app.store.ApplicationFilter) ProgramId(io.cdap.cdap.proto.id.ProgramId) BadRequestException(io.cdap.cdap.common.BadRequestException) KerberosPrincipalId(io.cdap.cdap.proto.id.KerberosPrincipalId) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) NamespacePathLocator(io.cdap.cdap.common.namespace.NamespacePathLocator) PUT(javax.ws.rs.PUT) FileReader(java.io.FileReader) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord) Collections(java.util.Collections) AtomicReference(java.util.concurrent.atomic.AtomicReference) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) HashSet(java.util.HashSet) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with ApplicationRecord

use of io.cdap.cdap.proto.ApplicationRecord in project cdap by caskdata.

the class ApplicationClient method list.

/**
 * Lists all applications currently deployed, optionally filtering to only include applications that use one of
 * the specified artifact names and the specified artifact version.
 *
 * @param namespace the namespace to list applications from
 * @param artifactNames the set of artifact names to allow. If empty, no filtering will be done.
 * @param artifactVersion the version of the artifact to filter by. If null, no filtering will be done.
 * @return list of {@link ApplicationRecord ApplicationRecords}.
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public List<ApplicationRecord> list(NamespaceId namespace, Set<String> artifactNames, @Nullable String artifactVersion) throws IOException, UnauthenticatedException, UnauthorizedException {
    if (artifactNames.isEmpty() && artifactVersion == null) {
        return list(namespace);
    }
    String path;
    if (!artifactNames.isEmpty() && artifactVersion != null) {
        path = String.format("apps?artifactName=%s&artifactVersion=%s", Joiner.on(',').join(artifactNames), artifactVersion);
    } else if (!artifactNames.isEmpty()) {
        path = "apps?artifactName=" + Joiner.on(',').join(artifactNames);
    } else {
        path = "apps?artifactVersion=" + artifactVersion;
    }
    HttpResponse response = restClient.execute(HttpMethod.GET, config.resolveNamespacedURLV3(namespace, path), config.getAccessToken());
    return ObjectResponse.fromJsonBody(response, new TypeToken<List<ApplicationRecord>>() {
    }).getResponseObject();
}
Also used : TypeToken(com.google.common.reflect.TypeToken) HttpResponse(io.cdap.common.http.HttpResponse) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord)

Example 3 with ApplicationRecord

use of io.cdap.cdap.proto.ApplicationRecord in project cdap by caskdata.

the class IntegrationTestBase method assertNoApps.

private void assertNoApps(NamespaceId namespace) throws Exception {
    ApplicationClient applicationClient = getApplicationClient();
    List<ApplicationRecord> applicationRecords = applicationClient.list(namespace);
    List<String> applicationIds = Lists.newArrayList();
    for (ApplicationRecord applicationRecord : applicationRecords) {
        applicationIds.add(applicationRecord.getName());
    }
    Assert.assertTrue("Must have no deployed apps, but found the following apps: " + Joiner.on(", ").join(applicationIds), applicationRecords.isEmpty());
}
Also used : ApplicationClient(io.cdap.cdap.client.ApplicationClient) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord)

Example 4 with ApplicationRecord

use of io.cdap.cdap.proto.ApplicationRecord in project cdap by caskdata.

the class PostUpgradeJobMain method restartPipelinesAndSchedules.

private static void restartPipelinesAndSchedules(ClientConfig clientConfig, long startTimeMillis, boolean restartSystemApps) throws Exception {
    long endTimeMillis = System.currentTimeMillis();
    ApplicationClient applicationClient = new ApplicationClient(clientConfig);
    ScheduleClient scheduleClient = new ScheduleClient(clientConfig);
    ProgramClient programClient = new ProgramClient(clientConfig);
    NamespaceClient namespaceClient = new NamespaceClient(clientConfig);
    List<NamespaceId> namespaceIdList = namespaceClient.list().stream().map(NamespaceMeta::getNamespaceId).collect(Collectors.toList());
    if (restartSystemApps) {
        namespaceIdList.add(NamespaceId.SYSTEM);
    }
    for (NamespaceId namespaceId : namespaceIdList) {
        for (ApplicationRecord record : applicationClient.list(namespaceId)) {
            ApplicationId applicationId = new ApplicationId(namespaceId.getNamespace(), record.getName(), record.getAppVersion());
            programClient.restart(applicationId, TimeUnit.MILLISECONDS.toSeconds(startTimeMillis), TimeUnit.MILLISECONDS.toSeconds(endTimeMillis));
        }
        // Re-enable schedules in a namespace AFTER programs have been restarted.
        scheduleClient.reEnableSuspendedSchedules(namespaceId, startTimeMillis, endTimeMillis);
    }
}
Also used : ApplicationClient(io.cdap.cdap.client.ApplicationClient) ProgramClient(io.cdap.cdap.client.ProgramClient) NamespaceClient(io.cdap.cdap.client.NamespaceClient) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleClient(io.cdap.cdap.client.ScheduleClient) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord)

Example 5 with ApplicationRecord

use of io.cdap.cdap.proto.ApplicationRecord in project cdap by caskdata.

the class ApplicationClientTestRun method testArtifactFilter.

@Test
public void testArtifactFilter() throws Exception {
    ApplicationId appId1 = NamespaceId.DEFAULT.app(FakeApp.NAME);
    ApplicationId appId2 = NamespaceId.DEFAULT.app("fake2");
    ApplicationId appId3 = NamespaceId.DEFAULT.app("fake3");
    try {
        // app1 should use fake-1.0.0-SNAPSHOT
        appClient.deploy(NamespaceId.DEFAULT, createAppJarFile(FakeApp.class, "otherfake", "1.0.0-SNAPSHOT"));
        appClient.deploy(NamespaceId.DEFAULT, createAppJarFile(FakeApp.class, "fake", "0.1.0-SNAPSHOT"));
        // app1 should end up with fake-1.0.0-SNAPSHOT
        appClient.deploy(NamespaceId.DEFAULT, createAppJarFile(FakeApp.class, "fake", "1.0.0-SNAPSHOT"));
        // app2 should use fake-0.1.0-SNAPSHOT
        appClient.deploy(appId2, new AppRequest<Config>(new ArtifactSummary("fake", "0.1.0-SNAPSHOT")));
        // app3 should use otherfake-1.0.0-SNAPSHOT
        appClient.deploy(appId3, new AppRequest<Config>(new ArtifactSummary("otherfake", "1.0.0-SNAPSHOT")));
        appClient.waitForDeployed(appId1, 30, TimeUnit.SECONDS);
        appClient.waitForDeployed(appId2, 30, TimeUnit.SECONDS);
        appClient.waitForDeployed(appId3, 30, TimeUnit.SECONDS);
        // check calls that should return nothing
        // these don't match anything
        Assert.assertTrue(appClient.list(NamespaceId.DEFAULT, "ghost", null).isEmpty());
        Assert.assertTrue(appClient.list(NamespaceId.DEFAULT, (String) null, "1.0.0").isEmpty());
        Assert.assertTrue(appClient.list(NamespaceId.DEFAULT, "ghost", "1.0.0").isEmpty());
        // these match one but not the other
        Assert.assertTrue(appClient.list(NamespaceId.DEFAULT, "otherfake", "0.1.0-SNAPSHOT").isEmpty());
        Assert.assertTrue(appClient.list(NamespaceId.DEFAULT, "fake", "1.0.0").isEmpty());
        // check filter by name only
        Set<ApplicationRecord> apps = Sets.newHashSet(appClient.list(NamespaceId.DEFAULT, "fake", null));
        Set<ApplicationRecord> expected = ImmutableSet.of(new ApplicationRecord(new ArtifactSummary("fake", "1.0.0-SNAPSHOT"), appId1, ""), new ApplicationRecord(new ArtifactSummary("fake", "0.1.0-SNAPSHOT"), appId2, ""));
        Assert.assertEquals(expected, apps);
        apps = Sets.newHashSet(appClient.list(NamespaceId.DEFAULT, "otherfake", null));
        expected = ImmutableSet.of(new ApplicationRecord(new ArtifactSummary("otherfake", "1.0.0-SNAPSHOT"), appId3, ""));
        Assert.assertEquals(expected, apps);
        // check filter by multiple names
        apps = Sets.newHashSet(appClient.list(NamespaceId.DEFAULT, ImmutableSet.of("fake", "otherfake"), null));
        expected = ImmutableSet.of(new ApplicationRecord(new ArtifactSummary("otherfake", "1.0.0-SNAPSHOT"), appId3, ""), new ApplicationRecord(new ArtifactSummary("fake", "1.0.0-SNAPSHOT"), appId1, ""), new ApplicationRecord(new ArtifactSummary("fake", "0.1.0-SNAPSHOT"), appId2, ""));
        Assert.assertEquals(expected, apps);
        // check filter by version only
        apps = Sets.newHashSet(appClient.list(NamespaceId.DEFAULT, (String) null, "0.1.0-SNAPSHOT"));
        expected = ImmutableSet.of(new ApplicationRecord(new ArtifactSummary("fake", "0.1.0-SNAPSHOT"), appId2, ""));
        Assert.assertEquals(expected, apps);
        apps = Sets.newHashSet(appClient.list(NamespaceId.DEFAULT, (String) null, "1.0.0-SNAPSHOT"));
        expected = ImmutableSet.of(new ApplicationRecord(new ArtifactSummary("fake", "1.0.0-SNAPSHOT"), appId1, ""), new ApplicationRecord(new ArtifactSummary("otherfake", "1.0.0-SNAPSHOT"), appId3, ""));
        Assert.assertEquals(expected, apps);
        // check filter by both
        apps = Sets.newHashSet(appClient.list(NamespaceId.DEFAULT, "fake", "0.1.0-SNAPSHOT"));
        expected = ImmutableSet.of(new ApplicationRecord(new ArtifactSummary("fake", "0.1.0-SNAPSHOT"), appId2, ""));
        Assert.assertEquals(expected, apps);
    } finally {
        appClient.deleteAll(NamespaceId.DEFAULT);
        appClient.waitForDeleted(appId1, 30, TimeUnit.SECONDS);
        appClient.waitForDeleted(appId2, 30, TimeUnit.SECONDS);
        appClient.waitForDeleted(appId3, 30, TimeUnit.SECONDS);
        Assert.assertEquals(0, appClient.list(NamespaceId.DEFAULT).size());
    }
}
Also used : FakeApp(io.cdap.cdap.client.app.FakeApp) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) Config(io.cdap.cdap.api.Config) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord) Test(org.junit.Test)

Aggregations

ApplicationRecord (io.cdap.cdap.proto.ApplicationRecord)11 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)6 ApplicationClient (io.cdap.cdap.client.ApplicationClient)4 TypeToken (com.google.common.reflect.TypeToken)3 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)3 BadRequestException (io.cdap.cdap.common.BadRequestException)3 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 NamespaceClient (io.cdap.cdap.client.NamespaceClient)2 ProgramClient (io.cdap.cdap.client.ProgramClient)2 ScheduleClient (io.cdap.cdap.client.ScheduleClient)2 NotFoundException (io.cdap.cdap.common.NotFoundException)2 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)2 IOException (java.io.IOException)2 Splitter (com.google.common.base.Splitter)1 Strings (com.google.common.base.Strings)1 Throwables (com.google.common.base.Throwables)1 Iterables (com.google.common.collect.Iterables)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1