Skip to content

Commit

Permalink
[apache#6421] improve(CLI): Add user and group command context CLI (a…
Browse files Browse the repository at this point in the history
…pache#6437)

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

 Add user and group command context CLI

### Why are the changes needed?

Fix: apache#6421

### 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 51c55d3 commit 440fac0
Show file tree
Hide file tree
Showing 21 changed files with 205 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ private void executeCommand() {
} else if (entity.equals(CommandEntities.FILESET)) {
new FilesetCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.USER)) {
new UserCommandHandler(this, line, command, ignore).handle();
new UserCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.GROUP)) {
new GroupCommandHandler(this, line, command, ignore).handle();
new GroupCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.TAG)) {
new TagCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.ROLE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public class GroupCommandHandler 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 String group;
Expand All @@ -39,16 +38,19 @@ public class GroupCommandHandler 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.
*/
public GroupCommandHandler(
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.context = context;

this.url = getUrl(line);
this.context.setUrl(getUrl(line));
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
}
Expand Down Expand Up @@ -111,37 +113,36 @@ private boolean executeCommand() {
/** Handles the "DETAILS" command. */
private void handleDetailsCommand() {
if (line.hasOption(GravitinoOptions.AUDIT)) {
gravitinoCommandLine.newGroupAudit(url, ignore, metalake, group).validate().handle();
gravitinoCommandLine.newGroupAudit(context, metalake, group).validate().handle();
} else {
gravitinoCommandLine.newGroupDetails(url, ignore, metalake, group).validate().handle();
gravitinoCommandLine.newGroupDetails(context, metalake, group).validate().handle();
}
}

/** Handles the "CREATE" command. */
private void handleCreateCommand() {
gravitinoCommandLine.newCreateGroup(url, ignore, metalake, group).validate().handle();
gravitinoCommandLine.newCreateGroup(context, metalake, group).validate().handle();
}

/** Handles the "DELETE" command. */
private void handleDeleteCommand() {
boolean force = line.hasOption(GravitinoOptions.FORCE);
gravitinoCommandLine.newDeleteGroup(url, ignore, force, metalake, group).validate().handle();
gravitinoCommandLine.newDeleteGroup(context, metalake, group).validate().handle();
}

/** Handles the "REVOKE" command. */
private void handleRevokeCommand() {
boolean revokeAll = line.hasOption(GravitinoOptions.ALL);
if (revokeAll) {
gravitinoCommandLine
.newRemoveAllRoles(url, ignore, metalake, group, CommandEntities.GROUP)
.newRemoveAllRoles(context, metalake, group, CommandEntities.GROUP)
.validate()
.handle();
System.out.printf("Removed all roles from group %s%n", group);
} else {
String[] revokeRoles = line.getOptionValues(GravitinoOptions.ROLE);
for (String role : revokeRoles) {
gravitinoCommandLine
.newRemoveRoleFromGroup(url, ignore, metalake, group, role)
.newRemoveRoleFromGroup(context, metalake, group, role)
.validate()
.handle();
}
Expand All @@ -153,16 +154,13 @@ private void handleRevokeCommand() {
private void handleGrantCommand() {
String[] grantRoles = line.getOptionValues(GravitinoOptions.ROLE);
for (String role : grantRoles) {
gravitinoCommandLine
.newAddRoleToGroup(url, ignore, metalake, group, role)
.validate()
.handle();
gravitinoCommandLine.newAddRoleToGroup(context, metalake, group, role).validate().handle();
}
System.out.printf("Grant roles %s to group %s%n", COMMA_JOINER.join(grantRoles), group);
}

/** Handles the "LIST" command. */
private void handleListCommand() {
gravitinoCommandLine.newListGroups(url, ignore, metalake).validate().handle();
gravitinoCommandLine.newListGroups(context, metalake).validate().handle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -386,71 +386,69 @@ protected ListTableProperties newListTableProperties(
return new ListTableProperties(context, metalake, catalog, schema, table);
}

protected UserDetails newUserDetails(String url, boolean ignore, String metalake, String user) {
return new UserDetails(url, ignore, metalake, user);
protected UserDetails newUserDetails(CommandContext context, String metalake, String user) {
return new UserDetails(context, metalake, user);
}

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

protected UserAudit newUserAudit(String url, boolean ignore, String metalake, String user) {
return new UserAudit(url, ignore, metalake, user);
protected UserAudit newUserAudit(CommandContext context, String metalake, String user) {
return new UserAudit(context, metalake, user);
}

protected CreateUser newCreateUser(String url, boolean ignore, String metalake, String user) {
return new CreateUser(url, ignore, metalake, user);
protected CreateUser newCreateUser(CommandContext context, String metalake, String user) {
return new CreateUser(context, metalake, user);
}

protected DeleteUser newDeleteUser(
String url, boolean ignore, boolean force, String metalake, String user) {
return new DeleteUser(url, ignore, force, metalake, user);
protected DeleteUser newDeleteUser(CommandContext context, String metalake, String user) {
return new DeleteUser(context, metalake, user);
}

protected RemoveRoleFromUser newRemoveRoleFromUser(
String url, boolean ignore, String metalake, String user, String role) {
return new RemoveRoleFromUser(url, ignore, metalake, user, role);
CommandContext context, String metalake, String user, String role) {
return new RemoveRoleFromUser(context, metalake, user, role);
}

protected AddRoleToUser newAddRoleToUser(
String url, boolean ignore, String metalake, String user, String role) {
return new AddRoleToUser(url, ignore, metalake, user, role);
CommandContext context, String metalake, String user, String role) {
return new AddRoleToUser(context, metalake, user, role);
}

protected GroupDetails newGroupDetails(String url, boolean ignore, String metalake, String user) {
return new GroupDetails(url, ignore, metalake, user);
protected GroupDetails newGroupDetails(CommandContext context, String metalake, String user) {
return new GroupDetails(context, metalake, user);
}

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

protected GroupAudit newGroupAudit(String url, boolean ignore, String metalake, String group) {
return new GroupAudit(url, ignore, metalake, group);
protected GroupAudit newGroupAudit(CommandContext context, String metalake, String group) {
return new GroupAudit(context, metalake, group);
}

protected CreateGroup newCreateGroup(String url, boolean ignore, String metalake, String user) {
return new CreateGroup(url, ignore, metalake, user);
protected CreateGroup newCreateGroup(CommandContext context, String metalake, String user) {
return new CreateGroup(context, metalake, user);
}

protected DeleteGroup newDeleteGroup(
String url, boolean ignore, boolean force, String metalake, String user) {
return new DeleteGroup(url, ignore, force, metalake, user);
protected DeleteGroup newDeleteGroup(CommandContext context, String metalake, String user) {
return new DeleteGroup(context, metalake, user);
}

protected RemoveRoleFromGroup newRemoveRoleFromGroup(
String url, boolean ignore, String metalake, String group, String role) {
return new RemoveRoleFromGroup(url, ignore, metalake, group, role);
CommandContext context, String metalake, String group, String role) {
return new RemoveRoleFromGroup(context, metalake, group, role);
}

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

protected AddRoleToGroup newAddRoleToGroup(
String url, boolean ignore, String metalake, String group, String role) {
return new AddRoleToGroup(url, ignore, metalake, group, role);
CommandContext context, String metalake, String group, String role) {
return new AddRoleToGroup(context, metalake, group, role);
}

protected RoleDetails newRoleDetails(String url, boolean ignore, String metalake, String role) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public class UserCommandHandler 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 String user;
Expand All @@ -39,16 +38,19 @@ public class UserCommandHandler 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.
*/
public UserCommandHandler(
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.context = context;

this.url = getUrl(line);
this.context.setUrl(getUrl(line));
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
}
Expand Down Expand Up @@ -110,58 +112,42 @@ private boolean executeCommand() {

/** Handles the "LIST" command. */
private void handleListCommand() {
this.gravitinoCommandLine
.newListUsers(this.url, this.ignore, this.metalake)
.validate()
.handle();
this.gravitinoCommandLine.newListUsers(context, metalake).validate().handle();
}

/** Handles the "DETAILS" command. */
private void handleDetailsCommand() {
if (line.hasOption(GravitinoOptions.AUDIT)) {
this.gravitinoCommandLine
.newUserAudit(this.url, this.ignore, this.metalake, user)
.validate()
.handle();
this.gravitinoCommandLine.newUserAudit(context, metalake, user).validate().handle();
} else {
this.gravitinoCommandLine
.newUserDetails(this.url, this.ignore, this.metalake, user)
.validate()
.handle();
this.gravitinoCommandLine.newUserDetails(context, metalake, user).validate().handle();
}
}

/** Handles the "CREATE" command. */
private void handleCreateCommand() {
this.gravitinoCommandLine
.newCreateUser(this.url, this.ignore, this.metalake, user)
.validate()
.handle();
this.gravitinoCommandLine.newCreateUser(context, metalake, user).validate().handle();
}

/** Handles the "DELETE" command. */
private void handleDeleteCommand() {
boolean force = line.hasOption(GravitinoOptions.FORCE);
this.gravitinoCommandLine
.newDeleteUser(this.url, this.ignore, force, this.metalake, user)
.validate()
.handle();
this.gravitinoCommandLine.newDeleteUser(context, metalake, user).validate().handle();
}

/** Handles the "REVOKE" command. */
private void handleRevokeCommand() {
boolean removeAll = line.hasOption(GravitinoOptions.ALL);
if (removeAll) {
gravitinoCommandLine
.newRemoveAllRoles(url, ignore, metalake, user, CommandEntities.USER)
.newRemoveAllRoles(context, metalake, user, CommandEntities.USER)
.validate()
.handle();
System.out.printf("Removed all roles from user %s%n", user);
} else {
String[] revokeRoles = line.getOptionValues(GravitinoOptions.ROLE);
for (String role : revokeRoles) {
this.gravitinoCommandLine
.newRemoveRoleFromUser(this.url, this.ignore, this.metalake, user, role)
.newRemoveRoleFromUser(context, metalake, user, role)
.validate()
.handle();
}
Expand All @@ -173,10 +159,7 @@ private void handleRevokeCommand() {
private void handleGrantCommand() {
String[] grantRoles = line.getOptionValues(GravitinoOptions.ROLE);
for (String role : grantRoles) {
this.gravitinoCommandLine
.newAddRoleToUser(this.url, this.ignore, this.metalake, user, role)
.validate()
.handle();
this.gravitinoCommandLine.newAddRoleToUser(context, metalake, user, role).validate().handle();
}
System.out.printf("Add roles %s to user %s%n", COMMA_JOINER.join(grantRoles), user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.gravitino.cli.commands;

import java.util.ArrayList;
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 @@ -36,15 +37,13 @@ public class AddRoleToGroup extends Command {
/**
* Adds a role to a group.
*
* @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 group The name of the group.
* @param role The name of the role.
*/
public AddRoleToGroup(
String url, boolean ignoreVersions, String metalake, String group, String role) {
super(url, ignoreVersions);
public AddRoleToGroup(CommandContext context, String metalake, String group, String role) {
super(context);
this.metalake = metalake;
this.group = group;
this.role = role;
Expand All @@ -68,6 +67,6 @@ public void handle() {
exitWithError(exp.getMessage());
}

System.out.println(role + " added to " + group);
printInformation(role + " added to " + group);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.gravitino.cli.commands;

import java.util.ArrayList;
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 @@ -36,15 +37,13 @@ public class AddRoleToUser extends Command {
/**
* Adds a role to a user.
*
* @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 user The name of the user.
* @param role The name of the role.
*/
public AddRoleToUser(
String url, boolean ignoreVersions, String metalake, String user, String role) {
super(url, ignoreVersions);
public AddRoleToUser(CommandContext context, String metalake, String user, String role) {
super(context);
this.metalake = metalake;
this.user = user;
this.role = role;
Expand All @@ -68,6 +67,6 @@ public void handle() {
exitWithError(exp.getMessage());
}

System.out.println(role + " added to " + user);
printInformation(role + " added to " + user);
}
}
Loading

0 comments on commit 440fac0

Please sign in to comment.