Skip to content

Commit

Permalink
[apache#4873] feat(core): support list group (apache#4879)
Browse files Browse the repository at this point in the history
support list group

support list group

Fix: apache#4873

no

UT

---------

Co-authored-by: Rory <[email protected]>
  • Loading branch information
LiuQhahah and jerqi committed Sep 27, 2024
1 parent b0ccd40 commit ec0a340
Show file tree
Hide file tree
Showing 22 changed files with 811 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,26 @@ public Group getGroup(String group) throws NoSuchGroupException, NoSuchMetalakeE
return getMetalake().getGroup(group);
}

/**
* List the groups.
*
* @return The Group list
* @throws NoSuchMetalakeException If the Metalake with the given name does not exist.
*/
public Group[] listGroups() throws NoSuchMetalakeException {
return getMetalake().listGroups();
}

/**
* List the group names.
*
* @return The group names list.
* @throws NoSuchMetalakeException If the Metalake with the given name does not exist.
*/
public String[] listGroupNames() throws NoSuchMetalakeException {
return getMetalake().listGroupNames();
}

/**
* Gets a Role.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.apache.gravitino.dto.responses.DropResponse;
import org.apache.gravitino.dto.responses.EntityListResponse;
import org.apache.gravitino.dto.responses.ErrorResponse;
import org.apache.gravitino.dto.responses.GroupListResponse;
import org.apache.gravitino.dto.responses.GroupResponse;
import org.apache.gravitino.dto.responses.NameListResponse;
import org.apache.gravitino.dto.responses.OwnerResponse;
Expand Down Expand Up @@ -635,6 +636,44 @@ public Group getGroup(String group) throws NoSuchGroupException, NoSuchMetalakeE
return resp.getGroup();
}

/**
* Lists the groups
*
* @return The Group list
* @throws NoSuchMetalakeException If the Metalake with the given name does not exist.
*/
public Group[] listGroups() throws NoSuchMetalakeException {
Map<String, String> params = new HashMap<>();
params.put("details", "true");

GroupListResponse resp =
restClient.get(
String.format(API_METALAKES_GROUPS_PATH, name(), BLANK_PLACEHOLDER),
params,
GroupListResponse.class,
Collections.emptyMap(),
ErrorHandlers.groupErrorHandler());
resp.validate();
return resp.getGroups();
}

/**
* Lists the group names
*
* @return The Group Name List
* @throws NoSuchMetalakeException If the Metalake with the given name does not exist.
*/
public String[] listGroupNames() throws NoSuchMetalakeException {
NameListResponse resp =
restClient.get(
String.format(API_METALAKES_GROUPS_PATH, name(), BLANK_PLACEHOLDER),
NameListResponse.class,
Collections.emptyMap(),
ErrorHandlers.groupErrorHandler());
resp.validate();
return resp.getNames();
}

