Skip to content

Commit

Permalink
[apache#6422] improve(CLI): Add roles and owner command context CLI (a…
Browse files Browse the repository at this point in the history
…pache#6438)

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

Add roles and owner command context CLI

### Why are the changes needed?

Fix: apache#6422 

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

No.

### How was this patch tested?

local test.
  • Loading branch information
Abyss-lord authored and youngyjd committed Feb 14, 2025
1 parent 440fac0 commit ea64c72
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private void executeCommand() {
if (CommandActions.HELP.equals(command)) {
handleHelpCommand();
} else if (line.hasOption(GravitinoOptions.OWNER)) {
new OwnerCommandHandler(this, line, command, ignore, entity).handle();
new OwnerCommandHandler(this, line, command, context, entity).handle();
} else if (entity.equals(CommandEntities.COLUMN)) {
new ColumnCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.TABLE)) {
Expand All @@ -136,7 +136,7 @@ private void executeCommand() {
} else if (entity.equals(CommandEntities.TAG)) {
new TagCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.ROLE)) {
new RoleCommandHandler(this, line, command, ignore).handle();
new RoleCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.MODEL)) {
new ModelCommandHandler(this, line, command, context).handle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public class OwnerCommandHandler extends CommandHandler {
private final GravitinoCommandLine gravitinoCommandLine;
private final CommandLine line;
private final String command;
private final boolean ignore;
private final String url;
private final CommandContext context;
private final FullName name;
private final String metalake;
private final String entityName;
Expand All @@ -42,21 +41,21 @@ public class OwnerCommandHandler extends CommandHandler {
* @param gravitinoCommandLine The Gravitino command line instance.
* @param line The command line arguments.
* @param command The command to execute.
* @param ignore Ignore server version mismatch.
* @param context The command context.
* @param entity The entity to execute the command on.
*/
public OwnerCommandHandler(
GravitinoCommandLine gravitinoCommandLine,
CommandLine line,
String command,
boolean ignore,
CommandContext context,
String entity) {
this.gravitinoCommandLine = gravitinoCommandLine;
this.line = line;
this.command = command;
this.ignore = ignore;
this.context = context;

this.url = getUrl(line);
this.context.setUrl(getUrl(line));
this.owner = line.getOptionValue(GravitinoOptions.USER);
this.group = line.getOptionValue(GravitinoOptions.GROUP);
this.name = new FullName(line);
Expand Down Expand Up @@ -102,22 +101,19 @@ private boolean executeCommand() {

/** Handles the "DETAILS" command. */
private void handleDetailsCommand() {
gravitinoCommandLine
.newOwnerDetails(url, ignore, metalake, entityName, entity)
.validate()
.handle();
gravitinoCommandLine.newOwnerDetails(context, metalake, entityName, entity).validate().handle();
}

/** Handles the "SET" command. */
private void handleSetCommand() {
if (owner != null && group == null) {
gravitinoCommandLine
.newSetOwner(url, ignore, metalake, entityName, entity, owner, false)
.newSetOwner(context, metalake, entityName, entity, owner, false)
.validate()
.handle();
} else if (owner == null && group != null) {
gravitinoCommandLine
.newSetOwner(url, ignore, metalake, entityName, entity, group, true)
.newSetOwner(context, metalake, entityName, entity, group, true)
.validate()
.handle();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,21 @@ public class RoleCommandHandler extends CommandHandler {
private final GravitinoCommandLine gravitinoCommandLine;
private final CommandLine line;
private final String command;
private final boolean ignore;
private final String url;
private final CommandContext context;
private String metalake;
private String[] roles;
private String[] privileges;

public RoleCommandHandler(
GravitinoCommandLine gravitinoCommandLine, CommandLine line, String command, boolean ignore) {
GravitinoCommandLine gravitinoCommandLine,
CommandLine line,
String command,
CommandContext context) {
this.gravitinoCommandLine = gravitinoCommandLine;
this.line = line;
this.command = command;
this.ignore = ignore;
this.url = getUrl(line);
this.context = context;
this.context.setUrl(getUrl(line));
}

/** Handles the command execution logic based on the provided command. */
Expand Down Expand Up @@ -106,32 +108,27 @@ private boolean executeCommand() {

private void handleDetailsCommand() {
if (line.hasOption(GravitinoOptions.AUDIT)) {
gravitinoCommandLine.newRoleAudit(url, ignore, metalake, getOneRole()).validate().handle();
gravitinoCommandLine.newRoleAudit(context, metalake, getOneRole()).validate().handle();
} else {
gravitinoCommandLine.newRoleDetails(url, ignore, metalake, getOneRole()).validate().handle();
gravitinoCommandLine.newRoleDetails(context, metalake, getOneRole()).validate().handle();
}
}

private void handleListCommand() {
gravitinoCommandLine.newListRoles(url, ignore, metalake).validate().handle();
gravitinoCommandLine.newListRoles(context, metalake).validate().handle();
}

private void handleCreateCommand() {
gravitinoCommandLine.newCreateRole(url, ignore, metalake, roles).validate().handle();
gravitinoCommandLine.newCreateRole(context, metalake, roles).validate().handle();
}

private void handleDeleteCommand() {
boolean forceDelete = line.hasOption(GravitinoOptions.FORCE);
gravitinoCommandLine
.newDeleteRole(url, ignore, forceDelete, metalake, roles)
.validate()
.handle();
gravitinoCommandLine.newDeleteRole(context, metalake, roles).validate().handle();
}

private void handleGrantCommand() {
gravitinoCommandLine
.newGrantPrivilegesToRole(
url, ignore, metalake, getOneRole(), new FullName(line), privileges)
.newGrantPrivilegesToRole(context, metalake, getOneRole(), new FullName(line), privileges)
.validate()
.handle();
}
Expand All @@ -140,13 +137,13 @@ private void handleRevokeCommand() {
boolean removeAll = line.hasOption(GravitinoOptions.ALL);
if (removeAll) {
gravitinoCommandLine
.newRevokeAllPrivileges(url, ignore, metalake, getOneRole(), new FullName(line))
.newRevokeAllPrivileges(context, metalake, getOneRole(), new FullName(line))
.validate()
.handle();
} else {
gravitinoCommandLine
.newRevokePrivilegesFromRole(
url, ignore, metalake, getOneRole(), new FullName(line), privileges)
context, metalake, getOneRole(), new FullName(line), privileges)
.validate()
.handle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,25 +451,24 @@ protected AddRoleToGroup newAddRoleToGroup(
return new AddRoleToGroup(context, metalake, group, role);
}

protected RoleDetails newRoleDetails(String url, boolean ignore, String metalake, String role) {
return new RoleDetails(url, ignore, metalake, role);
protected RoleDetails newRoleDetails(CommandContext context, String metalake, String role) {
return new RoleDetails(context, metalake, role);
}

protected ListRoles newListRoles(String url, boolean ignore, String metalake) {
return new ListRoles(url, ignore, metalake);
protected ListRoles newListRoles(CommandContext context, String metalake) {
return new ListRoles(context, metalake);
}

protected RoleAudit newRoleAudit(String url, boolean ignore, String metalake, String role) {
return new RoleAudit(url, ignore, metalake, role);
protected RoleAudit newRoleAudit(CommandContext context, String metalake, String role) {
return new RoleAudit(context, metalake, role);
}

protected CreateRole newCreateRole(String url, boolean ignore, String metalake, String[] roles) {
return new CreateRole(url, ignore, metalake, roles);
protected CreateRole newCreateRole(CommandContext context, String metalake, String[] roles) {
return new CreateRole(context, metalake, roles);
}

protected DeleteRole newDeleteRole(
String url, boolean ignore, boolean force, String metalake, String[] roles) {
return new DeleteRole(url, ignore, force, metalake, roles);
protected DeleteRole newDeleteRole(CommandContext context, String metalake, String[] roles) {
return new DeleteRole(context, metalake, roles);
}

protected TagDetails newTagDetails(CommandContext context, String metalake, String tag) {
Expand Down Expand Up @@ -549,19 +548,18 @@ protected ListColumns newListColumns(
}

protected SetOwner newSetOwner(
String url,
boolean ignore,
CommandContext context,
String metalake,
String entity,
String entityType,
String owner,
boolean isGroup) {
return new SetOwner(url, ignore, metalake, entity, entityType, owner, isGroup);
return new SetOwner(context, metalake, entity, entityType, owner, isGroup);
}

protected OwnerDetails newOwnerDetails(
String url, boolean ignore, String metalake, String entity, String entityType) {
return new OwnerDetails(url, ignore, metalake, entity, entityType);
CommandContext context, String metalake, String entity, String entityType) {
return new OwnerDetails(context, metalake, entity, entityType);
}

protected ListTopics newListTopics(
Expand Down Expand Up @@ -827,28 +825,18 @@ protected CreateTable newCreateTable(
}

protected GrantPrivilegesToRole newGrantPrivilegesToRole(
String url,
boolean ignore,
String metalake,
String role,
FullName entity,
String[] privileges) {
return new GrantPrivilegesToRole(url, ignore, metalake, role, entity, privileges);
CommandContext context, String metalake, String role, FullName entity, String[] privileges) {
return new GrantPrivilegesToRole(context, metalake, role, entity, privileges);
}

protected RevokePrivilegesFromRole newRevokePrivilegesFromRole(
String url,
boolean ignore,
String metalake,
String role,
FullName entity,
String[] privileges) {
return new RevokePrivilegesFromRole(url, ignore, metalake, role, entity, privileges);
CommandContext context, String metalake, String role, FullName entity, String[] privileges) {
return new RevokePrivilegesFromRole(context, metalake, role, entity, privileges);
}

protected RevokeAllPrivileges newRevokeAllPrivileges(
String url, boolean ignore, String metalake, String role, FullName entity) {
return new RevokeAllPrivileges(url, ignore, metalake, role, entity);
CommandContext context, String metalake, String role, FullName entity) {
return new RevokeAllPrivileges(context, metalake, role, entity);
}

protected MetalakeEnable newMetalakeEnable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.common.base.Joiner;
import java.util.Collections;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
Expand All @@ -33,13 +34,12 @@ public class CreateRole extends Command {
/**
* Create a new role.
*
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
* @param context The command context.
* @param metalake The name of the metalake.
* @param roles The array of roles.
*/
public CreateRole(String url, boolean ignoreVersions, String metalake, String[] roles) {
super(url, ignoreVersions);
public CreateRole(CommandContext context, String metalake, String[] roles) {
super(context);
this.metalake = metalake;
this.roles = roles;
}
Expand All @@ -60,6 +60,6 @@ public void handle() {
exitWithError(exp.getMessage());
}

System.out.println(Joiner.on(", ").join(roles) + " created");
printInformation(Joiner.on(", ").join(roles) + " created");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.collect.Lists;
import java.util.List;
import org.apache.gravitino.cli.AreYouSure;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
Expand All @@ -37,17 +38,14 @@ public class DeleteRole extends Command {
/**
* Delete a role.
*
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
* @param force Force operation.
* @param context The command context.
* @param metalake The name of the metalake.
* @param roles The name of the role.
*/
public DeleteRole(
String url, boolean ignoreVersions, boolean force, String metalake, String[] roles) {
super(url, ignoreVersions);
public DeleteRole(CommandContext context, String metalake, String[] roles) {
super(context);
this.metalake = metalake;
this.force = force;
this.force = context.force();
this.roles = roles;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Set;
import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.authorization.Privilege;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.FullName;
import org.apache.gravitino.cli.Privileges;
Expand All @@ -43,21 +44,15 @@ public class GrantPrivilegesToRole extends MetadataCommand {
/**
* Grants one or more privileges.
*
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
* @param context The command context.
* @param metalake The name of the metalake.
* @param role The name of the role.
* @param entity The name of the entity.
* @param privileges The list of privileges.
*/
public GrantPrivilegesToRole(
String url,
boolean ignoreVersions,
String metalake,
String role,
FullName entity,
String[] privileges) {
super(url, ignoreVersions);
CommandContext context, String metalake, String role, FullName entity, String[] privileges) {
super(context);
this.metalake = metalake;
this.entity = entity;
this.role = role;
Expand All @@ -73,8 +68,7 @@ public void handle() {

for (String privilege : privileges) {
if (!Privileges.isValid(privilege)) {
System.err.println(ErrorMessages.UNKNOWN_PRIVILEGE + " " + privilege);
return;
exitWithError(ErrorMessages.UNKNOWN_PRIVILEGE + " " + privilege);
}
PrivilegeDTO privilegeDTO =
PrivilegeDTO.builder()
Expand All @@ -87,21 +81,17 @@ public void handle() {
MetadataObject metadataObject = constructMetadataObject(entity, client);
client.grantPrivilegesToRole(role, metadataObject, privilegesSet);
} catch (NoSuchMetalakeException err) {
System.err.println(ErrorMessages.UNKNOWN_METALAKE);
return;
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (NoSuchRoleException err) {
System.err.println(ErrorMessages.UNKNOWN_ROLE);
return;
exitWithError(ErrorMessages.UNKNOWN_ROLE);
} catch (NoSuchMetadataObjectException err) {
System.err.println(ErrorMessages.UNKNOWN_USER);
return;
exitWithError(ErrorMessages.UNKNOWN_USER);
} catch (Exception exp) {
System.err.println(exp.getMessage());
return;
exitWithError(exp.getMessage());
}

String all = String.join(",", privileges);
System.out.println(role + " granted " + all + " on " + entity.getName());
printInformation(role + " granted " + all + " on " + entity.getName());
}

@Override
Expand Down
Loading

0 comments on commit ea64c72

Please sign in to comment.