diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/governance/workflows/elements/nodes/userTask/impl/CreateApprovalTaskImpl.java b/openmetadata-service/src/main/java/org/openmetadata/service/governance/workflows/elements/nodes/userTask/impl/CreateApprovalTaskImpl.java index af14ad5c3841..c20411d94689 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/governance/workflows/elements/nodes/userTask/impl/CreateApprovalTaskImpl.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/governance/workflows/elements/nodes/userTask/impl/CreateApprovalTaskImpl.java @@ -26,7 +26,7 @@ import org.openmetadata.service.exception.EntityNotFoundException; import org.openmetadata.service.governance.workflows.WorkflowHandler; import org.openmetadata.service.jdbi3.FeedRepository; -import org.openmetadata.service.resources.feeds.FeedResource; +import org.openmetadata.service.resources.feeds.FeedMapper; import org.openmetadata.service.resources.feeds.MessageParser; import org.openmetadata.service.util.WebsocketNotificationHandler; @@ -91,7 +91,7 @@ private Thread createApprovalTask(GlossaryTerm entity, List ass } catch (EntityNotFoundException ex) { TaskDetails taskDetails = new TaskDetails() - .withAssignees(FeedResource.formatAssignees(assignees)) + .withAssignees(FeedMapper.formatAssignees(assignees)) .withType(TaskType.RequestApproval) .withStatus(TaskStatus.Open); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java index e86f736d444c..a91b9221c9ce 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java @@ -2053,11 +2053,12 @@ public final void validateUsers(List entityReferences) { } } - private boolean validateIfAllRefsAreEntityType(List list, String entityType) { + private static boolean validateIfAllRefsAreEntityType( + List list, String entityType) { return list.stream().allMatch(obj -> obj.getType().equals(entityType)); } - public final void validateReviewers(List entityReferences) { + public static void validateReviewers(List entityReferences) { if (!nullOrEmpty(entityReferences)) { boolean areAllTeam = validateIfAllRefsAreEntityType(entityReferences, TEAM); boolean areAllUsers = validateIfAllRefsAreEntityType(entityReferences, USER); @@ -2415,7 +2416,7 @@ protected void checkSystemEntityDeletion(T entity) { } } - public final List validateOwners(List owners) { + public static List validateOwners(List owners) { if (nullOrEmpty(owners)) { return null; } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TestCaseRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TestCaseRepository.java index 42978ca47b72..741d407dc356 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TestCaseRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TestCaseRepository.java @@ -33,7 +33,6 @@ import org.openmetadata.schema.EntityTimeSeriesInterface; import org.openmetadata.schema.api.feed.CloseTask; import org.openmetadata.schema.api.feed.ResolveTask; -import org.openmetadata.schema.api.tests.CreateTestCaseResult; import org.openmetadata.schema.entity.data.Table; import org.openmetadata.schema.entity.teams.User; import org.openmetadata.schema.tests.TestCase; @@ -304,26 +303,10 @@ private void updateLogicalTestSuite(UUID testSuiteId) { public RestUtil.PutResponse addTestCaseResult( String updatedBy, UriInfo uriInfo, String fqn, TestCaseResult testCaseResult) { - // TODO: REMOVED ONCE DEPRECATED IN TEST CASE RESOURCE - CreateTestCaseResult createTestCaseResult = - new CreateTestCaseResult() - .withTimestamp(testCaseResult.getTimestamp()) - .withTestCaseStatus(testCaseResult.getTestCaseStatus()) - .withResult(testCaseResult.getResult()) - .withSampleData(testCaseResult.getSampleData()) - .withTestResultValue(testCaseResult.getTestResultValue()) - .withPassedRows(testCaseResult.getPassedRows()) - .withFailedRows(testCaseResult.getFailedRows()) - .withPassedRowsPercentage(testCaseResult.getPassedRowsPercentage()) - .withFailedRowsPercentage(testCaseResult.getFailedRowsPercentage()) - .withIncidentId(testCaseResult.getIncidentId()) - .withMaxBound(testCaseResult.getMaxBound()) - .withMinBound(testCaseResult.getMinBound()); - TestCaseResultRepository testCaseResultRepository = (TestCaseResultRepository) Entity.getEntityTimeSeriesRepository(TEST_CASE_RESULT); Response response = - testCaseResultRepository.addTestCaseResult(updatedBy, uriInfo, fqn, createTestCaseResult); + testCaseResultRepository.addTestCaseResult(updatedBy, uriInfo, fqn, testCaseResult); return new RestUtil.PutResponse<>( Response.Status.CREATED, (TestCaseResult) response.getEntity(), ENTITY_UPDATED); } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TestCaseResultRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TestCaseResultRepository.java index 4e247fbab2bc..0e42f27d26cb 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TestCaseResultRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TestCaseResultRepository.java @@ -17,7 +17,6 @@ import javax.ws.rs.core.UriInfo; import lombok.SneakyThrows; import org.openmetadata.common.utils.CommonUtil; -import org.openmetadata.schema.api.tests.CreateTestCaseResult; import org.openmetadata.schema.tests.ResultSummary; import org.openmetadata.schema.tests.TestCase; import org.openmetadata.schema.tests.TestSuite; @@ -80,9 +79,8 @@ public ResultList getTestCaseResults(String fqn, Long startTs, L } public Response addTestCaseResult( - String updatedBy, UriInfo uriInfo, String fqn, CreateTestCaseResult createTestCaseResult) { + String updatedBy, UriInfo uriInfo, String fqn, TestCaseResult testCaseResult) { TestCase testCase = Entity.getEntityByName(TEST_CASE, fqn, "", Include.ALL); - TestCaseResult testCaseResult = getTestCaseResult(createTestCaseResult, testCase); if (testCaseResult.getTestCaseStatus() == TestCaseStatus.Success) { testCaseRepository.deleteTestCaseFailedRowsSample(testCase.getId()); } @@ -299,24 +297,4 @@ public boolean hasTestCaseFailure(String fqn) throws IOException { .anyMatch( testCaseResult -> testCaseResult.getTestCaseStatus().equals(TestCaseStatus.Failed)); } - - private TestCaseResult getTestCaseResult( - CreateTestCaseResult createTestCaseResults, TestCase testCase) { - RestUtil.validateTimestampMilliseconds(createTestCaseResults.getTimestamp()); - return new TestCaseResult() - .withId(UUID.randomUUID()) - .withTestCaseFQN(testCase.getFullyQualifiedName()) - .withTimestamp(createTestCaseResults.getTimestamp()) - .withTestCaseStatus(createTestCaseResults.getTestCaseStatus()) - .withResult(createTestCaseResults.getResult()) - .withSampleData(createTestCaseResults.getSampleData()) - .withTestResultValue(createTestCaseResults.getTestResultValue()) - .withPassedRows(createTestCaseResults.getPassedRows()) - .withFailedRows(createTestCaseResults.getFailedRows()) - .withPassedRowsPercentage(createTestCaseResults.getPassedRowsPercentage()) - .withFailedRowsPercentage(createTestCaseResults.getFailedRowsPercentage()) - .withIncidentId(createTestCaseResults.getIncidentId()) - .withMaxBound(createTestCaseResults.getMaxBound()) - .withMinBound(createTestCaseResults.getMinBound()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/mapper/EntityMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/mapper/EntityMapper.java new file mode 100644 index 000000000000..a3be330862e4 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/mapper/EntityMapper.java @@ -0,0 +1,45 @@ +package org.openmetadata.service.mapper; + +import static org.openmetadata.schema.type.Include.NON_DELETED; +import static org.openmetadata.service.jdbi3.EntityRepository.validateOwners; +import static org.openmetadata.service.jdbi3.EntityRepository.validateReviewers; +import static org.openmetadata.service.util.EntityUtil.getEntityReferences; + +import java.util.List; +import java.util.UUID; +import org.openmetadata.common.utils.CommonUtil; +import org.openmetadata.schema.CreateEntity; +import org.openmetadata.schema.EntityInterface; +import org.openmetadata.schema.type.EntityReference; +import org.openmetadata.service.Entity; + +public interface EntityMapper { + T createToEntity(C create, String user); + + default T copy(T entity, CreateEntity request, String updatedBy) { + List owners = validateOwners(request.getOwners()); + EntityReference domain = validateDomain(request.getDomain()); + validateReviewers(request.getReviewers()); + entity.setId(UUID.randomUUID()); + entity.setName(request.getName()); + entity.setDisplayName(request.getDisplayName()); + entity.setDescription(request.getDescription()); + entity.setOwners(owners); + entity.setDomain(domain); + entity.setTags(request.getTags()); + entity.setDataProducts(getEntityReferences(Entity.DATA_PRODUCT, request.getDataProducts())); + entity.setLifeCycle(request.getLifeCycle()); + entity.setExtension(request.getExtension()); + entity.setUpdatedBy(updatedBy); + entity.setUpdatedAt(System.currentTimeMillis()); + entity.setReviewers(request.getReviewers()); + return entity; + } + + default EntityReference validateDomain(String domainFqn) { + if (CommonUtil.nullOrEmpty(domainFqn)) { + return null; + } + return Entity.getEntityReferenceByName(Entity.DOMAIN, domainFqn, NON_DELETED); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/mapper/EntityTimeSeriesMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/mapper/EntityTimeSeriesMapper.java new file mode 100644 index 000000000000..242cb8929698 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/mapper/EntityTimeSeriesMapper.java @@ -0,0 +1,7 @@ +package org.openmetadata.service.mapper; + +import org.openmetadata.schema.EntityTimeSeriesInterface; + +public interface EntityTimeSeriesMapper { + T createToEntity(C create, String user); +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventMapper.java new file mode 100644 index 000000000000..b6880919dd87 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventMapper.java @@ -0,0 +1,17 @@ +package org.openmetadata.service.resources.analytics; + +import org.openmetadata.schema.analytics.WebAnalyticEvent; +import org.openmetadata.schema.api.tests.CreateWebAnalyticEvent; +import org.openmetadata.service.mapper.EntityMapper; + +public class WebAnalyticEventMapper + implements EntityMapper { + @Override + public WebAnalyticEvent createToEntity(CreateWebAnalyticEvent create, String user) { + return copy(new WebAnalyticEvent(), create, user) + .withName(create.getName()) + .withDisplayName(create.getDisplayName()) + .withDescription(create.getDescription()) + .withEventType(create.getEventType()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventResource.java index 5ce147ef770e..d5ec469b4262 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventResource.java @@ -70,6 +70,7 @@ public class WebAnalyticEventResource public static final String COLLECTION_PATH = WebAnalyticEventRepository.COLLECTION_PATH; static final String FIELDS = "owners"; private static final Pattern HTML_PATTERN = Pattern.compile(".*\\<[^>]+>.*", Pattern.DOTALL); + private final WebAnalyticEventMapper mapper = new WebAnalyticEventMapper(); public WebAnalyticEventResource(Authorizer authorizer, Limits limits) { super(Entity.WEB_ANALYTIC_EVENT, authorizer, limits); @@ -169,7 +170,7 @@ public Response create( @Context SecurityContext securityContext, @Valid CreateWebAnalyticEvent create) { WebAnalyticEvent webAnalyticEvent = - getWebAnalyticEvent(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, webAnalyticEvent); } @@ -192,7 +193,7 @@ public Response createOrUpdate( @Context SecurityContext securityContext, @Valid CreateWebAnalyticEvent create) { WebAnalyticEvent webAnalyticEvent = - getWebAnalyticEvent(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, webAnalyticEvent); } @@ -554,15 +555,6 @@ public ResultList listWebAnalyticEventData( return repository.getWebAnalyticEventData(eventType, startTs, endTs); } - private WebAnalyticEvent getWebAnalyticEvent(CreateWebAnalyticEvent create, String user) { - return repository - .copy(new WebAnalyticEvent(), create, user) - .withName(create.getName()) - .withDisplayName(create.getDisplayName()) - .withDescription(create.getDescription()) - .withEventType(create.getEventType()); - } - public static WebAnalyticEventData sanitizeWebAnalyticEventData( WebAnalyticEventData webAnalyticEventDataInput) { Object inputData = webAnalyticEventDataInput.getEventData(); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APICollectionMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APICollectionMapper.java new file mode 100644 index 000000000000..60d3e9c44e77 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APICollectionMapper.java @@ -0,0 +1,20 @@ +package org.openmetadata.service.resources.apis; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; +import static org.openmetadata.service.util.EntityUtil.getEntityReferences; + +import org.openmetadata.schema.api.data.CreateAPICollection; +import org.openmetadata.schema.entity.data.APICollection; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class APICollectionMapper implements EntityMapper { + @Override + public APICollection createToEntity(CreateAPICollection create, String user) { + return copy(new APICollection(), create, user) + .withService(getEntityReference(Entity.API_SERVICE, create.getService())) + .withEndpointURL(create.getEndpointURL()) + .withApiEndpoints(getEntityReferences(Entity.API_ENDPOINT, create.getApiEndpoints())) + .withSourceHash(create.getSourceHash()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APICollectionResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APICollectionResource.java index 0f2581f1b0cc..b73d02c0c25d 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APICollectionResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APICollectionResource.java @@ -73,6 +73,7 @@ @Collection(name = "apiCollections") public class APICollectionResource extends EntityResource { public static final String COLLECTION_PATH = "v1/apiCollections/"; + private final APICollectionMapper mapper = new APICollectionMapper(); static final String FIELDS = "owners,apiEndpoints,tags,extension,domain,sourceHash"; @Override @@ -310,7 +311,7 @@ public Response create( @Context SecurityContext securityContext, @Valid CreateAPICollection create) { APICollection apiCollection = - getAPICollection(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, apiCollection); } @@ -392,7 +393,7 @@ public Response createOrUpdate( @Context SecurityContext securityContext, @Valid CreateAPICollection create) { APICollection apiCollection = - getAPICollection(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, apiCollection); } @@ -510,13 +511,4 @@ public Response restore( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private APICollection getAPICollection(CreateAPICollection create, String user) { - return repository - .copy(new APICollection(), create, user) - .withService(getEntityReference(Entity.API_SERVICE, create.getService())) - .withEndpointURL(create.getEndpointURL()) - .withApiEndpoints(getEntityReferences(Entity.API_ENDPOINT, create.getApiEndpoints())) - .withSourceHash(create.getSourceHash()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APIEndpointMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APIEndpointMapper.java new file mode 100644 index 000000000000..f197475efcf0 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APIEndpointMapper.java @@ -0,0 +1,21 @@ +package org.openmetadata.service.resources.apis; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; + +import org.openmetadata.schema.api.data.CreateAPIEndpoint; +import org.openmetadata.schema.entity.data.APIEndpoint; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class APIEndpointMapper implements EntityMapper { + @Override + public APIEndpoint createToEntity(CreateAPIEndpoint create, String user) { + return copy(new APIEndpoint(), create, user) + .withApiCollection(getEntityReference(Entity.API_COLLCECTION, create.getApiCollection())) + .withRequestMethod(create.getRequestMethod()) + .withEndpointURL(create.getEndpointURL()) + .withRequestSchema(create.getRequestSchema()) + .withResponseSchema(create.getResponseSchema()) + .withSourceHash(create.getSourceHash()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APIEndpointResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APIEndpointResource.java index 5060606c2a2a..40a853aec442 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APIEndpointResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apis/APIEndpointResource.java @@ -69,6 +69,8 @@ @Collection(name = "apiEndpoints") public class APIEndpointResource extends EntityResource { public static final String COLLECTION_PATH = "v1/apiEndpoints/"; + private final APIEndpointMapper mapper = new APIEndpointMapper(); + static final String FIELDS = "owners,followers,tags,extension,domain,dataProducts,sourceHash"; @Override @@ -304,7 +306,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateAPIEndpoint create) { - APIEndpoint apiEndpoint = getAPIEndpoint(create, securityContext.getUserPrincipal().getName()); + APIEndpoint apiEndpoint = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, apiEndpoint); } @@ -385,7 +388,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateAPIEndpoint create) { - APIEndpoint apiEndpoint = getAPIEndpoint(create, securityContext.getUserPrincipal().getName()); + APIEndpoint apiEndpoint = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, apiEndpoint); } @@ -552,15 +556,4 @@ public Response restore( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private APIEndpoint getAPIEndpoint(CreateAPIEndpoint create, String user) { - return repository - .copy(new APIEndpoint(), create, user) - .withApiCollection(getEntityReference(Entity.API_COLLCECTION, create.getApiCollection())) - .withRequestMethod(create.getRequestMethod()) - .withEndpointURL(create.getEndpointURL()) - .withRequestSchema(create.getRequestSchema()) - .withResponseSchema(create.getResponseSchema()) - .withSourceHash(create.getSourceHash()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppMapper.java new file mode 100644 index 000000000000..62b941626361 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppMapper.java @@ -0,0 +1,72 @@ +package org.openmetadata.service.resources.apps; + +import static org.openmetadata.service.Entity.BOT; +import static org.openmetadata.service.jdbi3.EntityRepository.validateOwners; + +import java.util.List; +import java.util.UUID; +import org.openmetadata.common.utils.CommonUtil; +import org.openmetadata.schema.entity.app.App; +import org.openmetadata.schema.entity.app.AppMarketPlaceDefinition; +import org.openmetadata.schema.entity.app.CreateApp; +import org.openmetadata.schema.type.EntityReference; +import org.openmetadata.schema.type.Include; +import org.openmetadata.service.Entity; +import org.openmetadata.service.jdbi3.AppMarketPlaceRepository; +import org.openmetadata.service.jdbi3.AppRepository; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.util.EntityUtil; + +public class AppMapper implements EntityMapper { + @Override + public App createToEntity(CreateApp createAppRequest, String updatedBy) { + AppMarketPlaceRepository appMarketPlaceRepository = + (AppMarketPlaceRepository) Entity.getEntityRepository(Entity.APP_MARKET_PLACE_DEF); + AppMarketPlaceDefinition marketPlaceDefinition = + appMarketPlaceRepository.getByName( + null, + createAppRequest.getName(), + new EntityUtil.Fields(appMarketPlaceRepository.getAllowedFields())); + List owners = validateOwners(createAppRequest.getOwners()); + App app = + new App() + .withId(UUID.randomUUID()) + .withName(marketPlaceDefinition.getName()) + .withDisplayName(createAppRequest.getDisplayName()) + .withDescription(createAppRequest.getDescription()) + .withOwners(owners) + .withUpdatedBy(updatedBy) + .withUpdatedAt(System.currentTimeMillis()) + .withDeveloper(marketPlaceDefinition.getDeveloper()) + .withDeveloperUrl(marketPlaceDefinition.getDeveloperUrl()) + .withPrivacyPolicyUrl(marketPlaceDefinition.getPrivacyPolicyUrl()) + .withSupportEmail(marketPlaceDefinition.getSupportEmail()) + .withClassName(marketPlaceDefinition.getClassName()) + .withAppType(marketPlaceDefinition.getAppType()) + .withScheduleType(marketPlaceDefinition.getScheduleType()) + .withAppConfiguration(createAppRequest.getAppConfiguration()) + .withRuntime(marketPlaceDefinition.getRuntime()) + .withPermission(marketPlaceDefinition.getPermission()) + .withAppSchedule(createAppRequest.getAppSchedule()) + .withAppLogoUrl(marketPlaceDefinition.getAppLogoUrl()) + .withAppScreenshots(marketPlaceDefinition.getAppScreenshots()) + .withFeatures(marketPlaceDefinition.getFeatures()) + .withSourcePythonClass(marketPlaceDefinition.getSourcePythonClass()) + .withAllowConfiguration(marketPlaceDefinition.getAllowConfiguration()) + .withSystem(marketPlaceDefinition.getSystem()) + .withSupportsInterrupt(marketPlaceDefinition.getSupportsInterrupt()); + + // validate Bot if provided + validateAndAddBot(app, createAppRequest.getBot()); + return app; + } + + private void validateAndAddBot(App app, String botName) { + AppRepository appRepository = (AppRepository) Entity.getEntityRepository(Entity.APPLICATION); + if (!CommonUtil.nullOrEmpty(botName)) { + app.setBot(Entity.getEntityReferenceByName(BOT, botName, Include.NON_DELETED)); + } else { + app.setBot(appRepository.createNewAppBot(app)); + } + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppMarketPlaceMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppMarketPlaceMapper.java new file mode 100644 index 000000000000..532cf057b101 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppMarketPlaceMapper.java @@ -0,0 +1,68 @@ +package org.openmetadata.service.resources.apps; + +import javax.ws.rs.BadRequestException; +import org.openmetadata.schema.entity.app.AppMarketPlaceDefinition; +import org.openmetadata.schema.entity.app.AppType; +import org.openmetadata.schema.entity.app.CreateAppMarketPlaceDefinitionReq; +import org.openmetadata.schema.entity.services.ingestionPipelines.PipelineServiceClientResponse; +import org.openmetadata.sdk.PipelineServiceClientInterface; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.util.JsonUtils; + +public class AppMarketPlaceMapper + implements EntityMapper { + private PipelineServiceClientInterface pipelineServiceClient; + + public AppMarketPlaceMapper(PipelineServiceClientInterface pipelineServiceClient) { + this.pipelineServiceClient = pipelineServiceClient; + } + + @Override + public AppMarketPlaceDefinition createToEntity( + CreateAppMarketPlaceDefinitionReq create, String user) { + AppMarketPlaceDefinition app = + copy(new AppMarketPlaceDefinition(), create, user) + .withDeveloper(create.getDeveloper()) + .withDeveloperUrl(create.getDeveloperUrl()) + .withSupportEmail(create.getSupportEmail()) + .withPrivacyPolicyUrl(create.getPrivacyPolicyUrl()) + .withClassName(create.getClassName()) + .withAppType(create.getAppType()) + .withScheduleType(create.getScheduleType()) + .withRuntime(create.getRuntime()) + .withAppConfiguration(create.getAppConfiguration()) + .withPermission(create.getPermission()) + .withAppLogoUrl(create.getAppLogoUrl()) + .withAppScreenshots(create.getAppScreenshots()) + .withFeatures(create.getFeatures()) + .withSourcePythonClass(create.getSourcePythonClass()) + .withAllowConfiguration(create.getAllowConfiguration()) + .withSystem(create.getSystem()) + .withSupportsInterrupt(create.getSupportsInterrupt()); + + // Validate App + validateApplication(app); + return app; + } + + private void validateApplication(AppMarketPlaceDefinition app) { + // Check if the className Exists in classPath + if (app.getAppType().equals(AppType.Internal)) { + // Check class name exists + try { + Class.forName(app.getClassName()); + } catch (ClassNotFoundException e) { + throw new BadRequestException( + "Application Cannot be registered, because the classname cannot be found on the Classpath."); + } + } else { + PipelineServiceClientResponse response = pipelineServiceClient.validateAppRegistration(app); + if (response.getCode() != 200) { + throw new BadRequestException( + String.format( + "Application Cannot be registered, Error from Pipeline Service Client. Status Code : %s , Reponse : %s", + response.getCode(), JsonUtils.pojoToJson(response))); + } + } + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppMarketPlaceResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppMarketPlaceResource.java index 11540d291828..374ab5ba719a 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppMarketPlaceResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppMarketPlaceResource.java @@ -18,7 +18,6 @@ import javax.validation.Valid; import javax.validation.constraints.Max; import javax.validation.constraints.Min; -import javax.ws.rs.BadRequestException; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.DefaultValue; @@ -39,9 +38,7 @@ import org.openmetadata.schema.api.data.RestoreEntity; import org.openmetadata.schema.entity.app.App; import org.openmetadata.schema.entity.app.AppMarketPlaceDefinition; -import org.openmetadata.schema.entity.app.AppType; import org.openmetadata.schema.entity.app.CreateAppMarketPlaceDefinitionReq; -import org.openmetadata.schema.entity.services.ingestionPipelines.PipelineServiceClientResponse; import org.openmetadata.schema.type.EntityHistory; import org.openmetadata.schema.type.Include; import org.openmetadata.sdk.PipelineServiceClientInterface; @@ -55,7 +52,6 @@ import org.openmetadata.service.resources.Collection; import org.openmetadata.service.resources.EntityResource; import org.openmetadata.service.security.Authorizer; -import org.openmetadata.service.util.JsonUtils; import org.openmetadata.service.util.ResultList; @Path("/v1/apps/marketplace") @@ -70,7 +66,7 @@ public class AppMarketPlaceResource extends EntityResource { public static final String COLLECTION_PATH = "/v1/apps/marketplace/"; private PipelineServiceClientInterface pipelineServiceClient; - + private AppMarketPlaceMapper mapper; static final String FIELDS = "owners,tags"; @Override @@ -80,6 +76,7 @@ public void initialize(OpenMetadataApplicationConfig config) { PipelineServiceClientFactory.createPipelineServiceClient( config.getPipelineServiceClientConfiguration()); + mapper = new AppMarketPlaceMapper(pipelineServiceClient); // Initialize Default Installed Applications List createAppMarketPlaceDefinitionReqs = getEntitiesFromSeedData( @@ -87,7 +84,7 @@ public void initialize(OpenMetadataApplicationConfig config) { String.format(".*json/data/%s/.*\\.json$", entityType), CreateAppMarketPlaceDefinitionReq.class); for (CreateAppMarketPlaceDefinitionReq definitionReq : createAppMarketPlaceDefinitionReqs) { - AppMarketPlaceDefinition definition = getApplicationDefinition(definitionReq, "admin"); + AppMarketPlaceDefinition definition = mapper.createToEntity(definitionReq, "admin"); // Update Fully Qualified Name repository.setFullyQualifiedName(definition); this.repository.createOrUpdate(null, definition); @@ -309,7 +306,7 @@ public Response create( @Context SecurityContext securityContext, @Valid CreateAppMarketPlaceDefinitionReq create) { AppMarketPlaceDefinition app = - getApplicationDefinition(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, app); } @@ -389,7 +386,7 @@ public Response createOrUpdate( @Context SecurityContext securityContext, @Valid CreateAppMarketPlaceDefinitionReq create) { AppMarketPlaceDefinition app = - getApplicationDefinition(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, app); } @@ -459,53 +456,4 @@ public Response restoreApp( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private AppMarketPlaceDefinition getApplicationDefinition( - CreateAppMarketPlaceDefinitionReq create, String updatedBy) { - AppMarketPlaceDefinition app = - repository - .copy(new AppMarketPlaceDefinition(), create, updatedBy) - .withDeveloper(create.getDeveloper()) - .withDeveloperUrl(create.getDeveloperUrl()) - .withSupportEmail(create.getSupportEmail()) - .withPrivacyPolicyUrl(create.getPrivacyPolicyUrl()) - .withClassName(create.getClassName()) - .withAppType(create.getAppType()) - .withScheduleType(create.getScheduleType()) - .withRuntime(create.getRuntime()) - .withAppConfiguration(create.getAppConfiguration()) - .withPermission(create.getPermission()) - .withAppLogoUrl(create.getAppLogoUrl()) - .withAppScreenshots(create.getAppScreenshots()) - .withFeatures(create.getFeatures()) - .withSourcePythonClass(create.getSourcePythonClass()) - .withAllowConfiguration(create.getAllowConfiguration()) - .withSystem(create.getSystem()) - .withSupportsInterrupt(create.getSupportsInterrupt()); - - // Validate App - validateApplication(app); - return app; - } - - private void validateApplication(AppMarketPlaceDefinition app) { - // Check if the className Exists in classPath - if (app.getAppType().equals(AppType.Internal)) { - // Check class name exists - try { - Class.forName(app.getClassName()); - } catch (ClassNotFoundException e) { - throw new BadRequestException( - "Application Cannot be registered, because the classname cannot be found on the Classpath."); - } - } else { - PipelineServiceClientResponse response = pipelineServiceClient.validateAppRegistration(app); - if (response.getCode() != 200) { - throw new BadRequestException( - String.format( - "Application Cannot be registered, Error from Pipeline Service Client. Status Code : %s , Reponse : %s", - response.getCode(), JsonUtils.pojoToJson(response))); - } - } - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppResource.java index 21a88f23d942..4edb03b13059 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/apps/AppResource.java @@ -3,7 +3,6 @@ import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty; import static org.openmetadata.schema.type.Include.ALL; import static org.openmetadata.service.Entity.APPLICATION; -import static org.openmetadata.service.Entity.BOT; import static org.openmetadata.service.Entity.FIELD_OWNERS; import static org.openmetadata.service.jdbi3.EntityRepository.getEntitiesFromSeedData; @@ -43,12 +42,10 @@ import javax.ws.rs.core.SecurityContext; import javax.ws.rs.core.UriInfo; import lombok.extern.slf4j.Slf4j; -import org.openmetadata.common.utils.CommonUtil; import org.openmetadata.schema.ServiceEntityInterface; import org.openmetadata.schema.api.data.RestoreEntity; import org.openmetadata.schema.entity.app.App; import org.openmetadata.schema.entity.app.AppExtension; -import org.openmetadata.schema.entity.app.AppMarketPlaceDefinition; import org.openmetadata.schema.entity.app.AppRunRecord; import org.openmetadata.schema.entity.app.AppType; import org.openmetadata.schema.entity.app.CreateApp; @@ -105,6 +102,7 @@ public class AppResource extends EntityResource { public static final List SCHEDULED_TYPES = List.of(ScheduleType.Scheduled, ScheduleType.ScheduledOrManual, ScheduleType.NoSchedule); public static final String SLACK_APPLICATION = "SlackApplication"; + private final AppMapper mapper = new AppMapper(); @Override public void initialize(OpenMetadataApplicationConfig config) { @@ -133,18 +131,10 @@ private void loadDefaultApplications(List defaultAppCreateRequests) { // Get Create App Requests for (CreateApp createApp : defaultAppCreateRequests) { try { - AppMarketPlaceDefinition definition = - repository - .getMarketPlace() - .getByName( - null, - createApp.getName(), - new EntityUtil.Fields(repository.getMarketPlace().getAllowedFields())); App app = getAppForInit(createApp.getName()); if (app == null) { app = - getApplication(definition, createApp, "admin") - .withFullyQualifiedName(createApp.getName()); + mapper.createToEntity(createApp, "admin").withFullyQualifiedName(createApp.getName()); repository.initializeEntity(app); } @@ -621,15 +611,7 @@ public App getVersion( }) public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateApp create) { - - AppMarketPlaceDefinition definition = - repository - .getMarketPlace() - .getByName( - uriInfo, - create.getName(), - new EntityUtil.Fields(repository.getMarketPlace().getAllowedFields())); - App app = getApplication(definition, create, securityContext.getUserPrincipal().getName()); + App app = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); limits.enforceLimits( securityContext, getResourceContext(), @@ -749,14 +731,7 @@ public Response patchApplication( public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateApp create) throws SchedulerException { - AppMarketPlaceDefinition definition = - repository - .getMarketPlace() - .getByName( - uriInfo, - create.getName(), - new EntityUtil.Fields(repository.getMarketPlace().getAllowedFields())); - App app = getApplication(definition, create, securityContext.getUserPrincipal().getName()); + App app = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); AppScheduler.getInstance().deleteScheduledApplication(app); if (SCHEDULED_TYPES.contains(app.getScheduleType())) { ApplicationHandler.getInstance() @@ -1100,52 +1075,6 @@ private void decryptOrNullify( } } - private App getApplication( - AppMarketPlaceDefinition marketPlaceDefinition, - CreateApp createAppRequest, - String updatedBy) { - List owners = repository.validateOwners(createAppRequest.getOwners()); - App app = - new App() - .withId(UUID.randomUUID()) - .withName(marketPlaceDefinition.getName()) - .withDisplayName(createAppRequest.getDisplayName()) - .withDescription(createAppRequest.getDescription()) - .withOwners(owners) - .withUpdatedBy(updatedBy) - .withUpdatedAt(System.currentTimeMillis()) - .withDeveloper(marketPlaceDefinition.getDeveloper()) - .withDeveloperUrl(marketPlaceDefinition.getDeveloperUrl()) - .withPrivacyPolicyUrl(marketPlaceDefinition.getPrivacyPolicyUrl()) - .withSupportEmail(marketPlaceDefinition.getSupportEmail()) - .withClassName(marketPlaceDefinition.getClassName()) - .withAppType(marketPlaceDefinition.getAppType()) - .withScheduleType(marketPlaceDefinition.getScheduleType()) - .withAppConfiguration(createAppRequest.getAppConfiguration()) - .withRuntime(marketPlaceDefinition.getRuntime()) - .withPermission(marketPlaceDefinition.getPermission()) - .withAppSchedule(createAppRequest.getAppSchedule()) - .withAppLogoUrl(marketPlaceDefinition.getAppLogoUrl()) - .withAppScreenshots(marketPlaceDefinition.getAppScreenshots()) - .withFeatures(marketPlaceDefinition.getFeatures()) - .withSourcePythonClass(marketPlaceDefinition.getSourcePythonClass()) - .withAllowConfiguration(marketPlaceDefinition.getAllowConfiguration()) - .withSystem(marketPlaceDefinition.getSystem()) - .withSupportsInterrupt(marketPlaceDefinition.getSupportsInterrupt()); - - // validate Bot if provided - validateAndAddBot(app, createAppRequest.getBot()); - return app; - } - - private void validateAndAddBot(App app, String botName) { - if (!CommonUtil.nullOrEmpty(botName)) { - app.setBot(Entity.getEntityReferenceByName(BOT, botName, Include.NON_DELETED)); - } else { - app.setBot(repository.createNewAppBot(app)); - } - } - private IngestionPipeline getIngestionPipeline( UriInfo uriInfo, SecurityContext securityContext, App app) { EntityReference pipelineRef = app.getPipelines().get(0); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/automations/WorkflowMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/automations/WorkflowMapper.java new file mode 100644 index 000000000000..2aa3af07e0d6 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/automations/WorkflowMapper.java @@ -0,0 +1,31 @@ +package org.openmetadata.service.resources.automations; + +import org.openmetadata.schema.entity.automations.CreateWorkflow; +import org.openmetadata.schema.entity.automations.Workflow; +import org.openmetadata.schema.services.connections.metadata.OpenMetadataConnection; +import org.openmetadata.service.OpenMetadataApplicationConfig; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.util.OpenMetadataConnectionBuilder; + +public class WorkflowMapper implements EntityMapper { + private final OpenMetadataApplicationConfig openMetadataApplicationConfig; + + public WorkflowMapper(OpenMetadataApplicationConfig config) { + this.openMetadataApplicationConfig = config; + } + + @Override + public Workflow createToEntity(CreateWorkflow create, String user) { + OpenMetadataConnection openMetadataServerConnection = + new OpenMetadataConnectionBuilder(openMetadataApplicationConfig).build(); + return copy(new Workflow(), create, user) + .withDescription(create.getDescription()) + .withRequest(create.getRequest()) + .withWorkflowType(create.getWorkflowType()) + .withDisplayName(create.getDisplayName()) + .withResponse(create.getResponse()) + .withStatus(create.getStatus()) + .withOpenMetadataServerConnection(openMetadataServerConnection) + .withName(create.getName()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/automations/WorkflowResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/automations/WorkflowResource.java index 0ec7efa9df40..01f22137ccad 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/automations/WorkflowResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/automations/WorkflowResource.java @@ -83,7 +83,7 @@ public class WorkflowResource extends EntityResource { public static final String COLLECTION_PATH = "/v1/automations/workflows"; static final String FIELDS = "owners"; - + private WorkflowMapper mapper; private PipelineServiceClientInterface pipelineServiceClient; private OpenMetadataApplicationConfig openMetadataApplicationConfig; @@ -94,7 +94,7 @@ public WorkflowResource(Authorizer authorizer, Limits limits) { @Override public void initialize(OpenMetadataApplicationConfig config) { this.openMetadataApplicationConfig = config; - + this.mapper = new WorkflowMapper(config); this.pipelineServiceClient = PipelineServiceClientFactory.createPipelineServiceClient( config.getPipelineServiceClientConfiguration()); @@ -331,7 +331,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateWorkflow create) { - Workflow workflow = getWorkflow(create, securityContext.getUserPrincipal().getName()); + Workflow workflow = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); Response response = create(uriInfo, securityContext, unmask(workflow)); return Response.fromResponse(response) .entity(decryptOrNullify(securityContext, (Workflow) response.getEntity())) @@ -452,7 +452,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateWorkflow create) { - Workflow workflow = getWorkflow(create, securityContext.getUserPrincipal().getName()); + Workflow workflow = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); workflow = unmask(workflow); Response response = createOrUpdate(uriInfo, securityContext, workflow); return Response.fromResponse(response) @@ -539,21 +539,6 @@ public Response restoreWorkflow( .build(); } - private Workflow getWorkflow(CreateWorkflow create, String user) { - OpenMetadataConnection openMetadataServerConnection = - new OpenMetadataConnectionBuilder(openMetadataApplicationConfig).build(); - return repository - .copy(new Workflow(), create, user) - .withDescription(create.getDescription()) - .withRequest(create.getRequest()) - .withWorkflowType(create.getWorkflowType()) - .withDisplayName(create.getDisplayName()) - .withResponse(create.getResponse()) - .withStatus(create.getStatus()) - .withOpenMetadataServerConnection(openMetadataServerConnection) - .withName(create.getName()); - } - private Workflow unmask(Workflow workflow) { repository.setFullyQualifiedName(workflow); Workflow originalWorkflow; diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/bots/BotMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/bots/BotMapper.java new file mode 100644 index 000000000000..170dbbcc7ec1 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/bots/BotMapper.java @@ -0,0 +1,85 @@ +package org.openmetadata.service.resources.bots; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; + +import java.util.List; +import org.openmetadata.schema.EntityInterface; +import org.openmetadata.schema.api.CreateBot; +import org.openmetadata.schema.entity.Bot; +import org.openmetadata.schema.entity.teams.User; +import org.openmetadata.schema.type.Include; +import org.openmetadata.schema.type.Relationship; +import org.openmetadata.service.Entity; +import org.openmetadata.service.exception.CatalogExceptionMessage; +import org.openmetadata.service.jdbi3.BotRepository; +import org.openmetadata.service.jdbi3.CollectionDAO; +import org.openmetadata.service.jdbi3.EntityRepository; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.util.EntityUtil; + +public class BotMapper implements EntityMapper { + @Override + public Bot createToEntity(CreateBot create, String user) { + BotRepository repository = (BotRepository) Entity.getEntityRepository(Entity.BOT); + Bot bot = getBot(create, user); + Bot originalBot = retrieveBot(bot.getName()); + User botUser = retrieveUser(bot); + if (botUser != null && !Boolean.TRUE.equals(botUser.getIsBot())) { + throw new IllegalArgumentException( + String.format("User [%s] is not a bot user", botUser.getName())); + } + if (userHasRelationshipWithAnyBot(botUser, originalBot)) { + List userBotRelationship = + retrieveBotRelationshipsFor(botUser); + bot = + repository.get( + null, + userBotRelationship.stream().findFirst().orElseThrow().getId(), + EntityUtil.Fields.EMPTY_FIELDS); + throw new IllegalArgumentException( + CatalogExceptionMessage.userAlreadyBot(botUser.getName(), bot.getName())); + } + // TODO: review this flow on https://github.com/open-metadata/OpenMetadata/issues/8321 + if (originalBot != null) { + bot.setProvider(originalBot.getProvider()); + } + return bot; + } + + private Bot getBot(CreateBot create, String user) { + return copy(new Bot(), create, user) + .withBotUser(getEntityReference(Entity.USER, create.getBotUser())) + .withProvider(create.getProvider()) + .withFullyQualifiedName(create.getName()); + } + + private boolean userHasRelationshipWithAnyBot(User user, Bot botUser) { + if (user == null) { + return false; + } + List userBotRelationship = + retrieveBotRelationshipsFor(user); + return !userBotRelationship.isEmpty() + && (botUser == null + || userBotRelationship.stream() + .anyMatch(relationship -> !relationship.getId().equals(botUser.getId()))); + } + + private List retrieveBotRelationshipsFor(User user) { + BotRepository repository = (BotRepository) Entity.getEntityRepository(Entity.BOT); + return repository.findFromRecords(user.getId(), Entity.USER, Relationship.CONTAINS, Entity.BOT); + } + + private User retrieveUser(Bot bot) { + EntityRepository userRepository = + Entity.getEntityRepository(Entity.USER); + return (User) + userRepository.findByNameOrNull( + bot.getBotUser().getFullyQualifiedName(), Include.NON_DELETED); + } + + private Bot retrieveBot(String botName) { + BotRepository repository = (BotRepository) Entity.getEntityRepository(Entity.BOT); + return repository.findByNameOrNull(botName, Include.NON_DELETED); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/bots/BotResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/bots/BotResource.java index 457485dcf575..eaf240ec5618 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/bots/BotResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/bots/BotResource.java @@ -48,30 +48,24 @@ import javax.ws.rs.core.SecurityContext; import javax.ws.rs.core.UriInfo; import lombok.extern.slf4j.Slf4j; -import org.openmetadata.schema.EntityInterface; import org.openmetadata.schema.api.CreateBot; import org.openmetadata.schema.api.data.RestoreEntity; import org.openmetadata.schema.entity.Bot; +import org.openmetadata.schema.entity.teams.Role; import org.openmetadata.schema.entity.teams.User; import org.openmetadata.schema.type.EntityHistory; import org.openmetadata.schema.type.Include; -import org.openmetadata.schema.type.Relationship; import org.openmetadata.schema.utils.EntityInterfaceUtil; import org.openmetadata.service.Entity; import org.openmetadata.service.OpenMetadataApplicationConfig; -import org.openmetadata.service.exception.CatalogExceptionMessage; import org.openmetadata.service.jdbi3.BotRepository; -import org.openmetadata.service.jdbi3.CollectionDAO.EntityRelationshipRecord; -import org.openmetadata.service.jdbi3.EntityRepository; import org.openmetadata.service.jdbi3.ListFilter; import org.openmetadata.service.jdbi3.UserRepository; import org.openmetadata.service.limits.Limits; import org.openmetadata.service.resources.Collection; import org.openmetadata.service.resources.EntityResource; -import org.openmetadata.service.resources.teams.RoleResource; import org.openmetadata.service.security.Authorizer; import org.openmetadata.service.security.SecurityUtil; -import org.openmetadata.service.util.EntityUtil; import org.openmetadata.service.util.ResultList; import org.openmetadata.service.util.UserUtil; @@ -87,6 +81,7 @@ @Collection(name = "bots", order = 4, requiredForOps = true) // initialize after user resource public class BotResource extends EntityResource { public static final String COLLECTION_PATH = "/v1/bots/"; + private final BotMapper mapper = new BotMapper(); public BotResource(Authorizer authorizer, Limits limits) { super(Entity.BOT, authorizer, limits); @@ -105,7 +100,17 @@ public void initialize(OpenMetadataApplicationConfig config) throws IOException .withIsAdmin(false); user.setRoles( listOrEmpty(botUser.getRoles()).stream() - .map(entityReference -> RoleResource.getRole(entityReference.getName())) + .map( + entityReference -> { + Role role = + Entity.getEntityByName( + Entity.ROLE, + entityReference.getName(), + "id", + Include.NON_DELETED, + true); + return role.getEntityReference(); + }) .toList()); // Add or update User Bot UserUtil.addOrUpdateBotUser(user); @@ -300,7 +305,7 @@ public Bot getVersion( }) public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateBot create) { - Bot bot = getBot(securityContext, create); + Bot bot = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, bot); } @@ -321,7 +326,7 @@ public Response create( }) public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateBot create) { - Bot bot = getBot(securityContext, create); + Bot bot = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, bot); } @@ -449,64 +454,4 @@ public Response restoreBot( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private Bot getBot(CreateBot create, String user) { - return repository - .copy(new Bot(), create, user) - .withBotUser(getEntityReference(Entity.USER, create.getBotUser())) - .withProvider(create.getProvider()) - .withFullyQualifiedName(create.getName()); - } - - private boolean userHasRelationshipWithAnyBot(User user, Bot botUser) { - if (user == null) { - return false; - } - List userBotRelationship = retrieveBotRelationshipsFor(user); - return !userBotRelationship.isEmpty() - && (botUser == null - || userBotRelationship.stream() - .anyMatch(relationship -> !relationship.getId().equals(botUser.getId()))); - } - - private List retrieveBotRelationshipsFor(User user) { - return repository.findFromRecords(user.getId(), Entity.USER, Relationship.CONTAINS, Entity.BOT); - } - - private Bot getBot(SecurityContext securityContext, CreateBot create) { - Bot bot = getBot(create, securityContext.getUserPrincipal().getName()); - Bot originalBot = retrieveBot(bot.getName()); - User botUser = retrieveUser(bot); - if (botUser != null && !Boolean.TRUE.equals(botUser.getIsBot())) { - throw new IllegalArgumentException( - String.format("User [%s] is not a bot user", botUser.getName())); - } - if (userHasRelationshipWithAnyBot(botUser, originalBot)) { - List userBotRelationship = retrieveBotRelationshipsFor(botUser); - bot = - repository.get( - null, - userBotRelationship.stream().findFirst().orElseThrow().getId(), - EntityUtil.Fields.EMPTY_FIELDS); - throw new IllegalArgumentException( - CatalogExceptionMessage.userAlreadyBot(botUser.getName(), bot.getName())); - } - // TODO: review this flow on https://github.com/open-metadata/OpenMetadata/issues/8321 - if (originalBot != null) { - bot.setProvider(originalBot.getProvider()); - } - return bot; - } - - private User retrieveUser(Bot bot) { - EntityRepository userRepository = - Entity.getEntityRepository(Entity.USER); - return (User) - userRepository.findByNameOrNull( - bot.getBotUser().getFullyQualifiedName(), Include.NON_DELETED); - } - - private Bot retrieveBot(String botName) { - return repository.findByNameOrNull(botName, Include.NON_DELETED); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/charts/ChartMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/charts/ChartMapper.java new file mode 100644 index 000000000000..095077f0a8e5 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/charts/ChartMapper.java @@ -0,0 +1,21 @@ +package org.openmetadata.service.resources.charts; + +import static org.openmetadata.service.util.EntityUtil.getEntityReferences; + +import org.openmetadata.schema.api.data.CreateChart; +import org.openmetadata.schema.entity.data.Chart; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.util.EntityUtil; + +public class ChartMapper implements EntityMapper { + @Override + public Chart createToEntity(CreateChart create, String user) { + return copy(new Chart(), create, user) + .withService(EntityUtil.getEntityReference(Entity.DASHBOARD_SERVICE, create.getService())) + .withChartType(create.getChartType()) + .withSourceUrl(create.getSourceUrl()) + .withSourceHash(create.getSourceHash()) + .withDashboards(getEntityReferences(Entity.DASHBOARD, create.getDashboards())); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/charts/ChartResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/charts/ChartResource.java index 31dc3da0797d..b9d8ca6268b0 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/charts/ChartResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/charts/ChartResource.java @@ -61,7 +61,6 @@ import org.openmetadata.service.resources.Collection; import org.openmetadata.service.resources.EntityResource; import org.openmetadata.service.security.Authorizer; -import org.openmetadata.service.util.EntityUtil; import org.openmetadata.service.util.ResultList; @Path("/v1/charts") @@ -74,6 +73,7 @@ @Collection(name = "charts") public class ChartResource extends EntityResource { public static final String COLLECTION_PATH = "v1/charts/"; + private final ChartMapper mapper = new ChartMapper(); static final String FIELDS = "owners,followers,tags,domain,dataProducts,sourceHash,dashboards"; @Override @@ -300,7 +300,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateChart create) { - Chart chart = getChart(create, securityContext.getUserPrincipal().getName()); + Chart chart = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, chart); } @@ -379,7 +379,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateChart create) { - Chart chart = getChart(create, securityContext.getUserPrincipal().getName()); + Chart chart = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, chart); } @@ -523,14 +523,4 @@ public Response restoreChart( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private Chart getChart(CreateChart create, String user) { - return repository - .copy(new Chart(), create, user) - .withService(EntityUtil.getEntityReference(Entity.DASHBOARD_SERVICE, create.getService())) - .withChartType(create.getChartType()) - .withSourceUrl(create.getSourceUrl()) - .withSourceHash(create.getSourceHash()) - .withDashboards(getEntityReferences(Entity.DASHBOARD, create.getDashboards())); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dashboards/DashboardMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dashboards/DashboardMapper.java new file mode 100644 index 000000000000..c7ba5a1084a5 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dashboards/DashboardMapper.java @@ -0,0 +1,23 @@ +package org.openmetadata.service.resources.dashboards; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; +import static org.openmetadata.service.util.EntityUtil.getEntityReferences; + +import org.openmetadata.schema.api.data.CreateDashboard; +import org.openmetadata.schema.entity.data.Dashboard; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class DashboardMapper implements EntityMapper { + @Override + public Dashboard createToEntity(CreateDashboard create, String user) { + return copy(new Dashboard(), create, user) + .withService(getEntityReference(Entity.DASHBOARD_SERVICE, create.getService())) + .withCharts(getEntityReferences(Entity.CHART, create.getCharts())) + .withDataModels(getEntityReferences(Entity.DASHBOARD_DATA_MODEL, create.getDataModels())) + .withSourceUrl(create.getSourceUrl()) + .withDashboardType(create.getDashboardType()) + .withProject(create.getProject()) + .withSourceHash(create.getSourceHash()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dashboards/DashboardResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dashboards/DashboardResource.java index 19aea5208efa..bc26f5a1138d 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dashboards/DashboardResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dashboards/DashboardResource.java @@ -76,6 +76,7 @@ public class DashboardResource extends EntityResource { + @Override + public Database createToEntity(CreateDatabase create, String user) { + return copy(new Database(), create, user) + .withService(getEntityReference(Entity.DATABASE_SERVICE, create.getService())) + .withSourceUrl(create.getSourceUrl()) + .withRetentionPeriod(create.getRetentionPeriod()) + .withSourceHash(create.getSourceHash()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/DatabaseResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/DatabaseResource.java index e46ce51b8ff7..646f0b0ee53b 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/DatabaseResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/DatabaseResource.java @@ -77,6 +77,7 @@ @Collection(name = "databases") public class DatabaseResource extends EntityResource { public static final String COLLECTION_PATH = "v1/databases/"; + private final DatabaseMapper mapper = new DatabaseMapper(); static final String FIELDS = "owners,databaseSchemas,usageSummary,location,tags,extension,domain,sourceHash"; @@ -310,7 +311,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDatabase create) { - Database database = getDatabase(create, securityContext.getUserPrincipal().getName()); + Database database = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, database); } @@ -390,7 +391,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDatabase create) { - Database database = getDatabase(create, securityContext.getUserPrincipal().getName()); + Database database = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, database); } @@ -705,13 +706,4 @@ public Database deleteDataProfilerConfig( Database database = repository.deleteDatabaseProfilerConfig(id); return addHref(uriInfo, database); } - - private Database getDatabase(CreateDatabase create, String user) { - return repository - .copy(new Database(), create, user) - .withService(getEntityReference(Entity.DATABASE_SERVICE, create.getService())) - .withSourceUrl(create.getSourceUrl()) - .withRetentionPeriod(create.getRetentionPeriod()) - .withSourceHash(create.getSourceHash()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/DatabaseSchemaMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/DatabaseSchemaMapper.java new file mode 100644 index 000000000000..b6d9e9c6fc43 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/DatabaseSchemaMapper.java @@ -0,0 +1,19 @@ +package org.openmetadata.service.resources.databases; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; + +import org.openmetadata.schema.api.data.CreateDatabaseSchema; +import org.openmetadata.schema.entity.data.DatabaseSchema; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class DatabaseSchemaMapper implements EntityMapper { + @Override + public DatabaseSchema createToEntity(CreateDatabaseSchema create, String user) { + return copy(new DatabaseSchema(), create, user) + .withDatabase(getEntityReference(Entity.DATABASE, create.getDatabase())) + .withSourceUrl(create.getSourceUrl()) + .withRetentionPeriod(create.getRetentionPeriod()) + .withSourceHash(create.getSourceHash()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/DatabaseSchemaResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/DatabaseSchemaResource.java index 72bbb666f508..fc0e4becc212 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/DatabaseSchemaResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/DatabaseSchemaResource.java @@ -75,6 +75,7 @@ @Collection(name = "databaseSchemas") public class DatabaseSchemaResource extends EntityResource { + private final DatabaseSchemaMapper mapper = new DatabaseSchemaMapper(); public static final String COLLECTION_PATH = "v1/databaseSchemas/"; static final String FIELDS = "owners,tables,usageSummary,tags,extension,domain,sourceHash"; @@ -309,7 +310,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDatabaseSchema create) { - DatabaseSchema schema = getDatabaseSchema(create, securityContext.getUserPrincipal().getName()); + DatabaseSchema schema = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, schema); } @@ -390,7 +392,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDatabaseSchema create) { - DatabaseSchema schema = getDatabaseSchema(create, securityContext.getUserPrincipal().getName()); + DatabaseSchema schema = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, schema); } @@ -739,13 +742,4 @@ public Response searchSchemaEntityRelationship( return Entity.getSearchRepository() .searchSchemaEntityRelationship(fqn, upstreamDepth, downstreamDepth, queryFilter, deleted); } - - private DatabaseSchema getDatabaseSchema(CreateDatabaseSchema create, String user) { - return repository - .copy(new DatabaseSchema(), create, user) - .withDatabase(getEntityReference(Entity.DATABASE, create.getDatabase())) - .withSourceUrl(create.getSourceUrl()) - .withRetentionPeriod(create.getRetentionPeriod()) - .withSourceHash(create.getSourceHash()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/StoredProcedureMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/StoredProcedureMapper.java new file mode 100644 index 000000000000..ca1013b8566c --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/StoredProcedureMapper.java @@ -0,0 +1,20 @@ +package org.openmetadata.service.resources.databases; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; + +import org.openmetadata.schema.api.data.CreateStoredProcedure; +import org.openmetadata.schema.entity.data.StoredProcedure; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class StoredProcedureMapper implements EntityMapper { + @Override + public StoredProcedure createToEntity(CreateStoredProcedure create, String user) { + return copy(new StoredProcedure(), create, user) + .withDatabaseSchema(getEntityReference(Entity.DATABASE_SCHEMA, create.getDatabaseSchema())) + .withStoredProcedureCode(create.getStoredProcedureCode()) + .withStoredProcedureType(create.getStoredProcedureType()) + .withSourceUrl(create.getSourceUrl()) + .withSourceHash(create.getSourceHash()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/StoredProcedureResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/StoredProcedureResource.java index 84626a53062b..f83976818d33 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/StoredProcedureResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/StoredProcedureResource.java @@ -42,6 +42,7 @@ @Collection(name = "storedProcedures") public class StoredProcedureResource extends EntityResource { + private final StoredProcedureMapper mapper = new StoredProcedureMapper(); public static final String COLLECTION_PATH = "v1/storedProcedures/"; static final String FIELDS = "owners,tags,followers,extension,domain,sourceHash"; @@ -271,7 +272,7 @@ public Response create( @Context SecurityContext securityContext, @Valid CreateStoredProcedure create) { StoredProcedure storedProcedure = - getStoredProcedure(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, storedProcedure); } @@ -353,7 +354,7 @@ public Response createOrUpdate( @Context SecurityContext securityContext, @Valid CreateStoredProcedure create) { StoredProcedure storedProcedure = - getStoredProcedure(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, storedProcedure); } @@ -527,14 +528,4 @@ public Response restoreStoredProcedure( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private StoredProcedure getStoredProcedure(CreateStoredProcedure create, String user) { - return repository - .copy(new StoredProcedure(), create, user) - .withDatabaseSchema(getEntityReference(Entity.DATABASE_SCHEMA, create.getDatabaseSchema())) - .withStoredProcedureCode(create.getStoredProcedureCode()) - .withStoredProcedureType(create.getStoredProcedureType()) - .withSourceUrl(create.getSourceUrl()) - .withSourceHash(create.getSourceHash()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/TableMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/TableMapper.java new file mode 100644 index 000000000000..48b609724064 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/TableMapper.java @@ -0,0 +1,53 @@ +package org.openmetadata.service.resources.databases; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; + +import java.util.UUID; +import org.openmetadata.schema.api.data.CreateTable; +import org.openmetadata.schema.api.tests.CreateCustomMetric; +import org.openmetadata.schema.entity.data.Table; +import org.openmetadata.schema.tests.CustomMetric; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class TableMapper implements EntityMapper { + @Override + public Table createToEntity(CreateTable create, String user) { + return validateNewTable( + copy(new Table(), create, user) + .withColumns(create.getColumns()) + .withSourceUrl(create.getSourceUrl()) + .withLocationPath(create.getLocationPath()) + .withTableConstraints(create.getTableConstraints()) + .withTablePartition(create.getTablePartition()) + .withTableType(create.getTableType()) + .withFileFormat(create.getFileFormat()) + .withSchemaDefinition(create.getSchemaDefinition()) + .withTableProfilerConfig(create.getTableProfilerConfig()) + .withDatabaseSchema( + getEntityReference(Entity.DATABASE_SCHEMA, create.getDatabaseSchema()))) + .withDatabaseSchema(getEntityReference(Entity.DATABASE_SCHEMA, create.getDatabaseSchema())) + .withRetentionPeriod(create.getRetentionPeriod()) + .withSourceHash(create.getSourceHash()); + } + + public CustomMetric createCustomMetricToEntity(CreateCustomMetric create, String user) { + return new CustomMetric() + .withId(UUID.randomUUID()) + .withDescription(create.getDescription()) + .withName(create.getName()) + .withColumnName(create.getColumnName()) + .withOwners(create.getOwners()) + .withExpression(create.getExpression()) + .withUpdatedBy(user) + .withUpdatedAt(System.currentTimeMillis()); + } + + public static Table validateNewTable(Table table) { + table.setId(UUID.randomUUID()); + DatabaseUtil.validateConstraints(table.getColumns(), table.getTableConstraints()); + DatabaseUtil.validateTablePartition(table.getColumns(), table.getTablePartition()); + DatabaseUtil.validateColumns(table.getColumns()); + return table; + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/TableResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/TableResource.java index 529aa0769776..d74e7b13587c 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/TableResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/databases/TableResource.java @@ -90,6 +90,7 @@ @Consumes(MediaType.APPLICATION_JSON) @Collection(name = "tables") public class TableResource extends EntityResource { + private final TableMapper mapper = new TableMapper(); public static final String COLLECTION_PATH = "v1/tables/"; static final String FIELDS = "tableConstraints,tablePartition,usageSummary,owners,customMetrics,columns," @@ -367,7 +368,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateTable create) { - Table table = getTable(create, securityContext.getUserPrincipal().getName()); + Table table = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, table); } @@ -391,7 +392,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateTable create) { - Table table = getTable(create, securityContext.getUserPrincipal().getName()); + Table table = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, table); } @@ -1152,7 +1153,9 @@ public Table addCustomMetric( OperationContext operationContext = new OperationContext(entityType, MetadataOperation.EDIT_DATA_PROFILE); authorizer.authorize(securityContext, operationContext, getResourceContextById(id)); - CustomMetric customMetric = getCustomMetric(securityContext, createCustomMetric); + CustomMetric customMetric = + mapper.createCustomMetricToEntity( + createCustomMetric, securityContext.getUserPrincipal().getName()); Table table = repository.addCustomMetric(id, customMetric); return addHref(uriInfo, table); } @@ -1312,44 +1315,4 @@ public Response searchEntityRelationship( return Entity.getSearchRepository() .searchEntityRelationship(fqn, upstreamDepth, downstreamDepth, queryFilter, deleted); } - - public static Table validateNewTable(Table table) { - table.setId(UUID.randomUUID()); - DatabaseUtil.validateConstraints(table.getColumns(), table.getTableConstraints()); - DatabaseUtil.validateTablePartition(table.getColumns(), table.getTablePartition()); - DatabaseUtil.validateColumns(table.getColumns()); - return table; - } - - private Table getTable(CreateTable create, String user) { - return validateNewTable( - repository - .copy(new Table(), create, user) - .withColumns(create.getColumns()) - .withSourceUrl(create.getSourceUrl()) - .withLocationPath(create.getLocationPath()) - .withTableConstraints(create.getTableConstraints()) - .withTablePartition(create.getTablePartition()) - .withTableType(create.getTableType()) - .withFileFormat(create.getFileFormat()) - .withSchemaDefinition(create.getSchemaDefinition()) - .withTableProfilerConfig(create.getTableProfilerConfig()) - .withDatabaseSchema( - getEntityReference(Entity.DATABASE_SCHEMA, create.getDatabaseSchema()))) - .withDatabaseSchema(getEntityReference(Entity.DATABASE_SCHEMA, create.getDatabaseSchema())) - .withRetentionPeriod(create.getRetentionPeriod()) - .withSourceHash(create.getSourceHash()); - } - - private CustomMetric getCustomMetric(SecurityContext securityContext, CreateCustomMetric create) { - return new CustomMetric() - .withId(UUID.randomUUID()) - .withDescription(create.getDescription()) - .withName(create.getName()) - .withColumnName(create.getColumnName()) - .withOwners(create.getOwners()) - .withExpression(create.getExpression()) - .withUpdatedBy(securityContext.getUserPrincipal().getName()) - .withUpdatedAt(System.currentTimeMillis()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/datainsight/DataInsightChartMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/datainsight/DataInsightChartMapper.java new file mode 100644 index 000000000000..8d3523e47133 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/datainsight/DataInsightChartMapper.java @@ -0,0 +1,19 @@ +package org.openmetadata.service.resources.datainsight; + +import org.openmetadata.schema.api.dataInsight.CreateDataInsightChart; +import org.openmetadata.schema.dataInsight.DataInsightChart; +import org.openmetadata.service.mapper.EntityMapper; + +public class DataInsightChartMapper + implements EntityMapper { + @Override + public DataInsightChart createToEntity(CreateDataInsightChart create, String user) { + return copy(new DataInsightChart(), create, user) + .withName(create.getName()) + .withDescription(create.getDescription()) + .withDataIndexType(create.getDataIndexType()) + .withDimensions(create.getDimensions()) + .withMetrics(create.getMetrics()) + .withDisplayName(create.getDisplayName()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/datainsight/DataInsightChartResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/datainsight/DataInsightChartResource.java index da081c044bab..50a9c365f963 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/datainsight/DataInsightChartResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/datainsight/DataInsightChartResource.java @@ -65,6 +65,7 @@ public class DataInsightChartResource extends EntityResource { + private final DataInsightChartMapper mapper = new DataInsightChartMapper(); public static final String COLLECTION_PATH = DataInsightChartRepository.COLLECTION_PATH; public static final String FIELDS = "owners"; private final SearchRepository searchRepository; @@ -299,7 +300,7 @@ public Response create( @Context SecurityContext securityContext, @Valid CreateDataInsightChart create) { DataInsightChart dataInsightChart = - getDataInsightChart(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, dataInsightChart); } @@ -381,7 +382,7 @@ public Response createOrUpdate( @Context SecurityContext securityContext, @Valid CreateDataInsightChart create) { DataInsightChart dataInsightChart = - getDataInsightChart(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, dataInsightChart); } @@ -536,15 +537,4 @@ public Response listDataInsightChartResult( return searchRepository.listDataInsightChartResult( startTs, endTs, tier, team, dataInsightChartName, size, from, queryFilter, dataReportIndex); } - - private DataInsightChart getDataInsightChart(CreateDataInsightChart create, String user) { - return repository - .copy(new DataInsightChart(), create, user) - .withName(create.getName()) - .withDescription(create.getDescription()) - .withDataIndexType(create.getDataIndexType()) - .withDimensions(create.getDimensions()) - .withMetrics(create.getMetrics()) - .withDisplayName(create.getDisplayName()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/datamodels/DashboardDataModelMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/datamodels/DashboardDataModelMapper.java new file mode 100644 index 000000000000..3785f1500b01 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/datamodels/DashboardDataModelMapper.java @@ -0,0 +1,25 @@ +package org.openmetadata.service.resources.datamodels; + +import org.openmetadata.schema.api.data.CreateDashboardDataModel; +import org.openmetadata.schema.entity.data.DashboardDataModel; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.resources.databases.DatabaseUtil; +import org.openmetadata.service.util.EntityUtil; + +public class DashboardDataModelMapper + implements EntityMapper { + @Override + public DashboardDataModel createToEntity(CreateDashboardDataModel create, String user) { + DatabaseUtil.validateColumns(create.getColumns()); + return copy(new DashboardDataModel(), create, user) + .withService(EntityUtil.getEntityReference(Entity.DASHBOARD_SERVICE, create.getService())) + .withDataModelType(create.getDataModelType()) + .withSql(create.getSql()) + .withDataModelType(create.getDataModelType()) + .withServiceType(create.getServiceType()) + .withColumns(create.getColumns()) + .withProject(create.getProject()) + .withSourceHash(create.getSourceHash()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/datamodels/DashboardDataModelResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/datamodels/DashboardDataModelResource.java index 5be6a779a363..4250b3304b5b 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/datamodels/DashboardDataModelResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/datamodels/DashboardDataModelResource.java @@ -56,9 +56,7 @@ import org.openmetadata.service.limits.Limits; import org.openmetadata.service.resources.Collection; import org.openmetadata.service.resources.EntityResource; -import org.openmetadata.service.resources.databases.DatabaseUtil; import org.openmetadata.service.security.Authorizer; -import org.openmetadata.service.util.EntityUtil; import org.openmetadata.service.util.ResultList; @Path("/v1/dashboard/datamodels") @@ -71,6 +69,7 @@ @Collection(name = "datamodels") public class DashboardDataModelResource extends EntityResource { + private final DashboardDataModelMapper mapper = new DashboardDataModelMapper(); public static final String COLLECTION_PATH = "/v1/dashboard/datamodels"; protected static final String FIELDS = "owners,tags,followers,domain,sourceHash,extension"; @@ -300,7 +299,7 @@ public Response create( @Context SecurityContext securityContext, @Valid CreateDashboardDataModel create) { DashboardDataModel dashboardDataModel = - getDataModel(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, dashboardDataModel); } @@ -382,7 +381,7 @@ public Response createOrUpdate( @Context SecurityContext securityContext, @Valid CreateDashboardDataModel create) { DashboardDataModel dashboardDataModel = - getDataModel(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, dashboardDataModel); } @@ -541,18 +540,4 @@ public Response restoreDataModel( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private DashboardDataModel getDataModel(CreateDashboardDataModel create, String user) { - DatabaseUtil.validateColumns(create.getColumns()); - return repository - .copy(new DashboardDataModel(), create, user) - .withService(EntityUtil.getEntityReference(Entity.DASHBOARD_SERVICE, create.getService())) - .withDataModelType(create.getDataModelType()) - .withSql(create.getSql()) - .withDataModelType(create.getDataModelType()) - .withServiceType(create.getServiceType()) - .withColumns(create.getColumns()) - .withProject(create.getProject()) - .withSourceHash(create.getSourceHash()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/docstore/DocStoreMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/docstore/DocStoreMapper.java new file mode 100644 index 000000000000..7f1e259c6e57 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/docstore/DocStoreMapper.java @@ -0,0 +1,44 @@ +package org.openmetadata.service.resources.docstore; + +import javax.ws.rs.core.Response; +import org.openmetadata.schema.email.EmailTemplate; +import org.openmetadata.schema.email.TemplateValidationResponse; +import org.openmetadata.schema.entities.docStore.CreateDocument; +import org.openmetadata.schema.entities.docStore.Document; +import org.openmetadata.service.Entity; +import org.openmetadata.service.exception.CustomExceptionMessage; +import org.openmetadata.service.jdbi3.DocumentRepository; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.security.Authorizer; +import org.openmetadata.service.util.JsonUtils; +import org.openmetadata.service.util.email.DefaultTemplateProvider; + +public class DocStoreMapper implements EntityMapper { + public DocStoreMapper(Authorizer authorizer) { + this.authorizer = authorizer; + } + + private final Authorizer authorizer; + + @Override + public Document createToEntity(CreateDocument create, String user) { + DocumentRepository documentRepository = + (DocumentRepository) Entity.getEntityRepository(Entity.DOCUMENT); + // Validate email template + if (create.getEntityType().equals(DefaultTemplateProvider.ENTITY_TYPE_EMAIL_TEMPLATE)) { + // Only Admins Can do these operations + authorizer.authorizeAdmin(user); + String content = JsonUtils.convertValue(create.getData(), EmailTemplate.class).getTemplate(); + TemplateValidationResponse validationResp = + documentRepository.validateEmailTemplate(create.getName(), content); + if (Boolean.FALSE.equals(validationResp.getIsValid())) { + throw new CustomExceptionMessage( + Response.status(400).entity(validationResp).build(), validationResp.getMessage()); + } + } + return copy(new Document(), create, user) + .withFullyQualifiedName(create.getFullyQualifiedName()) + .withData(create.getData()) + .withEntityType(create.getEntityType()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/docstore/DocStoreResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/docstore/DocStoreResource.java index fb69f12fec28..a0420f7122db 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/docstore/DocStoreResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/docstore/DocStoreResource.java @@ -57,16 +57,13 @@ import org.openmetadata.schema.type.MetadataOperation; import org.openmetadata.service.Entity; import org.openmetadata.service.OpenMetadataApplicationConfig; -import org.openmetadata.service.exception.CustomExceptionMessage; import org.openmetadata.service.jdbi3.DocumentRepository; import org.openmetadata.service.jdbi3.ListFilter; import org.openmetadata.service.limits.Limits; import org.openmetadata.service.resources.Collection; import org.openmetadata.service.resources.EntityResource; import org.openmetadata.service.security.Authorizer; -import org.openmetadata.service.util.JsonUtils; import org.openmetadata.service.util.ResultList; -import org.openmetadata.service.util.email.DefaultTemplateProvider; @Slf4j @Path("/v1/docStore") @@ -76,6 +73,7 @@ @Collection(name = "knowledgePanel", order = 2) public class DocStoreResource extends EntityResource { public static final String COLLECTION_PATH = "/v1/docStore"; + private DocStoreMapper mapper; @Override public Document addHref(UriInfo uriInfo, Document doc) { @@ -91,6 +89,7 @@ protected List getEntitySpecificOperations() { public DocStoreResource(Authorizer authorizer, Limits limits) { super(Entity.DOCUMENT, authorizer, limits); + this.mapper = new DocStoreMapper(authorizer); } public static class DocumentList extends ResultList { @@ -301,7 +300,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDocument cd) { - Document doc = getDocument(cd, securityContext); + Document doc = mapper.createToEntity(cd, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, doc); } @@ -324,7 +323,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDocument cd) { - Document doc = getDocument(cd, securityContext); + Document doc = mapper.createToEntity(cd, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, doc); } @@ -489,24 +488,4 @@ public Response resetEmailTemplate( .build(); } } - - private Document getDocument(CreateDocument cd, SecurityContext securityContext) { - // Validate email template - if (cd.getEntityType().equals(DefaultTemplateProvider.ENTITY_TYPE_EMAIL_TEMPLATE)) { - // Only Admins Can do these operations - authorizer.authorizeAdmin(securityContext); - String content = JsonUtils.convertValue(cd.getData(), EmailTemplate.class).getTemplate(); - TemplateValidationResponse validationResp = - repository.validateEmailTemplate(cd.getName(), content); - if (Boolean.FALSE.equals(validationResp.getIsValid())) { - throw new CustomExceptionMessage( - Response.status(400).entity(validationResp).build(), validationResp.getMessage()); - } - } - return repository - .copy(new Document(), cd, securityContext.getUserPrincipal().getName()) - .withFullyQualifiedName(cd.getFullyQualifiedName()) - .withData(cd.getData()) - .withEntityType(cd.getEntityType()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DataProductMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DataProductMapper.java new file mode 100644 index 000000000000..f91693a86367 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DataProductMapper.java @@ -0,0 +1,34 @@ +package org.openmetadata.service.resources.domains; + +import static org.openmetadata.common.utils.CommonUtil.listOrEmpty; +import static org.openmetadata.service.util.EntityUtil.getEntityReferences; + +import java.util.ArrayList; +import java.util.List; +import org.openmetadata.schema.api.domains.CreateDataProduct; +import org.openmetadata.schema.entity.domains.DataProduct; +import org.openmetadata.schema.type.EntityReference; +import org.openmetadata.schema.type.Include; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.util.EntityUtil; + +public class DataProductMapper implements EntityMapper { + @Override + public DataProduct createToEntity(CreateDataProduct create, String user) { + List experts = create.getExperts(); + DataProduct dataProduct = + copy(new DataProduct(), create, user) + .withFullyQualifiedName(create.getName()) + .withStyle(create.getStyle()) + .withExperts( + EntityUtil.populateEntityReferences(getEntityReferences(Entity.USER, experts))); + dataProduct.withAssets(new ArrayList<>()); + for (EntityReference asset : listOrEmpty(create.getAssets())) { + asset = Entity.getEntityReference(asset, Include.NON_DELETED); + dataProduct.getAssets().add(asset); + dataProduct.getAssets().sort(EntityUtil.compareEntityReference); + } + return dataProduct; + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DataProductResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DataProductResource.java index f5a20bbe770a..1794f49d42c8 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DataProductResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DataProductResource.java @@ -13,7 +13,6 @@ package org.openmetadata.service.resources.domains; -import static org.openmetadata.common.utils.CommonUtil.listOrEmpty; import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty; import io.swagger.v3.oas.annotations.ExternalDocumentation; @@ -25,8 +24,6 @@ import io.swagger.v3.oas.annotations.parameters.RequestBody; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.tags.Tag; -import java.util.ArrayList; -import java.util.List; import java.util.UUID; import javax.json.JsonPatch; import javax.validation.Valid; @@ -64,7 +61,6 @@ import org.openmetadata.service.resources.Collection; import org.openmetadata.service.resources.EntityResource; import org.openmetadata.service.security.Authorizer; -import org.openmetadata.service.util.EntityUtil; import org.openmetadata.service.util.ResultList; @Slf4j @@ -79,6 +75,7 @@ @Collection(name = "dataProducts", order = 4) // initialize after user resource public class DataProductResource extends EntityResource { public static final String COLLECTION_PATH = "/v1/dataProducts/"; + private final DataProductMapper mapper = new DataProductMapper(); static final String FIELDS = "domain,owners,experts,assets,extension"; public DataProductResource(Authorizer authorizer, Limits limits) { @@ -285,7 +282,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDataProduct create) { - DataProduct dataProduct = getDataProduct(create, securityContext.getUserPrincipal().getName()); + DataProduct dataProduct = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, dataProduct); } @@ -309,7 +307,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDataProduct create) { - DataProduct dataProduct = getDataProduct(create, securityContext.getUserPrincipal().getName()); + DataProduct dataProduct = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, dataProduct); } @@ -464,22 +463,4 @@ public Response delete( String name) { return deleteByName(uriInfo, securityContext, name, true, true); } - - private DataProduct getDataProduct(CreateDataProduct create, String user) { - List experts = create.getExperts(); - DataProduct dataProduct = - repository - .copy(new DataProduct(), create, user) - .withFullyQualifiedName(create.getName()) - .withStyle(create.getStyle()) - .withExperts( - EntityUtil.populateEntityReferences(getEntityReferences(Entity.USER, experts))); - dataProduct.withAssets(new ArrayList<>()); - for (EntityReference asset : listOrEmpty(create.getAssets())) { - asset = Entity.getEntityReference(asset, Include.NON_DELETED); - dataProduct.getAssets().add(asset); - dataProduct.getAssets().sort(EntityUtil.compareEntityReference); - } - return dataProduct; - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DomainMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DomainMapper.java new file mode 100644 index 000000000000..fb4ef61c6cd6 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DomainMapper.java @@ -0,0 +1,28 @@ +package org.openmetadata.service.resources.domains; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; +import static org.openmetadata.service.util.EntityUtil.getEntityReferences; + +import java.util.List; +import org.openmetadata.schema.api.domains.CreateDomain; +import org.openmetadata.schema.entity.domains.Domain; +import org.openmetadata.schema.type.Include; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.util.EntityUtil; + +public class DomainMapper implements EntityMapper { + @Override + public Domain createToEntity(CreateDomain create, String user) { + List experts = create.getExperts(); + return copy(new Domain(), create, user) + .withStyle(create.getStyle()) + .withDomainType(create.getDomainType()) + .withFullyQualifiedName(create.getName()) + .withParent( + Entity.getEntityReference( + getEntityReference(Entity.DOMAIN, create.getParent()), Include.NON_DELETED)) + .withExperts( + EntityUtil.populateEntityReferences(getEntityReferences(Entity.USER, experts))); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DomainResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DomainResource.java index b416b2e35f69..f3b446ce4c98 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DomainResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/domains/DomainResource.java @@ -22,7 +22,6 @@ import io.swagger.v3.oas.annotations.parameters.RequestBody; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.tags.Tag; -import java.util.List; import java.util.UUID; import javax.json.JsonPatch; import javax.validation.Valid; @@ -49,7 +48,6 @@ import org.openmetadata.schema.entity.domains.Domain; import org.openmetadata.schema.type.ChangeEvent; import org.openmetadata.schema.type.EntityHistory; -import org.openmetadata.schema.type.Include; import org.openmetadata.schema.type.api.BulkAssets; import org.openmetadata.schema.type.api.BulkOperationResult; import org.openmetadata.service.Entity; @@ -59,7 +57,6 @@ import org.openmetadata.service.resources.Collection; import org.openmetadata.service.resources.EntityResource; import org.openmetadata.service.security.Authorizer; -import org.openmetadata.service.util.EntityUtil; import org.openmetadata.service.util.ResultList; @Slf4j @@ -73,6 +70,7 @@ @Collection(name = "domains", order = 4) // initialize after user resource public class DomainResource extends EntityResource { public static final String COLLECTION_PATH = "/v1/domains/"; + private final DomainMapper mapper = new DomainMapper(); static final String FIELDS = "children,owners,experts"; public DomainResource(Authorizer authorizer, Limits limits) { @@ -262,7 +260,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDomain create) { - Domain domain = getDomain(create, securityContext.getUserPrincipal().getName()); + Domain domain = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, domain); } @@ -286,7 +284,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDomain create) { - Domain domain = getDomain(create, securityContext.getUserPrincipal().getName()); + Domain domain = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, domain); } @@ -435,18 +433,4 @@ public Response delete( String name) { return deleteByName(uriInfo, securityContext, name, true, true); } - - private Domain getDomain(CreateDomain create, String user) { - List experts = create.getExperts(); - return repository - .copy(new Domain(), create, user) - .withStyle(create.getStyle()) - .withDomainType(create.getDomainType()) - .withFullyQualifiedName(create.getName()) - .withParent( - Entity.getEntityReference( - getEntityReference(Entity.DOMAIN, create.getParent()), Include.NON_DELETED)) - .withExperts( - EntityUtil.populateEntityReferences(getEntityReferences(Entity.USER, experts))); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseMapper.java new file mode 100644 index 000000000000..a23a13486305 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseMapper.java @@ -0,0 +1,27 @@ +package org.openmetadata.service.resources.dqtests; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; + +import org.openmetadata.schema.api.tests.CreateTestCase; +import org.openmetadata.schema.tests.TestCase; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.resources.feeds.MessageParser; + +public class TestCaseMapper implements EntityMapper { + @Override + public TestCase createToEntity(CreateTestCase create, String user) { + MessageParser.EntityLink entityLink = MessageParser.EntityLink.parse(create.getEntityLink()); + return copy(new TestCase(), create, user) + .withDescription(create.getDescription()) + .withName(create.getName()) + .withDisplayName(create.getDisplayName()) + .withParameterValues(create.getParameterValues()) + .withEntityLink(create.getEntityLink()) + .withComputePassedFailedRowCount(create.getComputePassedFailedRowCount()) + .withUseDynamicAssertion(create.getUseDynamicAssertion()) + .withEntityFQN(entityLink.getFullyQualifiedFieldValue()) + .withTestSuite(getEntityReference(Entity.TEST_SUITE, create.getTestSuite())) + .withTestDefinition(getEntityReference(Entity.TEST_DEFINITION, create.getTestDefinition())); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResolutionStatusMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResolutionStatusMapper.java new file mode 100644 index 000000000000..d62017b1d194 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResolutionStatusMapper.java @@ -0,0 +1,28 @@ +package org.openmetadata.service.resources.dqtests; + +import org.openmetadata.schema.api.tests.CreateTestCaseResolutionStatus; +import org.openmetadata.schema.entity.teams.User; +import org.openmetadata.schema.tests.TestCase; +import org.openmetadata.schema.tests.type.TestCaseResolutionStatus; +import org.openmetadata.schema.type.Include; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityTimeSeriesMapper; + +public class TestCaseResolutionStatusMapper + implements EntityTimeSeriesMapper { + @Override + public TestCaseResolutionStatus createToEntity( + CreateTestCaseResolutionStatus create, String user) { + TestCase testCaseEntity = + Entity.getEntityByName(Entity.TEST_CASE, create.getTestCaseReference(), null, Include.ALL); + User userEntity = Entity.getEntityByName(Entity.USER, user, null, Include.ALL); + + return new TestCaseResolutionStatus() + .withTimestamp(System.currentTimeMillis()) + .withTestCaseResolutionStatusType(create.getTestCaseResolutionStatusType()) + .withTestCaseResolutionStatusDetails(create.getTestCaseResolutionStatusDetails()) + .withUpdatedBy(userEntity.getEntityReference()) + .withUpdatedAt(System.currentTimeMillis()) + .withTestCaseReference(testCaseEntity.getEntityReference()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResolutionStatusResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResolutionStatusResource.java index 4a12f5eff632..1fa08546cd17 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResolutionStatusResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResolutionStatusResource.java @@ -33,11 +33,8 @@ import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.openmetadata.schema.api.tests.CreateTestCaseResolutionStatus; -import org.openmetadata.schema.entity.teams.User; -import org.openmetadata.schema.tests.TestCase; import org.openmetadata.schema.tests.type.TestCaseResolutionStatus; import org.openmetadata.schema.tests.type.TestCaseResolutionStatusTypes; -import org.openmetadata.schema.type.Include; import org.openmetadata.schema.type.MetadataOperation; import org.openmetadata.service.Entity; import org.openmetadata.service.jdbi3.ListFilter; @@ -63,6 +60,7 @@ public class TestCaseResolutionStatusResource extends EntityTimeSeriesResource { public static final String COLLECTION_PATH = "/v1/dataQuality/testCases/testCaseIncidentStatus"; + private TestCaseResolutionStatusMapper mapper = new TestCaseResolutionStatusMapper(); public TestCaseResolutionStatusResource(Authorizer authorizer) { super(Entity.TEST_CASE_RESOLUTION_STATUS, authorizer); @@ -232,20 +230,12 @@ public Response create( new OperationContext(Entity.TEST_CASE, MetadataOperation.EDIT_TESTS); ResourceContextInterface resourceContext = ReportDataContext.builder().build(); authorizer.authorize(securityContext, operationContext, resourceContext); - - TestCase testCaseEntity = - Entity.getEntityByName( - Entity.TEST_CASE, - createTestCaseResolutionStatus.getTestCaseReference(), - null, - Include.ALL); TestCaseResolutionStatus testCaseResolutionStatus = - getTestCaseResolutionStatus( - testCaseEntity, - createTestCaseResolutionStatus, - securityContext.getUserPrincipal().getName()); - - return create(testCaseResolutionStatus, testCaseEntity.getFullyQualifiedName()); + mapper.createToEntity( + createTestCaseResolutionStatus, securityContext.getUserPrincipal().getName()); + return create( + testCaseResolutionStatus, + testCaseResolutionStatus.getTestCaseReference().getFullyQualifiedName()); } @PATCH @@ -284,21 +274,4 @@ public Response patch( repository.patch(id, patch, securityContext.getUserPrincipal().getName()); return response.toResponse(); } - - private TestCaseResolutionStatus getTestCaseResolutionStatus( - TestCase testCaseEntity, - CreateTestCaseResolutionStatus createTestCaseResolutionStatus, - String userName) { - User userEntity = Entity.getEntityByName(Entity.USER, userName, null, Include.ALL); - - return new TestCaseResolutionStatus() - .withTimestamp(System.currentTimeMillis()) - .withTestCaseResolutionStatusType( - createTestCaseResolutionStatus.getTestCaseResolutionStatusType()) - .withTestCaseResolutionStatusDetails( - createTestCaseResolutionStatus.getTestCaseResolutionStatusDetails()) - .withUpdatedBy(userEntity.getEntityReference()) - .withUpdatedAt(System.currentTimeMillis()) - .withTestCaseReference(testCaseEntity.getEntityReference()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResource.java index 4e96e18eba93..48ad9ccd2768 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResource.java @@ -44,6 +44,7 @@ import org.openmetadata.schema.api.data.RestoreEntity; import org.openmetadata.schema.api.tests.CreateLogicalTestCases; import org.openmetadata.schema.api.tests.CreateTestCase; +import org.openmetadata.schema.api.tests.CreateTestCaseResult; import org.openmetadata.schema.entity.teams.User; import org.openmetadata.schema.tests.TestCase; import org.openmetadata.schema.tests.TestSuite; @@ -89,7 +90,8 @@ @Collection(name = "TestCases") public class TestCaseResource extends EntityResource { public static final String COLLECTION_PATH = "/v1/dataQuality/testCases"; - + private final TestCaseMapper mapper = new TestCaseMapper(); + private final TestCaseResultMapper testCaseResultMapper = new TestCaseResultMapper(); static final String FIELDS = "owners,testSuite,testDefinition,testSuites,incidentId,domain,tags"; static final String SEARCH_FIELDS_EXCLUDE = "testPlatforms,table,database,databaseSchema,service,testSuite,dataQualityDimension,testCaseType,originEntityFQN"; @@ -635,7 +637,7 @@ public Response create( // Override OperationContext to change the entity to table and operation from CREATE to // EDIT_TESTS EntityLink entityLink = EntityLink.parse(create.getEntityLink()); - TestCase test = getTestCase(create, securityContext.getUserPrincipal().getName(), entityLink); + TestCase test = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); OperationContext operationContext = new OperationContext(Entity.TABLE, MetadataOperation.EDIT_TESTS); ResourceContextInterface resourceContext = @@ -759,7 +761,7 @@ public Response createOrUpdate( OperationContext operationContext = new OperationContext(Entity.TABLE, MetadataOperation.EDIT_TESTS); authorizer.authorize(securityContext, operationContext, resourceContext); - TestCase test = getTestCase(create, securityContext.getUserPrincipal().getName(), entityLink); + TestCase test = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); repository.isTestSuiteExecutable(create.getTestSuite()); repository.prepareInternal(test, true); PutResponse response = repository.createOrUpdate(uriInfo, test); @@ -915,10 +917,29 @@ public Response addTestCaseResult( TestCase testCase = repository.findByName(fqn, Include.ALL); repository.deleteTestCaseFailedRowsSample(testCase.getId()); } - RestUtil.validateTimestampMilliseconds(testCaseResult.getTimestamp()); + // TODO: REMOVED ONCE DEPRECATED IN TEST CASE RESOURCE + CreateTestCaseResult createTestCaseResult = + new CreateTestCaseResult() + .withTimestamp(testCaseResult.getTimestamp()) + .withTestCaseStatus(testCaseResult.getTestCaseStatus()) + .withResult(testCaseResult.getResult()) + .withSampleData(testCaseResult.getSampleData()) + .withTestResultValue(testCaseResult.getTestResultValue()) + .withPassedRows(testCaseResult.getPassedRows()) + .withFailedRows(testCaseResult.getFailedRows()) + .withPassedRowsPercentage(testCaseResult.getPassedRowsPercentage()) + .withFailedRowsPercentage(testCaseResult.getFailedRowsPercentage()) + .withIncidentId(testCaseResult.getIncidentId()) + .withMaxBound(testCaseResult.getMaxBound()) + .withMinBound(testCaseResult.getMinBound()) + .withFqn(fqn); return repository .addTestCaseResult( - securityContext.getUserPrincipal().getName(), uriInfo, fqn, testCaseResult) + securityContext.getUserPrincipal().getName(), + uriInfo, + fqn, + testCaseResultMapper.createToEntity( + createTestCaseResult, securityContext.getUserPrincipal().getName())) .toResponse(); } @@ -1177,19 +1198,4 @@ protected static ResourceContextInterface getResourceContext( } return resourceContext; } - - private TestCase getTestCase(CreateTestCase create, String user, EntityLink entityLink) { - return repository - .copy(new TestCase(), create, user) - .withDescription(create.getDescription()) - .withName(create.getName()) - .withDisplayName(create.getDisplayName()) - .withParameterValues(create.getParameterValues()) - .withEntityLink(create.getEntityLink()) - .withComputePassedFailedRowCount(create.getComputePassedFailedRowCount()) - .withUseDynamicAssertion(create.getUseDynamicAssertion()) - .withEntityFQN(entityLink.getFullyQualifiedFieldValue()) - .withTestSuite(getEntityReference(Entity.TEST_SUITE, create.getTestSuite())) - .withTestDefinition(getEntityReference(Entity.TEST_DEFINITION, create.getTestDefinition())); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResultMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResultMapper.java new file mode 100644 index 000000000000..b38fd6964d8f --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResultMapper.java @@ -0,0 +1,36 @@ +package org.openmetadata.service.resources.dqtests; + +import static org.openmetadata.service.Entity.TEST_CASE; + +import java.util.UUID; +import org.openmetadata.schema.api.tests.CreateTestCaseResult; +import org.openmetadata.schema.tests.TestCase; +import org.openmetadata.schema.tests.type.TestCaseResult; +import org.openmetadata.schema.type.Include; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityTimeSeriesMapper; +import org.openmetadata.service.util.RestUtil; + +public class TestCaseResultMapper + implements EntityTimeSeriesMapper { + @Override + public TestCaseResult createToEntity(CreateTestCaseResult create, String user) { + TestCase testCase = Entity.getEntityByName(TEST_CASE, create.getFqn(), "", Include.ALL); + RestUtil.validateTimestampMilliseconds(create.getTimestamp()); + return new TestCaseResult() + .withId(UUID.randomUUID()) + .withTestCaseFQN(testCase.getFullyQualifiedName()) + .withTimestamp(create.getTimestamp()) + .withTestCaseStatus(create.getTestCaseStatus()) + .withResult(create.getResult()) + .withSampleData(create.getSampleData()) + .withTestResultValue(create.getTestResultValue()) + .withPassedRows(create.getPassedRows()) + .withFailedRows(create.getFailedRows()) + .withPassedRowsPercentage(create.getPassedRowsPercentage()) + .withFailedRowsPercentage(create.getFailedRowsPercentage()) + .withIncidentId(create.getIncidentId()) + .withMaxBound(create.getMaxBound()) + .withMinBound(create.getMinBound()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResultResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResultResource.java index 8561d314b3f9..080b7ddf1d1f 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResultResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestCaseResultResource.java @@ -67,6 +67,7 @@ @Collection(name = "TestCaseResults") public class TestCaseResultResource extends EntityTimeSeriesResource { + private final TestCaseResultMapper mapper = new TestCaseResultMapper(); static final String FIELDS = "testCase,testDefinition"; public TestCaseResultResource(Authorizer authorizer) { @@ -101,12 +102,17 @@ public Response addTestCaseResult( @PathParam("fqn") String fqn, @Valid CreateTestCaseResult createTestCaseResults) { + // Needed in further validation to check if the testCase exists + createTestCaseResults.withFqn(fqn); ResourceContextInterface resourceContext = TestCaseResourceContext.builder().name(fqn).build(); OperationContext operationContext = new OperationContext(Entity.TABLE, MetadataOperation.EDIT_TESTS); authorizer.authorize(securityContext, operationContext, resourceContext); return repository.addTestCaseResult( - securityContext.getUserPrincipal().getName(), uriInfo, fqn, createTestCaseResults); + securityContext.getUserPrincipal().getName(), + uriInfo, + fqn, + mapper.createToEntity(createTestCaseResults, securityContext.getUserPrincipal().getName())); } @GET diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestDefinitionMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestDefinitionMapper.java new file mode 100644 index 000000000000..1b587fd77474 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestDefinitionMapper.java @@ -0,0 +1,19 @@ +package org.openmetadata.service.resources.dqtests; + +import org.openmetadata.schema.api.tests.CreateTestDefinition; +import org.openmetadata.schema.tests.TestDefinition; +import org.openmetadata.service.mapper.EntityMapper; + +public class TestDefinitionMapper implements EntityMapper { + @Override + public TestDefinition createToEntity(CreateTestDefinition create, String user) { + return copy(new TestDefinition(), create, user) + .withDescription(create.getDescription()) + .withEntityType(create.getEntityType()) + .withTestPlatforms(create.getTestPlatforms()) + .withSupportedDataTypes(create.getSupportedDataTypes()) + .withDisplayName(create.getDisplayName()) + .withParameterDefinition(create.getParameterDefinition()) + .withName(create.getName()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestDefinitionResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestDefinitionResource.java index 9cd9126e1910..d59de3ccc2a1 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestDefinitionResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestDefinitionResource.java @@ -63,6 +63,7 @@ @Collection(name = "TestDefinitions") public class TestDefinitionResource extends EntityResource { + private final TestDefinitionMapper mapper = new TestDefinitionMapper(); public static final String COLLECTION_PATH = "/v1/dataQuality/testDefinitions"; static final String FIELDS = "owners"; @@ -314,7 +315,7 @@ public Response create( @Context SecurityContext securityContext, @Valid CreateTestDefinition create) { TestDefinition testDefinition = - getTestDefinition(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, testDefinition); } @@ -367,7 +368,7 @@ public Response createOrUpdate( @Context SecurityContext securityContext, @Valid CreateTestDefinition create) { TestDefinition testDefinition = - getTestDefinition(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, testDefinition); } @@ -452,16 +453,4 @@ public Response restoreTestDefinition( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private TestDefinition getTestDefinition(CreateTestDefinition create, String user) { - return repository - .copy(new TestDefinition(), create, user) - .withDescription(create.getDescription()) - .withEntityType(create.getEntityType()) - .withTestPlatforms(create.getTestPlatforms()) - .withSupportedDataTypes(create.getSupportedDataTypes()) - .withDisplayName(create.getDisplayName()) - .withParameterDefinition(create.getParameterDefinition()) - .withName(create.getName()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestSuiteMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestSuiteMapper.java new file mode 100644 index 000000000000..d0d364158057 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestSuiteMapper.java @@ -0,0 +1,31 @@ +package org.openmetadata.service.resources.dqtests; + +import org.openmetadata.schema.api.tests.CreateTestSuite; +import org.openmetadata.schema.entity.data.Table; +import org.openmetadata.schema.tests.TestSuite; +import org.openmetadata.schema.type.EntityReference; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class TestSuiteMapper implements EntityMapper { + @Override + public TestSuite createToEntity(CreateTestSuite create, String user) { + TestSuite testSuite = + copy(new TestSuite(), create, user) + .withDescription(create.getDescription()) + .withDisplayName(create.getDisplayName()) + .withName(create.getName()); + if (create.getExecutableEntityReference() != null) { + Table table = + Entity.getEntityByName(Entity.TABLE, create.getExecutableEntityReference(), null, null); + EntityReference entityReference = + new EntityReference() + .withId(table.getId()) + .withFullyQualifiedName(table.getFullyQualifiedName()) + .withName(table.getName()) + .withType(Entity.TABLE); + testSuite.setExecutableEntityReference(entityReference); + } + return testSuite; + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestSuiteResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestSuiteResource.java index bc4887c1d5d0..2b5585a9797f 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestSuiteResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/dqtests/TestSuiteResource.java @@ -40,12 +40,10 @@ import org.openmetadata.schema.EntityInterface; import org.openmetadata.schema.api.data.RestoreEntity; import org.openmetadata.schema.api.tests.CreateTestSuite; -import org.openmetadata.schema.entity.data.Table; import org.openmetadata.schema.tests.DataQualityReport; import org.openmetadata.schema.tests.TestSuite; import org.openmetadata.schema.tests.type.TestSummary; import org.openmetadata.schema.type.EntityHistory; -import org.openmetadata.schema.type.EntityReference; import org.openmetadata.schema.type.Include; import org.openmetadata.schema.type.MetadataOperation; import org.openmetadata.service.Entity; @@ -73,6 +71,7 @@ @Collection(name = "TestSuites") public class TestSuiteResource extends EntityResource { public static final String COLLECTION_PATH = "/v1/dataQuality/testSuites"; + private final TestSuiteMapper mapper = new TestSuiteMapper(); public static final String EXECUTABLE_TEST_SUITE_DELETION_ERROR = "Cannot delete logical test suite. To delete logical test suite, use DELETE /v1/dataQuality/testSuites/<...>"; public static final String NON_EXECUTABLE_TEST_SUITE_DELETION_ERROR = @@ -556,7 +555,8 @@ public Response create( create = create.withExecutableEntityReference( null); // entity reference is not applicable for logical test suites - TestSuite testSuite = getTestSuite(create, securityContext.getUserPrincipal().getName()); + TestSuite testSuite = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); testSuite.setExecutable(false); return create(uriInfo, securityContext, testSuite); } @@ -581,7 +581,8 @@ public Response createExecutable( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateTestSuite create) { - TestSuite testSuite = getTestSuite(create, securityContext.getUserPrincipal().getName()); + TestSuite testSuite = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); testSuite.setExecutable(true); return create(uriInfo, securityContext, testSuite); } @@ -637,7 +638,8 @@ public Response createOrUpdate( create = create.withExecutableEntityReference( null); // entity reference is not applicable for logical test suites - TestSuite testSuite = getTestSuite(create, securityContext.getUserPrincipal().getName()); + TestSuite testSuite = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); testSuite.setExecutable(false); return createOrUpdate(uriInfo, securityContext, testSuite); } @@ -662,7 +664,8 @@ public Response createOrUpdateExecutable( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateTestSuite create) { - TestSuite testSuite = getTestSuite(create, securityContext.getUserPrincipal().getName()); + TestSuite testSuite = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); testSuite.setExecutable(true); return createOrUpdate(uriInfo, securityContext, testSuite); } @@ -829,25 +832,4 @@ public Response restoreTestSuite( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private TestSuite getTestSuite(CreateTestSuite create, String user) { - TestSuite testSuite = - repository - .copy(new TestSuite(), create, user) - .withDescription(create.getDescription()) - .withDisplayName(create.getDisplayName()) - .withName(create.getName()); - if (create.getExecutableEntityReference() != null) { - Table table = - Entity.getEntityByName(Entity.TABLE, create.getExecutableEntityReference(), null, null); - EntityReference entityReference = - new EntityReference() - .withId(table.getId()) - .withFullyQualifiedName(table.getFullyQualifiedName()) - .withName(table.getName()) - .withType(Entity.TABLE); - testSuite.setExecutableEntityReference(entityReference); - } - return testSuite; - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionMapper.java new file mode 100644 index 000000000000..2892c2488030 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionMapper.java @@ -0,0 +1,46 @@ +package org.openmetadata.service.resources.events.subscription; + +import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty; +import static org.openmetadata.service.events.subscription.AlertUtil.validateAndBuildFilteringConditions; +import static org.openmetadata.service.fernet.Fernet.encryptWebhookSecretKey; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import org.openmetadata.schema.api.events.CreateEventSubscription; +import org.openmetadata.schema.entity.events.EventSubscription; +import org.openmetadata.schema.entity.events.SubscriptionDestination; +import org.openmetadata.service.mapper.EntityMapper; + +public class EventSubscriptionMapper + implements EntityMapper { + @Override + public EventSubscription createToEntity(CreateEventSubscription create, String user) { + return copy(new EventSubscription(), create, user) + .withAlertType(create.getAlertType()) + .withTrigger(create.getTrigger()) + .withEnabled(create.getEnabled()) + .withBatchSize(create.getBatchSize()) + .withFilteringRules( + validateAndBuildFilteringConditions( + create.getResources(), create.getAlertType(), create.getInput())) + .withDestinations(encryptWebhookSecretKey(getSubscriptions(create.getDestinations()))) + .withProvider(create.getProvider()) + .withRetries(create.getRetries()) + .withPollInterval(create.getPollInterval()) + .withInput(create.getInput()); + } + + private List getSubscriptions( + List subscriptions) { + List result = new ArrayList<>(); + subscriptions.forEach( + subscription -> { + if (nullOrEmpty(subscription.getId())) { + subscription.withId(UUID.randomUUID()); + } + result.add(subscription); + }); + return result; + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java index 51d943faa3b4..d81538868c14 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java @@ -16,8 +16,6 @@ import static org.openmetadata.common.utils.CommonUtil.listOrEmpty; import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty; import static org.openmetadata.schema.api.events.CreateEventSubscription.AlertType.NOTIFICATION; -import static org.openmetadata.service.events.subscription.AlertUtil.validateAndBuildFilteringConditions; -import static org.openmetadata.service.fernet.Fernet.encryptWebhookSecretKey; import io.swagger.v3.oas.annotations.ExternalDocumentation; import io.swagger.v3.oas.annotations.Operation; @@ -108,6 +106,7 @@ public class EventSubscriptionResource extends EntityResource { public static final String COLLECTION_PATH = "/v1/events/subscriptions"; public static final String FIELDS = "owners,filteringRules"; + private final EventSubscriptionMapper mapper = new EventSubscriptionMapper(); public EventSubscriptionResource(Authorizer authorizer, Limits limits) { super(Entity.EVENT_SUBSCRIPTION, authorizer, limits); @@ -296,7 +295,7 @@ public Response createEventSubscription( @Valid CreateEventSubscription request) throws SchedulerException { EventSubscription eventSub = - getEventSubscription(request, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(request, securityContext.getUserPrincipal().getName()); // Only one Creation is allowed Response response = create(uriInfo, securityContext, eventSub); EventSubscriptionScheduler.getInstance().addSubscriptionPublisher(eventSub); @@ -323,7 +322,7 @@ public Response createOrUpdateEventSubscription( @Context SecurityContext securityContext, @Valid CreateEventSubscription create) { EventSubscription eventSub = - getEventSubscription(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); Response response = createOrUpdate(uriInfo, securityContext, eventSub); EventSubscriptionScheduler.getInstance() .updateEventSubscription((EventSubscription) response.getEntity()); @@ -1354,36 +1353,6 @@ public Response sendTestMessageAlert( return Response.ok(resultDestinations).build(); } - private EventSubscription getEventSubscription(CreateEventSubscription create, String user) { - return repository - .copy(new EventSubscription(), create, user) - .withAlertType(create.getAlertType()) - .withTrigger(create.getTrigger()) - .withEnabled(create.getEnabled()) - .withBatchSize(create.getBatchSize()) - .withFilteringRules( - validateAndBuildFilteringConditions( - create.getResources(), create.getAlertType(), create.getInput())) - .withDestinations(encryptWebhookSecretKey(getSubscriptions(create.getDestinations()))) - .withProvider(create.getProvider()) - .withRetries(create.getRetries()) - .withPollInterval(create.getPollInterval()) - .withInput(create.getInput()); - } - - private List getSubscriptions( - List subscriptions) { - List result = new ArrayList<>(); - subscriptions.forEach( - subscription -> { - if (nullOrEmpty(subscription.getId())) { - subscription.withId(UUID.randomUUID()); - } - result.add(subscription); - }); - return result; - } - public static List getNotificationsFilterDescriptors() throws IOException { List entityNotificationDescriptors = diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/FeedMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/FeedMapper.java new file mode 100644 index 000000000000..541096507d11 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/FeedMapper.java @@ -0,0 +1,56 @@ +package org.openmetadata.service.resources.feeds; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.UUID; +import org.openmetadata.schema.api.CreateTaskDetails; +import org.openmetadata.schema.api.feed.CreateThread; +import org.openmetadata.schema.entity.feed.Thread; +import org.openmetadata.schema.type.EntityReference; +import org.openmetadata.schema.type.TaskDetails; +import org.openmetadata.schema.type.TaskStatus; +import org.openmetadata.service.Entity; + +public class FeedMapper { + public Thread createToEntity(CreateThread create, String user) { + UUID randomUUID = UUID.randomUUID(); + return new Thread() + .withId(randomUUID) + .withThreadTs(System.currentTimeMillis()) + .withMessage(create.getMessage()) + .withCreatedBy(create.getFrom()) + .withAbout(create.getAbout()) + .withAddressedTo(create.getAddressedTo()) + .withReactions(Collections.emptyList()) + .withType(create.getType()) + .withTask(getTaskDetails(create.getTaskDetails())) + .withAnnouncement(create.getAnnouncementDetails()) + .withChatbot(create.getChatbotDetails()) + .withUpdatedBy(user) + .withUpdatedAt(System.currentTimeMillis()) + .withEntityRef(new EntityReference().withId(randomUUID).withType(Entity.THREAD)) + .withGeneratedBy(Thread.GeneratedBy.USER); + } + + private TaskDetails getTaskDetails(CreateTaskDetails create) { + if (create != null) { + return new TaskDetails() + .withAssignees(formatAssignees(create.getAssignees())) + .withType(create.getType()) + .withStatus(TaskStatus.Open) + .withOldValue(create.getOldValue()) + .withSuggestion(create.getSuggestion()); + } + return null; + } + + public static List formatAssignees(List assignees) { + List result = new ArrayList<>(); + assignees.forEach( + assignee -> + result.add( + new EntityReference().withId(assignee.getId()).withType(assignee.getType()))); + return result; + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/FeedResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/FeedResource.java index a1f6565bfdaa..b998185addcd 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/FeedResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/FeedResource.java @@ -28,8 +28,6 @@ import io.swagger.v3.oas.annotations.parameters.RequestBody; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.tags.Tag; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.UUID; import javax.json.JsonPatch; @@ -52,7 +50,6 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.SecurityContext; import javax.ws.rs.core.UriInfo; -import org.openmetadata.schema.api.CreateTaskDetails; import org.openmetadata.schema.api.feed.CloseTask; import org.openmetadata.schema.api.feed.CreatePost; import org.openmetadata.schema.api.feed.CreateThread; @@ -62,7 +59,6 @@ import org.openmetadata.schema.type.EntityReference; import org.openmetadata.schema.type.MetadataOperation; import org.openmetadata.schema.type.Post; -import org.openmetadata.schema.type.TaskDetails; import org.openmetadata.schema.type.TaskStatus; import org.openmetadata.schema.type.ThreadType; import org.openmetadata.service.Entity; @@ -90,6 +86,8 @@ @Collection(name = "feeds") public class FeedResource { public static final String COLLECTION_PATH = "/v1/feed/"; + private final FeedMapper mapper = new FeedMapper(); + private final PostMapper postMapper = new PostMapper(); private final FeedRepository dao; private final Authorizer authorizer; @@ -421,7 +419,7 @@ public Response createThread( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateThread create) { - Thread thread = getThread(securityContext, create); + Thread thread = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); addHref(uriInfo, dao.create(thread)); return Response.created(thread.getHref()) .entity(thread) @@ -452,7 +450,7 @@ public Response addPost( @PathParam("id") UUID id, @Valid CreatePost createPost) { - Post post = getPost(createPost); + Post post = postMapper.createToEntity(createPost, securityContext.getUserPrincipal().getName()); Thread thread = addHref( uriInfo, dao.addPostToThread(id, post, securityContext.getUserPrincipal().getName())); @@ -588,54 +586,4 @@ public ResultList getPosts( UUID id) { return new ResultList<>(dao.listPosts(id)); } - - private Thread getThread(SecurityContext securityContext, CreateThread create) { - UUID randomUUID = UUID.randomUUID(); - return new Thread() - .withId(randomUUID) - .withThreadTs(System.currentTimeMillis()) - .withMessage(create.getMessage()) - .withCreatedBy(create.getFrom()) - .withAbout(create.getAbout()) - .withAddressedTo(create.getAddressedTo()) - .withReactions(Collections.emptyList()) - .withType(create.getType()) - .withTask(getTaskDetails(create.getTaskDetails())) - .withAnnouncement(create.getAnnouncementDetails()) - .withChatbot(create.getChatbotDetails()) - .withUpdatedBy(securityContext.getUserPrincipal().getName()) - .withUpdatedAt(System.currentTimeMillis()) - .withEntityRef(new EntityReference().withId(randomUUID).withType(Entity.THREAD)) - .withGeneratedBy(Thread.GeneratedBy.USER); - } - - private Post getPost(CreatePost create) { - return new Post() - .withId(UUID.randomUUID()) - .withMessage(create.getMessage()) - .withFrom(create.getFrom()) - .withReactions(Collections.emptyList()) - .withPostTs(System.currentTimeMillis()); - } - - private TaskDetails getTaskDetails(CreateTaskDetails create) { - if (create != null) { - return new TaskDetails() - .withAssignees(formatAssignees(create.getAssignees())) - .withType(create.getType()) - .withStatus(TaskStatus.Open) - .withOldValue(create.getOldValue()) - .withSuggestion(create.getSuggestion()); - } - return null; - } - - public static List formatAssignees(List assignees) { - List result = new ArrayList<>(); - assignees.forEach( - assignee -> - result.add( - new EntityReference().withId(assignee.getId()).withType(assignee.getType()))); - return result; - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/PostMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/PostMapper.java new file mode 100644 index 000000000000..2e281168f9d8 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/PostMapper.java @@ -0,0 +1,17 @@ +package org.openmetadata.service.resources.feeds; + +import java.util.Collections; +import java.util.UUID; +import org.openmetadata.schema.api.feed.CreatePost; +import org.openmetadata.schema.type.Post; + +public class PostMapper { + public Post createToEntity(CreatePost create, String user) { + return new Post() + .withId(UUID.randomUUID()) + .withMessage(create.getMessage()) + .withFrom(create.getFrom()) + .withReactions(Collections.emptyList()) + .withPostTs(System.currentTimeMillis()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/SuggestionMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/SuggestionMapper.java new file mode 100644 index 000000000000..c032cfa578a3 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/SuggestionMapper.java @@ -0,0 +1,72 @@ +package org.openmetadata.service.resources.feeds; + +import static org.openmetadata.common.utils.CommonUtil.listOrEmpty; + +import java.util.UUID; +import javax.ws.rs.core.Response; +import org.openmetadata.schema.api.feed.CreateSuggestion; +import org.openmetadata.schema.entity.feed.Suggestion; +import org.openmetadata.schema.type.Include; +import org.openmetadata.schema.type.SuggestionStatus; +import org.openmetadata.schema.type.SuggestionType; +import org.openmetadata.schema.type.TagLabel; +import org.openmetadata.sdk.exception.SuggestionException; +import org.openmetadata.service.Entity; +import org.openmetadata.service.resources.tags.TagLabelUtil; +import org.openmetadata.service.util.UserUtil; + +public class SuggestionMapper { + private final String INVALID_SUGGESTION_REQUEST = "INVALID_SUGGESTION_REQUEST"; + + public Suggestion createToEntity(CreateSuggestion create, String user) { + validate(create); + return new Suggestion() + .withId(UUID.randomUUID()) + .withDescription(create.getDescription()) + .withEntityLink(create.getEntityLink()) + .withType(create.getType()) + .withDescription(create.getDescription()) + .withTagLabels(create.getTagLabels()) + .withStatus(SuggestionStatus.Open) + .withCreatedBy(UserUtil.getUserOrBot(user)) + .withCreatedAt(System.currentTimeMillis()) + .withUpdatedBy(user) + .withUpdatedAt(System.currentTimeMillis()); + } + + private void validate(CreateSuggestion suggestion) { + if (suggestion.getEntityLink() == null) { + throw new SuggestionException( + Response.Status.BAD_REQUEST, + INVALID_SUGGESTION_REQUEST, + "Suggestion's entityLink cannot be null."); + } + MessageParser.EntityLink entityLink = + MessageParser.EntityLink.parse(suggestion.getEntityLink()); + Entity.getEntityReferenceByName( + entityLink.getEntityType(), entityLink.getEntityFQN(), Include.NON_DELETED); + + if (suggestion.getType() == SuggestionType.SuggestDescription) { + if (suggestion.getDescription() == null || suggestion.getDescription().isEmpty()) { + throw new SuggestionException( + Response.Status.BAD_REQUEST, + INVALID_SUGGESTION_REQUEST, + "Suggestion's description cannot be empty."); + } + } else if (suggestion.getType() == SuggestionType.SuggestTagLabel) { + if (suggestion.getTagLabels().isEmpty()) { + throw new SuggestionException( + Response.Status.BAD_REQUEST, + INVALID_SUGGESTION_REQUEST, + "Suggestion's tag label's cannot be empty."); + } else { + for (TagLabel label : listOrEmpty(suggestion.getTagLabels())) { + TagLabelUtil.applyTagCommonFields(label); + } + } + } else { + throw new SuggestionException( + Response.Status.BAD_REQUEST, INVALID_SUGGESTION_REQUEST, "Invalid Suggestion Type."); + } + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/SuggestionsResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/SuggestionsResource.java index 8a411075783e..131493dd36b5 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/SuggestionsResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/feeds/SuggestionsResource.java @@ -13,7 +13,6 @@ package org.openmetadata.service.resources.feeds; -import static org.openmetadata.common.utils.CommonUtil.listOrEmpty; import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty; import static org.openmetadata.schema.type.EventType.SUGGESTION_CREATED; import static org.openmetadata.schema.type.EventType.SUGGESTION_REJECTED; @@ -54,20 +53,16 @@ import org.openmetadata.schema.type.MetadataOperation; import org.openmetadata.schema.type.SuggestionStatus; import org.openmetadata.schema.type.SuggestionType; -import org.openmetadata.schema.type.TagLabel; -import org.openmetadata.sdk.exception.SuggestionException; import org.openmetadata.service.Entity; import org.openmetadata.service.jdbi3.SuggestionFilter; import org.openmetadata.service.jdbi3.SuggestionRepository; import org.openmetadata.service.resources.Collection; -import org.openmetadata.service.resources.tags.TagLabelUtil; import org.openmetadata.service.security.Authorizer; import org.openmetadata.service.security.policyevaluator.OperationContext; import org.openmetadata.service.security.policyevaluator.PostResourceContext; import org.openmetadata.service.security.policyevaluator.ResourceContextInterface; import org.openmetadata.service.util.RestUtil; import org.openmetadata.service.util.ResultList; -import org.openmetadata.service.util.UserUtil; @Path("/v1/suggestions") @Tag( @@ -79,9 +74,9 @@ @Collection(name = "suggestions") public class SuggestionsResource { public static final String COLLECTION_PATH = "/v1/suggestions/"; + private final SuggestionMapper mapper = new SuggestionMapper(); private final SuggestionRepository dao; private final Authorizer authorizer; - private final String INVALID_SUGGESTION_REQUEST = "INVALID_SUGGESTION_REQUEST"; public static void addHref(UriInfo uriInfo, List suggestions) { if (uriInfo != null) { @@ -410,7 +405,8 @@ public Response createSuggestion( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateSuggestion create) { - Suggestion suggestion = getSuggestion(securityContext, create); + Suggestion suggestion = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); addHref(uriInfo, dao.create(suggestion)); return Response.created(suggestion.getHref()) .entity(suggestion) @@ -479,56 +475,4 @@ public Response deleteSuggestions( return dao.deleteSuggestionsForAnEntity(entity, securityContext.getUserPrincipal().getName()) .toResponse(); } - - private Suggestion getSuggestion(SecurityContext securityContext, CreateSuggestion create) { - validate(create); - return new Suggestion() - .withId(UUID.randomUUID()) - .withDescription(create.getDescription()) - .withEntityLink(create.getEntityLink()) - .withType(create.getType()) - .withDescription(create.getDescription()) - .withTagLabels(create.getTagLabels()) - .withStatus(SuggestionStatus.Open) - .withCreatedBy(UserUtil.getUserOrBot(securityContext.getUserPrincipal().getName())) - .withCreatedAt(System.currentTimeMillis()) - .withUpdatedBy(securityContext.getUserPrincipal().getName()) - .withUpdatedAt(System.currentTimeMillis()); - } - - private void validate(CreateSuggestion suggestion) { - if (suggestion.getEntityLink() == null) { - throw new SuggestionException( - Response.Status.BAD_REQUEST, - INVALID_SUGGESTION_REQUEST, - "Suggestion's entityLink cannot be null."); - } - MessageParser.EntityLink entityLink = - MessageParser.EntityLink.parse(suggestion.getEntityLink()); - Entity.getEntityReferenceByName( - entityLink.getEntityType(), entityLink.getEntityFQN(), Include.NON_DELETED); - - if (suggestion.getType() == SuggestionType.SuggestDescription) { - if (suggestion.getDescription() == null || suggestion.getDescription().isEmpty()) { - throw new SuggestionException( - Response.Status.BAD_REQUEST, - INVALID_SUGGESTION_REQUEST, - "Suggestion's description cannot be empty."); - } - } else if (suggestion.getType() == SuggestionType.SuggestTagLabel) { - if (suggestion.getTagLabels().isEmpty()) { - throw new SuggestionException( - Response.Status.BAD_REQUEST, - INVALID_SUGGESTION_REQUEST, - "Suggestion's tag label's cannot be empty."); - } else { - for (TagLabel label : listOrEmpty(suggestion.getTagLabels())) { - TagLabelUtil.applyTagCommonFields(label); - } - } - } else { - throw new SuggestionException( - Response.Status.BAD_REQUEST, INVALID_SUGGESTION_REQUEST, "Invalid Suggestion Type."); - } - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryMapper.java new file mode 100644 index 000000000000..0a5a6f950d97 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryMapper.java @@ -0,0 +1,14 @@ +package org.openmetadata.service.resources.glossary; + +import org.openmetadata.schema.api.data.CreateGlossary; +import org.openmetadata.schema.entity.data.Glossary; +import org.openmetadata.service.mapper.EntityMapper; + +public class GlossaryMapper implements EntityMapper { + @Override + public Glossary createToEntity(CreateGlossary create, String user) { + return copy(new Glossary(), create, user) + .withProvider(create.getProvider()) + .withMutuallyExclusive(create.getMutuallyExclusive()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryResource.java index 2947dd330924..169a30a34c7a 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryResource.java @@ -79,6 +79,7 @@ public class GlossaryResource extends EntityResource { public static final String COLLECTION_PATH = "v1/glossaries/"; static final String FIELDS = "owners,tags,reviewers,usageCount,termCount,domain,extension"; + private final GlossaryMapper mapper = new GlossaryMapper(); public GlossaryResource(Authorizer authorizer, Limits limits) { super(Entity.GLOSSARY, authorizer, limits); @@ -296,7 +297,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateGlossary create) { - Glossary glossary = getGlossary(create, securityContext.getUserPrincipal().getName()); + Glossary glossary = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, glossary); } @@ -377,7 +378,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateGlossary create) { - Glossary glossary = getGlossary(create, securityContext.getUserPrincipal().getName()); + Glossary glossary = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, glossary); } @@ -607,16 +608,4 @@ public Response importCsvAsync( boolean dryRun) { return importCsvInternalAsync(securityContext, name, csv, dryRun); } - - private Glossary getGlossary(CreateGlossary create, String user) { - return getGlossary(repository, create, user); - } - - public static Glossary getGlossary( - GlossaryRepository repository, CreateGlossary create, String updatedBy) { - return repository - .copy(new Glossary(), create, updatedBy) - .withProvider(create.getProvider()) - .withMutuallyExclusive(create.getMutuallyExclusive()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryTermMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryTermMapper.java new file mode 100644 index 000000000000..47c6e494725d --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryTermMapper.java @@ -0,0 +1,24 @@ +package org.openmetadata.service.resources.glossary; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; +import static org.openmetadata.service.util.EntityUtil.getEntityReferences; + +import org.openmetadata.schema.api.data.CreateGlossaryTerm; +import org.openmetadata.schema.entity.data.GlossaryTerm; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class GlossaryTermMapper implements EntityMapper { + @Override + public GlossaryTerm createToEntity(CreateGlossaryTerm create, String user) { + return copy(new GlossaryTerm(), create, user) + .withSynonyms(create.getSynonyms()) + .withStyle(create.getStyle()) + .withGlossary(getEntityReference(Entity.GLOSSARY, create.getGlossary())) + .withParent(getEntityReference(Entity.GLOSSARY_TERM, create.getParent())) + .withRelatedTerms(getEntityReferences(Entity.GLOSSARY_TERM, create.getRelatedTerms())) + .withReferences(create.getReferences()) + .withProvider(create.getProvider()) + .withMutuallyExclusive(create.getMutuallyExclusive()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryTermResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryTermResource.java index e7673154bc39..11ffd396f8b9 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryTermResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryTermResource.java @@ -88,6 +88,8 @@ name = "glossaryTerms", order = 7) // Initialized after Glossary, Classification, and Tags public class GlossaryTermResource extends EntityResource { + private final GlossaryTermMapper mapper = new GlossaryTermMapper(); + private final GlossaryMapper glossaryMapper = new GlossaryMapper(); public static final String COLLECTION_PATH = "v1/glossaryTerms/"; static final String FIELDS = "children,relatedTerms,reviewers,owners,tags,usageCount,domain,extension,childrenCount"; @@ -126,8 +128,7 @@ public void initialize(OpenMetadataApplicationConfig config) throws IOException GLOSSARY, ".*json/data/glossary/.*Glossary\\.json$", LoadGlossary.class); for (LoadGlossary loadGlossary : loadGlossaries) { Glossary glossary = - GlossaryResource.getGlossary( - glossaryRepository, loadGlossary.getCreateGlossary(), ADMIN_USER_NAME); + glossaryMapper.createToEntity(loadGlossary.getCreateGlossary(), ADMIN_USER_NAME); glossary.setFullyQualifiedName(glossary.getName()); glossaryRepository.initializeEntity(glossary); @@ -135,7 +136,7 @@ public void initialize(OpenMetadataApplicationConfig config) throws IOException for (CreateGlossaryTerm createTerm : loadGlossary.getCreateTerms()) { createTerm.withGlossary(glossary.getName()); createTerm.withProvider(glossary.getProvider()); - GlossaryTerm term = getGlossaryTerm(createTerm, ADMIN_USER_NAME); + GlossaryTerm term = mapper.createToEntity(createTerm, ADMIN_USER_NAME); repository.setFullyQualifiedName(term); // FQN required for ordering tags based on hierarchy termsToCreate.add(term); } @@ -411,7 +412,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateGlossaryTerm create) { - GlossaryTerm term = getGlossaryTerm(create, securityContext.getUserPrincipal().getName()); + GlossaryTerm term = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, term); } @@ -493,7 +494,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateGlossaryTerm create) { - GlossaryTerm term = getGlossaryTerm(create, securityContext.getUserPrincipal().getName()); + GlossaryTerm term = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, term); } @@ -681,17 +682,4 @@ public Response restoreTable( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private GlossaryTerm getGlossaryTerm(CreateGlossaryTerm create, String user) { - return repository - .copy(new GlossaryTerm(), create, user) - .withSynonyms(create.getSynonyms()) - .withStyle(create.getStyle()) - .withGlossary(getEntityReference(Entity.GLOSSARY, create.getGlossary())) - .withParent(getEntityReference(Entity.GLOSSARY_TERM, create.getParent())) - .withRelatedTerms(getEntityReferences(Entity.GLOSSARY_TERM, create.getRelatedTerms())) - .withReferences(create.getReferences()) - .withProvider(create.getProvider()) - .withMutuallyExclusive(create.getMutuallyExclusive()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/governance/WorkflowDefinitionMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/governance/WorkflowDefinitionMapper.java new file mode 100644 index 000000000000..452ca49309f4 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/governance/WorkflowDefinitionMapper.java @@ -0,0 +1,18 @@ +package org.openmetadata.service.resources.governance; + +import org.openmetadata.schema.api.governance.CreateWorkflowDefinition; +import org.openmetadata.schema.governance.workflows.WorkflowDefinition; +import org.openmetadata.service.mapper.EntityMapper; + +public class WorkflowDefinitionMapper + implements EntityMapper { + @Override + public WorkflowDefinition createToEntity(CreateWorkflowDefinition create, String user) { + return copy(new WorkflowDefinition(), create, user) + .withFullyQualifiedName(create.getName()) + .withType(WorkflowDefinition.Type.fromValue(create.getType().toString())) + .withTrigger(create.getTrigger()) + .withNodes(create.getNodes()) + .withEdges(create.getEdges()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/governance/WorkflowDefinitionResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/governance/WorkflowDefinitionResource.java index f76306509325..73832c1313e9 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/governance/WorkflowDefinitionResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/governance/WorkflowDefinitionResource.java @@ -59,6 +59,7 @@ public class WorkflowDefinitionResource extends EntityResource { public static final String COLLECTION_PATH = "v1/governance/workflowDefinitions/"; static final String FIELDS = "owners"; + private final WorkflowDefinitionMapper mapper = new WorkflowDefinitionMapper(); public WorkflowDefinitionResource(Authorizer authorizer, Limits limits) { super(Entity.WORKFLOW_DEFINITION, authorizer, limits); @@ -277,7 +278,7 @@ public Response create( @Context SecurityContext securityContext, @Valid CreateWorkflowDefinition create) { WorkflowDefinition workflowDefinition = - getWorkflowDefinition(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, workflowDefinition); } @@ -359,7 +360,7 @@ public Response createOrUpdate( @Context SecurityContext securityContext, @Valid CreateWorkflowDefinition create) { WorkflowDefinition workflowDefinition = - getWorkflowDefinition(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, workflowDefinition); } @@ -475,15 +476,4 @@ public Response trigger( return Response.status(Response.Status.NOT_FOUND).entity(fqn).build(); } } - - private WorkflowDefinition getWorkflowDefinition(CreateWorkflowDefinition create, String user) { - // TODO: Validate the NodeType and NodeSubType. - return repository - .copy(new WorkflowDefinition(), create, user) - .withFullyQualifiedName(create.getName()) - .withType(WorkflowDefinition.Type.fromValue(create.getType().toString())) - .withTrigger(create.getTrigger()) - .withNodes(create.getNodes()) - .withEdges(create.getEdges()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/kpi/KpiMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/kpi/KpiMapper.java new file mode 100644 index 000000000000..6687a2ff765f --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/kpi/KpiMapper.java @@ -0,0 +1,22 @@ +package org.openmetadata.service.resources.kpi; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; + +import org.openmetadata.schema.api.dataInsight.kpi.CreateKpiRequest; +import org.openmetadata.schema.dataInsight.kpi.Kpi; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class KpiMapper implements EntityMapper { + @Override + public Kpi createToEntity(CreateKpiRequest create, String user) { + return copy(new Kpi(), create, user) + .withStartDate(create.getStartDate()) + .withEndDate(create.getEndDate()) + .withTargetValue(create.getTargetValue()) + .withDataInsightChart( + getEntityReference( + Entity.DATA_INSIGHT_CUSTOM_CHART, create.getDataInsightChart().value())) + .withMetricType(create.getMetricType()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/kpi/KpiResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/kpi/KpiResource.java index d47001dc6402..0f2dcdd69788 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/kpi/KpiResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/kpi/KpiResource.java @@ -62,6 +62,7 @@ @Collection(name = "kpi") public class KpiResource extends EntityResource { public static final String COLLECTION_PATH = "/v1/kpi"; + private final KpiMapper mapper = new KpiMapper(); static final String FIELDS = "owners,dataInsightChart,kpiResult"; @Override @@ -284,7 +285,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateKpiRequest create) { - Kpi kpi = getKpi(create, securityContext.getUserPrincipal().getName()); + Kpi kpi = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); // TODO fix this // dao.validateDataInsightChartOneToOneMapping(kpi.getDataInsightChart().getId()); return create(uriInfo, securityContext, kpi); @@ -365,7 +366,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateKpiRequest create) { - Kpi kpi = getKpi(create, securityContext.getUserPrincipal().getName()); + Kpi kpi = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, kpi); } @@ -507,16 +508,4 @@ public KpiResult listKpiResults( String name) { return repository.getKpiResult(name); } - - private Kpi getKpi(CreateKpiRequest create, String user) { - return repository - .copy(new Kpi(), create, user) - .withStartDate(create.getStartDate()) - .withEndDate(create.getEndDate()) - .withTargetValue(create.getTargetValue()) - .withDataInsightChart( - getEntityReference( - Entity.DATA_INSIGHT_CUSTOM_CHART, create.getDataInsightChart().value())) - .withMetricType(create.getMetricType()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/metrics/MetricMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/metrics/MetricMapper.java new file mode 100644 index 000000000000..473783f50634 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/metrics/MetricMapper.java @@ -0,0 +1,20 @@ +package org.openmetadata.service.resources.metrics; + +import static org.openmetadata.service.util.EntityUtil.getEntityReferences; + +import org.openmetadata.schema.api.data.CreateMetric; +import org.openmetadata.schema.entity.data.Metric; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class MetricMapper implements EntityMapper { + @Override + public Metric createToEntity(CreateMetric create, String user) { + return copy(new Metric(), create, user) + .withMetricExpression(create.getMetricExpression()) + .withGranularity(create.getGranularity()) + .withRelatedMetrics(getEntityReferences(Entity.METRIC, create.getRelatedMetrics())) + .withMetricType(create.getMetricType()) + .withUnitOfMeasurement(create.getUnitOfMeasurement()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/metrics/MetricResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/metrics/MetricResource.java index 4be491b81a58..b2e644498d7e 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/metrics/MetricResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/metrics/MetricResource.java @@ -73,6 +73,7 @@ @Collection(name = "metrics") public class MetricResource extends EntityResource { public static final String COLLECTION_PATH = "v1/metrics/"; + private final MetricMapper mapper = new MetricMapper(); static final String FIELDS = "owners,relatedMetrics,followers,tags,extension,domain,dataProducts"; public MetricResource(Authorizer authorizer, Limits limits) { @@ -279,7 +280,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateMetric create) { - Metric metric = getMetric(create, securityContext.getUserPrincipal().getName()); + Metric metric = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, metric); } @@ -302,7 +303,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateMetric create) { - Metric metric = getMetric(create, securityContext.getUserPrincipal().getName()); + Metric metric = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, metric); } @@ -519,14 +520,4 @@ public Response restore( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private Metric getMetric(CreateMetric create, String user) { - return repository - .copy(new Metric(), create, user) - .withMetricExpression(create.getMetricExpression()) - .withGranularity(create.getGranularity()) - .withRelatedMetrics(getEntityReferences(Entity.METRIC, create.getRelatedMetrics())) - .withMetricType(create.getMetricType()) - .withUnitOfMeasurement(create.getUnitOfMeasurement()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/mlmodels/MlModelMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/mlmodels/MlModelMapper.java new file mode 100644 index 000000000000..29f3358bf827 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/mlmodels/MlModelMapper.java @@ -0,0 +1,25 @@ +package org.openmetadata.service.resources.mlmodels; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; + +import org.openmetadata.schema.api.data.CreateMlModel; +import org.openmetadata.schema.entity.data.MlModel; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class MlModelMapper implements EntityMapper { + @Override + public MlModel createToEntity(CreateMlModel create, String user) { + return copy(new MlModel(), create, user) + .withService(getEntityReference(Entity.MLMODEL_SERVICE, create.getService())) + .withDashboard(getEntityReference(Entity.DASHBOARD, create.getDashboard())) + .withAlgorithm(create.getAlgorithm()) + .withMlFeatures(create.getMlFeatures()) + .withMlHyperParameters(create.getMlHyperParameters()) + .withMlStore(create.getMlStore()) + .withServer(create.getServer()) + .withTarget(create.getTarget()) + .withSourceUrl(create.getSourceUrl()) + .withSourceHash(create.getSourceHash()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/mlmodels/MlModelResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/mlmodels/MlModelResource.java index d0ed303ac86c..092e6c7489a9 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/mlmodels/MlModelResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/mlmodels/MlModelResource.java @@ -73,6 +73,7 @@ @Collection(name = "mlmodels") public class MlModelResource extends EntityResource { public static final String COLLECTION_PATH = "v1/mlmodels/"; + private final MlModelMapper mapper = new MlModelMapper(); static final String FIELDS = "owners,dashboard,followers,tags,usageSummary,extension,domain,sourceHash"; @@ -250,7 +251,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateMlModel create) { - MlModel mlModel = getMlModel(create, securityContext.getUserPrincipal().getName()); + MlModel mlModel = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, mlModel); } @@ -331,7 +332,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateMlModel create) { - MlModel mlModel = getMlModel(create, securityContext.getUserPrincipal().getName()); + MlModel mlModel = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, mlModel); } @@ -552,19 +553,4 @@ public Response restoreMlModel( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private MlModel getMlModel(CreateMlModel create, String user) { - return repository - .copy(new MlModel(), create, user) - .withService(getEntityReference(Entity.MLMODEL_SERVICE, create.getService())) - .withDashboard(getEntityReference(Entity.DASHBOARD, create.getDashboard())) - .withAlgorithm(create.getAlgorithm()) - .withMlFeatures(create.getMlFeatures()) - .withMlHyperParameters(create.getMlHyperParameters()) - .withMlStore(create.getMlStore()) - .withServer(create.getServer()) - .withTarget(create.getTarget()) - .withSourceUrl(create.getSourceUrl()) - .withSourceHash(create.getSourceHash()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/pipelines/PipelineMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/pipelines/PipelineMapper.java new file mode 100644 index 000000000000..78143bafdb0a --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/pipelines/PipelineMapper.java @@ -0,0 +1,23 @@ +package org.openmetadata.service.resources.pipelines; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; + +import org.openmetadata.schema.api.data.CreatePipeline; +import org.openmetadata.schema.entity.data.Pipeline; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class PipelineMapper implements EntityMapper { + @Override + public Pipeline createToEntity(CreatePipeline create, String user) { + return copy(new Pipeline(), create, user) + .withService(getEntityReference(Entity.PIPELINE_SERVICE, create.getService())) + .withTasks(create.getTasks()) + .withSourceUrl(create.getSourceUrl()) + .withConcurrency(create.getConcurrency()) + .withStartDate(create.getStartDate()) + .withPipelineLocation(create.getPipelineLocation()) + .withScheduleInterval(create.getScheduleInterval()) + .withSourceHash(create.getSourceHash()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/pipelines/PipelineResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/pipelines/PipelineResource.java index ee9228690c63..210a270975e6 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/pipelines/PipelineResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/pipelines/PipelineResource.java @@ -76,6 +76,7 @@ @Collection(name = "pipelines") public class PipelineResource extends EntityResource { public static final String COLLECTION_PATH = "v1/pipelines/"; + private final PipelineMapper mapper = new PipelineMapper(); static final String FIELDS = "owners,tasks,pipelineStatus,followers,tags,extension,scheduleInterval,domain,sourceHash"; @@ -311,7 +312,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreatePipeline create) { - Pipeline pipeline = getPipeline(create, securityContext.getUserPrincipal().getName()); + Pipeline pipeline = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, pipeline); } @@ -392,7 +393,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreatePipeline create) { - Pipeline pipeline = getPipeline(create, securityContext.getUserPrincipal().getName()); + Pipeline pipeline = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, pipeline); } @@ -669,17 +670,4 @@ public Response restorePipeline( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private Pipeline getPipeline(CreatePipeline create, String user) { - return repository - .copy(new Pipeline(), create, user) - .withService(getEntityReference(Entity.PIPELINE_SERVICE, create.getService())) - .withTasks(create.getTasks()) - .withSourceUrl(create.getSourceUrl()) - .withConcurrency(create.getConcurrency()) - .withStartDate(create.getStartDate()) - .withPipelineLocation(create.getPipelineLocation()) - .withScheduleInterval(create.getScheduleInterval()) - .withSourceHash(create.getSourceHash()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/policies/PolicyMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/policies/PolicyMapper.java new file mode 100644 index 000000000000..60b5aa6322d5 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/policies/PolicyMapper.java @@ -0,0 +1,20 @@ +package org.openmetadata.service.resources.policies; + +import org.openmetadata.schema.api.policies.CreatePolicy; +import org.openmetadata.schema.entity.policies.Policy; +import org.openmetadata.schema.type.EntityReference; +import org.openmetadata.service.mapper.EntityMapper; + +public class PolicyMapper implements EntityMapper { + @Override + public Policy createToEntity(CreatePolicy create, String user) { + Policy policy = + copy(new Policy(), create, user) + .withRules(create.getRules()) + .withEnabled(create.getEnabled()); + if (create.getLocation() != null) { + policy = policy.withLocation(new EntityReference().withId(create.getLocation())); + } + return policy; + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/policies/PolicyResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/policies/PolicyResource.java index 663ff9c556f7..411a489899b2 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/policies/PolicyResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/policies/PolicyResource.java @@ -51,7 +51,6 @@ import org.openmetadata.schema.entity.policies.Policy; import org.openmetadata.schema.entity.policies.accessControl.Rule; import org.openmetadata.schema.type.EntityHistory; -import org.openmetadata.schema.type.EntityReference; import org.openmetadata.schema.type.Function; import org.openmetadata.schema.type.Include; import org.openmetadata.schema.type.MetadataOperation; @@ -82,6 +81,7 @@ @Consumes(MediaType.APPLICATION_JSON) @Collection(name = "policies", order = 0, requiredForOps = true) public class PolicyResource extends EntityResource { + private final PolicyMapper mapper = new PolicyMapper(); public static final String COLLECTION_PATH = "v1/policies/"; public static final String FIELDS = "owners,location,teams,roles"; @@ -359,7 +359,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreatePolicy create) { - Policy policy = getPolicy(create, securityContext.getUserPrincipal().getName()); + Policy policy = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, policy); } @@ -439,7 +439,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreatePolicy create) { - Policy policy = getPolicy(create, securityContext.getUserPrincipal().getName()); + Policy policy = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, policy); } @@ -531,16 +531,4 @@ public void validateCondition( authorizer.authorizeAdmin(securityContext); CompiledRule.validateExpression(expression, Boolean.class); } - - private Policy getPolicy(CreatePolicy create, String user) { - Policy policy = - repository - .copy(new Policy(), create, user) - .withRules(create.getRules()) - .withEnabled(create.getEnabled()); - if (create.getLocation() != null) { - policy = policy.withLocation(new EntityReference().withId(create.getLocation())); - } - return policy; - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/query/QueryMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/query/QueryMapper.java new file mode 100644 index 000000000000..581e81679780 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/query/QueryMapper.java @@ -0,0 +1,29 @@ +package org.openmetadata.service.resources.query; + +import static org.openmetadata.service.Entity.USER; +import static org.openmetadata.service.util.EntityUtil.getEntityReference; +import static org.openmetadata.service.util.EntityUtil.getEntityReferences; + +import org.openmetadata.schema.api.data.CreateQuery; +import org.openmetadata.schema.entity.data.Query; +import org.openmetadata.schema.type.Votes; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.util.EntityUtil; + +public class QueryMapper implements EntityMapper { + @Override + public Query createToEntity(CreateQuery create, String user) { + return copy(new Query(), create, user) + .withQuery(create.getQuery()) + .withChecksum(EntityUtil.hash(create.getQuery())) + .withService(getEntityReference(Entity.DATABASE_SERVICE, create.getService())) + .withDuration(create.getDuration()) + .withVotes(new Votes().withUpVotes(0).withDownVotes(0)) + .withUsers(getEntityReferences(USER, create.getUsers())) + .withQueryUsedIn(EntityUtil.populateEntityReferences(create.getQueryUsedIn())) + .withQueryDate(create.getQueryDate()) + .withTriggeredBy(create.getTriggeredBy()) + .withProcessedLineage(create.getProcessedLineage()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/query/QueryResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/query/QueryResource.java index 1839fa8d4928..c82743da06e0 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/query/QueryResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/query/QueryResource.java @@ -1,7 +1,5 @@ package org.openmetadata.service.resources.query; -import static org.openmetadata.service.Entity.USER; - import io.swagger.v3.oas.annotations.ExternalDocumentation; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; @@ -42,7 +40,6 @@ import org.openmetadata.schema.type.EntityHistory; import org.openmetadata.schema.type.EntityReference; import org.openmetadata.schema.type.MetadataOperation; -import org.openmetadata.schema.type.Votes; import org.openmetadata.service.Entity; import org.openmetadata.service.jdbi3.ListFilter; import org.openmetadata.service.jdbi3.QueryRepository; @@ -52,7 +49,6 @@ import org.openmetadata.service.security.Authorizer; import org.openmetadata.service.security.mask.PIIMasker; import org.openmetadata.service.security.policyevaluator.OperationContext; -import org.openmetadata.service.util.EntityUtil; import org.openmetadata.service.util.ResultList; @Path("/v1/queries") @@ -64,6 +60,7 @@ @Consumes(MediaType.APPLICATION_JSON) @Collection(name = "queries") public class QueryResource extends EntityResource { + private final QueryMapper mapper = new QueryMapper(); public static final String COLLECTION_PATH = "v1/queries/"; static final String FIELDS = "owners,followers,users,votes,tags,queryUsedIn"; @@ -285,7 +282,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateQuery create) { - Query query = getQuery(create, securityContext.getUserPrincipal().getName()); + Query query = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, query); } @@ -309,7 +306,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateQuery create) { - Query query = getQuery(create, securityContext.getUserPrincipal().getName()); + Query query = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, query); } @@ -633,19 +630,4 @@ public Response delete( String fqn) { return deleteByName(uriInfo, securityContext, fqn, false, true); } - - private Query getQuery(CreateQuery create, String user) { - return repository - .copy(new Query(), create, user) - .withQuery(create.getQuery()) - .withChecksum(EntityUtil.hash(create.getQuery())) - .withService(getEntityReference(Entity.DATABASE_SERVICE, create.getService())) - .withDuration(create.getDuration()) - .withVotes(new Votes().withUpVotes(0).withDownVotes(0)) - .withUsers(getEntityReferences(USER, create.getUsers())) - .withQueryUsedIn(EntityUtil.populateEntityReferences(create.getQueryUsedIn())) - .withQueryDate(create.getQueryDate()) - .withTriggeredBy(create.getTriggeredBy()) - .withProcessedLineage(create.getProcessedLineage()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/searchindex/SearchIndexMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/searchindex/SearchIndexMapper.java new file mode 100644 index 000000000000..b7df4b11e337 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/searchindex/SearchIndexMapper.java @@ -0,0 +1,20 @@ +package org.openmetadata.service.resources.searchindex; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; + +import org.openmetadata.schema.api.data.CreateSearchIndex; +import org.openmetadata.schema.entity.data.SearchIndex; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class SearchIndexMapper implements EntityMapper { + @Override + public SearchIndex createToEntity(CreateSearchIndex create, String user) { + return copy(new SearchIndex(), create, user) + .withService(getEntityReference(Entity.SEARCH_SERVICE, create.getService())) + .withFields(create.getFields()) + .withSearchIndexSettings(create.getSearchIndexSettings()) + .withSourceHash(create.getSourceHash()) + .withIndexType(create.getIndexType()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/searchindex/SearchIndexResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/searchindex/SearchIndexResource.java index 24831f21253e..e65dea4cf4ad 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/searchindex/SearchIndexResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/searchindex/SearchIndexResource.java @@ -75,6 +75,7 @@ @Consumes(MediaType.APPLICATION_JSON) @Collection(name = "searchIndexes") public class SearchIndexResource extends EntityResource { + private final SearchIndexMapper mapper = new SearchIndexMapper(); public static final String COLLECTION_PATH = "v1/searchIndexes/"; static final String FIELDS = "owners,followers,tags,extension,domain,dataProducts,sourceHash"; @@ -309,7 +310,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateSearchIndex create) { - SearchIndex searchIndex = getSearchIndex(create, securityContext.getUserPrincipal().getName()); + SearchIndex searchIndex = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, searchIndex); } @@ -389,7 +391,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateSearchIndex create) { - SearchIndex searchIndex = getSearchIndex(create, securityContext.getUserPrincipal().getName()); + SearchIndex searchIndex = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, searchIndex); } @@ -626,16 +629,4 @@ public Response restoreSearchIndex( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private SearchIndex getSearchIndex(CreateSearchIndex create, String user) { - SearchIndex searchIndex = - repository - .copy(new SearchIndex(), create, user) - .withService(getEntityReference(Entity.SEARCH_SERVICE, create.getService())) - .withFields(create.getFields()) - .withSearchIndexSettings(create.getSearchIndexSettings()) - .withSourceHash(create.getSourceHash()) - .withIndexType(create.getIndexType()); - return searchIndex; - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/apiservices/APIServiceMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/apiservices/APIServiceMapper.java new file mode 100644 index 000000000000..9e5187875f61 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/apiservices/APIServiceMapper.java @@ -0,0 +1,14 @@ +package org.openmetadata.service.resources.services.apiservices; + +import org.openmetadata.schema.api.services.CreateApiService; +import org.openmetadata.schema.entity.services.ApiService; +import org.openmetadata.service.mapper.EntityMapper; + +public class APIServiceMapper implements EntityMapper { + @Override + public ApiService createToEntity(CreateApiService create, String user) { + return copy(new ApiService(), create, user) + .withServiceType(create.getServiceType()) + .withConnection(create.getConnection()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/apiservices/APIServiceResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/apiservices/APIServiceResource.java index 1a23c9b1c9a9..47f2345b06b0 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/apiservices/APIServiceResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/apiservices/APIServiceResource.java @@ -76,6 +76,7 @@ @Collection(name = "apiServices") public class APIServiceResource extends ServiceEntityResource { + private final APIServiceMapper mapper = new APIServiceMapper(); public static final String COLLECTION_PATH = "v1/services/apiServices/"; static final String FIELDS = "pipelines,owners,tags,domain"; @@ -345,7 +346,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateApiService create) { - ApiService service = getService(create, securityContext.getUserPrincipal().getName()); + ApiService service = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); Response response = create(uriInfo, securityContext, service); decryptOrNullify(securityContext, (ApiService) response.getEntity()); return response; @@ -370,7 +372,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateApiService update) { - ApiService service = getService(update, securityContext.getUserPrincipal().getName()); + ApiService service = + mapper.createToEntity(update, securityContext.getUserPrincipal().getName()); Response response = createOrUpdate(uriInfo, securityContext, unmask(service)); decryptOrNullify(securityContext, (ApiService) response.getEntity()); return response; @@ -513,13 +516,6 @@ public Response restoreAPIService( return restoreEntity(uriInfo, securityContext, restore.getId()); } - private ApiService getService(CreateApiService create, String user) { - return repository - .copy(new ApiService(), create, user) - .withServiceType(create.getServiceType()) - .withConnection(create.getConnection()); - } - @Override protected ApiService nullifyConnection(ApiService service) { return service.withConnection(null); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/dashboard/DashboardServiceMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/dashboard/DashboardServiceMapper.java new file mode 100644 index 000000000000..3ec9cab15750 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/dashboard/DashboardServiceMapper.java @@ -0,0 +1,15 @@ +package org.openmetadata.service.resources.services.dashboard; + +import org.openmetadata.schema.api.services.CreateDashboardService; +import org.openmetadata.schema.entity.services.DashboardService; +import org.openmetadata.service.mapper.EntityMapper; + +public class DashboardServiceMapper + implements EntityMapper { + @Override + public DashboardService createToEntity(CreateDashboardService create, String user) { + return copy(new DashboardService(), create, user) + .withServiceType(create.getServiceType()) + .withConnection(create.getConnection()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/dashboard/DashboardServiceResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/dashboard/DashboardServiceResource.java index fa72220f3ede..d000ac35775d 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/dashboard/DashboardServiceResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/dashboard/DashboardServiceResource.java @@ -72,6 +72,7 @@ public class DashboardServiceResource extends ServiceEntityResource< DashboardService, DashboardServiceRepository, DashboardConnection> { + private final DashboardServiceMapper mapper = new DashboardServiceMapper(); public static final String COLLECTION_PATH = "v1/services/dashboardServices"; static final String FIELDS = "owners,domain"; @@ -333,7 +334,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDashboardService create) { - DashboardService service = getService(create, securityContext.getUserPrincipal().getName()); + DashboardService service = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); Response response = create(uriInfo, securityContext, service); decryptOrNullify(securityContext, (DashboardService) response.getEntity()); return response; @@ -358,7 +360,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDashboardService update) { - DashboardService service = getService(update, securityContext.getUserPrincipal().getName()); + DashboardService service = + mapper.createToEntity(update, securityContext.getUserPrincipal().getName()); Response response = createOrUpdate(uriInfo, securityContext, unmask(service)); decryptOrNullify(securityContext, (DashboardService) response.getEntity()); return response; @@ -507,13 +510,6 @@ public Response restoreDashboardService( return restoreEntity(uriInfo, securityContext, restore.getId()); } - private DashboardService getService(CreateDashboardService create, String user) { - return repository - .copy(new DashboardService(), create, user) - .withServiceType(create.getServiceType()) - .withConnection(create.getConnection()); - } - @Override protected DashboardService nullifyConnection(DashboardService service) { return service.withConnection(null); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/database/DatabaseServiceMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/database/DatabaseServiceMapper.java new file mode 100644 index 000000000000..db8ac8818afd --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/database/DatabaseServiceMapper.java @@ -0,0 +1,14 @@ +package org.openmetadata.service.resources.services.database; + +import org.openmetadata.schema.api.services.CreateDatabaseService; +import org.openmetadata.schema.entity.services.DatabaseService; +import org.openmetadata.service.mapper.EntityMapper; + +public class DatabaseServiceMapper implements EntityMapper { + @Override + public DatabaseService createToEntity(CreateDatabaseService create, String user) { + return copy(new DatabaseService(), create, user) + .withServiceType(create.getServiceType()) + .withConnection(create.getConnection()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/database/DatabaseServiceResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/database/DatabaseServiceResource.java index adf3fd7b8d59..0faf6fd73139 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/database/DatabaseServiceResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/database/DatabaseServiceResource.java @@ -80,6 +80,7 @@ @Collection(name = "databaseServices") public class DatabaseServiceResource extends ServiceEntityResource { + private final DatabaseServiceMapper mapper = new DatabaseServiceMapper(); public static final String COLLECTION_PATH = "v1/services/databaseServices/"; static final String FIELDS = "pipelines,owners,tags,domain"; @@ -352,7 +353,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDatabaseService create) { - DatabaseService service = getService(create, securityContext.getUserPrincipal().getName()); + DatabaseService service = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); Response response = create(uriInfo, securityContext, service); decryptOrNullify(securityContext, (DatabaseService) response.getEntity()); return response; @@ -377,7 +379,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateDatabaseService update) { - DatabaseService service = getService(update, securityContext.getUserPrincipal().getName()); + DatabaseService service = + mapper.createToEntity(update, securityContext.getUserPrincipal().getName()); Response response = createOrUpdate(uriInfo, securityContext, unmask(service)); decryptOrNullify(securityContext, (DatabaseService) response.getEntity()); return response; @@ -642,13 +645,6 @@ public Response restoreDatabaseService( return restoreEntity(uriInfo, securityContext, restore.getId()); } - private DatabaseService getService(CreateDatabaseService create, String user) { - return repository - .copy(new DatabaseService(), create, user) - .withServiceType(create.getServiceType()) - .withConnection(create.getConnection()); - } - @Override protected DatabaseService nullifyConnection(DatabaseService service) { return service.withConnection(null); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/ingestionpipelines/IngestionPipelineMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/ingestionpipelines/IngestionPipelineMapper.java new file mode 100644 index 000000000000..e2134d9b11f7 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/ingestionpipelines/IngestionPipelineMapper.java @@ -0,0 +1,31 @@ +package org.openmetadata.service.resources.services.ingestionpipelines; + +import org.openmetadata.schema.api.services.ingestionPipelines.CreateIngestionPipeline; +import org.openmetadata.schema.entity.services.ingestionPipelines.IngestionPipeline; +import org.openmetadata.schema.services.connections.metadata.OpenMetadataConnection; +import org.openmetadata.service.OpenMetadataApplicationConfig; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.util.OpenMetadataConnectionBuilder; + +public class IngestionPipelineMapper + implements EntityMapper { + private final OpenMetadataApplicationConfig openMetadataApplicationConfig; + + public IngestionPipelineMapper(OpenMetadataApplicationConfig openMetadataApplicationConfig) { + this.openMetadataApplicationConfig = openMetadataApplicationConfig; + } + + @Override + public IngestionPipeline createToEntity(CreateIngestionPipeline create, String user) { + OpenMetadataConnection openMetadataServerConnection = + new OpenMetadataConnectionBuilder(openMetadataApplicationConfig).build(); + + return copy(new IngestionPipeline(), create, user) + .withPipelineType(create.getPipelineType()) + .withAirflowConfig(create.getAirflowConfig()) + .withOpenMetadataServerConnection(openMetadataServerConnection) + .withSourceConfig(create.getSourceConfig()) + .withLoggerLevel(create.getLoggerLevel()) + .withService(create.getService()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/ingestionpipelines/IngestionPipelineResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/ingestionpipelines/IngestionPipelineResource.java index 51757d6c9f6d..3a64d212caee 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/ingestionpipelines/IngestionPipelineResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/ingestionpipelines/IngestionPipelineResource.java @@ -97,6 +97,7 @@ @Collection(name = "IngestionPipelines") public class IngestionPipelineResource extends EntityResource { + private IngestionPipelineMapper mapper; public static final String COLLECTION_PATH = "v1/services/ingestionPipelines/"; private PipelineServiceClientInterface pipelineServiceClient; private OpenMetadataApplicationConfig openMetadataApplicationConfig; @@ -116,7 +117,7 @@ public IngestionPipelineResource(Authorizer authorizer, Limits limits) { @Override public void initialize(OpenMetadataApplicationConfig config) { this.openMetadataApplicationConfig = config; - + this.mapper = new IngestionPipelineMapper(config); this.pipelineServiceClient = PipelineServiceClientFactory.createPipelineServiceClient( config.getPipelineServiceClientConfiguration()); @@ -426,7 +427,7 @@ public Response create( @Context SecurityContext securityContext, @Valid CreateIngestionPipeline create) { IngestionPipeline ingestionPipeline = - getIngestionPipeline(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); Response response = create(uriInfo, securityContext, ingestionPipeline); validateProfileSample(ingestionPipeline); decryptOrNullify(securityContext, (IngestionPipeline) response.getEntity(), false); @@ -516,7 +517,7 @@ public Response createOrUpdate( @Context SecurityContext securityContext, @Valid CreateIngestionPipeline update) { IngestionPipeline ingestionPipeline = - getIngestionPipeline(update, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(update, securityContext.getUserPrincipal().getName()); unmask(ingestionPipeline); Response response = createOrUpdate(uriInfo, securityContext, ingestionPipeline); validateProfileSample(ingestionPipeline); @@ -936,20 +937,6 @@ public IngestionPipeline deletePipelineStatus( return addHref(uriInfo, ingestionPipeline); } - private IngestionPipeline getIngestionPipeline(CreateIngestionPipeline create, String user) { - OpenMetadataConnection openMetadataServerConnection = - new OpenMetadataConnectionBuilder(openMetadataApplicationConfig).build(); - - return repository - .copy(new IngestionPipeline(), create, user) - .withPipelineType(create.getPipelineType()) - .withAirflowConfig(create.getAirflowConfig()) - .withOpenMetadataServerConnection(openMetadataServerConnection) - .withSourceConfig(create.getSourceConfig()) - .withLoggerLevel(create.getLoggerLevel()) - .withService(create.getService()); - } - private void unmask(IngestionPipeline ingestionPipeline) { repository.setFullyQualifiedName(ingestionPipeline); IngestionPipeline originalIngestionPipeline = diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/messaging/MessagingServiceMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/messaging/MessagingServiceMapper.java new file mode 100644 index 000000000000..666ba51cccb5 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/messaging/MessagingServiceMapper.java @@ -0,0 +1,15 @@ +package org.openmetadata.service.resources.services.messaging; + +import org.openmetadata.schema.api.services.CreateMessagingService; +import org.openmetadata.schema.entity.services.MessagingService; +import org.openmetadata.service.mapper.EntityMapper; + +public class MessagingServiceMapper + implements EntityMapper { + @Override + public MessagingService createToEntity(CreateMessagingService create, String user) { + return copy(new MessagingService(), create, user) + .withConnection(create.getConnection()) + .withServiceType(create.getServiceType()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/messaging/MessagingServiceResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/messaging/MessagingServiceResource.java index 139861ecf301..beb48d97dda8 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/messaging/MessagingServiceResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/messaging/MessagingServiceResource.java @@ -72,6 +72,7 @@ public class MessagingServiceResource extends ServiceEntityResource< MessagingService, MessagingServiceRepository, MessagingConnection> { + private final MessagingServiceMapper mapper = new MessagingServiceMapper(); public static final String COLLECTION_PATH = "v1/services/messagingServices/"; public static final String FIELDS = "owners,domain"; @@ -339,7 +340,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateMessagingService create) { - MessagingService service = getService(create, securityContext.getUserPrincipal().getName()); + MessagingService service = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); Response response = create(uriInfo, securityContext, service); decryptOrNullify(securityContext, (MessagingService) response.getEntity()); return response; @@ -365,7 +367,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateMessagingService update) { - MessagingService service = getService(update, securityContext.getUserPrincipal().getName()); + MessagingService service = + mapper.createToEntity(update, securityContext.getUserPrincipal().getName()); Response response = createOrUpdate(uriInfo, securityContext, unmask(service)); decryptOrNullify(securityContext, (MessagingService) response.getEntity()); return response; @@ -513,13 +516,6 @@ public Response restoreMessagingService( return restoreEntity(uriInfo, securityContext, restore.getId()); } - private MessagingService getService(CreateMessagingService create, String user) { - return repository - .copy(new MessagingService(), create, user) - .withConnection(create.getConnection()) - .withServiceType(create.getServiceType()); - } - @Override protected MessagingService nullifyConnection(MessagingService service) { return service.withConnection(null); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/metadata/MetadataServiceMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/metadata/MetadataServiceMapper.java new file mode 100644 index 000000000000..a546eb60140e --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/metadata/MetadataServiceMapper.java @@ -0,0 +1,14 @@ +package org.openmetadata.service.resources.services.metadata; + +import org.openmetadata.schema.api.services.CreateMetadataService; +import org.openmetadata.schema.entity.services.MetadataService; +import org.openmetadata.service.mapper.EntityMapper; + +public class MetadataServiceMapper implements EntityMapper { + @Override + public MetadataService createToEntity(CreateMetadataService create, String user) { + return copy(new MetadataService(), create, user) + .withServiceType(create.getServiceType()) + .withConnection(create.getConnection()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/metadata/MetadataServiceResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/metadata/MetadataServiceResource.java index dc0ca583ad43..58b77643b4c0 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/metadata/MetadataServiceResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/metadata/MetadataServiceResource.java @@ -16,7 +16,6 @@ import java.util.List; import java.util.Objects; import java.util.UUID; -import java.util.stream.Collectors; import javax.json.JsonPatch; import javax.validation.Valid; import javax.validation.constraints.Max; @@ -75,6 +74,7 @@ @Collection(name = "metadataServices", order = 8) // init before IngestionPipelineService public class MetadataServiceResource extends ServiceEntityResource { + private final MetadataServiceMapper mapper = new MetadataServiceMapper(); public static final String OPENMETADATA_SERVICE = "OpenMetadata"; public static final String COLLECTION_PATH = "v1/services/metadataServices/"; public static final String FIELDS = "pipelines,owners,tags"; @@ -320,7 +320,7 @@ public EntityHistory listVersions( return json; } }) - .collect(Collectors.toList()); + .toList(); entityHistory.setVersions(versions); return entityHistory; } @@ -378,7 +378,7 @@ public Response create( @Context SecurityContext securityContext, @Valid CreateMetadataService create) { MetadataService service = - getMetadataService(create, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); Response response = create(uriInfo, securityContext, service); decryptOrNullify(securityContext, (MetadataService) response.getEntity()); return response; @@ -404,7 +404,7 @@ public Response createOrUpdate( @Context SecurityContext securityContext, @Valid CreateMetadataService update) { MetadataService service = - getMetadataService(update, securityContext.getUserPrincipal().getName()); + mapper.createToEntity(update, securityContext.getUserPrincipal().getName()); Response response = createOrUpdate(uriInfo, securityContext, unmask(service)); decryptOrNullify(securityContext, (MetadataService) response.getEntity()); return response; @@ -552,13 +552,6 @@ public Response restoreMetadataService( return restoreEntity(uriInfo, securityContext, restore.getId()); } - private MetadataService getMetadataService(CreateMetadataService create, String user) { - return repository - .copy(new MetadataService(), create, user) - .withServiceType(create.getServiceType()) - .withConnection(create.getConnection()); - } - @Override protected MetadataService nullifyConnection(MetadataService service) { return service.withConnection(null); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/mlmodel/MlModelServiceMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/mlmodel/MlModelServiceMapper.java new file mode 100644 index 000000000000..fac0635e55fa --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/mlmodel/MlModelServiceMapper.java @@ -0,0 +1,14 @@ +package org.openmetadata.service.resources.services.mlmodel; + +import org.openmetadata.schema.api.services.CreateMlModelService; +import org.openmetadata.schema.entity.services.MlModelService; +import org.openmetadata.service.mapper.EntityMapper; + +public class MlModelServiceMapper implements EntityMapper { + @Override + public MlModelService createToEntity(CreateMlModelService create, String user) { + return copy(new MlModelService(), create, user) + .withServiceType(create.getServiceType()) + .withConnection(create.getConnection()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/mlmodel/MlModelServiceResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/mlmodel/MlModelServiceResource.java index 5b70603806ed..bf1c389d5d28 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/mlmodel/MlModelServiceResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/mlmodel/MlModelServiceResource.java @@ -73,6 +73,7 @@ @Collection(name = "mlmodelServices") public class MlModelServiceResource extends ServiceEntityResource { + private final MlModelServiceMapper mapper = new MlModelServiceMapper(); public static final String COLLECTION_PATH = "v1/services/mlmodelServices/"; public static final String FIELDS = "pipelines,owners,tags,domain"; @@ -351,7 +352,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateMlModelService create) { - MlModelService service = getService(create, securityContext.getUserPrincipal().getName()); + MlModelService service = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); Response response = create(uriInfo, securityContext, service); decryptOrNullify(securityContext, (MlModelService) response.getEntity()); return response; @@ -377,7 +379,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateMlModelService update) { - MlModelService service = getService(update, securityContext.getUserPrincipal().getName()); + MlModelService service = + mapper.createToEntity(update, securityContext.getUserPrincipal().getName()); Response response = createOrUpdate(uriInfo, securityContext, unmask(service)); decryptOrNullify(securityContext, (MlModelService) response.getEntity()); return response; @@ -526,13 +529,6 @@ public Response restoreTable( return restoreEntity(uriInfo, securityContext, restore.getId()); } - private MlModelService getService(CreateMlModelService create, String user) { - return repository - .copy(new MlModelService(), create, user) - .withServiceType(create.getServiceType()) - .withConnection(create.getConnection()); - } - @Override protected MlModelService nullifyConnection(MlModelService service) { return service.withConnection(null); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/pipeline/PipelineServiceMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/pipeline/PipelineServiceMapper.java new file mode 100644 index 000000000000..a6eec765aa4a --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/pipeline/PipelineServiceMapper.java @@ -0,0 +1,14 @@ +package org.openmetadata.service.resources.services.pipeline; + +import org.openmetadata.schema.api.services.CreatePipelineService; +import org.openmetadata.schema.entity.services.PipelineService; +import org.openmetadata.service.mapper.EntityMapper; + +public class PipelineServiceMapper implements EntityMapper { + @Override + public PipelineService createToEntity(CreatePipelineService create, String user) { + return copy(new PipelineService(), create, user) + .withServiceType(create.getServiceType()) + .withConnection(create.getConnection()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/pipeline/PipelineServiceResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/pipeline/PipelineServiceResource.java index b6b5406ce008..1727413224f0 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/pipeline/PipelineServiceResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/pipeline/PipelineServiceResource.java @@ -71,6 +71,7 @@ @Collection(name = "pipelineServices") public class PipelineServiceResource extends ServiceEntityResource { + private final PipelineServiceMapper mapper = new PipelineServiceMapper(); public static final String COLLECTION_PATH = "v1/services/pipelineServices/"; static final String FIELDS = "pipelines,owners,domain"; @@ -352,7 +353,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreatePipelineService create) { - PipelineService service = getService(create, securityContext.getUserPrincipal().getName()); + PipelineService service = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); Response response = create(uriInfo, securityContext, service); decryptOrNullify(securityContext, (PipelineService) response.getEntity()); return response; @@ -378,7 +380,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreatePipelineService update) { - PipelineService service = getService(update, securityContext.getUserPrincipal().getName()); + PipelineService service = + mapper.createToEntity(update, securityContext.getUserPrincipal().getName()); Response response = createOrUpdate(uriInfo, securityContext, unmask(service)); decryptOrNullify(securityContext, (PipelineService) response.getEntity()); return response; @@ -529,13 +532,6 @@ public Response restorePipelineService( return restoreEntity(uriInfo, securityContext, restore.getId()); } - private PipelineService getService(CreatePipelineService create, String user) { - return repository - .copy(new PipelineService(), create, user) - .withServiceType(create.getServiceType()) - .withConnection(create.getConnection()); - } - @Override protected PipelineService nullifyConnection(PipelineService service) { return service.withConnection(null); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/searchIndexes/SearchServiceMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/searchIndexes/SearchServiceMapper.java new file mode 100644 index 000000000000..d4a220908eb1 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/searchIndexes/SearchServiceMapper.java @@ -0,0 +1,14 @@ +package org.openmetadata.service.resources.services.searchIndexes; + +import org.openmetadata.schema.api.services.CreateSearchService; +import org.openmetadata.schema.entity.services.SearchService; +import org.openmetadata.service.mapper.EntityMapper; + +public class SearchServiceMapper implements EntityMapper { + @Override + public SearchService createToEntity(CreateSearchService create, String user) { + return copy(new SearchService(), create, user) + .withServiceType(create.getServiceType()) + .withConnection(create.getConnection()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/searchIndexes/SearchServiceResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/searchIndexes/SearchServiceResource.java index 59584f45fbf1..a1f7c592f4aa 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/searchIndexes/SearchServiceResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/searchIndexes/SearchServiceResource.java @@ -63,6 +63,7 @@ @Collection(name = "searchServices") public class SearchServiceResource extends ServiceEntityResource { + private final SearchServiceMapper mapper = new SearchServiceMapper(); public static final String COLLECTION_PATH = "v1/services/searchServices/"; static final String FIELDS = "pipelines,owners,tags,domain"; @@ -333,7 +334,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateSearchService create) { - SearchService service = getService(create, securityContext.getUserPrincipal().getName()); + SearchService service = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); Response response = create(uriInfo, securityContext, service); decryptOrNullify(securityContext, (SearchService) response.getEntity()); return response; @@ -358,7 +360,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateSearchService update) { - SearchService service = getService(update, securityContext.getUserPrincipal().getName()); + SearchService service = + mapper.createToEntity(update, securityContext.getUserPrincipal().getName()); Response response = createOrUpdate(uriInfo, securityContext, unmask(service)); decryptOrNullify(securityContext, (SearchService) response.getEntity()); return response; @@ -502,13 +505,6 @@ public Response restoreSearchService( return restoreEntity(uriInfo, securityContext, restore.getId()); } - private SearchService getService(CreateSearchService create, String user) { - return repository - .copy(new SearchService(), create, user) - .withServiceType(create.getServiceType()) - .withConnection(create.getConnection()); - } - @Override protected SearchService nullifyConnection(SearchService service) { return service.withConnection(null); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/storage/StorageServiceMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/storage/StorageServiceMapper.java new file mode 100644 index 000000000000..4fb0c4aa02c3 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/storage/StorageServiceMapper.java @@ -0,0 +1,14 @@ +package org.openmetadata.service.resources.services.storage; + +import org.openmetadata.schema.api.services.CreateStorageService; +import org.openmetadata.schema.entity.services.StorageService; +import org.openmetadata.service.mapper.EntityMapper; + +public class StorageServiceMapper implements EntityMapper { + @Override + public StorageService createToEntity(CreateStorageService create, String user) { + return copy(new StorageService(), create, user) + .withServiceType(create.getServiceType()) + .withConnection(create.getConnection()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/storage/StorageServiceResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/storage/StorageServiceResource.java index cfbe55a5ca62..ed5e489099a2 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/storage/StorageServiceResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/services/storage/StorageServiceResource.java @@ -63,6 +63,7 @@ @Collection(name = "storageServices") public class StorageServiceResource extends ServiceEntityResource { + private final StorageServiceMapper mapper = new StorageServiceMapper(); public static final String COLLECTION_PATH = "v1/services/storageServices/"; static final String FIELDS = "pipelines,owners,tags,domain"; @@ -332,7 +333,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateStorageService create) { - StorageService service = getService(create, securityContext.getUserPrincipal().getName()); + StorageService service = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); Response response = create(uriInfo, securityContext, service); decryptOrNullify(securityContext, (StorageService) response.getEntity()); return response; @@ -357,7 +359,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateStorageService update) { - StorageService service = getService(update, securityContext.getUserPrincipal().getName()); + StorageService service = + mapper.createToEntity(update, securityContext.getUserPrincipal().getName()); Response response = createOrUpdate(uriInfo, securityContext, unmask(service)); decryptOrNullify(securityContext, (StorageService) response.getEntity()); return response; @@ -473,13 +476,6 @@ public Response restoreStorageService( return restoreEntity(uriInfo, securityContext, restore.getId()); } - private StorageService getService(CreateStorageService create, String user) { - return repository - .copy(new StorageService(), create, user) - .withServiceType(create.getServiceType()) - .withConnection(create.getConnection()); - } - @Override protected StorageService nullifyConnection(StorageService service) { return service.withConnection(null); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/storages/ContainerMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/storages/ContainerMapper.java new file mode 100644 index 000000000000..e16ea5865565 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/storages/ContainerMapper.java @@ -0,0 +1,25 @@ +package org.openmetadata.service.resources.storages; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; + +import org.openmetadata.schema.api.data.CreateContainer; +import org.openmetadata.schema.entity.data.Container; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class ContainerMapper implements EntityMapper { + @Override + public Container createToEntity(CreateContainer create, String user) { + return copy(new Container(), create, user) + .withService(getEntityReference(Entity.STORAGE_SERVICE, create.getService())) + .withParent(create.getParent()) + .withDataModel(create.getDataModel()) + .withPrefix(create.getPrefix()) + .withNumberOfObjects(create.getNumberOfObjects()) + .withSize(create.getSize()) + .withFullPath(create.getFullPath()) + .withFileFormats(create.getFileFormats()) + .withSourceUrl(create.getSourceUrl()) + .withSourceHash(create.getSourceHash()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/storages/ContainerResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/storages/ContainerResource.java index ae0f3a4edeac..806b18a893aa 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/storages/ContainerResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/storages/ContainerResource.java @@ -59,6 +59,7 @@ @Consumes(MediaType.APPLICATION_JSON) @Collection(name = "containers") public class ContainerResource extends EntityResource { + private final ContainerMapper mapper = new ContainerMapper(); public static final String COLLECTION_PATH = "v1/containers/"; static final String FIELDS = "parent,children,dataModel,owners,tags,followers,extension,domain,sourceHash"; @@ -239,7 +240,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateContainer create) { - Container container = getContainer(create, securityContext.getUserPrincipal().getName()); + Container container = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, container); } @@ -320,7 +322,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateContainer create) { - Container container = getContainer(create, securityContext.getUserPrincipal().getName()); + Container container = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, container); } @@ -543,19 +546,4 @@ public Response restoreContainer( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private Container getContainer(CreateContainer create, String user) { - return repository - .copy(new Container(), create, user) - .withService(getEntityReference(Entity.STORAGE_SERVICE, create.getService())) - .withParent(create.getParent()) - .withDataModel(create.getDataModel()) - .withPrefix(create.getPrefix()) - .withNumberOfObjects(create.getNumberOfObjects()) - .withSize(create.getSize()) - .withFullPath(create.getFullPath()) - .withFileFormats(create.getFileFormats()) - .withSourceUrl(create.getSourceUrl()) - .withSourceHash(create.getSourceHash()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/ClassificationMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/ClassificationMapper.java new file mode 100644 index 000000000000..b75fe17280d1 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/ClassificationMapper.java @@ -0,0 +1,15 @@ +package org.openmetadata.service.resources.tags; + +import org.openmetadata.schema.api.classification.CreateClassification; +import org.openmetadata.schema.entity.classification.Classification; +import org.openmetadata.service.mapper.EntityMapper; + +public class ClassificationMapper implements EntityMapper { + @Override + public Classification createToEntity(CreateClassification create, String user) { + return copy(new Classification(), create, user) + .withFullyQualifiedName(create.getName()) + .withProvider(create.getProvider()) + .withMutuallyExclusive(create.getMutuallyExclusive()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/ClassificationResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/ClassificationResource.java index 18a60fa1a986..e25160ca878e 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/ClassificationResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/ClassificationResource.java @@ -77,6 +77,7 @@ order = 4) // Initialize before TagResource, Glossary, and GlossaryTerms public class ClassificationResource extends EntityResource { + private final ClassificationMapper mapper = new ClassificationMapper(); public static final String TAG_COLLECTION_PATH = "/v1/classifications/"; static final String FIELDS = "usageCount,termCount"; @@ -302,7 +303,8 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateClassification create) { - Classification category = getClassification(create, securityContext); + Classification category = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, category); } @@ -315,7 +317,8 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateClassification create) { - Classification category = getClassification(create, securityContext); + Classification category = + mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, category); } @@ -447,18 +450,4 @@ public Response restore( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private Classification getClassification( - CreateClassification create, SecurityContext securityContext) { - return getClassification(repository, create, securityContext.getUserPrincipal().getName()); - } - - public static Classification getClassification( - ClassificationRepository repository, CreateClassification create, String updatedBy) { - return repository - .copy(new Classification(), create, updatedBy) - .withFullyQualifiedName(create.getName()) - .withProvider(create.getProvider()) - .withMutuallyExclusive(create.getMutuallyExclusive()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/TagMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/TagMapper.java new file mode 100644 index 000000000000..e45961dc8b17 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/TagMapper.java @@ -0,0 +1,21 @@ +package org.openmetadata.service.resources.tags; + +import static org.openmetadata.service.Entity.CLASSIFICATION; +import static org.openmetadata.service.Entity.TAG; +import static org.openmetadata.service.util.EntityUtil.getEntityReference; + +import org.openmetadata.schema.api.classification.CreateTag; +import org.openmetadata.schema.entity.classification.Tag; +import org.openmetadata.service.mapper.EntityMapper; + +public class TagMapper implements EntityMapper { + @Override + public Tag createToEntity(CreateTag create, String user) { + return copy(new Tag(), create, user) + .withStyle(create.getStyle()) + .withParent(getEntityReference(TAG, create.getParent())) + .withClassification(getEntityReference(CLASSIFICATION, create.getClassification())) + .withProvider(create.getProvider()) + .withMutuallyExclusive(create.getMutuallyExclusive()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/TagResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/TagResource.java index 1ce89ee5054b..0a0fe3d62c43 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/TagResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/TagResource.java @@ -15,7 +15,6 @@ import static org.openmetadata.service.Entity.ADMIN_USER_NAME; import static org.openmetadata.service.Entity.CLASSIFICATION; -import static org.openmetadata.service.Entity.TAG; import io.swagger.v3.oas.annotations.ExternalDocumentation; import io.swagger.v3.oas.annotations.Operation; @@ -91,6 +90,8 @@ name = "tags", order = 5) // initialize after Classification, and before Glossary and GlossaryTerm public class TagResource extends EntityResource { + private final ClassificationMapper classificationMapper = new ClassificationMapper(); + private final TagMapper mapper = new TagMapper(); public static final String TAG_COLLECTION_PATH = "/v1/tags/"; static final String FIELDS = "children,usageCount"; @@ -119,15 +120,14 @@ public void initialize(OpenMetadataApplicationConfig config) throws IOException CLASSIFICATION, ".*json/data/tags/.*\\.json$", LoadTags.class); for (LoadTags loadTags : loadTagsList) { Classification classification = - ClassificationResource.getClassification( - classificationRepository, loadTags.getCreateClassification(), ADMIN_USER_NAME); + classificationMapper.createToEntity(loadTags.getCreateClassification(), ADMIN_USER_NAME); classificationRepository.initializeEntity(classification); List tagsToCreate = new ArrayList<>(); for (CreateTag createTag : loadTags.getCreateTags()) { createTag.withClassification(classification.getName()); createTag.withProvider(classification.getProvider()); - Tag tag = getTag(createTag, ADMIN_USER_NAME); + Tag tag = mapper.createToEntity(createTag, ADMIN_USER_NAME); repository.setFullyQualifiedName(tag); // FQN required for ordering tags based on hierarchy tagsToCreate.add(tag); } @@ -352,7 +352,7 @@ public Tag getVersion( }) public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateTag create) { - Tag tag = getTag(securityContext, create); + Tag tag = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, tag); } @@ -430,7 +430,7 @@ public Response patch( }) public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateTag create) { - Tag tag = getTag(create, securityContext.getUserPrincipal().getName()); + Tag tag = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, tag); } @@ -563,18 +563,4 @@ public Tag addHref(UriInfo uriInfo, Tag tag) { Entity.withHref(uriInfo, tag.getParent()); return tag; } - - private Tag getTag(SecurityContext securityContext, CreateTag create) { - return getTag(create, securityContext.getUserPrincipal().getName()); - } - - private Tag getTag(CreateTag create, String updateBy) { - return repository - .copy(new Tag(), create, updateBy) - .withStyle(create.getStyle()) - .withParent(getEntityReference(TAG, create.getParent())) - .withClassification(getEntityReference(CLASSIFICATION, create.getClassification())) - .withProvider(create.getProvider()) - .withMutuallyExclusive(create.getMutuallyExclusive()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/PersonaMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/PersonaMapper.java new file mode 100644 index 000000000000..e33d6f7b441e --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/PersonaMapper.java @@ -0,0 +1,15 @@ +package org.openmetadata.service.resources.teams; + +import org.openmetadata.schema.api.teams.CreatePersona; +import org.openmetadata.schema.entity.teams.Persona; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.util.EntityUtil; + +public class PersonaMapper implements EntityMapper { + @Override + public Persona createToEntity(CreatePersona create, String user) { + return copy(new Persona(), create, user) + .withUsers(EntityUtil.toEntityReferences(create.getUsers(), Entity.USER)); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/PersonaResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/PersonaResource.java index 7f9db2ee7beb..e084f9131ea5 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/PersonaResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/PersonaResource.java @@ -46,7 +46,6 @@ import org.openmetadata.service.resources.Collection; import org.openmetadata.service.resources.EntityResource; import org.openmetadata.service.security.Authorizer; -import org.openmetadata.service.util.EntityUtil; import org.openmetadata.service.util.ResultList; @Slf4j @@ -60,6 +59,7 @@ @Consumes(MediaType.APPLICATION_JSON) @Collection(name = "personas", order = 2) public class PersonaResource extends EntityResource { + private final PersonaMapper mapper = new PersonaMapper(); public static final String COLLECTION_PATH = "/v1/personas"; static final String FIELDS = "users"; @@ -278,7 +278,7 @@ public Persona getVersion( }) public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreatePersona cp) { - Persona persona = getPersona(cp, securityContext.getUserPrincipal().getName()); + Persona persona = mapper.createToEntity(cp, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, persona); } @@ -299,7 +299,7 @@ public Response create( }) public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreatePersona cp) { - Persona persona = getPersona(cp, securityContext.getUserPrincipal().getName()); + Persona persona = mapper.createToEntity(cp, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, persona); } @@ -398,10 +398,4 @@ public Response delete( String name) { return deleteByName(uriInfo, securityContext, name, false, true); } - - private Persona getPersona(CreatePersona cp, String user) { - return repository - .copy(new Persona(), cp, user) - .withUsers(EntityUtil.toEntityReferences(cp.getUsers(), Entity.USER)); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleMapper.java new file mode 100644 index 000000000000..3042da9e5226 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleMapper.java @@ -0,0 +1,20 @@ +package org.openmetadata.service.resources.teams; + +import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty; +import static org.openmetadata.service.util.EntityUtil.getEntityReferences; + +import org.openmetadata.schema.api.teams.CreateRole; +import org.openmetadata.schema.entity.teams.Role; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class RoleMapper implements EntityMapper { + @Override + public Role createToEntity(CreateRole create, String user) { + if (nullOrEmpty(create.getPolicies())) { + throw new IllegalArgumentException("At least one policy is required to create a role"); + } + return copy(new Role(), create, user) + .withPolicies(getEntityReferences(Entity.POLICY, create.getPolicies())); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleResource.java index 9b5631c01707..aa3f2f519af6 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleResource.java @@ -13,8 +13,6 @@ package org.openmetadata.service.resources.teams; -import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty; - import io.dropwizard.jersey.PATCH; import io.swagger.v3.oas.annotations.ExternalDocumentation; import io.swagger.v3.oas.annotations.Operation; @@ -81,6 +79,7 @@ requiredForOps = true) // Load roles after PolicyResource are loaded at Order 0 @Slf4j public class RoleResource extends EntityResource { + private final RoleMapper mapper = new RoleMapper(); public static final String COLLECTION_PATH = "/v1/roles/"; public static final String FIELDS = "policies,teams,users"; @@ -335,7 +334,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateRole createRole) { - Role role = getRole(createRole, securityContext.getUserPrincipal().getName()); + Role role = mapper.createToEntity(createRole, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, role); } @@ -358,7 +357,7 @@ public Response createOrUpdateRole( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateRole createRole) { - Role role = getRole(createRole, securityContext.getUserPrincipal().getName()); + Role role = mapper.createToEntity(createRole, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, role); } @@ -487,18 +486,4 @@ public Response restoreRole( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private Role getRole(CreateRole create, String user) { - if (nullOrEmpty(create.getPolicies())) { - throw new IllegalArgumentException("At least one policy is required to create a role"); - } - return repository - .copy(new Role(), create, user) - .withPolicies(getEntityReferences(Entity.POLICY, create.getPolicies())); - } - - public static EntityReference getRole(String roleName) { - RoleRepository roleRepository = (RoleRepository) Entity.getEntityRepository(Entity.ROLE); - return roleRepository.getReferenceByName(roleName, Include.NON_DELETED); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/TeamMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/TeamMapper.java new file mode 100644 index 000000000000..6118c5ade5d7 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/TeamMapper.java @@ -0,0 +1,33 @@ +package org.openmetadata.service.resources.teams; + +import static org.openmetadata.service.exception.CatalogExceptionMessage.CREATE_GROUP; +import static org.openmetadata.service.exception.CatalogExceptionMessage.CREATE_ORGANIZATION; + +import org.openmetadata.schema.api.teams.CreateTeam; +import org.openmetadata.schema.entity.teams.Team; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.util.EntityUtil; + +public class TeamMapper implements EntityMapper { + @Override + public Team createToEntity(CreateTeam create, String user) { + if (create.getTeamType().equals(CreateTeam.TeamType.ORGANIZATION)) { + throw new IllegalArgumentException(CREATE_ORGANIZATION); + } + if (create.getTeamType().equals(CreateTeam.TeamType.GROUP) && create.getChildren() != null) { + throw new IllegalArgumentException(CREATE_GROUP); + } + 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)) + .withTeamType(create.getTeamType()) + .withParents(EntityUtil.toEntityReferences(create.getParents(), Entity.TEAM)) + .withChildren(EntityUtil.toEntityReferences(create.getChildren(), Entity.TEAM)) + .withPolicies(EntityUtil.toEntityReferences(create.getPolicies(), Entity.POLICY)) + .withEmail(create.getEmail()) + .withDomains(EntityUtil.getEntityReferences(Entity.DOMAIN, create.getDomains())); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/TeamResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/TeamResource.java index 6f0070dff28a..e3374cbca785 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/TeamResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/TeamResource.java @@ -14,8 +14,6 @@ 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; @@ -52,7 +50,6 @@ import lombok.extern.slf4j.Slf4j; import org.openmetadata.schema.api.data.RestoreEntity; import org.openmetadata.schema.api.teams.CreateTeam; -import org.openmetadata.schema.api.teams.CreateTeam.TeamType; import org.openmetadata.schema.entity.teams.Team; import org.openmetadata.schema.entity.teams.TeamHierarchy; import org.openmetadata.schema.type.ChangeEvent; @@ -73,7 +70,6 @@ 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; @@ -92,6 +88,7 @@ requiredForOps = true) // Load after roles, and policy resources public class TeamResource extends EntityResource { public static final String COLLECTION_PATH = "/v1/teams/"; + private final TeamMapper mapper = new TeamMapper(); static final String FIELDS = "owners,profile,users,owns,defaultRoles,parents,children,policies,userCount,childrenCount,domains"; @@ -378,7 +375,7 @@ public Team getVersion( }) public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateTeam ct) { - Team team = getTeam(ct, securityContext.getUserPrincipal().getName()); + Team team = mapper.createToEntity(ct, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, team); } @@ -399,7 +396,7 @@ public Response create( }) public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateTeam ct) { - Team team = getTeam(ct, securityContext.getUserPrincipal().getName()); + Team team = mapper.createToEntity(ct, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, team); } @@ -752,25 +749,4 @@ public Response importCsvAsync( String csv) { return importCsvInternalAsync(securityContext, name, csv, dryRun); } - - private Team getTeam(CreateTeam ct, String user) { - if (ct.getTeamType().equals(TeamType.ORGANIZATION)) { - throw new IllegalArgumentException(CREATE_ORGANIZATION); - } - if (ct.getTeamType().equals(TeamType.GROUP) && ct.getChildren() != null) { - throw new IllegalArgumentException(CREATE_GROUP); - } - return repository - .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)) - .withTeamType(ct.getTeamType()) - .withParents(EntityUtil.toEntityReferences(ct.getParents(), Entity.TEAM)) - .withChildren(EntityUtil.toEntityReferences(ct.getChildren(), Entity.TEAM)) - .withPolicies(EntityUtil.toEntityReferences(ct.getPolicies(), Entity.POLICY)) - .withEmail(ct.getEmail()) - .withDomains(EntityUtil.getEntityReferences(Entity.DOMAIN, ct.getDomains())); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/UserMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/UserMapper.java new file mode 100644 index 000000000000..10ce880b7dc9 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/UserMapper.java @@ -0,0 +1,33 @@ +package org.openmetadata.service.resources.teams; + +import java.util.UUID; +import org.openmetadata.schema.api.teams.CreateUser; +import org.openmetadata.schema.entity.teams.User; +import org.openmetadata.schema.utils.EntityInterfaceUtil; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; +import org.openmetadata.service.util.EntityUtil; + +public class UserMapper implements EntityMapper { + @Override + public User createToEntity(CreateUser create, String user) { + return new User() + .withId(UUID.randomUUID()) + .withName(create.getName().toLowerCase()) + .withFullyQualifiedName(EntityInterfaceUtil.quoteName(create.getName().toLowerCase())) + .withEmail(create.getEmail().toLowerCase()) + .withDescription(create.getDescription()) + .withDisplayName(create.getDisplayName()) + .withIsBot(create.getIsBot()) + .withIsAdmin(create.getIsAdmin()) + .withProfile(create.getProfile()) + .withPersonas(create.getPersonas()) + .withDefaultPersona(create.getDefaultPersona()) + .withTimezone(create.getTimezone()) + .withUpdatedBy(user.toLowerCase()) + .withUpdatedAt(System.currentTimeMillis()) + .withTeams(EntityUtil.toEntityReferences(create.getTeams(), Entity.TEAM)) + .withRoles(EntityUtil.toEntityReferences(create.getRoles(), Entity.ROLE)) + .withDomains(EntityUtil.getEntityReferences(Entity.DOMAIN, create.getDomains())); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/topics/TopicMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/topics/TopicMapper.java new file mode 100644 index 000000000000..470bfdb82840 --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/topics/TopicMapper.java @@ -0,0 +1,27 @@ +package org.openmetadata.service.resources.topics; + +import static org.openmetadata.service.util.EntityUtil.getEntityReference; + +import org.openmetadata.schema.api.data.CreateTopic; +import org.openmetadata.schema.entity.data.Topic; +import org.openmetadata.service.Entity; +import org.openmetadata.service.mapper.EntityMapper; + +public class TopicMapper implements EntityMapper { + @Override + public Topic createToEntity(CreateTopic create, String user) { + return copy(new Topic(), create, user) + .withService(getEntityReference(Entity.MESSAGING_SERVICE, create.getService())) + .withPartitions(create.getPartitions()) + .withMessageSchema(create.getMessageSchema()) + .withCleanupPolicies(create.getCleanupPolicies()) + .withMaximumMessageSize(create.getMaximumMessageSize()) + .withMinimumInSyncReplicas(create.getMinimumInSyncReplicas()) + .withRetentionSize(create.getRetentionSize()) + .withRetentionTime(create.getRetentionTime()) + .withReplicationFactor(create.getReplicationFactor()) + .withTopicConfig(create.getTopicConfig()) + .withSourceUrl(create.getSourceUrl()) + .withSourceHash(create.getSourceHash()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/topics/TopicResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/topics/TopicResource.java index a5a2916a34bf..8a7f5b46ae35 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/topics/TopicResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/topics/TopicResource.java @@ -77,6 +77,7 @@ @Collection(name = "topics") public class TopicResource extends EntityResource { public static final String COLLECTION_PATH = "v1/topics/"; + private final TopicMapper mapper = new TopicMapper(); static final String FIELDS = "owners,followers,tags,extension,domain,dataProducts,sourceHash"; @Override @@ -302,7 +303,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateTopic create) { - Topic topic = getTopic(create, securityContext.getUserPrincipal().getName()); + Topic topic = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, topic); } @@ -381,7 +382,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateTopic create) { - Topic topic = getTopic(create, securityContext.getUserPrincipal().getName()); + Topic topic = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, topic); } @@ -597,21 +598,4 @@ public Response restoreTopic( @Valid RestoreEntity restore) { return restoreEntity(uriInfo, securityContext, restore.getId()); } - - private Topic getTopic(CreateTopic create, String user) { - return repository - .copy(new Topic(), create, user) - .withService(getEntityReference(Entity.MESSAGING_SERVICE, create.getService())) - .withPartitions(create.getPartitions()) - .withMessageSchema(create.getMessageSchema()) - .withCleanupPolicies(create.getCleanupPolicies()) - .withMaximumMessageSize(create.getMaximumMessageSize()) - .withMinimumInSyncReplicas(create.getMinimumInSyncReplicas()) - .withRetentionSize(create.getRetentionSize()) - .withRetentionTime(create.getRetentionTime()) - .withReplicationFactor(create.getReplicationFactor()) - .withTopicConfig(create.getTopicConfig()) - .withSourceUrl(create.getSourceUrl()) - .withSourceHash(create.getSourceHash()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeMapper.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeMapper.java new file mode 100644 index 000000000000..e8b21b942f5c --- /dev/null +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeMapper.java @@ -0,0 +1,15 @@ +package org.openmetadata.service.resources.types; + +import org.openmetadata.schema.api.CreateType; +import org.openmetadata.schema.entity.Type; +import org.openmetadata.service.mapper.EntityMapper; + +public class TypeMapper implements EntityMapper { + @Override + public Type createToEntity(CreateType create, String user) { + return copy(new Type(), create, user) + .withFullyQualifiedName(create.getName()) + .withCategory(create.getCategory()) + .withSchema(create.getSchema()); + } +} diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeResource.java index 65c9b2a34364..dc90db9aea29 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/types/TypeResource.java @@ -84,6 +84,7 @@ @Slf4j public class TypeResource extends EntityResource { public static final String COLLECTION_PATH = "v1/metadata/types/"; + private final TypeMapper mapper = new TypeMapper(); public SchemaFieldExtractor extractor; @Override @@ -324,7 +325,7 @@ public Response create( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateType create) { - Type type = getType(create, securityContext.getUserPrincipal().getName()); + Type type = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return create(uriInfo, securityContext, type); } @@ -403,7 +404,7 @@ public Response createOrUpdate( @Context UriInfo uriInfo, @Context SecurityContext securityContext, @Valid CreateType create) { - Type type = getType(create, securityContext.getUserPrincipal().getName()); + Type type = mapper.createToEntity(create, securityContext.getUserPrincipal().getName()); return createOrUpdate(uriInfo, securityContext, type); } @@ -520,12 +521,4 @@ public Response getAllCustomPropertiesByEntityType( .build(); } } - - private Type getType(CreateType create, String user) { - return repository - .copy(new Type(), create, user) - .withFullyQualifiedName(create.getName()) - .withCategory(create.getCategory()) - .withSchema(create.getSchema()); - } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/Authorizer.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/Authorizer.java index 26dbe0babfde..f94193c0d817 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/Authorizer.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/Authorizer.java @@ -43,6 +43,8 @@ void authorize( void authorizeAdmin(SecurityContext securityContext); + void authorizeAdmin(String adminName); + void authorizeAdminOrBot(SecurityContext securityContext); boolean shouldMaskPasswords(SecurityContext securityContext); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/DefaultAuthorizer.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/DefaultAuthorizer.java index 51b0e32ca80e..a8636b53ad44 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/DefaultAuthorizer.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/DefaultAuthorizer.java @@ -101,6 +101,15 @@ public void authorizeAdmin(SecurityContext securityContext) { throw new AuthorizationException(notAdmin(securityContext.getUserPrincipal().getName())); } + @Override + public void authorizeAdmin(String adminName) { + SubjectContext subjectContext = SubjectContext.getSubjectContext(adminName); + if (subjectContext.isAdmin()) { + return; + } + throw new AuthorizationException(notAdmin(adminName)); + } + @Override public void authorizeAdminOrBot(SecurityContext securityContext) { SubjectContext subjectContext = getSubjectContext(securityContext); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/NoopAuthorizer.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/NoopAuthorizer.java index 7b9ed7ee6bd7..355bfe809905 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/NoopAuthorizer.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/NoopAuthorizer.java @@ -96,6 +96,11 @@ public void authorizeAdmin(SecurityContext securityContext) { /* Always authorize */ } + @Override + public void authorizeAdmin(String adminName) { + /* Always authorize */ + } + @Override public void authorizeAdminOrBot(SecurityContext securityContext) { /* Always authorize */ diff --git a/openmetadata-spec/src/main/resources/json/schema/api/tests/createTestCaseResult.json b/openmetadata-spec/src/main/resources/json/schema/api/tests/createTestCaseResult.json index 5e008a5e091d..d064bbbe6fde 100644 --- a/openmetadata-spec/src/main/resources/json/schema/api/tests/createTestCaseResult.json +++ b/openmetadata-spec/src/main/resources/json/schema/api/tests/createTestCaseResult.json @@ -6,6 +6,10 @@ "javaType": "org.openmetadata.schema.api.tests.CreateTestCaseResult", "type": "object", "properties": { + "fqn": { + "description": "Fqn of the test case against which this test case result is added.", + "type": "string" + }, "timestamp": { "description": "Data one which test case result is taken.", "$ref": "../../type/basic.json#/definitions/timestamp" diff --git a/openmetadata-ui/src/main/resources/ui/src/generated/api/tests/createTestCaseResult.ts b/openmetadata-ui/src/main/resources/ui/src/generated/api/tests/createTestCaseResult.ts index 80dae91b90fc..a34a653597f7 100644 --- a/openmetadata-ui/src/main/resources/ui/src/generated/api/tests/createTestCaseResult.ts +++ b/openmetadata-ui/src/main/resources/ui/src/generated/api/tests/createTestCaseResult.ts @@ -10,9 +10,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - - - /** +/** * Schema to create a new test case result . */ export interface CreateTestCaseResult { @@ -24,6 +22,10 @@ export interface CreateTestCaseResult { * Percentage of rows that failed. */ failedRowsPercentage?: number; + /** + * Fqn of the test case against which this test case result is added. + */ + fqn?: string; /** * Incident State ID associated with this result. This association happens when the result * is created, and will stay there even when the incident is resolved.