/**
* Gets a Role.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static org.apache.hc.core5.http.HttpStatus.SC_SERVER_ERROR;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.time.Instant;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.gravitino.authorization.Group;
import org.apache.gravitino.authorization.User;
Expand All @@ -35,6 +37,7 @@
import org.apache.gravitino.dto.requests.GroupAddRequest;
import org.apache.gravitino.dto.requests.UserAddRequest;
import org.apache.gravitino.dto.responses.ErrorResponse;
import org.apache.gravitino.dto.responses.GroupListResponse;
import org.apache.gravitino.dto.responses.GroupResponse;
import org.apache.gravitino.dto.responses.MetalakeResponse;
import org.apache.gravitino.dto.responses.NameListResponse;
Expand Down Expand Up @@ -327,6 +330,55 @@ public void testRemoveGroups() throws Exception {
Assertions.assertThrows(RuntimeException.class, () -> gravitinoClient.removeGroup(groupName));
}

@Test
public void testListGroupNames() throws JsonProcessingException {
String groupPath = withSlash(String.format(API_METALAKES_GROUPS_PATH, metalakeName, ""));
NameListResponse listResponse = new NameListResponse(new String[] {"group1", "group2"});
buildMockResource(Method.GET, groupPath, null, listResponse, SC_OK);
Assertions.assertArrayEquals(
new String[] {"group1", "group2"}, gravitinoClient.listGroupNames());
ErrorResponse errRespNoMetaLake =
ErrorResponse.notFound(NoSuchMetalakeException.class.getSimpleName(), "metalake not found");
buildMockResource(Method.GET, groupPath, null, errRespNoMetaLake, SC_NOT_FOUND);
Exception ex =
Assertions.assertThrows(
NoSuchMetalakeException.class, () -> gravitinoClient.listGroupNames());
Assertions.assertEquals("metalake not found", ex.getMessage());

// Test RuntimeException
ErrorResponse errResp = ErrorResponse.internalError("internal error");
buildMockResource(Method.GET, groupPath, null, errResp, SC_SERVER_ERROR);

Assertions.assertThrows(RuntimeException.class, () -> gravitinoClient.listGroupNames());
}

@Test
public void testListGroups() throws JsonProcessingException {
String groupPath = withSlash(String.format(API_METALAKES_GROUPS_PATH, metalakeName, ""));
GroupDTO group1 = mockGroupDTO("group1");
GroupDTO group2 = mockGroupDTO("group2");
GroupDTO group3 = mockGroupDTO("group3");
Map<String, String> params = new HashMap<>();
GroupListResponse listResponse = new GroupListResponse(new GroupDTO[] {group1, group2, group3});
buildMockResource(Method.GET, groupPath, params, null, listResponse, SC_OK);

Group[] groups = gravitinoClient.listGroups();
Assertions.assertEquals(3, groups.length);
assertGroup(group1, groups[0]);
assertGroup(group2, groups[1]);
assertGroup(group3, groups[2]);
ErrorResponse errResNoMetaLake =
ErrorResponse.notFound(NoSuchMetalakeException.class.getSimpleName(), "metalake not found");
buildMockResource(Method.GET, groupPath, params, null, errResNoMetaLake, SC_NOT_FOUND);
Exception ex =
Assertions.assertThrows(NoSuchMetalakeException.class, () -> gravitinoClient.listGroups());
Assertions.assertEquals("metalake not found", ex.getMessage());
// Test RuntimeException
ErrorResponse errResp = ErrorResponse.internalError("internal error");
buildMockResource(Method.GET, groupPath, params, null, errResp, SC_SERVER_ERROR);
Assertions.assertThrows(RuntimeException.class, () -> gravitinoClient.listGroups());
}

private UserDTO mockUserDTO(String name) {
return UserDTO.builder()
.withName(name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.dto.responses;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import java.util.Arrays;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.apache.gravitino.dto.authorization.GroupDTO;

/** Represents a response for a list of groups. */
@Getter
@ToString
@EqualsAndHashCode(callSuper = true)
public class GroupListResponse extends BaseResponse {

@JsonProperty("groups")
private final GroupDTO[] groups;

/**
* Constructor for GroupListResponse.
*
* @param groups The array of group DTOs.
*/
public GroupListResponse(GroupDTO[] groups) {
super(0);
this.groups = groups;
}

/** Default constructor for GroupListResponse. (Used for Jackson deserialization.) */
public GroupListResponse() {
super();
this.groups = null;
}

/**
* Validates the response data.
*
* @throws IllegalArgumentException if the name or audit is not set.
*/
@Override
public void validate() throws IllegalArgumentException {
super.validate();

Preconditions.checkArgument(groups != null, "groups must not be null");
Arrays.stream(groups)
.forEach(
group -> {
Preconditions.checkArgument(
StringUtils.isNotBlank(group.name()), "group 'name' must not be blank");
Preconditions.checkArgument(
group.auditInfo() != null, "group 'auditInfo' must not be null");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,19 @@ public static UserDTO[] toDTOs(User[] users) {
return Arrays.stream(users).map(DTOConverters::toDTO).toArray(UserDTO[]::new);
}

/**
* Converts an array of Groups to an array of GroupDTOs.
*
* @param groups The groups to be converted.
* @return The array of GroupDTOs.
*/
public static GroupDTO[] toDTOs(Group[] groups) {
if (ArrayUtils.isEmpty(groups)) {
return new GroupDTO[0];
}
return Arrays.stream(groups).map(DTOConverters::toDTO).toArray(GroupDTO[]::new);
}

/**
* Converts a DistributionDTO to a Distribution.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ Group addGroup(String metalake, String group)
Group getGroup(String metalake, String group)
throws NoSuchGroupException, NoSuchMetalakeException;

/**
* List groups
*
* @param metalake The Metalake of the Group.
* @return The list of groups
* @throws NoSuchMetalakeException If the Metalake with the given name does not exist.
*/
Group[] listGroups(String metalake);

/**
* List group names
*
* @param metalake The Metalake of the Group.
* @return The list of group names
* @throws NoSuchMetalakeException If the Metalake with the given name does not exist.
*/
String[] listGroupNames(String metalake);

/**
* Grant roles to a user.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ public Group getGroup(String metalake, String group)
return userGroupManager.getGroup(metalake, group);
}

@Override
public Group[] listGroups(String metalake) throws NoSuchMetalakeException {
return userGroupManager.listGroups(metalake);
}

@Override
public String[] listGroupNames(String metalake) throws NoSuchMetalakeException {
return userGroupManager.listGroupNames(metalake);
}

@Override
public User grantRolesToUser(String metalake, List<String> roles, String user)
throws NoSuchUserException, NoSuchRoleException, NoSuchMetalakeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Arrays;
import java.util.Collections;
import org.apache.gravitino.Entity;
import org.apache.gravitino.Entity.EntityType;
import org.apache.gravitino.EntityAlreadyExistsException;
import org.apache.gravitino.EntityStore;
import org.apache.gravitino.Namespace;
Expand Down Expand Up @@ -124,23 +125,6 @@ User[] listUsers(String metalake) {
return listUsersInternal(metalake, true /* allFields */);
}

private User[] listUsersInternal(String metalake, boolean allFields) {
try {
AuthorizationUtils.checkMetalakeExists(metalake);

Namespace namespace = AuthorizationUtils.ofUserNamespace(metalake);
return store
.list(namespace, UserEntity.class, Entity.EntityType.USER, allFields)
.toArray(new User[0]);
} catch (NoSuchEntityException e) {
LOG.error("Metalake {} does not exist", metalake, e);
throw new NoSuchMetalakeException(METALAKE_DOES_NOT_EXIST_MSG, metalake);
} catch (IOException ioe) {
LOG.error("Listing user under metalake {} failed due to storage issues", metalake, ioe);
throw new RuntimeException(ioe);
}
}

Group addGroup(String metalake, String group) throws GroupAlreadyExistsException {
try {
AuthorizationUtils.checkMetalakeExists(metalake);
Expand Down Expand Up @@ -197,4 +181,47 @@ Group getGroup(String metalake, String group) {
throw new RuntimeException(ioe);
}
}

Group[] listGroups(String metalake) {
return listGroupInternal(metalake, true);
}

String[] listGroupNames(String metalake) {
return Arrays.stream(listGroupInternal(metalake, false))
.map(Group::name)
.toArray(String[]::new);
}

private User[] listUsersInternal(String metalake, boolean allFields) {
try {
AuthorizationUtils.checkMetalakeExists(metalake);

Namespace namespace = AuthorizationUtils.ofUserNamespace(metalake);
return store
.list(namespace, UserEntity.class, Entity.EntityType.USER, allFields)
.toArray(new User[0]);
} catch (NoSuchEntityException e) {
LOG.error("Metalake {} does not exist", metalake, e);
throw new NoSuchMetalakeException(METALAKE_DOES_NOT_EXIST_MSG, metalake);
} catch (IOException ioe) {
LOG.error("Listing user under metalake {} failed due to storage issues", metalake, ioe);
throw new RuntimeException(ioe);
}
}

private Group[] listGroupInternal(String metalake, boolean allFields) {
try {
AuthorizationUtils.checkMetalakeExists(metalake);
Namespace namespace = AuthorizationUtils.ofGroupNamespace(metalake);
return store
.list(namespace, GroupEntity.class, EntityType.GROUP, allFields)
.toArray(new Group[0]);
} catch (NoSuchEntityException e) {
LOG.error("Metalake {} does not exist", metalake, e);
throw new NoSuchMetalakeException(METALAKE_DOES_NOT_EXIST_MSG, metalake);
} catch (IOException ioe) {
LOG.error("Listing group under metalake {} failed due to storage issues", metalake, ioe);
throw new RuntimeException(ioe);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ public Group getGroup(String metalake, String group)
return dispatcher.getGroup(metalake, group);
}

@Override
public Group[] listGroups(String metalake) throws NoSuchMetalakeException {
return dispatcher.listGroups(metalake);
}

@Override
public String[] listGroupNames(String metalake) throws NoSuchMetalakeException {
return dispatcher.listGroupNames(metalake);
}

@Override
public User grantRolesToUser(String metalake, List<String> roles, String user)
throws NoSuchUserException, NoSuchRoleException, NoSuchMetalakeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public <E extends Entity & HasIdentifier> List<E> list(
return (List<E>) UserMetaService.getInstance().listUsersByNamespace(namespace, allFields);
case ROLE:
return (List<E>) RoleMetaService.getInstance().listRolesByNamespace(namespace);
case GROUP:
return (List<E>) GroupMetaService.getInstance().listGroupsByNamespace(namespace, allFields);
default:
throw new UnsupportedEntityTypeException(
"Unsupported entity type: %s for list operation", entityType);
Expand Down
Loading

0 comments on commit ec0a340

Please sign in to comment.