Skip to content

Commit

Permalink
Package management client command (apache#8817)
Browse files Browse the repository at this point in the history
---

Master Issue: apache#8676

*Motivation*

Add commands for package management service in pulsar-admin tool.

*Modifications*

- Add commands for package management service
  • Loading branch information
zymap authored Dec 8, 2020
1 parent cabea15 commit 652dc1e
Show file tree
Hide file tree
Showing 4 changed files with 369 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.pulsar.client.admin.internal.LookupImpl;
import org.apache.pulsar.client.admin.internal.NamespacesImpl;
import org.apache.pulsar.client.admin.internal.NonPersistentTopicsImpl;
import org.apache.pulsar.client.admin.internal.PackagesImpl;
import org.apache.pulsar.client.admin.internal.ProxyStatsImpl;
import org.apache.pulsar.client.admin.internal.PulsarAdminBuilderImpl;
import org.apache.pulsar.client.admin.internal.ResourceQuotasImpl;
Expand Down Expand Up @@ -94,6 +95,7 @@ public class PulsarAdmin implements Closeable {
private final Sinks sinks;
private final Worker worker;
private final Schemas schemas;
private final Packages packages;
protected final WebTarget root;
protected final Authentication auth;
private final int connectTimeout;
Expand Down Expand Up @@ -218,6 +220,7 @@ public PulsarAdmin(String serviceUrl,
this.worker = new WorkerImpl(root, auth, readTimeoutMs);
this.schemas = new SchemasImpl(root, auth, readTimeoutMs);
this.bookies = new BookiesImpl(root, auth, readTimeoutMs);
this.packages = new PackagesImpl(root, auth, asyncHttpConnector.getHttpClient(), readTimeoutMs);

if (originalCtxLoader != null) {
Thread.currentThread().setContextClassLoader(originalCtxLoader);
Expand Down Expand Up @@ -434,6 +437,13 @@ public Schemas schemas() {
return schemas;
}

/**
* @return the packages management object
*/
public Packages packages() {
return packages;
}

/**
* Close the Pulsar admin client to release all the resources.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class PackagesImpl extends ComponentResource implements Packages {
private final WebTarget packages;
private final AsyncHttpClient httpClient;

protected PackagesImpl(WebTarget webTarget, Authentication auth, AsyncHttpClient client, long readTimeoutMs) {
public PackagesImpl(WebTarget webTarget, Authentication auth, AsyncHttpClient client, long readTimeoutMs) {
super(auth, readTimeoutMs);
this.httpClient = client;
this.packages = webTarget.path("/admin/v3/packages");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.admin.cli;

import com.beust.jcommander.DynamicParameter;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import org.apache.pulsar.client.admin.Packages;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.packages.management.core.common.PackageMetadata;

import java.util.HashMap;
import java.util.Map;

/**
* Commands for administering packages.
*/
@Parameters(commandDescription = "Operations about packages")
class CmdPackages extends CmdBase {

private final Packages packages;

CmdPackages(PulsarAdmin admin) {
super("packages", admin);
this.packages = admin.packages();

jcommander.addCommand("get-metadata", new GetMetadataCmd());
jcommander.addCommand("update-metadata", new UpdateMetadataCmd());
jcommander.addCommand("upload", new UploadCmd());
jcommander.addCommand("download", new DownloadCmd());
jcommander.addCommand("list", new ListPackagesCmd());
jcommander.addCommand("list-versions", new ListPackageVersionsCmd());
jcommander.addCommand("delete", new DeletePackageCmd());
}

@Parameters(commandDescription = "Get a package metadata information.")
private class GetMetadataCmd extends CliCommand {
@Parameter(description = "type://tenant/namespace/packageName@version", required = true)
private String packageName;

@Override
void run() throws Exception {
print(packages.getMetadata(packageName));
}
}

@Parameters(commandDescription = "Update a package metadata information.")
private class UpdateMetadataCmd extends CliCommand {
@Parameter(description = "type://tenant/namespace/packageName@version", required = true)
private String packageName;

@Parameter(names = "--description", description= "descriptions of a package", required = true)
private String description;

@Parameter(names = "--contact", description = "contact info of a package")
private String contact;

@DynamicParameter(names = {"--properties", "-P"}, description ="external information of a package")
private Map<String, String> properties = new HashMap<>();

@Override
void run() throws Exception {
packages.updateMetadata(packageName, PackageMetadata.builder()
.description(description).contact(contact).properties(properties).build());
print(String.format("The metadata of the package '%s' updated successfully", packageName));
}
}

@Parameters(commandDescription = "Upload a package")
private class UploadCmd extends CliCommand {
@Parameter(description = "type://tenant/namespace/packageName@version", required = true)
private String packageName;

@Parameter(names = "--description", description= "descriptions of a package", required = true)
private String description;

@Parameter(names = "--contact", description = "contact information of a package")
private String contact;

@DynamicParameter(names = {"--properties", "-P"}, description ="external infromations of a package")
private Map<String, String> properties = new HashMap<>();

@Parameter(names = "--path", description = "descriptions of a package", required = true)
private String path;

@Override
void run() throws Exception {
PackageMetadata metadata = PackageMetadata.builder()
.description(description)
.contact(contact)
.properties(properties).build();
packages.upload(metadata, packageName, path);
print(String.format("The package '%s' uploaded from path '%s' successfully", packageName, path));
}
}

@Parameters(commandDescription = "Download a package")
private class DownloadCmd extends CliCommand {
@Parameter(description = "type://tenant/namespace/packageName@version", required = true)
private String packageName;

@Parameter(names = "--path", description = "download destiny path of the package", required = true)
private String path;

@Override
void run() throws Exception {
packages.download(packageName, path);
print(String.format("The package '%s' downloaded to path '%s' successfully", packageName, path));
}
}

@Parameters(commandDescription = "List all versions of the given package")
private class ListPackageVersionsCmd extends CliCommand {
@Parameter(description = "the package name you want to query, don't need to specify the package version.\n" +
"type://tenant/namespace/packageName", required = true)
private String packageName;

@Override
void run() throws Exception {
print(packages.listPackageVersions(packageName).toString());
}
}

@Parameters(commandDescription = "List all packages with given type in the specified namespace")
private class ListPackagesCmd extends CliCommand {
@Parameter(names = "--type", description = "type of the package", required = true)
private String type;

@Parameter(description = "namespace of the package", required = true)
private String namespace;

@Override
void run() throws Exception {
print(packages.listPackages(type, namespace));
}
}

@Parameters(commandDescription = "Delete a package")
private class DeletePackageCmd extends CliCommand{
@Parameter(description = "type://tenant/namespace/packageName@version", required = true)
private String packageName;

@Override
void run() throws Exception {
packages.delete(packageName);
print(String.format("The package '%s' deleted successfully", packageName));
}
}
}
Loading

0 comments on commit 652dc1e

Please sign in to comment.