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

Support returning teams for GET /roles #2958

Merged
merged 1 commit into from
Feb 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public RoleRepository(CollectionDAO dao) {
@Override
public Role setFields(Role role, Fields fields) throws IOException {
role.setPolicy(fields.contains("policy") ? getPolicyForRole(role) : null);
role.setTeams(fields.contains("teams") ? getTeamsForRole(role) : null);
role.setUsers(fields.contains("users") ? getUsersForRole(role) : null);
return role;
}
Expand All @@ -92,6 +93,11 @@ private List<EntityReference> getUsersForRole(@NonNull Role role) throws IOExcep
return EntityUtil.populateEntityReferences(ids, Entity.USER);
}

private List<EntityReference> getTeamsForRole(@NonNull Role role) throws IOException {
List<String> ids = findFrom(role.getId(), Entity.ROLE, Relationship.HAS, Entity.TEAM, toBoolean(Include.ALL));
return EntityUtil.populateEntityReferences(ids, Entity.TEAM);
}

@Override
public void restorePatchAttributes(Role original, Role updated) {
// Patch can't make changes to following fields. Ignore the changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class RoleResource {

public static Role addHref(UriInfo uriInfo, Role role) {
Entity.withHref(uriInfo, role.getPolicy());
Entity.withHref(uriInfo, role.getTeams());
Entity.withHref(uriInfo, role.getUsers());
return role;
}
Expand All @@ -104,7 +105,7 @@ public RoleList(List<Role> roles, String beforeCursor, String afterCursor, int t
}
}

public static final String FIELDS = "policy,users";
public static final String FIELDS = "policy,teams,users";
public static final List<String> FIELD_LIST = Arrays.asList(FIELDS.replace(" ", "").split(","));

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
"description": "If `true`, this role is set as default and will be assigned to all users.",
"type": "boolean",
"default": false
},
"teams": {
"description": "Teams that have this role assigned.",
"$ref": "../../type/entityReference.json#/definitions/entityReferenceList"
}
},
"required": ["id", "name"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public Role createRolesAndSetDefault(TestInfo test, @Positive int numberOfRoles,
throws IOException {
// Create a set of roles.
for (int i = 0; i < numberOfRoles; i++) {
CreateRole create = createRequest(test, offset + i + 1);
vivekratnavel marked this conversation as resolved.
Show resolved Hide resolved
CreateRole create = createRequest(test, offset + i);
createAndCheckRole(create, ADMIN_AUTH_HEADERS);
}

Expand Down Expand Up @@ -190,6 +190,14 @@ protected void prepareGetWithDifferentFields(Role role) throws HttpResponseExcep
userResourceTest.createEntity(
userResourceTest.createRequest(role.getName() + "user2", "", "", null).withRoles(List.of(role.getId())),
ADMIN_AUTH_HEADERS);
// Assign two arbitrary teams this role for testing.
TeamResourceTest teamResourceTest = new TeamResourceTest();
teamResourceTest.createEntity(
teamResourceTest.createRequest(role.getName() + "team1", "", "", null).withDefaultRoles(List.of(role.getId())),
ADMIN_AUTH_HEADERS);
teamResourceTest.createEntity(
teamResourceTest.createRequest(role.getName() + "team2", "", "", null).withDefaultRoles(List.of(role.getId())),
ADMIN_AUTH_HEADERS);
}

/** Validate returned fields GET .../roles/{id}?fields="..." or GET .../roles/name/{name}?fields="..." */
Expand All @@ -205,13 +213,14 @@ public void validateGetWithDifferentFields(Role expectedRole, boolean byName) th
validateRole(role, expectedRole.getDescription(), expectedRole.getDisplayName(), updatedBy);

// .../roles?fields=policy,users
String fields = "policy,users";
String fields = "policy,teams,users";
mithmatt marked this conversation as resolved.
Show resolved Hide resolved
role =
byName
? getEntityByName(expectedRole.getName(), null, fields, ADMIN_AUTH_HEADERS)
: getEntity(expectedRole.getId(), fields, ADMIN_AUTH_HEADERS);
validateRole(role, expectedRole.getDescription(), expectedRole.getDisplayName(), updatedBy);
TestUtils.validateEntityReference(role.getPolicy());
TestUtils.validateEntityReference(role.getTeams());
TestUtils.validateEntityReference(role.getUsers());
}

Expand Down
2 changes: 1 addition & 1 deletion ingestion-core/src/metadata/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

from incremental import Version

__version__ = Version("metadata", 0, 9, 0, dev=12)
__version__ = Version("metadata", 0, 9, 0, dev=13)
__all__ = ["__version__"]