Skip to content

Commit

Permalink
[#5131] Support tags in CLI (#5161)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Add support for tags in the CLI.

### Why are the changes needed?

To add support for tags in CLI.

Fix: # (issue)

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

Yes, it expands on the command line and adds new commands dealing with
tags.

### How was this patch tested?

Compiled locally.
  • Loading branch information
justinmclean authored Nov 11, 2024
1 parent f3fed4a commit 73c7863
Show file tree
Hide file tree
Showing 22 changed files with 1,231 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class CommandEntities {
public static final String COLUMN = "column";
public static final String USER = "user";
public static final String GROUP = "group";
public static final String TAG = "tag";

private static final HashSet<String> VALID_ENTITIES = new HashSet<>();

Expand All @@ -44,6 +45,7 @@ public class CommandEntities {
VALID_ENTITIES.add(COLUMN);
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 @@ -37,4 +37,10 @@ public class ErrorMessages {
public static final String USER_EXISTS = "User already exists.";
public static final String UNKNOWN_GROUP = "Unknown group.";
public static final String GROUP_EXISTS = "Group already exists.";
public static final String UNKNOWN_TAG = "Unknown tag.";
public static final String TAG_EXISTS = "Tag already exists.";
public static final String INVALID_SET_COMMAND =
"Unsupported combination of options either use --name or --property and --value.";
public static final String INVALID_REMOVE_COMMAND =
"Unsupported combination of options either use --name or --property.";
}
46 changes: 46 additions & 0 deletions clients/cli/src/main/java/org/apache/gravitino/cli/FullName.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,50 @@ public String getNamePart(int position) {
System.err.println(ErrorMessages.MISSING_NAME);
return null;
}

/**
* Helper method to determine a specific part of the full name exits.
*
* @param partNo The part of the name to obtain.
* @return True if the part exitsts.
*/
public boolean hasNamePart(int partNo) {
/* Extract the name part from the full name if available. */
if (line.hasOption(GravitinoOptions.NAME)) {
String[] names = line.getOptionValue(GravitinoOptions.NAME).split("\\.");
int length = names.length;
int position = partNo;

return position <= length;
}

return false;
}

/**
* Does the catalog name exist?
*
* @return True if the catalog name exists, or false if it does not.
*/
public boolean hasCatalogName() {
return hasNamePart(1);
}

/**
* Does the schema name exist?
*
* @return True if the schema name exists, or false if it does not.
*/
public boolean hasSchemaName() {
return hasNamePart(2);
}

/**
* Does the table name exist?
*
* @return True if the table name exists, or false if it does not.
*/
public boolean hasTableName() {
return hasNamePart(3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,53 @@
import org.apache.gravitino.cli.commands.CreateGroup;
import org.apache.gravitino.cli.commands.CreateMetalake;
import org.apache.gravitino.cli.commands.CreateSchema;
import org.apache.gravitino.cli.commands.CreateTag;
import org.apache.gravitino.cli.commands.CreateUser;
import org.apache.gravitino.cli.commands.DeleteCatalog;
import org.apache.gravitino.cli.commands.DeleteGroup;
import org.apache.gravitino.cli.commands.DeleteMetalake;
import org.apache.gravitino.cli.commands.DeleteSchema;
import org.apache.gravitino.cli.commands.DeleteTable;
import org.apache.gravitino.cli.commands.DeleteTag;
import org.apache.gravitino.cli.commands.DeleteUser;
import org.apache.gravitino.cli.commands.GroupDetails;
import org.apache.gravitino.cli.commands.ListAllTags;
import org.apache.gravitino.cli.commands.ListCatalogProperties;
import org.apache.gravitino.cli.commands.ListCatalogs;
import org.apache.gravitino.cli.commands.ListColumns;
import org.apache.gravitino.cli.commands.ListEntityTags;
import org.apache.gravitino.cli.commands.ListGroups;
import org.apache.gravitino.cli.commands.ListMetalakeProperties;
import org.apache.gravitino.cli.commands.ListMetalakes;
import org.apache.gravitino.cli.commands.ListSchema;
import org.apache.gravitino.cli.commands.ListSchemaProperties;
import org.apache.gravitino.cli.commands.ListTables;
import org.apache.gravitino.cli.commands.ListTagProperties;
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.RemoveCatalogProperty;
import org.apache.gravitino.cli.commands.RemoveMetalakeProperty;
import org.apache.gravitino.cli.commands.RemoveSchemaProperty;
import org.apache.gravitino.cli.commands.RemoveTagProperty;
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.SetTagProperty;
import org.apache.gravitino.cli.commands.TableAudit;
import org.apache.gravitino.cli.commands.TableDetails;
import org.apache.gravitino.cli.commands.TagDetails;
import org.apache.gravitino.cli.commands.TagEntity;
import org.apache.gravitino.cli.commands.UntagEntity;
import org.apache.gravitino.cli.commands.UpdateCatalogComment;
import org.apache.gravitino.cli.commands.UpdateCatalogName;
import org.apache.gravitino.cli.commands.UpdateMetalakeComment;
import org.apache.gravitino.cli.commands.UpdateMetalakeName;
import org.apache.gravitino.cli.commands.UpdateTagComment;
import org.apache.gravitino.cli.commands.UpdateTagName;
import org.apache.gravitino.cli.commands.UserDetails;

/* Gravitino Command line */
Expand Down Expand Up @@ -167,6 +179,8 @@ private void executeCommand() {
handleUserCommand();
} else if (entity.equals(CommandEntities.GROUP)) {
handleGroupCommand();
} else if (entity.equals(CommandEntities.TAG)) {
handleTagCommand();
}
}

Expand Down Expand Up @@ -367,6 +381,60 @@ protected void handleGroupCommand() {
}
}

/** Handles the command execution for Tags based on command type and the command line options. */
protected void handleTagCommand() {
String url = getUrl();
FullName name = new FullName(line);
String metalake = name.getMetalakeName();
String tag = line.getOptionValue(GravitinoOptions.TAG);

if (CommandActions.DETAILS.equals(command)) {
new TagDetails(url, ignore, metalake, tag).handle();
} else if (CommandActions.LIST.equals(command)) {
if (!name.hasCatalogName()) {
new ListAllTags(url, ignore, metalake).handle();
} else {
new ListEntityTags(url, ignore, metalake, name).handle();
}
} else if (CommandActions.CREATE.equals(command)) {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
new CreateTag(url, ignore, metalake, tag, comment).handle();
} else if (CommandActions.DELETE.equals(command)) {
new DeleteTag(url, ignore, metalake, tag).handle();
} else if (CommandActions.SET.equals(command)) {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
String value = line.getOptionValue(GravitinoOptions.VALUE);

if (name == null && property != null && value != null) {
new SetTagProperty(url, ignore, metalake, tag, property, value).handle();
} else if (name != null && property == null && value == null) {
new TagEntity(url, ignore, metalake, name, tag).handle();
} else {
System.err.println(ErrorMessages.INVALID_SET_COMMAND);
}
} else if (CommandActions.REMOVE.equals(command)) {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
if (property != null) {
new RemoveTagProperty(url, ignore, metalake, tag, property).handle();
} else if (name != null) {
new UntagEntity(url, ignore, metalake, name, tag).handle();
} else {
System.err.println(ErrorMessages.INVALID_REMOVE_COMMAND);
}
} else if (CommandActions.PROPERTIES.equals(command)) {
new ListTagProperties(url, ignore, metalake, tag).handle();
} else if (CommandActions.UPDATE.equals(command)) {
if (line.hasOption(GravitinoOptions.COMMENT)) {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
new UpdateTagComment(url, ignore, metalake, tag, comment).handle();
}
if (line.hasOption(GravitinoOptions.RENAME)) {
String newName = line.getOptionValue(GravitinoOptions.RENAME);
new UpdateTagName(url, ignore, metalake, tag, newName).handle();
}
}
}

/**
* Handles the command execution for Columns based on command type and the command line options.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class GravitinoOptions {
public static final String PROPERTIES = "properties";
public static final String USER = "user";
public static final String GROUP = "group";
public static final String TAG = "tag";
public static final String AUDIT = "audit";

/**
Expand All @@ -55,9 +56,9 @@ public Options options() {
options.addOption(createSimpleOption("s", SERVER, "Gravitino server version"));
options.addOption(createArgOption("u", URL, "Gravitino URL (default: http://localhost:8090)"));
options.addOption(createArgOption("n", NAME, "full entity name (dot separated)"));
options.addOption(createArgOption("m", METALAKE, "Metalake name"));
options.addOption(createSimpleOption("i", IGNORE, "Ignore client/sever version check"));
options.addOption(createSimpleOption("a", AUDIT, "Display audit information"));
options.addOption(createArgOption("m", METALAKE, "metalake name"));
options.addOption(createSimpleOption("i", IGNORE, "ignore client/sever version check"));
options.addOption(createSimpleOption("a", AUDIT, "display audit information"));

// Create/update options
options.addOption(createArgOption("r", RENAME, "new entity name"));
Expand All @@ -66,9 +67,10 @@ public Options options() {
options.addOption(createArgOption("V", VALUE, "property value"));
options.addOption(
createArgOption(
"t", PROVIDER, "provider one of hadoop, hive, mysql, postgres, iceberg, kafka"));
"z", PROVIDER, "provider one of hadoop, hive, mysql, postgres, iceberg, kafka"));
options.addOption(createArgOption("l", USER, "user name"));
options.addOption(createArgOption("g", GROUP, "group name"));
options.addOption(createArgOption("t", TAG, "tag name"));

// Properties option can have multiple values
Option properties =
Expand Down
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.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
import org.apache.gravitino.exceptions.TagAlreadyExistsException;

public class CreateTag extends Command {
protected final String metalake;
protected final String tag;
protected final String comment;

/**
* Create a new tag.
*
* @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 tag The name of the tag.
* @param comment The comment of the tag.
*/
public CreateTag(
String url, boolean ignoreVersions, String metalake, String tag, String comment) {
super(url, ignoreVersions);
this.metalake = metalake;
this.tag = tag;
this.comment = comment;
}

/** Create a new tag. */
@Override
public void handle() {
try {
GravitinoClient client = buildClient(metalake);
client.createTag(tag, comment, null);
} catch (NoSuchMetalakeException err) {
System.err.println(ErrorMessages.UNKNOWN_METALAKE);
return;
} catch (TagAlreadyExistsException err) {
System.err.println(ErrorMessages.TAG_EXISTS);
return;
} catch (Exception exp) {
System.err.println(exp.getMessage());
return;
}

System.out.println(tag + " created");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.NoSuchMetalakeException;
import org.apache.gravitino.exceptions.NoSuchTagException;

public class DeleteTag extends Command {

protected final String metalake;
protected final String tag;

/**
* Delete a tag.
*
* @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 tag The name of the tag.
*/
public DeleteTag(String url, boolean ignoreVersions, String metalake, String tag) {
super(url, ignoreVersions);
this.metalake = metalake;
this.tag = tag;
}

/** Delete a catalog. */
@Override
public void handle() {
boolean deleted = false;

try {
GravitinoClient client = buildClient(metalake);
deleted = client.deleteTag(tag);
} catch (NoSuchMetalakeException err) {
System.err.println(ErrorMessages.UNKNOWN_METALAKE);
return;
} catch (NoSuchTagException err) {
System.err.println(ErrorMessages.UNKNOWN_TAG);
return;
} catch (Exception exp) {
System.err.println(exp.getMessage());
return;
}

if (deleted) {
System.out.println(tag + " deleted.");
} else {
System.out.println(tag + " not deleted.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class DeleteUser extends Command {
protected final String user;

/**
* Deletes a user.
* Delete a user.
*
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
Expand Down
Loading

0 comments on commit 73c7863

Please sign in to comment.