Search in sources :

Example 1 with PackageName

use of org.apache.pulsar.packages.management.core.common.PackageName in project pulsar by apache.

the class PackagesImpl method listPackageVersionsAsync.

@Override
public CompletableFuture<List<String>> listPackageVersionsAsync(String packageName) {
    PackageName name = PackageName.get(packageName);
    WebTarget path = packages.path(String.format("%s/%s/%s/%s", name.getPkgType().toString(), name.getTenant(), name.getNamespace(), name.getName()));
    final CompletableFuture<List<String>> future = new CompletableFuture<>();
    asyncGetRequest(path, new InvocationCallback<List<String>>() {

        @Override
        public void completed(List<String> strings) {
            future.complete(strings);
        }

        @Override
        public void failed(Throwable throwable) {
            future.completeExceptionally(getApiException(throwable.getCause()));
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PackageName(org.apache.pulsar.packages.management.core.common.PackageName) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget)

Example 2 with PackageName

use of org.apache.pulsar.packages.management.core.common.PackageName in project pulsar by apache.

the class PackagesImpl method deleteAsync.

@Override
public CompletableFuture<Void> deleteAsync(String packageName) {
    PackageName name = PackageName.get(packageName);
    WebTarget path = packages.path(name.toRestPath());
    return asyncDeleteRequest(path);
}
Also used : PackageName(org.apache.pulsar.packages.management.core.common.PackageName) WebTarget(javax.ws.rs.client.WebTarget)

Example 3 with PackageName

use of org.apache.pulsar.packages.management.core.common.PackageName in project pulsar by apache.

the class PackagesManagementImplTest method testPackagesManagementFlow.

@Test
public void testPackagesManagementFlow() {
    PackageName packageName = PackageName.get("function://tenant/ns/non-existent-package@v1");
    // get a non-existent package metadata should fail
    try {
        packagesManagement.getMeta(packageName).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // update a non-existent package metadata should fail
    PackageMetadata failedUpdateMetadata = PackageMetadata.builder().description("Failed update package metadata").build();
    try {
        packagesManagement.updateMeta(packageName, failedUpdateMetadata).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // download a non-existent package should fail
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        packagesManagement.download(packageName, outputStream).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // delete a non-existent package should fail
    try {
        packagesManagement.delete(packageName).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // list a non-existent package version should fail
    try {
        packagesManagement.list(packageName).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // list the packages in a non-existent namespace should fail
    try {
        packagesManagement.list(packageName.getPkgType(), packageName.getTenant(), packageName.getNamespace()).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // upload a package
    PackageMetadata metadata = PackageMetadata.builder().contact("test@apache.org").description("A mocked test package").createTime(System.currentTimeMillis()).build();
    try (ByteArrayInputStream inputStream = new ByteArrayInputStream(PackageMetadataUtil.toBytes(metadata))) {
        packagesManagement.upload(packageName, metadata, inputStream).get();
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
    // get an existent package metadata should succeed
    try {
        PackageMetadata getPackageMetadata = packagesManagement.getMeta(packageName).get();
        Assert.assertEquals(metadata, getPackageMetadata);
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
    // download an existent package should succeed
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        packagesManagement.download(packageName, outputStream).get();
        PackageMetadata getPackage = PackageMetadataUtil.fromBytes(outputStream.toByteArray());
        Assert.assertEquals(metadata, getPackage);
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
    // update an existent package metadata should succeed
    metadata.setModificationTime(System.currentTimeMillis());
    try {
        packagesManagement.updateMeta(packageName, metadata).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // get the updated metadata
    try {
        PackageMetadata updatedMetadata = packagesManagement.getMeta(packageName).get();
        Assert.assertEquals(metadata, updatedMetadata);
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
    // list an existent package version should success
    try {
        List<String> versions = packagesManagement.list(packageName).get();
        Assert.assertEquals(1, versions.size());
        Assert.assertEquals(packageName.getVersion(), versions.get(0));
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // list the packages in a non-existent namespace should fail
    try {
        List<String> packageNames = packagesManagement.list(packageName.getPkgType(), packageName.getTenant(), packageName.getNamespace()).get();
        Assert.assertEquals(1, packageNames.size());
        Assert.assertEquals(packageName.getName(), packageNames.get(0));
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // delete an existent package should succeed
    try {
        packagesManagement.delete(packageName).get();
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
}
Also used : PackageName(org.apache.pulsar.packages.management.core.common.PackageName) ByteArrayInputStream(java.io.ByteArrayInputStream) PackageMetadata(org.apache.pulsar.packages.management.core.common.PackageMetadata) PackagesManagementException(org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PackagesManagementException(org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 4 with PackageName

use of org.apache.pulsar.packages.management.core.common.PackageName in project pulsar by yahoo.

the class PackagesBase method getPackageNameAsync.

private CompletableFuture<PackageName> getPackageNameAsync(String type, String tenant, String namespace, String packageName, String version) {
    CompletableFuture<PackageName> future = new CompletableFuture<>();
    try {
        PackageName name = PackageName.get(type, tenant, namespace, packageName, version);
        future.complete(name);
    } catch (IllegalArgumentException illegalArgumentException) {
        future.completeExceptionally(illegalArgumentException);
    }
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PackageName(org.apache.pulsar.packages.management.core.common.PackageName)

Example 5 with PackageName

use of org.apache.pulsar.packages.management.core.common.PackageName in project pulsar by yahoo.

the class PackagesManagementImpl method getMeta.

@Override
public CompletableFuture<PackageMetadata> getMeta(PackageName packageName) {
    CompletableFuture<PackageMetadata> future = new CompletableFuture<>();
    String metadataPath = metadataPath(packageName);
    checkMetadataNotExistsAndThrowException(packageName).whenComplete((ignore, throwable) -> {
        if (throwable != null) {
            future.completeExceptionally(throwable);
            return;
        }
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            storage.readAsync(metadataPath, outputStream).thenCompose(aVoid -> metadataReadFromStream(outputStream)).whenComplete((metadata, t) -> {
                if (t != null) {
                    future.completeExceptionally(t);
                } else {
                    future.complete(metadata);
                }
            });
        } catch (IOException e) {
            future.completeExceptionally(new PackagesManagementException(String.format("Read package '%s' metadata failed", packageName.toString()), e));
        }
    });
    return future;
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PackagesManagementException(org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) PackageMetadataUtil(org.apache.pulsar.packages.management.core.common.PackageMetadataUtil) PackageMetadata(org.apache.pulsar.packages.management.core.common.PackageMetadata) PackageType(org.apache.pulsar.packages.management.core.common.PackageType) List(java.util.List) PackagesStorage(org.apache.pulsar.packages.management.core.PackagesStorage) ByteArrayInputStream(java.io.ByteArrayInputStream) NotFoundException(org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException.NotFoundException) PackagesManagement(org.apache.pulsar.packages.management.core.PackagesManagement) PackageName(org.apache.pulsar.packages.management.core.common.PackageName) InputStream(java.io.InputStream) CompletableFuture(java.util.concurrent.CompletableFuture) PackageMetadata(org.apache.pulsar.packages.management.core.common.PackageMetadata) PackagesManagementException(org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

PackageName (org.apache.pulsar.packages.management.core.common.PackageName)25 CompletableFuture (java.util.concurrent.CompletableFuture)17 PackageMetadata (org.apache.pulsar.packages.management.core.common.PackageMetadata)14 IOException (java.io.IOException)12 List (java.util.List)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 InputStream (java.io.InputStream)11 WebTarget (javax.ws.rs.client.WebTarget)9 PackagesManagementException (org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException)9 OutputStream (java.io.OutputStream)8 PackagesStorage (org.apache.pulsar.packages.management.core.PackagesStorage)8 PackagesManagement (org.apache.pulsar.packages.management.core.PackagesManagement)6 PackageMetadataUtil (org.apache.pulsar.packages.management.core.common.PackageMetadataUtil)6 PackageType (org.apache.pulsar.packages.management.core.common.PackageType)6 NotFoundException (org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException.NotFoundException)6 File (java.io.File)5 Test (org.testng.annotations.Test)4 Gson (com.google.gson.Gson)3 Files (java.nio.file.Files)3