Skip to content

Commit

Permalink
[#6136] improvement(CLI): Move check for enable and disable command i…
Browse files Browse the repository at this point in the history
…n Gravitino CLI to command (#6535)

### What changes were proposed in this pull request?

Move check for enable and disable command in Gravitino CLI to command

### Why are the changes needed?

Fix: #6136 

### Does this PR introduce _any_ user-facing change?

No

### How was this patch tested?

local test
```bash
gcli metalake update -m demo_metalake --disable -i
demo_metalake has been disabled.

gcli metalake update -m demo_metalake --enable -i
demo_metalake has been enabled.

gcli metalake update -m demo_metalake --enable --disable -i
Unable to us --enable and --disable at the same time

 gcli metalake update -m demo_metalake --enable --all  -i
demo_metalake has been enabled. and all catalogs in this metalake have been enabled.

gcli catalog update -m demo_metalake --name Hive_catalog --disable -i
demo_metalake.Hive_catalog has been disabled.

gcli catalog update -m demo_metalake --name Hive_catalog --enable -i
demo_metalake.Hive_catalog has been enabled.

gcli catalog update -m demo_metalake --name Hive_catalog --enable --disable -i
Unable to us --enable and --disable at the same time
```

---------

Co-authored-by: Justin Mclean <justin@classsoftware.com>
  • Loading branch information
Abyss-lord and justinmclean authored Feb 27, 2025
1 parent a95933d commit 7224525
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,9 @@ private void handlePropertiesCommand() {

/** Handles the "UPDATE" command. */
private void handleUpdateCommand() {
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
System.err.println(ErrorMessages.INVALID_ENABLE_DISABLE);
Main.exit(-1);
}
if (line.hasOption(GravitinoOptions.ENABLE)) {
boolean enableMetalake = line.hasOption(GravitinoOptions.ALL);
gravitinoCommandLine
.newCatalogEnable(context, metalake, catalog, enableMetalake)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.DISABLE)) {
gravitinoCommandLine.newCatalogDisable(context, metalake, catalog).validate().handle();

if (line.hasOption(GravitinoOptions.ENABLE) || line.hasOption(GravitinoOptions.DISABLE)) {
gravitinoCommandLine.newManageCatalog(context, metalake, catalog).validate().handle();
}

if (line.hasOption(GravitinoOptions.COMMENT)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ public String auth() {
return auth;
}

/**
* Returns the command line.
*
* @return The command line.
*/
public CommandLine line() {
return line;
}
/**
* Retrieves the Gravitino URL from the command line options or the GRAVITINO_URL environment
* variable or the Gravitino config file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ErrorMessages {
public static final String ENTITY_IN_USE = " in use, please disable it first.";

public static final String INVALID_ENABLE_DISABLE =
"Unable to us --enable and --disable at the same time";
"Unable to use --enable and --disable at the same time.";
public static final String INVALID_OWNER_COMMAND =
"Unsupported combination of options either use --user or --group.";
public static final String INVALID_REMOVE_COMMAND =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,8 @@ private void handlePropertiesCommand() {

/** Handles the "UPDATE" command. */
private void handleUpdateCommand() {
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
System.err.println(ErrorMessages.INVALID_ENABLE_DISABLE);
Main.exit(-1);
}
if (line.hasOption(GravitinoOptions.ENABLE)) {
boolean enableAllCatalogs = line.hasOption(GravitinoOptions.ALL);
gravitinoCommandLine
.newMetalakeEnable(context, metalake, enableAllCatalogs)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.DISABLE)) {
gravitinoCommandLine.newMetalakeDisable(context, metalake).validate().handle();
if (line.hasOption(GravitinoOptions.ENABLE) || line.hasOption(GravitinoOptions.DISABLE)) {
gravitinoCommandLine.newManageMetalake(context, metalake).validate().handle();
}

if (line.hasOption(GravitinoOptions.COMMENT)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.apache.gravitino.cli.commands.AddRoleToUser;
import org.apache.gravitino.cli.commands.CatalogAudit;
import org.apache.gravitino.cli.commands.CatalogDetails;
import org.apache.gravitino.cli.commands.CatalogDisable;
import org.apache.gravitino.cli.commands.CatalogEnable;
import org.apache.gravitino.cli.commands.ClientVersion;
import org.apache.gravitino.cli.commands.ColumnAudit;
import org.apache.gravitino.cli.commands.CreateCatalog;
Expand Down Expand Up @@ -78,10 +76,10 @@
import org.apache.gravitino.cli.commands.ListTopicProperties;
import org.apache.gravitino.cli.commands.ListTopics;
import org.apache.gravitino.cli.commands.ListUsers;
import org.apache.gravitino.cli.commands.ManageCatalog;
import org.apache.gravitino.cli.commands.ManageMetalake;
import org.apache.gravitino.cli.commands.MetalakeAudit;
import org.apache.gravitino.cli.commands.MetalakeDetails;
import org.apache.gravitino.cli.commands.MetalakeDisable;
import org.apache.gravitino.cli.commands.MetalakeEnable;
import org.apache.gravitino.cli.commands.ModelAudit;
import org.apache.gravitino.cli.commands.ModelDetails;
import org.apache.gravitino.cli.commands.OwnerDetails;
Expand Down Expand Up @@ -839,23 +837,13 @@ protected RevokeAllPrivileges newRevokeAllPrivileges(
return new RevokeAllPrivileges(context, metalake, role, entity);
}

protected MetalakeEnable newMetalakeEnable(
CommandContext context, String metalake, boolean enableAllCatalogs) {
return new MetalakeEnable(context, metalake, enableAllCatalogs);
protected ManageMetalake newManageMetalake(CommandContext context, String metalake) {
return new ManageMetalake(context, metalake);
}

protected MetalakeDisable newMetalakeDisable(CommandContext context, String metalake) {
return new MetalakeDisable(context, metalake);
}

protected CatalogEnable newCatalogEnable(
CommandContext context, String metalake, String catalog, boolean enableMetalake) {
return new CatalogEnable(context, metalake, catalog, enableMetalake);
}

protected CatalogDisable newCatalogDisable(
protected ManageCatalog newManageCatalog(
CommandContext context, String metalake, String catalog) {
return new CatalogDisable(context, metalake, catalog);
return new ManageCatalog(context, metalake, catalog);
}

protected ListModel newListModel(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,63 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.gravitino.cli.commands;

import org.apache.commons.cli.CommandLine;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.GravitinoOptions;
import org.apache.gravitino.client.GravitinoAdminClient;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.MetalakeNotInUseException;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;

/** Enable catalog. */
public class CatalogEnable extends Command {
/** Disable or enable a catalog. */
public class ManageCatalog extends Command {
private final String metalake;
private final String catalog;
private final CommandLine line;
private final boolean enableMetalake;

/**
* Enable catalog
* Constrcut a new instance of the {@code ManageCatalog}.
*
* @param context The command context.
* @param metalake The name of the metalake.
* @param catalog The name of the catalog.
* @param enableMetalake Whether to enable it's metalake
* @param context the command context.
* @param metalake the metalake name.
* @param catalog the catalog name.
*/
public CatalogEnable(
CommandContext context, String metalake, String catalog, boolean enableMetalake) {
public ManageCatalog(CommandContext context, String metalake, String catalog) {
super(context);
this.metalake = metalake;
this.catalog = catalog;
this.enableMetalake = enableMetalake;
this.line = context.line();
this.enableMetalake = line.hasOption(GravitinoOptions.ALL);
}

/** Enable catalog. */
/** Disable or enable a catalog. */
@Override
public void handle() {
if (line.hasOption(GravitinoOptions.ENABLE)) {
enableCatalog();
} else if (line.hasOption(GravitinoOptions.DISABLE)) {
disableCatalog();
}
}

/** {@inheritDoc} */
@Override
public Command validate() {
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
exitWithError(ErrorMessages.INVALID_ENABLE_DISABLE);
}

return super.validate();
}

/** Enable a catalog. */
private void enableCatalog() {
try {
if (enableMetalake) {
GravitinoAdminClient adminClient = buildAdminClient();
Expand All @@ -71,4 +93,20 @@ public void handle() {

printInformation(metalake + "." + catalog + " has been enabled.");
}

/** Disable a catalog. */
private void disableCatalog() {
try {
GravitinoClient client = buildClient(metalake);
client.disableCatalog(catalog);
} catch (NoSuchMetalakeException noSuchMetalakeException) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (NoSuchCatalogException noSuchCatalogException) {
exitWithError(ErrorMessages.UNKNOWN_CATALOG);
} catch (Exception exp) {
exitWithError(exp.getMessage());
}

printInformation(metalake + "." + catalog + " has been disabled.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,56 @@
package org.apache.gravitino.cli.commands;

import java.util.Arrays;
import org.apache.commons.cli.CommandLine;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.GravitinoOptions;
import org.apache.gravitino.client.GravitinoAdminClient;
import org.apache.gravitino.client.GravitinoMetalake;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;

/** Enable metalake. */
public class MetalakeEnable extends Command {

/** Disable or enable metalake. */
public class ManageMetalake extends Command {
private final String metalake;
private final CommandLine line;
private Boolean enableAllCatalogs;

/**
* Enable a metalake
* Construct a new instance of the {@code ManageMetalake}.
*
* @param context The command context.
* @param metalake The name of the metalake.
* @param enableAllCatalogs Whether to enable all catalogs.
* @param context the command context.
* @param metalake the name of the metalake.
*/
public MetalakeEnable(CommandContext context, String metalake, boolean enableAllCatalogs) {
public ManageMetalake(CommandContext context, String metalake) {
super(context);
this.metalake = metalake;
this.enableAllCatalogs = enableAllCatalogs;
this.line = context.line();

this.enableAllCatalogs = line.hasOption(GravitinoOptions.ALL);
}

/** Enable metalake. */
/** Disable or enable the metalake. */
@Override
public void handle() {
if (line.hasOption(GravitinoOptions.ENABLE)) {
enableMetalake();
} else if (line.hasOption(GravitinoOptions.DISABLE)) {
disableMetalake();
}
}

/** {@inheritDoc} */
@Override
public Command validate() {
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
exitWithError(ErrorMessages.INVALID_ENABLE_DISABLE);
}

return super.validate();
}

/** Enable the metalake. */
private void enableMetalake() {
StringBuilder msgBuilder = new StringBuilder(metalake);
try {
GravitinoAdminClient client = buildAdminClient();
Expand All @@ -68,4 +90,18 @@ public void handle() {

printInformation(msgBuilder.toString());
}

/** Disable the metalake. */
private void disableMetalake() {
try {
GravitinoAdminClient client = buildAdminClient();
client.disableMetalake(metalake);
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (Exception exp) {
exitWithError(exp.getMessage());
}

printInformation(metalake + " has been disabled.");
}
}
Loading

0 comments on commit 7224525

Please sign in to comment.