Skip to content

Commit

Permalink
[apache#6153]refactor: Break up role commands in Gravitino CLI (apach…
Browse files Browse the repository at this point in the history
…e#6170)

### What changes were proposed in this pull request? 
Break up role commands in Gravitino command line class

### Why are the changes needed?
For readability and maintainability.

### Fix: apache#6153

### Does this PR introduce any user-facing change?  
None.

### How was this patch tested?
Tested locally.
  • Loading branch information
cool9850311 authored Jan 10, 2025
1 parent 14ec383 commit b438ef4
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void executeCommand() {
} else if (entity.equals(CommandEntities.TAG)) {
handleTagCommand();
} else if (entity.equals(CommandEntities.ROLE)) {
handleRoleCommand();
new RoleCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.MODEL)) {
new ModelCommandHandler(this, line, command, ignore).handle();
}
Expand Down Expand Up @@ -401,77 +401,6 @@ private String getOneTag(String[] tags) {
return tags[0];
}

/** Handles the command execution for Roles based on command type and the command line options. */
protected void handleRoleCommand() {
String url = getUrl();
String auth = getAuth();
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
FullName name = new FullName(line);
String metalake = name.getMetalakeName();
String[] privileges = line.getOptionValues(GravitinoOptions.PRIVILEGE);

Command.setAuthenticationMode(auth, userName);

String[] roles = line.getOptionValues(GravitinoOptions.ROLE);
if (roles == null && !CommandActions.LIST.equals(command)) {
System.err.println(ErrorMessages.MISSING_ROLE);
Main.exit(-1);
}

if (roles != null) {
roles = Arrays.stream(roles).distinct().toArray(String[]::new);
}

switch (command) {
case CommandActions.DETAILS:
if (line.hasOption(GravitinoOptions.AUDIT)) {
newRoleAudit(url, ignore, metalake, getOneRole(roles)).validate().handle();
} else {
newRoleDetails(url, ignore, metalake, getOneRole(roles)).validate().handle();
}
break;

case CommandActions.LIST:
newListRoles(url, ignore, metalake).validate().handle();
break;

case CommandActions.CREATE:
newCreateRole(url, ignore, metalake, roles).validate().handle();
break;

case CommandActions.DELETE:
boolean forceDelete = line.hasOption(GravitinoOptions.FORCE);
newDeleteRole(url, ignore, forceDelete, metalake, roles).validate().handle();
break;

case CommandActions.GRANT:
newGrantPrivilegesToRole(url, ignore, metalake, getOneRole(roles), name, privileges)
.validate()
.handle();
break;

case CommandActions.REVOKE:
newRevokePrivilegesFromRole(url, ignore, metalake, getOneRole(roles), name, privileges)
.validate()
.handle();
break;

default:
System.err.println(ErrorMessages.UNSUPPORTED_ACTION);
Main.exit(-1);
break;
}
}

private String getOneRole(String[] roles) {
if (roles == null || roles.length != 1) {
System.err.println(ErrorMessages.MULTIPLE_ROLE_COMMAND_ERROR);
Main.exit(-1);
}

return roles[0];
}

/**
* 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
@@ -0,0 +1,155 @@
/*
* 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;

import java.util.Arrays;
import org.apache.commons.cli.CommandLine;
import org.apache.gravitino.cli.commands.Command;

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 String metalake;
private String[] roles;
private String[] privileges;

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

/** Handles the command execution logic based on the provided command. */
public void handle() {
String auth = getAuth(line);
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(auth, userName);

metalake = new FullName(line).getMetalakeName();

roles = line.getOptionValues(GravitinoOptions.ROLE);
if (roles == null && !CommandActions.LIST.equals(command)) {
System.err.println(ErrorMessages.MISSING_ROLE);
Main.exit(-1);
}
if (roles != null) {
roles = Arrays.stream(roles).distinct().toArray(String[]::new);
}

privileges = line.getOptionValues(GravitinoOptions.PRIVILEGE);

if (!executeCommand()) {
System.err.println(ErrorMessages.UNSUPPORTED_ACTION);
Main.exit(-1);
}
}

/**
* Executes the specific command based on the command type.
*
* @return true if the command is supported, false otherwise
*/
private boolean executeCommand() {
switch (command) {
case CommandActions.DETAILS:
handleDetailsCommand();
return true;

case CommandActions.LIST:
handleListCommand();
return true;

case CommandActions.CREATE:
handleCreateCommand();
return true;

case CommandActions.DELETE:
handleDeleteCommand();
return true;

case CommandActions.GRANT:
handleGrantCommand();
return true;

case CommandActions.REVOKE:
handleRevokeCommand();
return true;

default:
return false;
}
}

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

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

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

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

private void handleGrantCommand() {
gravitinoCommandLine
.newGrantPrivilegesToRole(
url, ignore, metalake, getOneRole(), new FullName(line), privileges)
.validate()
.handle();
}

private void handleRevokeCommand() {
gravitinoCommandLine
.newRevokePrivilegesFromRole(
url, ignore, metalake, getOneRole(), new FullName(line), privileges)
.validate()
.handle();
}

private String getOneRole() {
if (roles == null || roles.length != 1) {
System.err.println(ErrorMessages.MULTIPLE_ROLE_COMMAND_ERROR);
Main.exit(-1);
}

return roles[0];
}
}

0 comments on commit b438ef4

Please sign in to comment.