use of org.apache.atlas.v1.model.instance.Referenceable in project atlas by apache.
the class EntityResource method getEntityDefinitionByAttribute.
/**
* Fetch the complete definition of an entity given its qualified name.
*
* @param entityType
* @param attribute
* @param value
*/
public Response getEntityDefinitionByAttribute(String entityType, String attribute, String value) {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Fetching entity definition for type={}, qualified name={}", entityType, value);
}
entityType = ParamChecker.notEmpty(entityType, "Entity type cannot be null");
attribute = ParamChecker.notEmpty(attribute, "attribute name cannot be null");
value = ParamChecker.notEmpty(value, "attribute value cannot be null");
Map<String, Object> attributes = new HashMap<>();
attributes.put(attribute, value);
AtlasEntityWithExtInfo entityInfo;
try {
entityInfo = entitiesStore.getByUniqueAttributes(getEntityType(entityType), attributes);
} catch (AtlasBaseException e) {
LOG.error("Cannot find entity with type: {}, attribute: {} and value: {}", entityType, attribute, value);
throw toWebApplicationException(e);
}
Referenceable entity = null;
if (entityInfo != null) {
entity = restAdapters.getReferenceable(entityInfo);
}
Map<String, Object> response = new HashMap<>();
response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
Response.Status status = Response.Status.NOT_FOUND;
if (entity != null) {
response.put(AtlasClient.DEFINITION, entity);
status = Response.Status.OK;
} else {
response.put(AtlasClient.ERROR, Servlets.escapeJsonString(String.format("An entity with type={%s}, " + "qualifiedName={%s} does not exist", entityType, value)));
}
return Response.status(status).entity(AtlasJson.toV1Json(response)).build();
} catch (AtlasBaseException e) {
LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
throw toWebApplicationException(e);
} catch (IllegalArgumentException e) {
LOG.error("Bad type={}, qualifiedName={}", entityType, value, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
} catch (WebApplicationException e) {
LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
throw e;
} catch (Throwable e) {
LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
}
use of org.apache.atlas.v1.model.instance.Referenceable in project atlas by apache.
the class EntityNotificationIT method testDeleteEntity.
public void testDeleteEntity() throws Exception {
final String tableName = "table-" + randomString();
final String dbName = "db-" + randomString();
final Referenceable HiveDBInstance = createHiveDBInstanceBuiltIn(dbName);
final Id dbId = createInstance(HiveDBInstance);
final Referenceable tableInstance = createHiveTableInstanceBuiltIn(dbName, tableName, dbId);
final Id tableId = createInstance(tableInstance);
final String guid = tableId._getId();
waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(OperationType.ENTITY_CREATE, HIVE_TABLE_TYPE_BUILTIN, guid));
final String name = (String) tableInstance.get(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME);
atlasClientV1.deleteEntity(HIVE_TABLE_TYPE_BUILTIN, AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, name);
waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(OperationType.ENTITY_DELETE, HIVE_TABLE_TYPE_BUILTIN, guid));
}
use of org.apache.atlas.v1.model.instance.Referenceable in project atlas by apache.
the class EntityNotificationIT method testAddTrait.
public void testAddTrait() throws Exception {
String superSuperTraitName = "SuperTrait" + randomString();
String superTraitName = "SuperTrait" + randomString();
traitName = "Trait" + randomString();
createTrait(superSuperTraitName);
createTrait(superTraitName, superSuperTraitName);
createTrait(traitName, superTraitName);
Struct traitInstance = new Struct(traitName);
String traitInstanceJSON = AtlasType.toV1Json(traitInstance);
LOG.debug("Trait instance = {}", traitInstanceJSON);
final String guid = tableId._getId();
atlasClientV1.addTrait(guid, traitInstance);
EntityNotificationV1 entityNotification = waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(OperationType.TRAIT_ADD, HIVE_TABLE_TYPE_BUILTIN, guid));
Referenceable entity = entityNotification.getEntity();
assertTrue(entity.getTraitNames().contains(traitName));
List<Struct> allTraits = entityNotification.getAllTraits();
List<String> allTraitNames = new LinkedList<>();
for (Struct struct : allTraits) {
allTraitNames.add(struct.getTypeName());
}
assertTrue(allTraitNames.contains(traitName));
assertTrue(allTraitNames.contains(superTraitName));
assertTrue(allTraitNames.contains(superSuperTraitName));
String anotherTraitName = "Trait" + randomString();
createTrait(anotherTraitName, superTraitName);
traitInstance = new Struct(anotherTraitName);
traitInstanceJSON = AtlasType.toV1Json(traitInstance);
LOG.debug("Trait instance = {}", traitInstanceJSON);
atlasClientV1.addTrait(guid, traitInstance);
entityNotification = waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(OperationType.TRAIT_ADD, HIVE_TABLE_TYPE_BUILTIN, guid));
allTraits = entityNotification.getAllTraits();
allTraitNames = new LinkedList<>();
for (Struct struct : allTraits) {
allTraitNames.add(struct.getTypeName());
}
assertTrue(allTraitNames.contains(traitName));
assertTrue(allTraitNames.contains(anotherTraitName));
// verify that the super type shows up twice in all traits
assertEquals(2, Collections.frequency(allTraitNames, superTraitName));
}
use of org.apache.atlas.v1.model.instance.Referenceable in project atlas by apache.
the class NotificationHookConsumerIT method testUpdateEntityPartial.
@Test
public void testUpdateEntityPartial() throws Exception {
final Referenceable entity = new Referenceable(DATABASE_TYPE_BUILTIN);
final String dbName = "db" + randomString();
entity.set(NAME, dbName);
entity.set(DESCRIPTION, randomString());
entity.set(QUALIFIED_NAME, dbName);
entity.set(CLUSTER_NAME, randomString());
atlasClientV1.createEntity(entity);
final Referenceable newEntity = new Referenceable(DATABASE_TYPE_BUILTIN);
newEntity.set("owner", randomString());
sendHookMessage(new EntityPartialUpdateRequest(TEST_USER, DATABASE_TYPE_BUILTIN, QUALIFIED_NAME, dbName, newEntity));
waitFor(MAX_WAIT_TIME, new Predicate() {
@Override
public boolean evaluate() throws Exception {
Referenceable localEntity = atlasClientV1.getEntity(DATABASE_TYPE_BUILTIN, QUALIFIED_NAME, dbName);
return (localEntity.get("owner") != null && localEntity.get("owner").equals(newEntity.get("owner")));
}
});
// Its partial update and un-set fields are not updated
Referenceable actualEntity = atlasClientV1.getEntity(DATABASE_TYPE_BUILTIN, QUALIFIED_NAME, dbName);
assertEquals(actualEntity.get(DESCRIPTION), entity.get(DESCRIPTION));
}
use of org.apache.atlas.v1.model.instance.Referenceable in project atlas by apache.
the class NotificationHookConsumerIT method testMessageHandleFailureConsumerContinues.
@Test
public void testMessageHandleFailureConsumerContinues() throws Exception {
// send invalid message - update with invalid type
sendHookMessage(new EntityPartialUpdateRequest(TEST_USER, randomString(), null, null, new Referenceable(randomString())));
// send valid message
final Referenceable entity = new Referenceable(DATABASE_TYPE_BUILTIN);
final String dbName = "db" + randomString();
entity.set(NAME, dbName);
entity.set(DESCRIPTION, randomString());
entity.set(QUALIFIED_NAME, dbName);
entity.set(CLUSTER_NAME, randomString());
sendHookMessage(new EntityCreateRequest(TEST_USER, entity));
waitFor(MAX_WAIT_TIME, new Predicate() {
@Override
public boolean evaluate() throws Exception {
ArrayNode results = searchByDSL(String.format("%s where name='%s'", DATABASE_TYPE_BUILTIN, entity.get(NAME)));
return results.size() == 1;
}
});
}
Aggregations