5
0
mirror of https://github.com/apache/sqoop.git synced 2025-05-16 17:00:53 +08:00

SQOOP-2144: Sqoop2: Show command for CLI

(Abraham Elmahrek via Jarek Jarcec Cecho)
This commit is contained in:
Jarek Jarcec Cecho 2015-02-28 17:09:25 -08:00
parent ebceb3c7a1
commit a021b7cdb5
9 changed files with 317 additions and 6 deletions

View File

@ -70,9 +70,11 @@ public JSONObject extract(boolean skipSensitive) {
@SuppressWarnings("unchecked")
protected JSONArray extractPrincipals() {
JSONArray principalsArray = new JSONArray();
if (principals != null) {
for (MPrincipal principal : principals) {
principalsArray.add(extractPrincipal(principal));
}
}
return principalsArray;
}

View File

@ -73,9 +73,11 @@ public JSONObject extract(boolean skipSensitive) {
@SuppressWarnings("unchecked")
protected JSONArray extractPrivileges() {
JSONArray privilegesArray = new JSONArray();
if (privileges != null) {
for (MPrivilege privilege : privileges) {
privilegesArray.add(extractPrivilege(privilege));
}
}
return privilegesArray;
}

View File

@ -69,9 +69,11 @@ public JSONObject extract(boolean skipSensitive) {
@SuppressWarnings("unchecked")
protected JSONArray extractRoles() {
JSONArray rolesArray = new JSONArray();
if (roles != null) {
for (MRole role : roles) {
rolesArray.add(extractRole(role));
}
}
return rolesArray;
}

View File

@ -36,6 +36,9 @@ protected ShowCommand(Shell shell) {
.put(Constants.FN_JOB, ShowJobFunction.class)
.put(Constants.FN_SUBMISSION, ShowSubmissionFunction.class)
.put(Constants.FN_OPTION, ShowOptionFunction.class)
.put(Constants.FN_ROLE, ShowRoleFunction.class)
.put(Constants.FN_PRINCIPAL, ShowPrincipalFunction.class)
.put(Constants.FN_PRIVILEGE, ShowPrivilegeFunction.class)
.build()
);
}

View File

@ -0,0 +1,77 @@
/**
* 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.sqoop.shell;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionBuilder;
import org.apache.sqoop.model.MPrincipal;
import org.apache.sqoop.model.MRole;
import org.apache.sqoop.shell.core.Constants;
import org.apache.sqoop.shell.utils.TableDisplayer;
import org.apache.sqoop.validation.Status;
import java.util.LinkedList;
import java.util.List;
import static org.apache.sqoop.shell.ShellEnvironment.client;
import static org.apache.sqoop.shell.ShellEnvironment.resourceString;
@SuppressWarnings("serial")
public class ShowPrincipalFunction extends SqoopFunction {
@SuppressWarnings("static-access")
public ShowPrincipalFunction() {
this.addOption(OptionBuilder
.withLongOpt(Constants.OPT_ROLE)
.withDescription(resourceString(Constants.RES_PROMPT_ROLE))
.hasArg()
.isRequired()
.create(Constants.OPT_ROLE_CHAR)
);
}
@Override
public Object executeFunction(CommandLine line, boolean isInteractive) {
MRole role = null;
if (line.hasOption(Constants.OPT_ROLE)) {
role = new MRole(line.getOptionValue(Constants.OPT_ROLE));
}
showPrincipals(role);
return Status.OK;
}
private void showPrincipals(MRole role) {
List<MPrincipal> principals = client.getPrincipalsByRole(role);
List<String> header = new LinkedList<String>();
header.add(resourceString(Constants.RES_TABLE_HEADER_PRINCIPAL_NAME));
header.add(resourceString(Constants.RES_TABLE_HEADER_PRINCIPAL_TYPE));
List<String> names = new LinkedList<String>();
List<String> types = new LinkedList<String>();
for (MPrincipal principal : principals) {
names.add(principal.getName());
types.add(principal.getType());
}
TableDisplayer.display(header, names, types);
}
}

View File

@ -0,0 +1,111 @@
/**
* 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.sqoop.shell;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionBuilder;
import org.apache.sqoop.common.SqoopException;
import org.apache.sqoop.model.MPrincipal;
import org.apache.sqoop.model.MPrivilege;
import org.apache.sqoop.model.MResource;
import org.apache.sqoop.shell.core.Constants;
import org.apache.sqoop.shell.core.ShellError;
import org.apache.sqoop.shell.utils.TableDisplayer;
import org.apache.sqoop.validation.Status;
import java.util.LinkedList;
import java.util.List;
import static org.apache.sqoop.shell.ShellEnvironment.client;
import static org.apache.sqoop.shell.ShellEnvironment.resourceString;
@SuppressWarnings("serial")
public class ShowPrivilegeFunction extends SqoopFunction {
@SuppressWarnings("static-access")
public ShowPrivilegeFunction() {
this.addOption(OptionBuilder
.withLongOpt(Constants.OPT_PRINCIPAL_TYPE)
.withDescription(resourceString(Constants.RES_PROMPT_PRINCIPAL_TYPE))
.hasArg()
.isRequired()
.create()
);
this.addOption(OptionBuilder
.withLongOpt(Constants.OPT_PRINCIPAL)
.withDescription(resourceString(Constants.RES_PROMPT_PRINCIPAL))
.hasArg()
.isRequired()
.create()
);
this.addOption(OptionBuilder
.withLongOpt(Constants.OPT_RESOURCE_TYPE)
.withDescription(resourceString(Constants.RES_PROMPT_RESOURCE_TYPE))
.hasArg()
.create()
);
this.addOption(OptionBuilder
.withLongOpt(Constants.OPT_RESOURCE)
.withDescription(resourceString(Constants.RES_PROMPT_RESOURCE))
.hasArg()
.create()
);
}
@Override
public Object executeFunction(CommandLine line, boolean isInteractive) {
if (line.hasOption(Constants.OPT_RESOURCE) ^ line.hasOption(Constants.OPT_RESOURCE_TYPE)) {
throw new SqoopException(ShellError.SHELL_0003,
ShellEnvironment.getResourceBundle().getString(Constants.RES_SHOW_PRIVILEGE_BAD_ARGUMENTS_RESOURCE_TYPE));
}
MPrincipal principal = new MPrincipal(
line.getOptionValue(Constants.OPT_PRINCIPAL),
line.getOptionValue(Constants.OPT_PRINCIPAL_TYPE));
MResource resource = (line.hasOption(Constants.OPT_RESOURCE))
? new MResource(line.getOptionValue(Constants.OPT_RESOURCE), line.getOptionValue(Constants.OPT_RESOURCE_TYPE)) : null;
showPrivileges(principal, resource);
return Status.OK;
}
private void showPrivileges(MPrincipal principal, MResource resource) {
List<MPrivilege> privileges = client.getPrivilegesByPrincipal(principal, resource);
List<String> header = new LinkedList<String>();
header.add(resourceString(Constants.RES_TABLE_HEADER_PRIVILEGE_ACTION));
header.add(resourceString(Constants.RES_TABLE_HEADER_RESOURCE_NAME));
header.add(resourceString(Constants.RES_TABLE_HEADER_RESOURCE_TYPE));
header.add(resourceString(Constants.RES_TABLE_HEADER_PRIVILEGE_WITH_GRANT));
List<String> actions = new LinkedList<String>();
List<String> resourceNames = new LinkedList<String>();
List<String> resourceTypes = new LinkedList<String>();
List<String> withGrant = new LinkedList<String>();
for (MPrivilege privilege : privileges) {
actions.add(privilege.getAction());
resourceNames.add(privilege.getResource().getName());
resourceTypes.add(privilege.getResource().getType());
withGrant.add(Boolean.toString(privilege.isWith_grant_option()));
}
TableDisplayer.display(header, actions, resourceNames, resourceTypes, withGrant);
}
}

View File

@ -0,0 +1,86 @@
/**
* 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.sqoop.shell;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionBuilder;
import org.apache.sqoop.common.SqoopException;
import org.apache.sqoop.model.MPrincipal;
import org.apache.sqoop.model.MRole;
import org.apache.sqoop.shell.core.Constants;
import org.apache.sqoop.shell.core.ShellError;
import org.apache.sqoop.shell.utils.TableDisplayer;
import org.apache.sqoop.validation.Status;
import java.util.LinkedList;
import java.util.List;
import static org.apache.sqoop.shell.ShellEnvironment.client;
import static org.apache.sqoop.shell.ShellEnvironment.resourceString;
@SuppressWarnings("serial")
public class ShowRoleFunction extends SqoopFunction {
@SuppressWarnings("static-access")
public ShowRoleFunction() {
this.addOption(OptionBuilder
.withLongOpt(Constants.OPT_PRINCIPAL_TYPE)
.withDescription(resourceString(Constants.RES_PROMPT_PRINCIPAL_TYPE))
.hasArg()
.create()
);
this.addOption(OptionBuilder
.withLongOpt(Constants.OPT_PRINCIPAL)
.withDescription(resourceString(Constants.RES_PROMPT_PRINCIPAL))
.hasArg()
.create()
);
}
@Override
public Object executeFunction(CommandLine line, boolean isInteractive) {
if (line.hasOption(Constants.OPT_PRINCIPAL) ^ line.hasOption(Constants.OPT_PRINCIPAL_TYPE)) {
throw new SqoopException(ShellError.SHELL_0003,
ShellEnvironment.getResourceBundle().getString(Constants.RES_SHOW_ROLE_BAD_ARGUMENTS_PRINCIPAL_TYPE));
}
MPrincipal principal = (line.hasOption(Constants.OPT_PRINCIPAL))
? new MPrincipal(
line.getOptionValue(Constants.OPT_PRINCIPAL),
line.getOptionValue(Constants.OPT_PRINCIPAL_TYPE))
: null;
showRoles(principal);
return Status.OK;
}
private void showRoles(MPrincipal principal) {
List<MRole> roles = (principal == null) ? client.getRoles() : client.getRolesByPrincipal(principal);
List<String> header = new LinkedList<String>();
header.add(resourceString(Constants.RES_TABLE_HEADER_ROLE_NAME));
List<String> names = new LinkedList<String>();
for (MRole role : roles) {
names.add(role.getName());
}
TableDisplayer.display(header, names);
}
}

View File

@ -136,6 +136,7 @@ public class Constants {
public static final String FN_VERSION = "version";
public static final String FN_DRIVER_CONFIG = "driver";
public static final String FN_ROLE = "role";
public static final String FN_PRINCIPAL = "principal";
public static final String FN_PRIVILEGE = "privilege";
public static final String PROP_HOMEDIR = "user.home";
@ -266,6 +267,10 @@ public class Constants {
"show.prompt_link_info";
public static final String RES_SHOW_PROMPT_LINK_CID_INFO =
"show.prompt_link_cid_info";
public static final String RES_SHOW_ROLE_BAD_ARGUMENTS_PRINCIPAL_TYPE =
"show.role.bad_arguments_principal_type";
public static final String RES_SHOW_PRIVILEGE_BAD_ARGUMENTS_RESOURCE_TYPE =
"show.privilege.bad_arguments_resource_type";
public static final String RES_SHOW_PROMPT_DISPLAY_ALL_CONNECTORS =
"show.prompt_display_all_connectors";
@ -378,6 +383,20 @@ public class Constants {
"table.header.date";
public static final String RES_TABLE_HEADER_ENABLED =
"table.header.enabled";
public static final String RES_TABLE_HEADER_ROLE_NAME =
"table.header.role.name";
public static final String RES_TABLE_HEADER_RESOURCE_NAME =
"table.header.resource.name";
public static final String RES_TABLE_HEADER_RESOURCE_TYPE =
"table.header.resource.type";
public static final String RES_TABLE_HEADER_PRIVILEGE_ACTION =
"table.header.privilege.action";
public static final String RES_TABLE_HEADER_PRIVILEGE_WITH_GRANT =
"table.header.privilege.with_grant";
public static final String RES_TABLE_HEADER_PRINCIPAL_NAME =
"table.header.principal.name";
public static final String RES_TABLE_HEADER_PRINCIPAL_TYPE =
"table.header.principal.type";
public static final String RES_CONFIG_DISPLAYER_LINK =
"config.displayer.link";

View File

@ -141,6 +141,8 @@ show.connector_usage = Usage: show connector
show.prompt_connectors_to_show = @|bold {0} connector(s) to show: |@
show.prompt_connector_info = Connector with id {0}:\n Name: {1} \n \
Class: {2}\n Version: {3}\n Supported Directions {4}
show.role.bad_arguments_principal_type = @|bold principal |@ and @|bold principal-type |@ must be used together.
show.privilege.bad_arguments_resource_type = @|bold resource |@ and @|bold resource-type |@ must be used together.
show.driver_usage = Usage: show driver
show.prompt_driver_opts = @|bold Driver specific options: |@\nPersistent id: {0}
@ -217,6 +219,13 @@ table.header.eid = External Id
table.header.status = Status
table.header.date = Last Update Date
table.header.enabled = Enabled
table.header.role.name = Role Name
table.header.privilege.action = Action
table.header.privilege.with_grant = With Grant
table.header.resource.name = Resource Name
table.header.resource.type = Resource Type
table.header.principal.name = Principal Name
table.header.principal.type = Principal Type
#Config displayer resources
config.displayer.link = link