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

[#4873] feat(core): support list group #4879

Merged
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 @@ -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 @@ -60,6 +60,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 @@ -623,6 +624,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
Expand Up @@ -133,8 +133,38 @@ void testManageGroups() {
// Get a not-existed group
Assertions.assertThrows(NoSuchGroupException.class, () -> metalake.getGroup("not-existed"));

Map<String, String> properties = Maps.newHashMap();
properties.put("k1", "v1");
SecurableObject metalakeObject =
SecurableObjects.ofMetalake(
metalakeName, Lists.newArrayList(Privileges.CreateCatalog.allow()));

// Test the group with the role
metalake.createRole("role2", properties, Lists.newArrayList(metalakeObject));
metalake.grantRolesToGroup(Lists.newArrayList("role2"), groupName);

// List groups
String anotherGroup = "group2#456";
metalake.addGroup(anotherGroup);
String[] groupNames = metalake.listGroupNames();
Arrays.sort(groupNames);
Assertions.assertEquals(Lists.newArrayList(groupName, anotherGroup), Arrays.asList(groupNames));

List<Group> groups =
Arrays.stream(metalake.listGroups())
.sorted(Comparator.comparing(Group::name))
.collect(Collectors.toList());
Assertions.assertEquals(
Lists.newArrayList(groupName, anotherGroup),
groups.stream().map(Group::name).collect(Collectors.toList()));
Assertions.assertEquals(Lists.newArrayList("role2"), groups.get(0).roles());

Assertions.assertTrue(metalake.removeGroup(groupName));
Assertions.assertFalse(metalake.removeGroup(groupName));

// clean up
metalake.removeGroup(anotherGroup);
metalake.deleteRole("role2");
}

@Test
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 @@ -127,6 +127,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 @@ -94,6 +94,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
Loading
Loading