forked from apache/gravitino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[apache#6153]refactor: Break up role commands in Gravitino CLI (apach…
…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
1 parent
14ec383
commit b438ef4
Showing
2 changed files
with
156 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
clients/cli/src/main/java/org/apache/gravitino/cli/RoleCommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |