Skip to content

Commit

Permalink
[apache#5962] feat(client): added audit cli command model (apache#6047)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

The audit command is one of the commands suggested by @justinmclean as
part of adding Model entity support for the CLI.

Also includes addition of relevant testing code for Model entity CLI
commands a whole.

### Why are the changes needed?

To add audit functionality for a Model using the CLI

Improvement: apache#5962 

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

Yes.

The audit command for a model was added.

### How was this patch tested?

Unit tests were added for Model CLI support and ran successfully for the
audit command.
  • Loading branch information
VigneshSK17 authored and Abyss-lord committed Jan 3, 2025
1 parent 2221095 commit 82a299e
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class CommandEntities {
VALID_ENTITIES.add(SCHEMA);
VALID_ENTITIES.add(TABLE);
VALID_ENTITIES.add(COLUMN);
VALID_ENTITIES.add(MODEL);
VALID_ENTITIES.add(USER);
VALID_ENTITIES.add(GROUP);
VALID_ENTITIES.add(TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class ErrorMessages {
public static final String UNKNOWN_ROLE = "Unknown role.";
public static final String ROLE_EXISTS = "Role already exists.";
public static final String TABLE_EXISTS = "Table already exists.";
public static final String MODEL_EXISTS = "Model already exists.";
public static final String INVALID_SET_COMMAND =
"Unsupported combination of options either use --name, --user, --group or --property and --value.";
public static final String INVALID_REMOVE_COMMAND =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public String getSchemaName() {
/**
* Retrieves the model name from the second part of the full name option.
*
* @return The model name, or null if not found
* @return The model name, or null if not found.
*/
public String getModelName() {
return getNamePart(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ private void executeCommand() {
handleCatalogCommand();
} else if (entity.equals(CommandEntities.METALAKE)) {
handleMetalakeCommand();
} else if (entity.equals(CommandEntities.MODEL)) {
handleModelCommand();
} else if (entity.equals(CommandEntities.TOPIC)) {
handleTopicCommand();
} else if (entity.equals(CommandEntities.FILESET)) {
Expand Down Expand Up @@ -1152,6 +1154,9 @@ private void handleFilesetCommand() {
}
}

/**
* Handles the command execution for Models based on command type and the command line options.
*/
private void handleModelCommand() {
String url = getUrl();
String auth = getAuth();
Expand Down Expand Up @@ -1182,7 +1187,11 @@ private void handleModelCommand() {

switch (command) {
case CommandActions.DETAILS:
newModelDetails(url, ignore, metalake, catalog, schema, model).handle();
if (line.hasOption(GravitinoOptions.AUDIT)) {
newModelAudit(url, ignore, metalake, catalog, schema, model).handle();
} else {
newModelDetails(url, ignore, metalake, catalog, schema, model).handle();
}
break;

case CommandActions.CREATE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
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;
import org.apache.gravitino.cli.commands.RegisterModel;
Expand Down Expand Up @@ -917,6 +918,11 @@ protected ListModel newListModel(
return new ListModel(url, ignore, metalake, catalog, schema);
}

protected ModelAudit newModelAudit(
String url, boolean ignore, String metalake, String catalog, String schema, String model) {
return new ModelAudit(url, ignore, metalake, catalog, schema, model);
}

protected ModelDetails newModelDetails(
String url, boolean ignore, String metalake, String catalog, String schema, String model) {
return new ModelDetails(url, ignore, metalake, catalog, schema, model);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.NameIdentifier;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
import org.apache.gravitino.exceptions.NoSuchModelException;
import org.apache.gravitino.model.Model;
import org.apache.gravitino.model.ModelCatalog;

/** Displays the audit information of a model. */
public class ModelAudit extends AuditCommand {

protected final String metalake;
protected final String catalog;
protected final String schema;
protected final String model;

/**
* Displays the audit information of a model.
*
* @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 schema The name of the schema.
* @param model The name of the model.
*/
public ModelAudit(
String url,
boolean ignoreVersions,
String metalake,
String catalog,
String schema,
String model) {
super(url, ignoreVersions);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
this.model = model;
}

/** Displays the audit information of a model. */
@Override
public void handle() {
NameIdentifier name = NameIdentifier.of(schema, model);
Model result;

try (GravitinoClient client = buildClient(this.metalake)) {
ModelCatalog modelCatalog = client.loadCatalog(catalog).asModelCatalog();
result = modelCatalog.getModel(name);
} catch (NoSuchMetalakeException err) {
System.err.println(ErrorMessages.UNKNOWN_METALAKE);
return;
} catch (NoSuchCatalogException err) {
System.err.println(ErrorMessages.UNKNOWN_CATALOG);
return;
} catch (NoSuchModelException err) {
System.err.println(ErrorMessages.UNKNOWN_MODEL);
return;
} catch (Exception exp) {
System.err.println(exp.getMessage());
return;
}

if (result != null) {
displayAuditInfo(result.auditInfo());
}
}
}
8 changes: 8 additions & 0 deletions clients/cli/src/main/resources/model_help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
gcli model [details]

Please set the metalake in the Gravitino configuration file or the environment variable before running any of these commands.

Example commands

Show model audit information
gcli model details --name catalog_postgres.hr --audit
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.gravitino.cli.commands.ListModel;
import org.apache.gravitino.cli.commands.ModelAudit;
import org.apache.gravitino.cli.commands.ModelDetails;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.testcontainers.shaded.com.google.common.base.Joiner;

public class TestModelCommand {
public class TestModelCommands {
private final Joiner joiner = Joiner.on(", ").skipNulls();
private CommandLine mockCommandLine;
private Options mockOptions;
Expand Down Expand Up @@ -267,4 +268,25 @@ void testModelDetailsCommandWithoutModel() {
+ joiner.join(Collections.singletonList(CommandEntities.MODEL)),
output);
}

@Test
void testModelAuditCommand() {
ModelAudit mockAudit = mock(ModelAudit.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.schema.model");
when(mockCommandLine.hasOption(GravitinoOptions.AUDIT)).thenReturn(true);

GravitinoCommandLine commandLine =
spy(
new GravitinoCommandLine(
mockCommandLine, mockOptions, CommandEntities.MODEL, CommandActions.DETAILS));
doReturn(mockAudit)
.when(commandLine)
.newModelAudit(
GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", "schema", "model");
commandLine.handleCommandLine();
verify(mockAudit).handle();
}
}
4 changes: 2 additions & 2 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ alias gcli='java -jar ../../cli/build/libs/gravitino-cli-*-incubating-SNAPSHOT.j
Or you use the `gcli.sh` script found in the `clients/cli/bin/` directory to run the CLI.

## Usage

f
The general structure for running commands with the Gravitino CLI is `gcli entity command [options]`.

```bash
usage: gcli [metalake|catalog|schema|table|column|user|group|tag|topic|fileset] [list|details|create|delete|update|set|remove|properties|revoke|grant] [options]
usage: gcli [metalake|catalog|schema|model|table|column|user|group|tag|topic|fileset] [list|details|create|delete|update|set|remove|properties|revoke|grant] [options]
Options
usage: gcli
-a,--audit display audit information
Expand Down

0 comments on commit 82a299e

Please sign in to comment.