Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#6153]refactor: Break up role commands in Gravitino CLI #6170

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -474,77 +474,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
Copy link
Member

@justinmclean justinmclean Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is missing a license header, causing the CI to fail.

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;
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All calls below are misisng calls to validate()

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];
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change error messages, as they need to be consistent with other error messages.

Loading