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

Update Create Team to use entityName for create requests #19087

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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 @@ -49,11 +49,17 @@
import static org.openmetadata.service.workflows.searchIndex.ReindexingUtil.ENTITY_TYPE_KEY;
import static org.openmetadata.service.workflows.searchIndex.ReindexingUtil.isDataInsightIndex;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -168,6 +174,7 @@ public class SearchIndexApp extends AbstractNativeApplication {
private final AtomicReference<Integer> batchSize = new AtomicReference<>(5);
private JobExecutionContext jobExecutionContext;
private volatile boolean stopped = false;
Queue<Map<String, Object>> entityRecords = new ConcurrentLinkedQueue<>();

public SearchIndexApp(CollectionDAO collectionDAO, SearchRepository searchRepository) {
super(collectionDAO, searchRepository);
Expand Down Expand Up @@ -309,6 +316,20 @@ private void performReindex(JobExecutionContext jobExecutionContext) throws Inte
} finally {
shutdownExecutor(jobExecutor, "JobExecutor", 20, TimeUnit.SECONDS);
shutdownExecutor(producerExecutor, "ReaderExecutor", 1, TimeUnit.MINUTES);

for (Map<String, Object> rec : entityRecords) {
try (BufferedWriter writer =
new BufferedWriter(
new FileWriter(
String.format(
"/Users/mohityadav/IdeaProjects/OMTesting/sample_data/%s.json",
rec.get(ENTITY_TYPE_KEY)),
true))) {
writer.write(JsonUtils.pojoToJson(rec, true));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Expand Down Expand Up @@ -657,6 +678,20 @@ private void processReadTask(
LOG.debug("Read Entities with entityType: {}, CurrentOffset: {}", entityType, offset);
if (resultList != null) {
ResultList<?> entities = extractEntities(entityType, resultList);
Map<String, Object> contextData = new HashMap<>();
List<Map<String, Object>> resultData = new ArrayList<>();
for (Object ob : entities.getData()) {
Map<String, Object> result = JsonUtils.getMap(ob);
Map<String, Object> ansert = new HashMap<>();
ansert.put("teamType", result.get("teamType"));
ansert.put("name", result.get("name"));
ansert.put("parents", result.get("parents"));
ansert.put("isJoinable", result.get("isJoinable"));
resultData.add(ansert);
}
contextData.put(ENTITY_TYPE_KEY, entityType);
contextData.put("records", resultData);
entityRecords.add(contextData);
if (!nullOrEmpty(entities.getData())) {
IndexingTask<?> task = new IndexingTask<>(entityType, entities, offset);
processTask(task, jobExecutionContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,17 @@ public final PutResponse<T> createOrUpdate(UriInfo uriInfo, T updated) {
return update(uriInfo, original, updated);
}

@Transaction
public final PutResponse<T> createOrUpdate(UriInfo uriInfo, String updatedJson) {
T updated = JsonUtils.readValue(updatedJson, entityClass);
T original = findByNameOrNull(updated.getFullyQualifiedName(), ALL);
if (original == null) { // If an original entity does not exist then create it, else update
return new PutResponse<>(
Status.CREATED, withHref(uriInfo, createNewEntity(updated)), ENTITY_CREATED);
}
return update(uriInfo, original, updated);
}

@SuppressWarnings("unused")
protected void postCreate(T entity) {
if (supportsSearch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,12 +764,13 @@ private Team getTeam(CreateTeam ct, String user) {
.copy(new Team(), ct, user)
.withProfile(ct.getProfile())
.withIsJoinable(ct.getIsJoinable())
.withUsers(EntityUtil.toEntityReferences(ct.getUsers(), Entity.USER))
.withDefaultRoles(EntityUtil.toEntityReferences(ct.getDefaultRoles(), Entity.ROLE))
.withUsers(EntityUtil.populateEntityReferenceFromFqn(ct.getUsers(), Entity.USER))
.withDefaultRoles(
EntityUtil.populateEntityReferenceFromFqn(ct.getDefaultRoles(), Entity.ROLE))
.withTeamType(ct.getTeamType())
.withParents(EntityUtil.toEntityReferences(ct.getParents(), Entity.TEAM))
.withChildren(EntityUtil.toEntityReferences(ct.getChildren(), Entity.TEAM))
.withPolicies(EntityUtil.toEntityReferences(ct.getPolicies(), Entity.POLICY))
.withParents(EntityUtil.populateEntityReferenceFromFqn(ct.getParents(), Entity.TEAM))
.withChildren(EntityUtil.populateEntityReferenceFromFqn(ct.getChildren(), Entity.TEAM))
.withPolicies(EntityUtil.populateEntityReferenceFromFqn(ct.getPolicies(), Entity.POLICY))
.withEmail(ct.getEmail())
.withDomains(EntityUtil.getEntityReferences(Entity.DOMAIN, ct.getDomains()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,31 @@ public static List<EntityReference> toEntityReferences(List<UUID> ids, String en
.collect(Collectors.toList());
}

public static List<EntityReference> populateEntityReferenceFromFqn(
List<String> fqns, String entityType) {
if (fqns == null) {
return null;
}
return populateEntityReferences(
fqns.stream()
.map(fqn -> new EntityReference().withFullyQualifiedName(fqn).withType(entityType))
.collect(Collectors.toList()));
}

public static List<UUID> refToIds(List<EntityReference> refs) {
if (refs == null) {
return null;
}
return refs.stream().map(EntityReference::getId).collect(Collectors.toList());
}

public static List<String> refToFqns(List<EntityReference> refs) {
if (refs == null) {
return null;
}
return refs.stream().map(EntityReference::getFullyQualifiedName).collect(Collectors.toList());
}

public static <T> boolean isDescriptionRequired(Class<T> clz) {
// Returns true if description field in entity is required
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void setup(TestInfo test) throws IOException, URISyntaxException {
.createRequest(test, 4)
.withDisplayName("Team2")
.withDescription("Team2 description")
.withUsers(List.of(USER2.getId()));
.withUsers(List.of(USER2.getFullyQualifiedName()));
TEAM2 = teamResourceTest.createAndCheckEntity(createTeam, ADMIN_AUTH_HEADERS);
EntityReference TEAM2_REF = TEAM2.getEntityReference();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void setup(TestInfo test) throws IOException, URISyntaxException {
.createRequest(test, 4)
.withDisplayName("Team2")
.withDescription("Team2 description")
.withUsers(List.of(USER2.getId()));
.withUsers(List.of(USER2.getFullyQualifiedName()));
TEAM2 = teamResourceTest.createAndCheckEntity(createTeam, ADMIN_AUTH_HEADERS);
EntityReference TEAM2_REF = TEAM2.getEntityReference();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ void get_policyTeamsAndRoles(TestInfo test) throws IOException {
for (int i = 0; i < 3; i++) {
// Team X has Policy X
CreateTeam createTeam =
TEAM_TEST.createRequest(test, i).withPolicies(List.of(policies.get(i).getId()));
TEAM_TEST
.createRequest(test, i)
.withPolicies(List.of(policies.get(i).getFullyQualifiedName()));
teams.add(TEAM_TEST.createEntity(createTeam, ADMIN_AUTH_HEADERS));
}

Expand Down Expand Up @@ -542,15 +544,27 @@ void test_roles_policies_scenarios() throws HttpResponseException {
TEAM_TEST
.createRequest("rolesPoliciesTeam2")
.withTeamType(DEPARTMENT)
.withDefaultRoles(listOf(DATA_STEWARD_ROLE.getId()));
.withDefaultRoles(listOf(DATA_STEWARD_ROLE.getFullyQualifiedName()));
Team team2 = TEAM_TEST.createEntity(createTeam, ADMIN_AUTH_HEADERS);
createTeam = TEAM_TEST.createRequest("rolesPoliciesTeam11").withParents(listOf(team1.getId()));
createTeam =
TEAM_TEST
.createRequest("rolesPoliciesTeam11")
.withParents(listOf(team1.getFullyQualifiedName()));
Team team11 = TEAM_TEST.createEntity(createTeam, ADMIN_AUTH_HEADERS);
createTeam = TEAM_TEST.createRequest("rolesPoliciesTeam12").withParents(listOf(team1.getId()));
createTeam =
TEAM_TEST
.createRequest("rolesPoliciesTeam12")
.withParents(listOf(team1.getFullyQualifiedName()));
Team team12 = TEAM_TEST.createEntity(createTeam, ADMIN_AUTH_HEADERS);
createTeam = TEAM_TEST.createRequest("rolesPoliciesTeam21").withParents(listOf(team2.getId()));
createTeam =
TEAM_TEST
.createRequest("rolesPoliciesTeam21")
.withParents(listOf(team2.getFullyQualifiedName()));
Team team21 = TEAM_TEST.createEntity(createTeam, ADMIN_AUTH_HEADERS);
createTeam = TEAM_TEST.createRequest("rolesPoliciesTeam22").withParents(listOf(team2.getId()));
createTeam =
TEAM_TEST
.createRequest("rolesPoliciesTeam22")
.withParents(listOf(team2.getFullyQualifiedName()));
Team team22 = TEAM_TEST.createEntity(createTeam, ADMIN_AUTH_HEADERS);

// Create users - Team2 has default role DataSteward
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ public Role validateGetWithDifferentFields(Role role, boolean byName)
teamResourceTest.createEntity(
teamResourceTest
.createRequest("roleTeam1", "", "", null)
.withDefaultRoles(List.of(role.getId())),
.withDefaultRoles(List.of(role.getFullyQualifiedName())),
ADMIN_AUTH_HEADERS);
teamResourceTest.createEntity(
teamResourceTest
.createRequest("roleTeam2", "", "", null)
.withDefaultRoles(List.of(role.getId())),
.withDefaultRoles(List.of(role.getFullyQualifiedName())),
ADMIN_AUTH_HEADERS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@ public void setupTeams(TestInfo test) throws HttpResponseException {
CreateTeam createTeam = createRequest(test, 1).withTeamType(DEPARTMENT);
TEAM1 = createEntity(createTeam, ADMIN_AUTH_HEADERS);

createTeam = createRequest(test, 11).withParents(List.of(TEAM1.getId()));
createTeam = createRequest(test, 11).withParents(List.of(TEAM1.getFullyQualifiedName()));
TEAM11 = createEntity(createTeam, ADMIN_AUTH_HEADERS);

// TEAM2 has Team only policy - users from other teams can't access its assets
createTeam =
createRequest(test, 2)
.withTeamType(DEPARTMENT)
.withPolicies(List.of(TEAM_ONLY_POLICY.getId()))
.withDefaultRoles(List.of(DATA_STEWARD_ROLE.getId()));
.withPolicies(List.of(TEAM_ONLY_POLICY.getFullyQualifiedName()))
.withDefaultRoles(List.of(DATA_STEWARD_ROLE.getFullyQualifiedName()));
TEAM2 = createEntity(createTeam, ADMIN_AUTH_HEADERS);

createTeam = createRequest(test, 21).withParents(List.of(TEAM2.getId()));
createTeam = createRequest(test, 21).withParents(List.of(TEAM2.getFullyQualifiedName()));
TEAM21 = createEntity(createTeam, ADMIN_AUTH_HEADERS);

TEAM11_REF = TEAM11.getEntityReference();
Expand Down Expand Up @@ -203,14 +203,16 @@ void post_teamWithUsersAndDefaultRoles_200_OK(TestInfo test) throws IOException
User user2 =
userResourceTest.createEntity(
userResourceTest.createRequest(test, 2), USER_WITH_CREATE_HEADERS);
List<UUID> users = Arrays.asList(user1.getId(), user2.getId());
List<String> users =
Arrays.asList(user1.getFullyQualifiedName(), user2.getFullyQualifiedName());

RoleResourceTest roleResourceTest = new RoleResourceTest();
Role role1 =
roleResourceTest.createEntity(roleResourceTest.createRequest(test, 1), ADMIN_AUTH_HEADERS);
Role role2 =
roleResourceTest.createEntity(roleResourceTest.createRequest(test, 2), ADMIN_AUTH_HEADERS);
List<UUID> roles = Arrays.asList(role1.getId(), role2.getId());
List<String> roles =
Arrays.asList(role1.getFullyQualifiedName(), role2.getFullyQualifiedName());

CreateTeam create =
createRequest(test)
Expand All @@ -236,12 +238,12 @@ void delete_validTeam_200_OK(TestInfo test) throws IOException {
UserResourceTest userResourceTest = new UserResourceTest();
User user1 =
userResourceTest.createEntity(userResourceTest.createRequest(test, 1), ADMIN_AUTH_HEADERS);
List<UUID> users = Collections.singletonList(user1.getId());
List<String> users = Collections.singletonList(user1.getFullyQualifiedName());

RoleResourceTest roleResourceTest = new RoleResourceTest();
Role role1 =
roleResourceTest.createEntity(roleResourceTest.createRequest(test, 1), ADMIN_AUTH_HEADERS);
List<UUID> roles = Collections.singletonList(role1.getId());
List<String> roles = Collections.singletonList(role1.getFullyQualifiedName());

CreateTeam create = createRequest(test).withUsers(users).withDefaultRoles(roles);
Team team = createAndCheckEntity(create, ADMIN_AUTH_HEADERS);
Expand Down Expand Up @@ -587,7 +589,9 @@ void put_patch_hierarchicalTeams() throws IOException {

// Change bu2 parent from Organization to bu1 using PUT operation
CreateTeam create =
createRequest("put2").withTeamType(BUSINESS_UNIT).withParents(List.of(bu1.getId()));
createRequest("put2")
.withTeamType(BUSINESS_UNIT)
.withParents(List.of(bu1.getFullyQualifiedName()));
ChangeDescription change = getChangeDescription(bu2, MINOR_UPDATE);
fieldDeleted(change, "parents", List.of(ORG_TEAM.getEntityReference()));
fieldAdded(change, "parents", List.of(bu1.getEntityReference()));
Expand Down Expand Up @@ -639,18 +643,19 @@ void patch_isJoinable_200(TestInfo test) throws IOException {
void patch_deleteUserAndDefaultRolesFromTeam_200(TestInfo test) throws IOException {
UserResourceTest userResourceTest = new UserResourceTest();
final int totalUsers = 20;
ArrayList<UUID> users = new ArrayList<>();
List<String> users = new ArrayList<>();
for (int i = 0; i < totalUsers; i++) {
User user =
userResourceTest.createEntity(
userResourceTest.createRequest(test, i), ADMIN_AUTH_HEADERS);
users.add(user.getId());
users.add(user.getFullyQualifiedName());
}

RoleResourceTest roleResourceTest = new RoleResourceTest();
roleResourceTest.createRoles(test, 5, 0);
List<Role> roles = roleResourceTest.listEntities(Map.of(), ADMIN_AUTH_HEADERS).getData();
List<UUID> rolesIds = roles.stream().map(Role::getId).collect(Collectors.toList());
List<String> rolesIds =
roles.stream().map(Role::getFullyQualifiedName).collect(Collectors.toList());

CreateTeam create =
createRequest(getEntityName(test), "description", "displayName", null)
Expand Down Expand Up @@ -682,7 +687,9 @@ void patch_deleteUserAndDefaultRolesFromTeam_200(TestInfo test) throws IOExcepti
@Test
void post_teamWithPolicies(TestInfo test) throws IOException {
CreateTeam create =
createRequest(getEntityName(test)).withPolicies(List.of(POLICY1.getId(), POLICY2.getId()));
createRequest(getEntityName(test))
.withPolicies(
List.of(POLICY1.getFullyQualifiedName(), POLICY2.getFullyQualifiedName()));
createAndCheckEntity(create, ADMIN_AUTH_HEADERS);
}

Expand All @@ -694,7 +701,7 @@ void put_teamWithPolicies(TestInfo test) throws IOException {
// Add policies to the team
create =
createRequest(getEntityName(test))
.withPolicies(List.of(POLICY1.getId(), POLICY2.getId()))
.withPolicies(List.of(POLICY1.getFullyQualifiedName(), POLICY2.getFullyQualifiedName()))
.withName(team.getName());
ChangeDescription change = getChangeDescription(team, MINOR_UPDATE);
fieldAdded(
Expand Down Expand Up @@ -917,7 +924,7 @@ void test_inheritDomain(TestInfo test) throws IOException {
Team team = createEntity(createTeam, ADMIN_AUTH_HEADERS);

// Create a children team without domain and ensure it inherits domain from the parent
createTeam = createRequest("team1").withParents(listOf(team.getId()));
createTeam = createRequest("team1").withParents(listOf(team.getFullyQualifiedName()));
assertDomainInheritance(createTeam, DOMAIN.getEntityReference());
}

Expand Down Expand Up @@ -1086,15 +1093,15 @@ public void validateCreatedEntity(
TestUtils.validateEntityReferences(team.getOwns());

List<EntityReference> expectedUsers = new ArrayList<>();
for (UUID userId : listOrEmpty(createRequest.getUsers())) {
expectedUsers.add(new EntityReference().withId(userId).withType(Entity.USER));
for (String fqn : listOrEmpty(createRequest.getUsers())) {
expectedUsers.add(new EntityReference().withFullyQualifiedName(fqn).withType(Entity.USER));
}
expectedUsers = expectedUsers.isEmpty() ? null : expectedUsers;
TestUtils.assertEntityReferences(expectedUsers, team.getUsers());
TestUtils.assertEntityReferenceIds(createRequest.getDefaultRoles(), team.getDefaultRoles());
TestUtils.assertEntityReferenceIds(createRequest.getParents(), team.getParents());
TestUtils.assertEntityReferenceIds(createRequest.getChildren(), team.getChildren());
TestUtils.assertEntityReferenceIds(createRequest.getPolicies(), team.getPolicies());
TestUtils.assertEntityReferencesFqn(expectedUsers, team.getUsers());
TestUtils.assertEntityReferenceFqn(createRequest.getDefaultRoles(), team.getDefaultRoles());
TestUtils.assertEntityReferenceFqn(createRequest.getParents(), team.getParents());
TestUtils.assertEntityReferenceFqn(createRequest.getChildren(), team.getChildren());
TestUtils.assertEntityReferenceFqn(createRequest.getPolicies(), team.getPolicies());
}

@Override
Expand Down Expand Up @@ -1152,7 +1159,7 @@ private Team createWithParents(
String teamName, TeamType teamType, Boolean isJoinable, EntityReference... parents)
throws HttpResponseException {
List<EntityReference> parentList = List.of(parents);
List<UUID> parentIds = EntityUtil.refToIds(parentList);
List<String> parentIds = EntityUtil.refToFqns(parentList);
Team team =
createEntity(
createRequest(teamName)
Expand All @@ -1167,7 +1174,7 @@ private Team createWithParents(
private Team createWithChildren(String teamName, TeamType teamType, EntityReference... children)
throws HttpResponseException {
List<EntityReference> childrenList = List.of(children);
List<UUID> childIds = EntityUtil.refToIds(childrenList);
List<String> childIds = EntityUtil.refToFqns(childrenList);
Team team =
createEntity(
createRequest(teamName).withChildren(childIds).withTeamType(teamType),
Expand Down
Loading
Loading