Skip to content

Commit

Permalink
[apache#5477] improvement(cli): display audit information on Catalog,…
Browse files Browse the repository at this point in the history
… Schema, Table (apache#5509)

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

Add the `--audit` option to display audit information on Catalog,
Schema, Table

### Why are the changes needed?

This change allows users to retrieve additional audit information on
Catalog/Schema/Table, providing more insights.

Close: apache#5477 

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

Yes, it adds the `--audit` option to
`CommandEntities.CATALOG/SCHEMA/TABLE`.

### How was this patch tested?

1. Follow the instructions in the [cli
README](https://github.com/apache/gravitino/tree/main/clients/cli) to
build the CLI sub-project.
2. Start the Gravitino Playground.

To test, use a command like the following:

```
gcli metalake details --metalake metalake_demo --audit
gcli catalog details --metalake metalake_demo --name catalog_postgres --audit
gcli schema details --metalake metalake_demo --name catalog_postgres.hr --audit
gcli table details --metalake metalake_demo --name catalog_postgres.hr.departments --audit
```

Check that the output matches the expected audit information.

![2024-11-08
002032](https://github.com/user-attachments/assets/636e9abf-fdf3-46f0-9e03-d3b485a06de9)
  • Loading branch information
orenccl authored Nov 8, 2024
1 parent 3a21ce4 commit eb3b6db
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
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.ClientVersion;
import org.apache.gravitino.cli.commands.CreateCatalog;
Expand All @@ -47,16 +48,18 @@
import org.apache.gravitino.cli.commands.ListSchemaProperties;
import org.apache.gravitino.cli.commands.ListTables;
import org.apache.gravitino.cli.commands.ListUsers;
import org.apache.gravitino.cli.commands.MetalakeAuditInfo;
import org.apache.gravitino.cli.commands.MetalakeAudit;
import org.apache.gravitino.cli.commands.MetalakeDetails;
import org.apache.gravitino.cli.commands.RemoveCatalogProperty;
import org.apache.gravitino.cli.commands.RemoveMetalakeProperty;
import org.apache.gravitino.cli.commands.RemoveSchemaProperty;
import org.apache.gravitino.cli.commands.SchemaAudit;
import org.apache.gravitino.cli.commands.SchemaDetails;
import org.apache.gravitino.cli.commands.ServerVersion;
import org.apache.gravitino.cli.commands.SetCatalogProperty;
import org.apache.gravitino.cli.commands.SetMetalakeProperty;
import org.apache.gravitino.cli.commands.SetSchemaProperty;
import org.apache.gravitino.cli.commands.TableAudit;
import org.apache.gravitino.cli.commands.TableDetails;
import org.apache.gravitino.cli.commands.UpdateCatalogComment;
import org.apache.gravitino.cli.commands.UpdateCatalogName;
Expand Down Expand Up @@ -177,7 +180,7 @@ private void handleMetalakeCommand() {

if (CommandActions.DETAILS.equals(command)) {
if (line.hasOption(GravitinoOptions.AUDIT)) {
new MetalakeAuditInfo(url, ignore, metalake).handle();
new MetalakeAudit(url, ignore, metalake).handle();
} else {
new MetalakeDetails(url, ignore, metalake).handle();
}
Expand Down Expand Up @@ -225,7 +228,11 @@ private void handleCatalogCommand() {
String catalog = name.getCatalogName();

if (CommandActions.DETAILS.equals(command)) {
new CatalogDetails(url, ignore, metalake, catalog).handle();
if (line.hasOption(GravitinoOptions.AUDIT)) {
new CatalogAudit(url, ignore, metalake, catalog).handle();
} else {
new CatalogDetails(url, ignore, metalake, catalog).handle();
}
} else if (CommandActions.CREATE.equals(command)) {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
String provider = line.getOptionValue(GravitinoOptions.PROVIDER);
Expand Down Expand Up @@ -272,7 +279,11 @@ private void handleSchemaCommand() {
String schema = name.getSchemaName();

if (CommandActions.DETAILS.equals(command)) {
new SchemaDetails(url, ignore, metalake, catalog, schema).handle();
if (line.hasOption(GravitinoOptions.AUDIT)) {
new SchemaAudit(url, ignore, metalake, catalog, schema).handle();
} else {
new SchemaDetails(url, ignore, metalake, catalog, schema).handle();
}
} else if (CommandActions.CREATE.equals(command)) {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
new CreateSchema(url, ignore, metalake, catalog, schema, comment).handle();
Expand Down Expand Up @@ -308,7 +319,11 @@ private void handleTableCommand() {
String table = name.getTableName();

if (CommandActions.DETAILS.equals(command)) {
new TableDetails(url, ignore, metalake, catalog, schema, table).handle();
if (line.hasOption(GravitinoOptions.AUDIT)) {
new TableAudit(url, ignore, metalake, catalog, schema, table).handle();
} else {
new TableDetails(url, ignore, metalake, catalog, schema, table).handle();
}
} else if (CommandActions.CREATE.equals(command)) {
// TODO
} else if (CommandActions.DELETE.equals(command)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.Audit;

public abstract class AuditCommand extends Command {
/**
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
*/
public AuditCommand(String url, boolean ignoreVersions) {
super(url, ignoreVersions);
}

/* Overridden in parent - do nothing */
@Override
public void handle() {}

/**
* Displays audit information for the given audit object.
*
* @param audit from a class that implements the Auditable interface.
*/
public void displayAuditInfo(Audit audit) {
String auditInfo =
"creator,create_time,modified,modified_time"
+ System.lineSeparator()
+ audit.creator()
+ ","
+ audit.createTime()
+ ","
+ audit.lastModifier()
+ ","
+ audit.lastModifiedTime();

System.out.println(auditInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.Catalog;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;

public class CatalogAudit extends AuditCommand {

protected final String metalake;
protected final String catalog;

/**
* Displays the audit information of a 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 CatalogAudit(String url, boolean ignoreVersions, String metalake, String catalog) {
super(url, ignoreVersions);
this.metalake = metalake;
this.catalog = catalog;
}

/** Displays the audit information of a specified catalog. */
@Override
public void handle() {
Catalog result;

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

if (result != null) {
displayAuditInfo(result.auditInfo());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.gravitino.exceptions.NoSuchMetalakeException;

/** Displays the audit information of a metalake. */
public class MetalakeAuditInfo extends Command {
public class MetalakeAudit extends AuditCommand {
protected final String metalake;

/**
Expand All @@ -35,7 +35,7 @@ public class MetalakeAuditInfo extends Command {
* @param ignoreVersions If true don't check the client/server versions match.
* @param metalake The name of the metalake.
*/
public MetalakeAuditInfo(String url, boolean ignoreVersions, String metalake) {
public MetalakeAudit(String url, boolean ignoreVersions, String metalake) {
super(url, ignoreVersions);
this.metalake = metalake;
}
Expand All @@ -54,17 +54,6 @@ public void handle() {
return;
}

String auditInfo =
"creator,createTime,lastModifier,lastModifiedTime"
+ System.lineSeparator()
+ audit.creator()
+ ","
+ audit.createTime()
+ ","
+ audit.lastModifier()
+ ","
+ audit.lastModifiedTime();

System.out.println(auditInfo);
displayAuditInfo(audit);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.Schema;
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.NoSuchSchemaException;

/** Displays the audit information of schema. */
public class SchemaAudit extends AuditCommand {

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

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

/** Displays the audit information of schema. */
@Override
public void handle() {
Schema result;

try (GravitinoClient client = buildClient(metalake)) {
result = client.loadCatalog(catalog).asSchemas().loadSchema(this.schema);
} catch (NoSuchMetalakeException err) {
System.err.println(ErrorMessages.UNKNOWN_METALAKE);
return;
} catch (NoSuchCatalogException err) {
System.err.println(ErrorMessages.UNKNOWN_CATALOG);
return;
} catch (NoSuchSchemaException err) {
System.err.println(ErrorMessages.UNKNOWN_SCHEMA);
return;
} catch (Exception exp) {
System.err.println(exp.getMessage());
return;
}

if (result != null) {
displayAuditInfo(result.auditInfo());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.rel.Table;

/** Displays the audit information of a table. */
public class TableAudit extends TableCommand {

protected final String schema;
protected final String table;

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

/** Displays the audit information of a table. */
@Override
public void handle() {
Table gTable;

try {
NameIdentifier name = NameIdentifier.of(schema, table);
gTable = tableCatalog().loadTable(name);
} catch (Exception exp) {
System.err.println(exp.getMessage());
return;
}

displayAuditInfo(gTable.auditInfo());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.apache.gravitino.rel.TableCatalog;

/* Common code for all table commands. */
public class TableCommand extends Command {
public class TableCommand extends AuditCommand {

protected final String metalake;
protected final String catalog;
Expand Down
Loading

0 comments on commit eb3b6db

Please sign in to comment.