You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@atlas.apache.org by sa...@apache.org on 2019/12/05 20:58:29 UTC
[atlas] branch master updated: ATLAS-3549 Add a new REST endpoint
to get EntityHeader using unique attributes
This is an automated email from the ASF dual-hosted git repository.
sarath pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/atlas.git
The following commit(s) were added to refs/heads/master by this push:
new 1e0962d ATLAS-3549 Add a new REST endpoint to get EntityHeader using unique attributes
1e0962d is described below
commit 1e0962d70008cccfb9d4f1db624963faa74d0c19
Author: Mandar Ambawane <ma...@freestoneinfotech.com>
AuthorDate: Thu Dec 5 19:36:13 2019 +0530
ATLAS-3549 Add a new REST endpoint to get EntityHeader using unique attributes
Signed-off-by: Sarath Subramanian <sa...@apache.org>
---
.../repository/store/graph/AtlasEntityStore.java | 3 ++
.../store/graph/v2/AtlasEntityStoreV2.java | 27 ++++++++++++
.../java/org/apache/atlas/web/rest/EntityREST.java | 41 ++++++++++++++++++
.../apache/atlas/web/adapters/TestEntityREST.java | 49 ++++++++++++++++++++++
4 files changed, 120 insertions(+)
diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/AtlasEntityStore.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/AtlasEntityStore.java
index 49dd5c5..928c70d 100644
--- a/repository/src/main/java/org/apache/atlas/repository/store/graph/AtlasEntityStore.java
+++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/AtlasEntityStore.java
@@ -72,6 +72,9 @@ public interface AtlasEntityStore {
*/
AtlasEntityHeader getHeaderById(String guid) throws AtlasBaseException;
+
+ public AtlasEntityHeader getEntityHeaderByUniqueAttributes(AtlasEntityType entityType, Map<String, Object> uniqAttributes) throws AtlasBaseException;
+
/**
* Batch GET to retrieve entities by their ID
* @param guid
diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasEntityStoreV2.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasEntityStoreV2.java
index c8e65ef..bc4cc8b 100644
--- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasEntityStoreV2.java
+++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasEntityStoreV2.java
@@ -264,6 +264,33 @@ public class AtlasEntityStoreV2 implements AtlasEntityStore {
return ret;
}
+ @Override
+ @GraphTransaction
+ public AtlasEntityHeader getEntityHeaderByUniqueAttributes(AtlasEntityType entityType, Map<String, Object> uniqAttributes) throws AtlasBaseException {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("==> getEntityHeaderByUniqueAttributes({}, {})", entityType.getTypeName(), uniqAttributes);
+ }
+
+ AtlasVertex entityVertex = AtlasGraphUtilsV2.getVertexByUniqueAttributes(entityType, uniqAttributes);
+
+ EntityGraphRetriever entityRetriever = new EntityGraphRetriever(typeRegistry);
+
+ AtlasEntityHeader ret = entityRetriever.toAtlasEntityHeader(entityVertex);
+
+ if (ret == null) {
+ throw new AtlasBaseException(AtlasErrorCode.INSTANCE_BY_UNIQUE_ATTRIBUTE_NOT_FOUND, entityType.getTypeName(),
+ uniqAttributes.toString());
+ }
+
+ AtlasAuthorizationUtils.verifyAccess(new AtlasEntityAccessRequest(typeRegistry, AtlasPrivilege.ENTITY_READ, ret), "read entity: typeName=", entityType.getTypeName(), ", uniqueAttributes=", uniqAttributes);
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("<== getEntityHeaderByUniqueAttributes({}, {}): {}", entityType.getTypeName(), uniqAttributes, ret);
+ }
+
+ return ret;
+ }
+
/**
* Check state of entities in the store
* @param request AtlasCheckStateRequest
diff --git a/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java b/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
index 33f4828..6845121 100644
--- a/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
+++ b/webapp/src/main/java/org/apache/atlas/web/rest/EntityREST.java
@@ -149,6 +149,47 @@ public class EntityREST {
}
/**
+ * Fetch AtlasEntityHeader given its type and unique attribute.
+ *
+ * In addition to the typeName path parameter, attribute key-value pair(s) can be provided in the following format
+ *
+ * attr:<attrName>=<attrValue>
+ *
+ * NOTE: The attrName and attrValue should be unique across entities, eg. qualifiedName
+ *
+ * The REST request would look something like this
+ *
+ * GET /v2/entity/uniqueAttribute/type/aType/header?attr:aTypeAttribute=someValue
+ *
+ * @param typeName
+ * @return AtlasEntityHeader
+ * @throws AtlasBaseException
+ */
+ @GET
+ @Path("/uniqueAttribute/type/{typeName}/header")
+ public AtlasEntityHeader getEntityHeaderByUniqueAttributes(@PathParam("typeName") String typeName,
+ @Context HttpServletRequest servletRequest) throws AtlasBaseException {
+ Servlets.validateQueryParamLength("typeName", typeName);
+
+ AtlasPerfTracer perf = null;
+
+ try {
+ Map<String, Object> attributes = getAttributes(servletRequest);
+ if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
+ perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityREST.getEntityHeaderByUniqueAttributes(" + typeName + "," + attributes + ")");
+ }
+
+ AtlasEntityType entityType = ensureEntityType(typeName);
+
+ validateUniqueAttribute(entityType, attributes);
+
+ return entitiesStore.getEntityHeaderByUniqueAttributes(entityType, attributes);
+ } finally {
+ AtlasPerfTracer.log(perf);
+ }
+ }
+
+ /**
* Fetch complete definition of an entity given its type and unique attribute.
*
* In addition to the typeName path parameter, attribute key-value pair(s) can be provided in the following format
diff --git a/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntityREST.java b/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntityREST.java
index b747124..9584f99 100644
--- a/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntityREST.java
+++ b/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntityREST.java
@@ -110,6 +110,55 @@ public class TestEntityREST {
TestEntitiesREST.verifyAttributes(response.getEntity().getAttributes(), dbEntity.getAttributes());
}
+ @Test
+ public void testGetEntityHeaderByUniqueAttributes() throws Exception {
+ createTestEntity();
+
+ String[] attrVal = {String.valueOf(dbEntity.getAttribute("name"))};
+
+ Map<String, String[]> paramMap = new HashMap<>();
+ paramMap.put("attr:name", attrVal);
+
+ HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
+ Mockito.when(mockRequest.getParameterMap()).thenReturn(paramMap);
+
+ AtlasEntityHeader response = entityREST.getEntityHeaderByUniqueAttributes(dbEntity.getTypeName(), mockRequest);
+
+ Assert.assertNotNull(response);
+ Assert.assertEquals(dbEntity.getAttribute("name"), response.getAttribute("name"));
+ Assert.assertEquals(dbEntity.getAttribute("description"), response.getAttribute("description"));
+ }
+
+ @Test(expectedExceptions = AtlasBaseException.class)
+ public void testGetEntityHeaderByUniqueAttributes_2() throws Exception {
+ createTestEntity();
+
+ String[] attrVal = {String.valueOf(dbEntity.getAttribute("name") + "_2")};
+
+ Map<String, String[]> paramMap = new HashMap<>();
+ paramMap.put("attr:name", attrVal);
+
+ HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
+ Mockito.when(mockRequest.getParameterMap()).thenReturn(paramMap);
+
+ entityREST.getEntityHeaderByUniqueAttributes(dbEntity.getTypeName(), mockRequest);
+ }
+
+ @Test(expectedExceptions = AtlasBaseException.class)
+ public void testGetEntityHeaderByUniqueAttributes_3() throws Exception {
+ createTestEntity();
+
+ String[] attrVal = {String.valueOf(dbEntity.getAttribute("description"))};
+
+ Map<String, String[]> paramMap = new HashMap<>();
+ paramMap.put("attr:description", attrVal);
+
+ HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
+ Mockito.when(mockRequest.getParameterMap()).thenReturn(paramMap);
+
+ entityREST.getEntityHeaderByUniqueAttributes(dbEntity.getTypeName(), mockRequest);
+ }
+
@Test(dependsOnMethods = "testGetEntityById")
public void testAddAndGetClassification() throws Exception {