Commit 1e0962d7 by Mandar Ambawane Committed by Sarath Subramanian

ATLAS-3549 Add a new REST endpoint to get EntityHeader using unique attributes

parent c95aba21
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment