World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#5826] fix(CLI): Fix Dropping a metalake via the Gravitino CLI gives an "in use" exception (#5907)
Lord of Abyssfb78e6582e89
diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.javaindex bc825443e9..7c8539ba1c 100644--- a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java@@ -213,6 +213,18 @@ public class GravitinoCommandLine extends TestableCommandLine {break;case CommandActions.UPDATE:+ if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {+ System.err.println("Unable to enable and disable at the same time");+ Main.exit(-1);+ }+ if (line.hasOption(GravitinoOptions.ENABLE)) {+ boolean enableAllCatalogs = line.hasOption(GravitinoOptions.ALL);+ newMetalakeEnable(url, ignore, metalake, enableAllCatalogs).handle();+ }+ if (line.hasOption(GravitinoOptions.DISABLE)) {+ newMetalakeDisable(url, ignore, metalake).handle();+ }+if (line.hasOption(GravitinoOptions.COMMENT)) {comment = line.getOptionValue(GravitinoOptions.COMMENT);newUpdateMetalakeComment(url, ignore, metalake, comment).handle();@@ -290,6 +302,18 @@ public class GravitinoCommandLine extends TestableCommandLine {break;case CommandActions.UPDATE:+ if (line.hasOption(GravitinoOptions.ENABLE) && line.hasOption(GravitinoOptions.DISABLE)) {+ System.err.println("Unable to enable and disable at the same time");+ Main.exit(-1);+ }+ if (line.hasOption(GravitinoOptions.ENABLE)) {+ boolean enableMetalake = line.hasOption(GravitinoOptions.ALL);+ newCatalogEnable(url, ignore, metalake, catalog, enableMetalake).handle();+ }+ if (line.hasOption(GravitinoOptions.DISABLE)) {+ newCatalogDisable(url, ignore, metalake, catalog).handle();+ }+if (line.hasOption(GravitinoOptions.COMMENT)) {String updateComment = line.getOptionValue(GravitinoOptions.COMMENT);newUpdateCatalogComment(url, ignore, metalake, catalog, updateComment).handle();diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoOptions.java b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoOptions.javaindex a42591026a..657566036d 100644--- a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoOptions.java+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoOptions.java@@ -59,6 +59,9 @@ public class GravitinoOptions {public static final String USER = "user";public static final String VALUE = "value";public static final String VERSION = "version";+ public static final String ALL = "all";+ public static final String ENABLE = "enable";+ public static final String DISABLE = "disable";/*** Builds and returns the CLI options for Gravitino.@@ -84,6 +87,8 @@ public class GravitinoOptions {options.addOption(createSimpleOption(PARTITION, "display partition information"));options.addOption(createSimpleOption("o", OWNER, "display entity owner"));options.addOption(createSimpleOption(null, SORTORDER, "display sortorder information"));+ options.addOption(createSimpleOption(null, ENABLE, "enable entities"));+ options.addOption(createSimpleOption(null, DISABLE, "disable entities"));// Create/update optionsoptions.addOption(createArgOption(RENAME, "new entity name"));@@ -102,6 +107,7 @@ public class GravitinoOptions {options.addOption(createArgOption(DEFAULT, "default column value"));options.addOption(createSimpleOption("o", OWNER, "display entity owner"));options.addOption(createArgOption(COLUMNFILE, "CSV file describing columns"));+ options.addOption(createSimpleOption(null, ALL, "all operation for --enable"));// Options that support multiple valuesoptions.addOption(createArgsOption("p", PROPERTIES, "property name/value pairs"));diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java b/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.javaindex 41909f7209..effe0da1f1 100644--- a/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java@@ -26,6 +26,8 @@ import org.apache.gravitino.cli.commands.AddRoleToGroup;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;@@ -75,6 +77,8 @@ import org.apache.gravitino.cli.commands.ListTopics;import org.apache.gravitino.cli.commands.ListUsers;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.OwnerDetails;import org.apache.gravitino.cli.commands.RemoveAllTags;import org.apache.gravitino.cli.commands.RemoveCatalogProperty;@@ -884,4 +888,23 @@ public class TestableCommandLine {String[] privileges) {return new RevokePrivilegesFromRole(url, ignore, metalake, role, entity, privileges);}++ protected MetalakeEnable newMetalakeEnable(+ String url, boolean ignore, String metalake, boolean enableAllCatalogs) {+ return new MetalakeEnable(url, ignore, metalake, enableAllCatalogs);+ }++ protected MetalakeDisable newMetalakeDisable(String url, boolean ignore, String metalake) {+ return new MetalakeDisable(url, ignore, metalake);+ }++ protected CatalogEnable newCatalogEnable(+ String url, boolean ignore, String metalake, String catalog, boolean enableMetalake) {+ return new CatalogEnable(url, ignore, metalake, catalog, enableMetalake);+ }++ protected CatalogDisable newCatalogDisable(+ String url, boolean ignore, String metalake, String catalog) {+ return new CatalogDisable(url, ignore, metalake, catalog);+ }}diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogDisable.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogDisable.javanew file mode 100644index 0000000000..620a4291ee--- /dev/null+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogDisable.java@@ -0,0 +1,62 @@+/*+ * 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.gravitino.cli.commands;++import org.apache.gravitino.cli.ErrorMessages;+import org.apache.gravitino.client.GravitinoClient;+import org.apache.gravitino.exceptions.NoSuchCatalogException;+import org.apache.gravitino.exceptions.NoSuchMetalakeException;++/** Disable catalog. */+public class CatalogDisable extends Command {++ private final String metalake;+ private final String catalog;++ /**+ * Disable catalog+ *+ * @param url The URL of the Gravitino server.+ * @param ignoreVersions If true don't check the client/server versions match.+ * @param metalake The name of the metalake.+ * @param catalog The name of the catalog.+ */+ public CatalogDisable(String url, boolean ignoreVersions, String metalake, String catalog) {+ super(url, ignoreVersions);+ this.metalake = metalake;+ this.catalog = catalog;+ }++ /** Disable catalog. */+ @Override+ public void handle() {+ 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());+ }++ System.out.println(metalake + "." + catalog + " has been disabled.");+ }+}diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogEnable.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogEnable.javanew file mode 100644index 0000000000..8646baee29--- /dev/null+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CatalogEnable.java@@ -0,0 +1,74 @@+/*+ * 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.gravitino.cli.commands;++import org.apache.gravitino.cli.ErrorMessages;+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 {+ private final String metalake;+ private final String catalog;+ private final boolean enableMetalake;++ /**+ * Enable catalog+ *+ * @param url The URL of the Gravitino server.+ * @param ignoreVersions If true don't check the client/server versions match.+ * @param metalake The name of the metalake.+ * @param catalog The name of the catalog.+ * @param enableMetalake Whether to enable it's metalake+ */+ public CatalogEnable(+ String url, boolean ignoreVersions, String metalake, String catalog, boolean enableMetalake) {+ super(url, ignoreVersions);+ this.metalake = metalake;+ this.catalog = catalog;+ this.enableMetalake = enableMetalake;+ }++ /** Enable catalog. */+ @Override+ public void handle() {+ try {+ if (enableMetalake) {+ GravitinoAdminClient adminClient = buildAdminClient();+ adminClient.enableMetalake(metalake);+ }+ GravitinoClient client = buildClient(metalake);+ client.enableCatalog(catalog);+ } catch (NoSuchMetalakeException noSuchMetalakeException) {+ exitWithError(ErrorMessages.UNKNOWN_METALAKE);+ } catch (NoSuchCatalogException noSuchCatalogException) {+ exitWithError(ErrorMessages.UNKNOWN_CATALOG);+ } catch (MetalakeNotInUseException notInUseException) {+ exitWithError(+ metalake + " not in use. please use --recursive option, or enable metalake first");+ } catch (Exception exp) {+ exitWithError(exp.getMessage());+ }++ System.out.println(metalake + "." + catalog + " has been enabled.");+ }+}diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteCatalog.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteCatalog.javaindex 6c5fbaee97..6aa8e5ad90 100644--- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteCatalog.java+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteCatalog.java@@ -22,6 +22,7 @@ package org.apache.gravitino.cli.commands;import org.apache.gravitino.cli.AreYouSure;import org.apache.gravitino.cli.ErrorMessages;import org.apache.gravitino.client.GravitinoClient;+import org.apache.gravitino.exceptions.CatalogInUseException;import org.apache.gravitino.exceptions.NoSuchCatalogException;import org.apache.gravitino.exceptions.NoSuchMetalakeException;@@ -64,6 +65,8 @@ public class DeleteCatalog extends Command {exitWithError(ErrorMessages.UNKNOWN_METALAKE);} catch (NoSuchCatalogException err) {exitWithError(ErrorMessages.UNKNOWN_CATALOG);+ } catch (CatalogInUseException catalogInUseException) {+ System.err.println(catalog + " in use, please disable it first.");} catch (Exception exp) {exitWithError(exp.getMessage());}diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteMetalake.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteMetalake.javaindex 386dde9213..e88ae41486 100644--- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteMetalake.java+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/DeleteMetalake.java@@ -22,6 +22,7 @@ package org.apache.gravitino.cli.commands;import org.apache.gravitino.cli.AreYouSure;import org.apache.gravitino.cli.ErrorMessages;import org.apache.gravitino.client.GravitinoAdminClient;+import org.apache.gravitino.exceptions.MetalakeInUseException;import org.apache.gravitino.exceptions.NoSuchMetalakeException;public class DeleteMetalake extends Command {@@ -56,6 +57,8 @@ public class DeleteMetalake extends Command {deleted = client.dropMetalake(metalake);} catch (NoSuchMetalakeException err) {exitWithError(ErrorMessages.UNKNOWN_METALAKE);+ } catch (MetalakeInUseException inUseException) {+ System.err.println(metalake + " in use, please disable it first.");} catch (Exception exp) {exitWithError(exp.getMessage());}diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeDisable.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeDisable.javanew file mode 100644index 0000000000..02e33a45d4--- /dev/null+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeDisable.java@@ -0,0 +1,56 @@+/*+ * 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.gravitino.cli.commands;++import org.apache.gravitino.cli.ErrorMessages;+import org.apache.gravitino.client.GravitinoAdminClient;+import org.apache.gravitino.exceptions.NoSuchMetalakeException;++/** Disable metalake. */+public class MetalakeDisable extends Command {+ private String metalake;++ /**+ * Disable metalake+ *+ * @param url The URL of the Gravitino server.+ * @param ignoreVersions If true don't check the client/server versions match.+ * @param metalake The name of the metalake.+ */+ public MetalakeDisable(String url, boolean ignoreVersions, String metalake) {+ super(url, ignoreVersions);+ this.metalake = metalake;+ }++ /** Disable metalake. */+ @Override+ public void handle() {+ try {+ GravitinoAdminClient client = buildAdminClient();+ client.disableMetalake(metalake);+ } catch (NoSuchMetalakeException err) {+ exitWithError(ErrorMessages.UNKNOWN_METALAKE);+ } catch (Exception exp) {+ exitWithError(exp.getMessage());+ }++ System.out.println(metalake + " has been disabled.");+ }+}diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeEnable.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeEnable.javanew file mode 100644index 0000000000..34ba23a61b--- /dev/null+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/MetalakeEnable.java@@ -0,0 +1,72 @@+/*+ * 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.gravitino.cli.commands;++import java.util.Arrays;+import org.apache.gravitino.cli.ErrorMessages;+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 {++ private final String metalake;+ private Boolean enableAllCatalogs;++ /**+ * Enable a metalake+ *+ * @param url The URL of the Gravitino server.+ * @param ignoreVersions If true don't check the client/server versions match.+ * @param metalake The name of the metalake.+ * @param enableAllCatalogs Whether to enable all catalogs.+ */+ public MetalakeEnable(+ String url, boolean ignoreVersions, String metalake, boolean enableAllCatalogs) {+ super(url, ignoreVersions);+ this.metalake = metalake;+ this.enableAllCatalogs = enableAllCatalogs;+ }++ /** Enable metalake. */+ @Override+ public void handle() {+ StringBuilder msgBuilder = new StringBuilder(metalake);+ try {+ GravitinoAdminClient client = buildAdminClient();+ client.enableMetalake(metalake);+ msgBuilder.append(" has been enabled.");++ if (enableAllCatalogs) {+ GravitinoMetalake metalakeObject = client.loadMetalake(metalake);+ String[] catalogs = metalakeObject.listCatalogs();+ Arrays.stream(catalogs).forEach(metalakeObject::enableCatalog);+ msgBuilder.append(" and all catalogs in this metalake have been enabled.");+ }+ } catch (NoSuchMetalakeException err) {+ exitWithError(ErrorMessages.UNKNOWN_METALAKE);+ } catch (Exception exp) {+ exitWithError(exp.getMessage());+ }++ System.out.println(msgBuilder);+ }+}diff --git a/clients/cli/src/main/resources/catalog_help.txt b/clients/cli/src/main/resources/catalog_help.txtindex 27ba7eeac3..c29e9dcadb 100644--- a/clients/cli/src/main/resources/catalog_help.txt+++ b/clients/cli/src/main/resources/catalog_help.txt@@ -47,4 +47,10 @@ Set a catalog's propertygcli catalog set --name catalog_mysql --property test --value valueRemove a catalog's property-gcli catalog remove --name catalog_mysql --property test\ No newline at end of file+gcli catalog remove --name catalog_mysql --property test++Enable a catalog+gcli catalog update -m metalake_demo --name catalog --enable++Disable a catalog+gcli catalog update -m metalake_demo --name catalog --disable\ No newline at end of filediff --git a/clients/cli/src/main/resources/metalake_help.txt b/clients/cli/src/main/resources/metalake_help.txtindex c80d244f52..f700d3a07e 100644--- a/clients/cli/src/main/resources/metalake_help.txt+++ b/clients/cli/src/main/resources/metalake_help.txt@@ -38,3 +38,9 @@ gcli metalake set --property test --value valueRemove a metalake's propertygcli metalake remove --property test++Enable a metalake+gcli metalake update -m metalake_demo --enable++Disable a metalke+gcli metalake update -m metalake_demo --disable\ No newline at end of filediff --git a/clients/cli/src/test/java/org/apache/gravitino/cli/TestCatalogCommands.java b/clients/cli/src/test/java/org/apache/gravitino/cli/TestCatalogCommands.javaindex eb8bc46d38..d751d67173 100644--- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestCatalogCommands.java+++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestCatalogCommands.java@@ -19,17 +19,24 @@package org.apache.gravitino.cli;+import static org.junit.Assert.assertThrows;+import static org.junit.Assert.assertTrue;import static org.mockito.Mockito.doReturn;import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.never;import static org.mockito.Mockito.spy;import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;+import java.io.ByteArrayOutputStream;+import java.io.PrintStream;import java.util.HashMap;import org.apache.commons.cli.CommandLine;import org.apache.commons.cli.Options;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.CreateCatalog;import org.apache.gravitino.cli.commands.DeleteCatalog;import org.apache.gravitino.cli.commands.ListCatalogProperties;@@ -38,6 +45,7 @@ import org.apache.gravitino.cli.commands.RemoveCatalogProperty;import org.apache.gravitino.cli.commands.SetCatalogProperty;import org.apache.gravitino.cli.commands.UpdateCatalogComment;import org.apache.gravitino.cli.commands.UpdateCatalogName;+import org.junit.jupiter.api.AfterEach;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;@@ -45,10 +53,28 @@ class TestCatalogCommands {private CommandLine mockCommandLine;private Options mockOptions;+ private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();+ private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();+ private final PrintStream originalOut = System.out;+ private final PrintStream originalErr = System.err;+@BeforeEachvoid setUp() {mockCommandLine = mock(CommandLine.class);mockOptions = mock(Options.class);+ System.setOut(new PrintStream(outContent));+ System.setErr(new PrintStream(errContent));+ }++ @AfterEach+ void restoreExitFlg() {+ Main.useExit = true;+ }++ @AfterEach+ public void restoreStreams() {+ System.setOut(originalOut);+ System.setErr(originalErr);}@Test@@ -291,4 +317,92 @@ class TestCatalogCommands {commandLine.handleCommandLine();verify(mockUpdateName).handle();}++ @Test+ void testEnableCatalogCommand() {+ CatalogEnable mockEnable = mock(CatalogEnable.class);+ when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);+ when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");+ when(mockCommandLine.hasOption(GravitinoOptions.NAME)).thenReturn(true);+ when(mockCommandLine.getOptionValue(GravitinoOptions.NAME)).thenReturn("catalog");+ when(mockCommandLine.hasOption(GravitinoOptions.ENABLE)).thenReturn(true);++ GravitinoCommandLine commandLine =+ spy(+ new GravitinoCommandLine(+ mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.UPDATE));+ doReturn(mockEnable)+ .when(commandLine)+ .newCatalogEnable(+ GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", false);+ commandLine.handleCommandLine();+ verify(mockEnable).handle();+ }++ @Test+ void testEnableCatalogCommandWithRecursive() {+ CatalogEnable mockEnable = mock(CatalogEnable.class);+ when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);+ when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");+ when(mockCommandLine.hasOption(GravitinoOptions.NAME)).thenReturn(true);+ when(mockCommandLine.getOptionValue(GravitinoOptions.NAME)).thenReturn("catalog");+ when(mockCommandLine.hasOption(GravitinoOptions.ALL)).thenReturn(true);+ when(mockCommandLine.hasOption(GravitinoOptions.ENABLE)).thenReturn(true);++ GravitinoCommandLine commandLine =+ spy(+ new GravitinoCommandLine(+ mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.UPDATE));+ doReturn(mockEnable)+ .when(commandLine)+ .newCatalogEnable(+ GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", true);+ commandLine.handleCommandLine();+ verify(mockEnable).handle();+ }++ @Test+ void testDisableCatalogCommand() {+ CatalogDisable mockDisable = mock(CatalogDisable.class);+ when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);+ when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");+ when(mockCommandLine.hasOption(GravitinoOptions.NAME)).thenReturn(true);+ when(mockCommandLine.getOptionValue(GravitinoOptions.NAME)).thenReturn("catalog");+ when(mockCommandLine.hasOption(GravitinoOptions.DISABLE)).thenReturn(true);++ GravitinoCommandLine commandLine =+ spy(+ new GravitinoCommandLine(+ mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.UPDATE));+ doReturn(mockDisable)+ .when(commandLine)+ .newCatalogDisable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog");+ commandLine.handleCommandLine();+ verify(mockDisable).handle();+ }++ @Test+ @SuppressWarnings("DefaultCharset")+ void testCatalogWithDisableAndEnableOptions() {+ Main.useExit = false;+ when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);+ when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");+ when(mockCommandLine.hasOption(GravitinoOptions.NAME)).thenReturn(true);+ when(mockCommandLine.getOptionValue(GravitinoOptions.NAME)).thenReturn("catalog");+ when(mockCommandLine.hasOption(GravitinoOptions.DISABLE)).thenReturn(true);+ when(mockCommandLine.hasOption(GravitinoOptions.ENABLE)).thenReturn(true);++ GravitinoCommandLine commandLine =+ spy(+ new GravitinoCommandLine(+ mockCommandLine, mockOptions, CommandEntities.CATALOG, CommandActions.UPDATE));++ assertThrows(RuntimeException.class, commandLine::handleCommandLine);+ verify(commandLine, never())+ .newCatalogEnable(+ GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", false);+ verify(commandLine, never())+ .newCatalogDisable(GravitinoCommandLine.DEFAULT_URL, false, "melake_demo", "catalog");+ assertTrue(errContent.toString().contains("Unable to enable and disable at the same time"));+ }}diff --git a/clients/cli/src/test/java/org/apache/gravitino/cli/TestMain.java b/clients/cli/src/test/java/org/apache/gravitino/cli/TestMain.javaindex 93de0a6bc9..377e569aa5 100644--- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestMain.java+++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestMain.java@@ -150,6 +150,7 @@ public class TestMain {assertEquals(CommandEntities.CATALOG, entity);}+ @Testpublic void metalakeWithHelpOption() throws ParseException {Options options = new GravitinoOptions().options();CommandLineParser parser = new DefaultParser();diff --git a/clients/cli/src/test/java/org/apache/gravitino/cli/TestMetalakeCommands.java b/clients/cli/src/test/java/org/apache/gravitino/cli/TestMetalakeCommands.javaindex b7468b635a..01eebb6dab 100644--- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestMetalakeCommands.java+++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestMetalakeCommands.java@@ -19,12 +19,16 @@package org.apache.gravitino.cli;+import static org.junit.Assert.assertTrue;import static org.mockito.Mockito.doReturn;import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.never;import static org.mockito.Mockito.spy;import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;+import java.io.ByteArrayOutputStream;+import java.io.PrintStream;import org.apache.commons.cli.CommandLine;import org.apache.commons.cli.Options;import org.apache.gravitino.cli.commands.CreateMetalake;@@ -33,21 +37,42 @@ import org.apache.gravitino.cli.commands.ListMetalakeProperties;import org.apache.gravitino.cli.commands.ListMetalakes;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.RemoveMetalakeProperty;import org.apache.gravitino.cli.commands.SetMetalakeProperty;import org.apache.gravitino.cli.commands.UpdateMetalakeComment;import org.apache.gravitino.cli.commands.UpdateMetalakeName;+import org.junit.Assert;+import org.junit.jupiter.api.AfterEach;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;class TestMetalakeCommands {private CommandLine mockCommandLine;private Options mockOptions;+ private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();+ private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();+ private final PrintStream originalOut = System.out;+ private final PrintStream originalErr = System.err;@BeforeEachvoid setUp() {mockCommandLine = mock(CommandLine.class);mockOptions = mock(Options.class);+ System.setOut(new PrintStream(outContent));+ System.setErr(new PrintStream(errContent));+ }++ @AfterEach+ void restoreExitFlg() {+ Main.useExit = true;+ }++ @AfterEach+ public void restoreStreams() {+ System.setOut(originalOut);+ System.setErr(originalErr);}@Test@@ -280,4 +305,80 @@ class TestMetalakeCommands {commandLine.handleCommandLine();verify(mockUpdateName).handle();}++ @Test+ void testEnableMetalakeCommand() {+ MetalakeEnable mockEnable = mock(MetalakeEnable.class);+ when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);+ when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");+ when(mockCommandLine.hasOption(GravitinoOptions.ENABLE)).thenReturn(true);+ GravitinoCommandLine commandLine =+ spy(+ new GravitinoCommandLine(+ mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.UPDATE));+ doReturn(mockEnable)+ .when(commandLine)+ .newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", false);+ commandLine.handleCommandLine();+ verify(mockEnable).handle();+ }++ @Test+ void testEnableMetalakeCommandWithRecursive() {+ MetalakeEnable mockEnable = mock(MetalakeEnable.class);+ when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);+ when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");+ when(mockCommandLine.hasOption(GravitinoOptions.ALL)).thenReturn(true);+ when(mockCommandLine.hasOption(GravitinoOptions.ENABLE)).thenReturn(true);+ GravitinoCommandLine commandLine =+ spy(+ new GravitinoCommandLine(+ mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.UPDATE));+ doReturn(mockEnable)+ .when(commandLine)+ .newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", true);+ commandLine.handleCommandLine();+ verify(mockEnable).handle();+ }++ @Test+ void testDisableMetalakeCommand() {+ MetalakeDisable mockDisable = mock(MetalakeDisable.class);+ when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);+ when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");+ when(mockCommandLine.hasOption(GravitinoOptions.DISABLE)).thenReturn(true);++ GravitinoCommandLine commandLine =+ spy(+ new GravitinoCommandLine(+ mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.UPDATE));+ doReturn(mockDisable)+ .when(commandLine)+ .newMetalakeDisable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo");++ commandLine.handleCommandLine();+ verify(mockDisable).handle();+ }++ @Test+ @SuppressWarnings("DefaultCharset")+ void testMetalakeWithDisableAndEnableOptions() {+ Main.useExit = false;+ when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);+ when(mockCommandLine.getOptionValue(CommandEntities.METALAKE)).thenReturn("metalake_demo");+ when(mockCommandLine.hasOption(GravitinoOptions.ENABLE)).thenReturn(true);+ when(mockCommandLine.hasOption(GravitinoOptions.DISABLE)).thenReturn(true);++ GravitinoCommandLine commandLine =+ spy(+ new GravitinoCommandLine(+ mockCommandLine, mockOptions, CommandEntities.METALAKE, CommandActions.UPDATE));++ Assert.assertThrows(RuntimeException.class, commandLine::handleCommandLine);+ verify(commandLine, never())+ .newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", false);+ verify(commandLine, never())+ .newMetalakeEnable(GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", false);+ assertTrue(errContent.toString().contains("Unable to enable and disable at the same time"));+ }}diff --git a/docs/cli.md b/docs/cli.mdindex e6e2f5aa60..64d720f2e8 100644--- a/docs/cli.md+++ b/docs/cli.md@@ -276,6 +276,24 @@ gcli metalake set --property test --value valuegcli metalake remove --property test```+#### Enable a metalake++```bash+gcli metalake update -m metalake_demo --enable+```++#### Enable a metalake and all catalogs++```bash+gcli metalake update -m metalake_demo --enable --all+```++#### Disable a metalake++```bash+gcli metalake update -m metalake_demo --disable+```+### Catalog commands#### Show all catalogs in a metalake@@ -390,6 +408,24 @@ gcli catalog set --name catalog_mysql --property test --value valuegcli catalog remove --name catalog_mysql --property test```+#### Enable a catalog++```bash+gcli catalog update -m metalake_demo --name catalog --enable+```++#### Enable a catalog and it's metalake++```bash+gcli catalog update -m metalake_demo --name catalog --enable --all+```++#### Disable a catalog++```bash+gcli catalog update -m metalake_demo --name catalog --disable+```+### Schema commands#### Show all schemas in a catalog