You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sqoop.apache.org by ja...@apache.org on 2015/03/01 02:09:57 UTC
sqoop git commit: SQOOP-2144: Sqoop2: Show command for CLI
Repository: sqoop
Updated Branches:
refs/heads/sqoop2 ebceb3c7a -> a021b7cdb
SQOOP-2144: Sqoop2: Show command for CLI
(Abraham Elmahrek via Jarek Jarcec Cecho)
Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/a021b7cd
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/a021b7cd
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/a021b7cd
Branch: refs/heads/sqoop2
Commit: a021b7cdb5b613aefc43c46dfbd9e4755f2805f6
Parents: ebceb3c
Author: Jarek Jarcec Cecho <ja...@apache.org>
Authored: Sat Feb 28 17:09:25 2015 -0800
Committer: Jarek Jarcec Cecho <ja...@apache.org>
Committed: Sat Feb 28 17:09:25 2015 -0800
----------------------------------------------------------------------
.../org/apache/sqoop/json/PrincipalBean.java | 6 +-
.../org/apache/sqoop/json/PrivilegeBean.java | 6 +-
.../java/org/apache/sqoop/json/RoleBean.java | 6 +-
.../org/apache/sqoop/shell/ShowCommand.java | 3 +
.../sqoop/shell/ShowPrincipalFunction.java | 77 +++++++++++++
.../sqoop/shell/ShowPrivilegeFunction.java | 111 +++++++++++++++++++
.../apache/sqoop/shell/ShowRoleFunction.java | 86 ++++++++++++++
.../org/apache/sqoop/shell/core/Constants.java | 19 ++++
.../main/resources/shell-resource.properties | 9 ++
9 files changed, 317 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/sqoop/blob/a021b7cd/common/src/main/java/org/apache/sqoop/json/PrincipalBean.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/json/PrincipalBean.java b/common/src/main/java/org/apache/sqoop/json/PrincipalBean.java
index 1016b4d..e540b75 100644
--- a/common/src/main/java/org/apache/sqoop/json/PrincipalBean.java
+++ b/common/src/main/java/org/apache/sqoop/json/PrincipalBean.java
@@ -70,8 +70,10 @@ public class PrincipalBean implements JsonBean {
@SuppressWarnings("unchecked")
protected JSONArray extractPrincipals() {
JSONArray principalsArray = new JSONArray();
- for (MPrincipal principal : principals) {
- principalsArray.add(extractPrincipal(principal));
+ if (principals != null) {
+ for (MPrincipal principal : principals) {
+ principalsArray.add(extractPrincipal(principal));
+ }
}
return principalsArray;
}
http://git-wip-us.apache.org/repos/asf/sqoop/blob/a021b7cd/common/src/main/java/org/apache/sqoop/json/PrivilegeBean.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/json/PrivilegeBean.java b/common/src/main/java/org/apache/sqoop/json/PrivilegeBean.java
index 02ef4b2..6819063 100644
--- a/common/src/main/java/org/apache/sqoop/json/PrivilegeBean.java
+++ b/common/src/main/java/org/apache/sqoop/json/PrivilegeBean.java
@@ -73,8 +73,10 @@ public class PrivilegeBean implements JsonBean {
@SuppressWarnings("unchecked")
protected JSONArray extractPrivileges() {
JSONArray privilegesArray = new JSONArray();
- for (MPrivilege privilege : privileges) {
- privilegesArray.add(extractPrivilege(privilege));
+ if (privileges != null) {
+ for (MPrivilege privilege : privileges) {
+ privilegesArray.add(extractPrivilege(privilege));
+ }
}
return privilegesArray;
}
http://git-wip-us.apache.org/repos/asf/sqoop/blob/a021b7cd/common/src/main/java/org/apache/sqoop/json/RoleBean.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/json/RoleBean.java b/common/src/main/java/org/apache/sqoop/json/RoleBean.java
index e552868..e1f5783 100644
--- a/common/src/main/java/org/apache/sqoop/json/RoleBean.java
+++ b/common/src/main/java/org/apache/sqoop/json/RoleBean.java
@@ -69,8 +69,10 @@ public class RoleBean implements JsonBean {
@SuppressWarnings("unchecked")
protected JSONArray extractRoles() {
JSONArray rolesArray = new JSONArray();
- for (MRole role : roles) {
- rolesArray.add(extractRole(role));
+ if (roles != null) {
+ for (MRole role : roles) {
+ rolesArray.add(extractRole(role));
+ }
}
return rolesArray;
}
http://git-wip-us.apache.org/repos/asf/sqoop/blob/a021b7cd/shell/src/main/java/org/apache/sqoop/shell/ShowCommand.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowCommand.java b/shell/src/main/java/org/apache/sqoop/shell/ShowCommand.java
index 329b4d6..82c52da 100644
--- a/shell/src/main/java/org/apache/sqoop/shell/ShowCommand.java
+++ b/shell/src/main/java/org/apache/sqoop/shell/ShowCommand.java
@@ -36,6 +36,9 @@ public class ShowCommand extends SqoopCommand {
.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()
);
}
http://git-wip-us.apache.org/repos/asf/sqoop/blob/a021b7cd/shell/src/main/java/org/apache/sqoop/shell/ShowPrincipalFunction.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowPrincipalFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowPrincipalFunction.java
new file mode 100644
index 0000000..94bca85
--- /dev/null
+++ b/shell/src/main/java/org/apache/sqoop/shell/ShowPrincipalFunction.java
@@ -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);
+ }
+}
http://git-wip-us.apache.org/repos/asf/sqoop/blob/a021b7cd/shell/src/main/java/org/apache/sqoop/shell/ShowPrivilegeFunction.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowPrivilegeFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowPrivilegeFunction.java
new file mode 100644
index 0000000..6aaa1aa
--- /dev/null
+++ b/shell/src/main/java/org/apache/sqoop/shell/ShowPrivilegeFunction.java
@@ -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);
+ }
+}
http://git-wip-us.apache.org/repos/asf/sqoop/blob/a021b7cd/shell/src/main/java/org/apache/sqoop/shell/ShowRoleFunction.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShowRoleFunction.java b/shell/src/main/java/org/apache/sqoop/shell/ShowRoleFunction.java
new file mode 100644
index 0000000..e4c9e81
--- /dev/null
+++ b/shell/src/main/java/org/apache/sqoop/shell/ShowRoleFunction.java
@@ -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);
+ }
+}
http://git-wip-us.apache.org/repos/asf/sqoop/blob/a021b7cd/shell/src/main/java/org/apache/sqoop/shell/core/Constants.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/sqoop/shell/core/Constants.java b/shell/src/main/java/org/apache/sqoop/shell/core/Constants.java
index fc8ef42..d15d812 100644
--- a/shell/src/main/java/org/apache/sqoop/shell/core/Constants.java
+++ b/shell/src/main/java/org/apache/sqoop/shell/core/Constants.java
@@ -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";
http://git-wip-us.apache.org/repos/asf/sqoop/blob/a021b7cd/shell/src/main/resources/shell-resource.properties
----------------------------------------------------------------------
diff --git a/shell/src/main/resources/shell-resource.properties b/shell/src/main/resources/shell-resource.properties
index 750fd59..271ee90 100644
--- a/shell/src/main/resources/shell-resource.properties
+++ b/shell/src/main/resources/shell-resource.properties
@@ -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