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 14 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
23 changes: 2 additions & 21 deletions ingestion/src/metadata/ingestion/source/database/sample_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
from metadata.generated.schema.entity.services.pipelineService import PipelineService
from metadata.generated.schema.entity.services.searchService import SearchService
from metadata.generated.schema.entity.services.storageService import StorageService
from metadata.generated.schema.entity.teams.team import Team
from metadata.generated.schema.entity.teams.user import User
from metadata.generated.schema.metadataIngestion.workflow import (
Source as WorkflowSource,
Expand Down Expand Up @@ -681,26 +680,8 @@ def ingest_teams(self) -> Iterable[Either[CreateTeamRequest]]:
"""
for team in self.teams["teams"]:
team_to_ingest = CreateTeamRequest(
name=team["name"], teamType=team["teamType"]
)
if team["parent"] is not None:
parent_list_id = []
for parent in team["parent"]:
tries = 3
parent_object = self.metadata.get_by_name(entity=Team, fqn=parent)
while not parent_object and tries > 0:
logger.info(f"Trying to GET {parent} Parent Team")
parent_object = self.metadata.get_by_name(
entity=Team,
fqn=parent,
)
tries -= 1

if parent_object:
parent_list_id.append(parent_object.id)

team_to_ingest.parents = parent_list_id

name=team["name"], teamType=team["teamType"], parents=team.get("parent")
)
yield Either(right=team_to_ingest)

def ingest_mysql(self) -> Iterable[Either[Entity]]:
Expand Down
10 changes: 8 additions & 2 deletions ingestion/tests/integration/ometa/test_ometa_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,18 @@ def setUpClass(cls) -> None:

cls.team_1 = cls.metadata.create_or_update(
data=get_create_team_entity(
name="Team 1", users=[cls.user_1.id, cls.user_2.id]
name="Team 1",
users=[
cls.user_1.fullyQualifiedName.root,
cls.user_2.fullyQualifiedName.root,
],
)
)

cls.team_2 = cls.metadata.create_or_update(
data=get_create_team_entity(name="Team 2", users=[cls.user_2.id])
data=get_create_team_entity(
name="Team 2", users=[cls.user_2.fullyQualifiedName.root]
)
)

cls.owner_user_1 = EntityReferenceList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,8 @@ def test_role_add_team(self):
data=CreateTeamRequest(
name="test-role-team-1",
teamType="Group",
users=[user.id],
defaultRoles=[role.id],
users=[user.fullyQualifiedName.root],
defaultRoles=[role.fullyQualifiedName.root],
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,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 @@ -84,6 +90,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 @@ -226,6 +233,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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might need to a give a relative path here cc @mohityadav766

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed , will remove this

rec.get(ENTITY_TYPE_KEY)),
true))) {
writer.write(JsonUtils.pojoToJson(rec, true));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Expand Down Expand Up @@ -574,6 +595,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 @@ -998,6 +998,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 @@ -21,12 +21,12 @@ public Team createToEntity(CreateTeam create, String user) {
return copy(new Team(), create, user)
.withProfile(create.getProfile())
.withIsJoinable(create.getIsJoinable())
.withUsers(EntityUtil.toEntityReferences(create.getUsers(), Entity.USER))
.withDefaultRoles(EntityUtil.toEntityReferences(create.getDefaultRoles(), Entity.ROLE))
.withUsers(EntityUtil.populateEntityReferenceFromFqn(create.getUsers(), Entity.USER))
.withDefaultRoles(EntityUtil.populateEntityReferenceFromFqn(create.getDefaultRoles(), Entity.ROLE))
.withTeamType(create.getTeamType())
.withParents(EntityUtil.toEntityReferences(create.getParents(), Entity.TEAM))
.withChildren(EntityUtil.toEntityReferences(create.getChildren(), Entity.TEAM))
.withPolicies(EntityUtil.toEntityReferences(create.getPolicies(), Entity.POLICY))
.withParents(EntityUtil.populateEntityReferenceFromFqn(create.getParents(), Entity.TEAM))
.withChildren(EntityUtil.populateEntityReferenceFromFqn(create.getChildren(), Entity.TEAM))
.withPolicies(EntityUtil.populateEntityReferenceFromFqn(create.getPolicies(), Entity.POLICY))
.withEmail(create.getEmail())
.withDomains(EntityUtil.getEntityReferences(Entity.DOMAIN, create.getDomains()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package org.openmetadata.service.resources.teams;

import static org.openmetadata.common.utils.CommonUtil.listOf;
import static org.openmetadata.service.exception.CatalogExceptionMessage.CREATE_GROUP;
import static org.openmetadata.service.exception.CatalogExceptionMessage.CREATE_ORGANIZATION;

import io.dropwizard.jersey.PATCH;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
Expand Down Expand Up @@ -70,6 +72,7 @@
import org.openmetadata.service.resources.EntityResource;
import org.openmetadata.service.security.Authorizer;
import org.openmetadata.service.util.CSVExportResponse;
import org.openmetadata.service.util.EntityUtil;
import org.openmetadata.service.util.JsonUtils;
import org.openmetadata.service.util.ResultList;

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
Loading
Loading