Fixed#6062: An API to obtain the association between Location and Table. (#6138)

* Fixed#6062: An API to obtain the association between Location and Table.

* Fixed#6062: An API to obtain the association between Location and Table.
This commit is contained in:
Parth Panchal 2022-07-19 15:43:58 +05:30 committed by GitHub
parent 0b2390bbd4
commit ad7c6ad0b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 0 deletions

View File

@ -443,6 +443,10 @@ public interface CollectionDAO {
List<EntityRelationshipRecord> findFrom(
@Bind("toId") String toId, @Bind("toEntity") String toEntity, @Bind("relation") int relation);
@SqlQuery("SELECT fromId, fromEntity, json FROM entity_relationship " + "WHERE toId = :toId ORDER BY fromId")
@RegisterRowMapper(FromRelationshipMapper.class)
List<EntityRelationshipRecord> findFrom(@Bind("toId") String toId);
//
// Delete Operations
//

View File

@ -897,6 +897,10 @@ public abstract class EntityRepository<T extends EntityInterface> {
.findFrom(toId.toString(), toEntityType, relationship.ordinal(), fromEntityType);
}
public List<EntityRelationshipRecord> findFrom(String toId) {
return daoCollection.relationshipDAO().findFrom(toId);
}
public EntityReference getContainer(UUID toId) throws IOException {
return getFromEntityRef(toId, Relationship.CONTAINS, null, true);
}

View File

@ -31,6 +31,7 @@ import org.openmetadata.catalog.resources.locations.LocationResource;
import org.openmetadata.catalog.type.EntityReference;
import org.openmetadata.catalog.type.Relationship;
import org.openmetadata.catalog.type.TagLabel;
import org.openmetadata.catalog.util.EntityUtil;
import org.openmetadata.catalog.util.EntityUtil.Fields;
import org.openmetadata.catalog.util.FullyQualifiedName;
import org.openmetadata.catalog.util.JsonUtils;
@ -209,6 +210,11 @@ public class LocationRepository extends EntityRepository<Location> {
CatalogExceptionMessage.invalidServiceEntity(service.getType(), Entity.LOCATION, STORAGE_SERVICE));
}
public List<EntityReference> getEntityDetails(String id) throws IOException {
List<CollectionDAO.EntityRelationshipRecord> records = findFrom(id);
return EntityUtil.getEntityReferences(records);
}
public void setService(Location location, EntityReference service) throws IOException {
if (service != null && location != null) {
service = getService(service); // Populate service details

View File

@ -60,6 +60,7 @@ import org.openmetadata.catalog.resources.EntityResource;
import org.openmetadata.catalog.security.Authorizer;
import org.openmetadata.catalog.type.ChangeEvent;
import org.openmetadata.catalog.type.EntityHistory;
import org.openmetadata.catalog.type.EntityReference;
import org.openmetadata.catalog.type.Include;
import org.openmetadata.catalog.util.EntityUtil.Fields;
import org.openmetadata.catalog.util.RestUtil;
@ -292,6 +293,33 @@ public class LocationResource extends EntityResource<Location, LocationRepositor
return getByNameInternal(uriInfo, securityContext, fqn, fieldsParam, include);
}
@GET
@Path("/association/{id}")
@Operation(
operationId = "getEntityByLocation",
summary = "Get a table associated with location",
tags = "locations",
description = "Get a table associated with location by given `id`",
responses = {
@ApiResponse(
responseCode = "200",
description = "location",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Location.class))),
@ApiResponse(responseCode = "404", description = "Location for instance {id} is not found")
})
public List<EntityReference> getTableFromLocation(
@Context UriInfo uriInfo,
@Context SecurityContext securityContext,
@Parameter(description = "location Id", schema = @Schema(type = "string")) @PathParam("id") String id,
@Parameter(
description = "location version number in the form `major`.`minor`",
schema = @Schema(type = "string", example = "0.1 or 1.1"))
@PathParam("version")
String version)
throws IOException {
return dao.getEntityDetails(id);
}
@GET
@Path("/{id}/versions/{version}")
@Operation(

View File

@ -25,8 +25,10 @@ import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.client.WebTarget;
@ -36,6 +38,7 @@ import org.apache.http.client.HttpResponseException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.openmetadata.catalog.CatalogApplicationTest;
import org.openmetadata.catalog.Entity;
import org.openmetadata.catalog.api.data.CreateLocation;
import org.openmetadata.catalog.entity.data.Location;
@ -103,6 +106,26 @@ public class LocationResourceTest extends EntityResourceTest<Location, CreateLoc
assertCommonFieldChange(fieldName, expected, actual);
}
private List<EntityReference> getAssociatedEntity(Location location) throws HttpResponseException {
WebTarget target = CatalogApplicationTest.getResource(String.format("locations/association/%s", location.getId()));
return (List<EntityReference>) TestUtils.get(target, List.class, ADMIN_AUTH_HEADERS);
}
@Test
void get_entity_from_location(TestInfo test) throws IOException {
Location location = createAndCheckEntity(createRequest(test), ADMIN_AUTH_HEADERS);
String actualValue = getAssociatedEntity(location).toString();
LinkedHashMap<Object, Object> expected = new LinkedHashMap<>();
expected.put("id", location.getService().getId());
expected.put("type", location.getService().getType());
expected.put("name", location.getService().getName());
expected.put("fullyQualifiedName", location.getService().getFullyQualifiedName());
expected.put("deleted", location.getService().getDeleted());
List<Map<Object, Object>> expectedValue = new ArrayList<>();
expectedValue.add(expected);
assertEquals(expectedValue.toString(), actualValue);
}
@Test
void get_locationListWithPrefix_2xx(TestInfo test) throws HttpResponseException {
// Create some nested locations.