yuqi1129 commented on code in PR #4515:
URL: https://github.com/apache/gravitino/pull/4515#discussion_r1721492852


##########
authorizations/authorization-ranger/src/main/java/org/apache/gravitino/authorization/ranger/RangerClientExt.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.authorization.ranger;
+
+import com.google.common.collect.ImmutableMap;
+import com.sun.jersey.api.client.GenericType;
+import com.sun.jersey.api.client.UniformInterfaceException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Map;
+import javax.ws.rs.HttpMethod;
+import javax.ws.rs.core.Response;
+import org.apache.gravitino.authorization.ranger.defines.VXGroup;
+import org.apache.gravitino.authorization.ranger.defines.VXGroupList;
+import org.apache.gravitino.authorization.ranger.defines.VXUser;
+import org.apache.gravitino.authorization.ranger.defines.VXUserList;
+import org.apache.ranger.RangerClient;
+import org.apache.ranger.RangerServiceException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Apache Ranger client extension <br>
+ * The class extends the RangerClient class and provides additional methods to 
create, search and
+ * delete users and groups
+ */
+public class RangerClientExt extends RangerClient {
+  private static final Logger LOG = 
LoggerFactory.getLogger(RangerClientExt.class);
+  private static final String URI_USER_BASE = "/service/xusers/users";
+  private static final String URI_USER_BY_ID = URI_USER_BASE + "/%d";
+  private static final String URI_GROUP_BASE = "/service/xusers/groups";
+  private static final String URI_GROUP_BY_ID = URI_GROUP_BASE + "/%d";
+  private static final String URI_CREATE_EXTERNAL_USER = URI_USER_BASE + 
"/external";
+
+  // Ranger user APIs
+  private static final API SEARCH_USER = new API(URI_USER_BASE, 
HttpMethod.GET, Response.Status.OK);
+  private static final API CREATE_EXTERNAL_USER =
+      new API(URI_CREATE_EXTERNAL_USER, HttpMethod.POST, Response.Status.OK);
+  private static final API DELETE_USER =
+      new API(URI_USER_BY_ID, HttpMethod.DELETE, Response.Status.NO_CONTENT);
+
+  // Ranger group APIs
+  private static final API CREATE_GROUP =
+      new API(URI_GROUP_BASE, HttpMethod.POST, Response.Status.OK);
+  private static final API SEARCH_GROUP =
+      new API(URI_GROUP_BASE, HttpMethod.GET, Response.Status.OK);
+  //  private static final API GET_GROUP = new API(URI_GROUP_BY_ID, 
HttpMethod.GET,
+  // Response.Status.OK);
+  private static final API DELETE_GROUP =
+      new API(URI_GROUP_BY_ID, HttpMethod.DELETE, Response.Status.NO_CONTENT);
+
+  // apache/ranger/intg/src/main/java/org/apache/ranger/RangerClient.java
+  // The private method callAPI of Ranger is called by reflection
+  // private <T> T callAPI(API api, Map<String, String> params, Object 
request, GenericType<T>
+  // responseType) throws RangerServiceException
+  private Method callAPIMethodGenericResponseType;
+
+  // private <T> T callAPI(API api, Map<String, String> params, Object 
request, Class<T>
+  // responseType) throws RangerServiceException
+  private Method callAPIMethodClassResponseType;
+  // private void callAPI(API api, Map<String, String> params) throws 
RangerServiceException
+  private Method callAPIMethodNonResponse;
+
+  public RangerClientExt(String hostName, String authType, String username, 
String password) {
+    super(hostName, authType, username, password, null);
+
+    // initialize callAPI method
+    try {
+      callAPIMethodGenericResponseType =
+          RangerClient.class.getDeclaredMethod(
+              "callAPI", API.class, Map.class, Object.class, 
GenericType.class);
+      callAPIMethodGenericResponseType.setAccessible(true);
+
+      callAPIMethodNonResponse =
+          RangerClient.class.getDeclaredMethod("callAPI", API.class, 
Map.class);
+      callAPIMethodNonResponse.setAccessible(true);
+
+      callAPIMethodClassResponseType =
+          RangerClient.class.getDeclaredMethod(
+              "callAPI", API.class, Map.class, Object.class, Class.class);
+      callAPIMethodClassResponseType.setAccessible(true);
+    } catch (NoSuchMethodException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  public Boolean createUser(VXUser user) throws RuntimeException {
+    try {
+      callAPIMethodClassResponseType.invoke(this, CREATE_EXTERNAL_USER, null, 
user, VXUser.class);
+    } catch (UniformInterfaceException e) {
+      LOG.error("Failed to create user: " + 
e.getResponse().getEntity(String.class));
+      return Boolean.FALSE;
+    } catch (InvocationTargetException | IllegalAccessException e) {
+      Throwable cause = e.getCause();
+      if (cause instanceof 
com.sun.jersey.api.client.UniformInterfaceException) {
+        com.sun.jersey.api.client.UniformInterfaceException uniformException =
+            (com.sun.jersey.api.client.UniformInterfaceException) cause;
+        int statusCode = uniformException.getResponse().getStatus();
+        if (statusCode == 204) {
+          // Because Ranger use asynchronous create user, so the response 
status is 204, research
+          // the user to check if it is created
+          VXUserList userList = searchUser(ImmutableMap.of("name", 
user.getName()));

Review Comment:
   Can we be assured that the user has already been created by calling 
"searchUser"?



##########
authorizations/authorization-ranger/src/main/java/org/apache/gravitino/authorization/ranger/RangerClientExt.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.authorization.ranger;
+
+import com.google.common.collect.ImmutableMap;
+import com.sun.jersey.api.client.GenericType;
+import com.sun.jersey.api.client.UniformInterfaceException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Map;
+import javax.ws.rs.HttpMethod;
+import javax.ws.rs.core.Response;
+import org.apache.gravitino.authorization.ranger.defines.VXGroup;
+import org.apache.gravitino.authorization.ranger.defines.VXGroupList;
+import org.apache.gravitino.authorization.ranger.defines.VXUser;
+import org.apache.gravitino.authorization.ranger.defines.VXUserList;
+import org.apache.ranger.RangerClient;
+import org.apache.ranger.RangerServiceException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Apache Ranger client extension <br>
+ * The class extends the RangerClient class and provides additional methods to 
create, search and
+ * delete users and groups
+ */
+public class RangerClientExt extends RangerClient {

Review Comment:
   It's better to use full words.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to