Skip to content

Commit 7224525

Browse files
[#6136] improvement(CLI): Move check for enable and disable command in 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>
1 parent a95933d commit 7224525

11 files changed

+136
-219
lines changed

clients/cli/src/main/java/org/apache/gravitino/cli/CatalogCommandHandler.java

+3-13
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,9 @@ private void handlePropertiesCommand() {
175175

176176
/** Handles the "UPDATE" command. */
177177
private void handleUpdateCommand() {
178-
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
179-
System.err.println(ErrorMessages.INVALID_ENABLE_DISABLE);
180-
Main.exit(-1);
181-
}
182-
if (line.hasOption(GravitinoOptions.ENABLE)) {
183-
boolean enableMetalake = line.hasOption(GravitinoOptions.ALL);
184-
gravitinoCommandLine
185-
.newCatalogEnable(context, metalake, catalog, enableMetalake)
186-
.validate()
187-
.handle();
188-
}
189-
if (line.hasOption(GravitinoOptions.DISABLE)) {
190-
gravitinoCommandLine.newCatalogDisable(context, metalake, catalog).validate().handle();
178+
179+
if (line.hasOption(GravitinoOptions.ENABLE) || line.hasOption(GravitinoOptions.DISABLE)) {
180+
gravitinoCommandLine.newManageCatalog(context, metalake, catalog).validate().handle();
191181
}
192182

193183
if (line.hasOption(GravitinoOptions.COMMENT)) {

clients/cli/src/main/java/org/apache/gravitino/cli/CommandContext.java

+8
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ public String auth() {
115115
return auth;
116116
}
117117

118+
/**
119+
* Returns the command line.
120+
*
121+
* @return The command line.
122+
*/
123+
public CommandLine line() {
124+
return line;
125+
}
118126
/**
119127
* Retrieves the Gravitino URL from the command line options or the GRAVITINO_URL environment
120128
* variable or the Gravitino config file.

clients/cli/src/main/java/org/apache/gravitino/cli/ErrorMessages.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class ErrorMessages {
3939
public static final String ENTITY_IN_USE = " in use, please disable it first.";
4040

4141
public static final String INVALID_ENABLE_DISABLE =
42-
"Unable to us --enable and --disable at the same time";
42+
"Unable to use --enable and --disable at the same time.";
4343
public static final String INVALID_OWNER_COMMAND =
4444
"Unsupported combination of options either use --user or --group.";
4545
public static final String INVALID_REMOVE_COMMAND =

clients/cli/src/main/java/org/apache/gravitino/cli/MetalakeCommandHandler.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,8 @@ private void handlePropertiesCommand() {
159159

160160
/** Handles the "UPDATE" command. */
161161
private void handleUpdateCommand() {
162-
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
163-
System.err.println(ErrorMessages.INVALID_ENABLE_DISABLE);
164-
Main.exit(-1);
165-
}
166-
if (line.hasOption(GravitinoOptions.ENABLE)) {
167-
boolean enableAllCatalogs = line.hasOption(GravitinoOptions.ALL);
168-
gravitinoCommandLine
169-
.newMetalakeEnable(context, metalake, enableAllCatalogs)
170-
.validate()
171-
.handle();
172-
}
173-
if (line.hasOption(GravitinoOptions.DISABLE)) {
174-
gravitinoCommandLine.newMetalakeDisable(context, metalake).validate().handle();
162+
if (line.hasOption(GravitinoOptions.ENABLE) || line.hasOption(GravitinoOptions.DISABLE)) {
163+
gravitinoCommandLine.newManageMetalake(context, metalake).validate().handle();
175164
}
176165

177166
if (line.hasOption(GravitinoOptions.COMMENT)) {

clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java

+6-18
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import org.apache.gravitino.cli.commands.AddRoleToUser;
2727
import org.apache.gravitino.cli.commands.CatalogAudit;
2828
import org.apache.gravitino.cli.commands.CatalogDetails;
29-
import org.apache.gravitino.cli.commands.CatalogDisable;
30-
import org.apache.gravitino.cli.commands.CatalogEnable;
3129
import org.apache.gravitino.cli.commands.ClientVersion;
3230
import org.apache.gravitino.cli.commands.ColumnAudit;
3331
import org.apache.gravitino.cli.commands.CreateCatalog;
@@ -78,10 +76,10 @@
7876
import org.apache.gravitino.cli.commands.ListTopicProperties;
7977
import org.apache.gravitino.cli.commands.ListTopics;
8078
import org.apache.gravitino.cli.commands.ListUsers;
79+
import org.apache.gravitino.cli.commands.ManageCatalog;
80+
import org.apache.gravitino.cli.commands.ManageMetalake;
8181
import org.apache.gravitino.cli.commands.MetalakeAudit;
8282
import org.apache.gravitino.cli.commands.MetalakeDetails;
83-
import org.apache.gravitino.cli.commands.MetalakeDisable;
84-
import org.apache.gravitino.cli.commands.MetalakeEnable;
8583
import org.apache.gravitino.cli.commands.ModelAudit;
8684
import org.apache.gravitino.cli.commands.ModelDetails;
8785
import org.apache.gravitino.cli.commands.OwnerDetails;
@@ -839,23 +837,13 @@ protected RevokeAllPrivileges newRevokeAllPrivileges(
839837
return new RevokeAllPrivileges(context, metalake, role, entity);
840838
}
841839

842-
protected MetalakeEnable newMetalakeEnable(
843-
CommandContext context, String metalake, boolean enableAllCatalogs) {
844-
return new MetalakeEnable(context, metalake, enableAllCatalogs);
840+
protected ManageMetalake newManageMetalake(CommandContext context, String metalake) {
841+
return new ManageMetalake(context, metalake);
845842
}
846843

847-
protected MetalakeDisable newMetalakeDisable(CommandContext context, String metalake) {
848-
return new MetalakeDisable(context, metalake);
849-
}
850-
851-
protected CatalogEnable newCatalogEnable(
852-
CommandContext context, String metalake, String catalog, boolean enableMetalake) {
853-
return new CatalogEnable(context, metalake, catalog, enableMetalake);
854-
}
855-
856-
protected CatalogDisable newCatalogDisable(
844+
protected ManageCatalog newManageCatalog(
857845
CommandContext context, String metalake, String catalog) {
858-
return new CatalogDisable(context, metalake, catalog);
846+
return new ManageCatalog(context, metalake, catalog);
859847
}
860848

861849
protected ListModel newListModel(

clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogDisable.java

-62
This file was deleted.

clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogEnable.java clients/cli/src/main/java/org/apache/gravitino/cli/commands/ManageCatalog.java

+49-11
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,63 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
1920
package org.apache.gravitino.cli.commands;
2021

22+
import org.apache.commons.cli.CommandLine;
2123
import org.apache.gravitino.cli.CommandContext;
2224
import org.apache.gravitino.cli.ErrorMessages;
25+
import org.apache.gravitino.cli.GravitinoOptions;
2326
import org.apache.gravitino.client.GravitinoAdminClient;
2427
import org.apache.gravitino.client.GravitinoClient;
2528
import org.apache.gravitino.exceptions.MetalakeNotInUseException;
2629
import org.apache.gravitino.exceptions.NoSuchCatalogException;
2730
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
2831

29-
/** Enable catalog. */
30-
public class CatalogEnable extends Command {
32+
/** Disable or enable a catalog. */
33+
public class ManageCatalog extends Command {
3134
private final String metalake;
3235
private final String catalog;
36+
private final CommandLine line;
3337
private final boolean enableMetalake;
3438

3539
/**
36-
* Enable catalog
40+
* Constrcut a new instance of the {@code ManageCatalog}.
3741
*
38-
* @param context The command context.
39-
* @param metalake The name of the metalake.
40-
* @param catalog The name of the catalog.
41-
* @param enableMetalake Whether to enable it's metalake
42+
* @param context the command context.
43+
* @param metalake the metalake name.
44+
* @param catalog the catalog name.
4245
*/
43-
public CatalogEnable(
44-
CommandContext context, String metalake, String catalog, boolean enableMetalake) {
46+
public ManageCatalog(CommandContext context, String metalake, String catalog) {
4547
super(context);
4648
this.metalake = metalake;
4749
this.catalog = catalog;
48-
this.enableMetalake = enableMetalake;
50+
this.line = context.line();
51+
this.enableMetalake = line.hasOption(GravitinoOptions.ALL);
4952
}
5053

51-
/** Enable catalog. */
54+
/** Disable or enable a catalog. */
5255
@Override
5356
public void handle() {
57+
if (line.hasOption(GravitinoOptions.ENABLE)) {
58+
enableCatalog();
59+
} else if (line.hasOption(GravitinoOptions.DISABLE)) {
60+
disableCatalog();
61+
}
62+
}
63+
64+
/** {@inheritDoc} */
65+
@Override
66+
public Command validate() {
67+
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
68+
exitWithError(ErrorMessages.INVALID_ENABLE_DISABLE);
69+
}
70+
71+
return super.validate();
72+
}
73+
74+
/** Enable a catalog. */
75+
private void enableCatalog() {
5476
try {
5577
if (enableMetalake) {
5678
GravitinoAdminClient adminClient = buildAdminClient();
@@ -71,4 +93,20 @@ public void handle() {
7193

7294
printInformation(metalake + "." + catalog + " has been enabled.");
7395
}
96+
97+
/** Disable a catalog. */
98+
private void disableCatalog() {
99+
try {
100+
GravitinoClient client = buildClient(metalake);
101+
client.disableCatalog(catalog);
102+
} catch (NoSuchMetalakeException noSuchMetalakeException) {
103+
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
104+
} catch (NoSuchCatalogException noSuchCatalogException) {
105+
exitWithError(ErrorMessages.UNKNOWN_CATALOG);
106+
} catch (Exception exp) {
107+
exitWithError(exp.getMessage());
108+
}
109+
110+
printInformation(metalake + "." + catalog + " has been disabled.");
111+
}
74112
}

clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeEnable.java clients/cli/src/main/java/org/apache/gravitino/cli/commands/ManageMetalake.java

+46-10
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,56 @@
2020
package org.apache.gravitino.cli.commands;
2121

2222
import java.util.Arrays;
23+
import org.apache.commons.cli.CommandLine;
2324
import org.apache.gravitino.cli.CommandContext;
2425
import org.apache.gravitino.cli.ErrorMessages;
26+
import org.apache.gravitino.cli.GravitinoOptions;
2527
import org.apache.gravitino.client.GravitinoAdminClient;
2628
import org.apache.gravitino.client.GravitinoMetalake;
2729
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
2830

29-
/** Enable metalake. */
30-
public class MetalakeEnable extends Command {
31-
31+
/** Disable or enable metalake. */
32+
public class ManageMetalake extends Command {
3233
private final String metalake;
34+
private final CommandLine line;
3335
private Boolean enableAllCatalogs;
3436

3537
/**
36-
* Enable a metalake
38+
* Construct a new instance of the {@code ManageMetalake}.
3739
*
38-
* @param context The command context.
39-
* @param metalake The name of the metalake.
40-
* @param enableAllCatalogs Whether to enable all catalogs.
40+
* @param context the command context.
41+
* @param metalake the name of the metalake.
4142
*/
42-
public MetalakeEnable(CommandContext context, String metalake, boolean enableAllCatalogs) {
43+
public ManageMetalake(CommandContext context, String metalake) {
4344
super(context);
4445
this.metalake = metalake;
45-
this.enableAllCatalogs = enableAllCatalogs;
46+
this.line = context.line();
47+
48+
this.enableAllCatalogs = line.hasOption(GravitinoOptions.ALL);
4649
}
4750

48-
/** Enable metalake. */
51+
/** Disable or enable the metalake. */
4952
@Override
5053
public void handle() {
54+
if (line.hasOption(GravitinoOptions.ENABLE)) {
55+
enableMetalake();
56+
} else if (line.hasOption(GravitinoOptions.DISABLE)) {
57+
disableMetalake();
58+
}
59+
}
60+
61+
/** {@inheritDoc} */
62+
@Override
63+
public Command validate() {
64+
if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {
65+
exitWithError(ErrorMessages.INVALID_ENABLE_DISABLE);
66+
}
67+
68+
return super.validate();
69+
}
70+
71+
/** Enable the metalake. */
72+
private void enableMetalake() {
5173
StringBuilder msgBuilder = new StringBuilder(metalake);
5274
try {
5375
GravitinoAdminClient client = buildAdminClient();
@@ -68,4 +90,18 @@ public void handle() {
6890

6991
printInformation(msgBuilder.toString());
7092
}
93+
94+
/** Disable the metalake. */
95+
private void disableMetalake() {
96+
try {
97+
GravitinoAdminClient client = buildAdminClient();
98+
client.disableMetalake(metalake);
99+
} catch (NoSuchMetalakeException err) {
100+
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
101+
} catch (Exception exp) {
102+
exitWithError(exp.getMessage());
103+
}
104+
105+
printInformation(metalake + " has been disabled.");
106+
}
71107
}

0 commit comments

Comments
 (0)