use of org.hypertrace.entity.data.service.v1.Empty in project entity-service by hypertrace.
the class EntityDataServiceImpl method delete.
/**
* Deletes an Entity by the EntityId and EntityType
*
* @param request ID of the entity to be deleted
* @param responseObserver Observer to be notified on about the Entity delete request
*/
@Override
public void delete(ByIdRequest request, StreamObserver<Empty> responseObserver) {
if (StringUtils.isEmpty(request.getEntityId())) {
LOG.info("{}. Invalid delete request:{}", request, ErrorMessages.ENTITY_ID_EMPTY);
responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_ID_EMPTY));
return;
}
Optional<String> tenantId = RequestContext.CURRENT.get().getTenantId();
if (tenantId.isEmpty()) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
Optional<Entity> existingEntity = getExistingEntity(tenantId.get(), request.getEntityType(), request.getEntityId());
Key key = this.entityNormalizer.getEntityDocKey(tenantId.get(), request.getEntityType(), request.getEntityId());
if (entitiesCollection.delete(key)) {
responseObserver.onNext(Empty.newBuilder().build());
responseObserver.onCompleted();
existingEntity.ifPresent(entity -> entityChangeEventGenerator.sendDeleteNotification(RequestContext.CURRENT.get(), List.of(entity)));
} else {
responseObserver.onError(new RuntimeException("Could not delete the entity."));
}
}
use of org.hypertrace.entity.data.service.v1.Empty in project entity-service by hypertrace.
the class EntityDataServiceImpl method upsertEntities.
@Override
public void upsertEntities(Entities request, StreamObserver<Empty> responseObserver) {
String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null);
if (tenantId == null) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
try {
Map<Key, Entity> entities = request.getEntityList().stream().map(entity -> this.entityNormalizer.normalize(tenantId, entity)).collect(Collectors.toUnmodifiableMap(entity -> this.entityNormalizer.getEntityDocKey(tenantId, entity), Function.identity()));
java.util.Collection<Entity> existingEntities = getExistingEntities(entities.values());
upsertEntities(entities, entitiesCollection, responseObserver);
entityChangeEventGenerator.sendChangeNotification(RequestContext.CURRENT.get(), existingEntities, entities.values());
} catch (Throwable throwable) {
LOG.warn("Failed to upsert: {}", request, throwable);
responseObserver.onError(throwable);
}
}
use of org.hypertrace.entity.data.service.v1.Empty in project entity-service by hypertrace.
the class EntityDataServiceImpl method upsertEnrichedEntities.
@Override
public void upsertEnrichedEntities(EnrichedEntities request, StreamObserver<Empty> responseObserver) {
String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null);
if (isNull(tenantId)) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
for (EnrichedEntity entity : request.getEntitiesList()) {
if (StringUtils.isEmpty(entity.getEntityType())) {
LOG.info("{}. Invalid upsertEnrichedEntities request:{}", entity, ErrorMessages.ENTITY_TYPE_EMPTY);
responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_TYPE_EMPTY));
return;
}
if (StringUtils.isEmpty(entity.getEntityId())) {
LOG.info("{}. Invalid upsertEnrichedEntities request:{}", entity, ErrorMessages.ENTITY_ID_EMPTY);
responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_ID_EMPTY));
return;
}
}
Map<Key, EnrichedEntity> entities = request.getEntitiesList().stream().collect(Collectors.toUnmodifiableMap(entity -> this.entityNormalizer.getEntityDocKey(tenantId, entity.getEntityType(), entity.getEntityId()), Function.identity()));
upsertEntities(entities, enrichedEntitiesCollection, responseObserver);
}
use of org.hypertrace.entity.data.service.v1.Empty in project hypertrace-ingester by hypertrace.
the class ApiEntityDao method upsertApiEntity.
public Entity upsertApiEntity(String tenantId, String serviceId, String serviceName, String apiType, String apiName) {
Preconditions.checkNotNull(tenantId, "tenantId can't be empty");
Preconditions.checkNotNull(serviceId, "serviceId can't be empty");
Preconditions.checkNotNull(serviceName, "serviceName can't be empty");
Preconditions.checkNotNull(apiType, "apiType can't be empty");
Preconditions.checkNotNull(apiName, "apiName can't be empty");
String pattern = apiName;
Entity.Builder entityBuilder = Entity.newBuilder().setTenantId(tenantId).setEntityType(EntityType.API.name()).setEntityName(pattern).putIdentifyingAttributes(API_URL_PATTERN_ATTR, createAttributeValue(pattern)).putIdentifyingAttributes(SERVICE_ID_ATTR, createAttributeValue(serviceId)).putIdentifyingAttributes(API_TYPE_ATTR, createAttributeValue(apiType)).putAttributes(SERVICE_NAME_ATTR, createAttributeValue(serviceName)).putAttributes(API_DISCOVERY_FROM_ATTR, createAttributeValue(DISCOVERED_FROM)).putAttributes(API_DISCOVERY_STATE_ATTR, createAttributeValue(DISCOVERED_STATE)).putAttributes(API_NAME_ATTR, createAttributeValue(apiName));
Entity entity = entityBuilder.build();
LOGGER.info("Upserting Api entity: [{}]", TextFormat.shortDebugString(entity));
return edsClient.upsert(entity);
}
use of org.hypertrace.entity.data.service.v1.Empty in project entity-service by hypertrace.
the class EntityDataServiceImpl method upsertRelationships.
@Override
public void upsertRelationships(EntityRelationships request, StreamObserver<Empty> responseObserver) {
String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null);
if (tenantId == null) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
Map<Key, Document> entityRelations = new HashMap<>();
try {
for (EntityRelationship relationship : request.getRelationshipList()) {
if (StringUtils.isEmpty(relationship.getFromEntityId()) || StringUtils.isEmpty(relationship.getToEntityId()) || StringUtils.isEmpty(relationship.getEntityRelationshipType())) {
LOG.warn("Invalid relationship upsert request:{}", relationship);
continue;
}
Document document = convertEntityRelationshipToDocument(tenantId, relationship);
Key key = new EntityRelationshipDocKey(tenantId, relationship.getEntityRelationshipType(), relationship.getFromEntityId(), relationship.getToEntityId());
entityRelations.put(key, document);
}
boolean status = relationshipsCollection.bulkUpsert(entityRelations);
if (status) {
responseObserver.onNext(Empty.newBuilder().build());
responseObserver.onCompleted();
} else {
responseObserver.onError(new RuntimeException("Could not bulk upsert relationships."));
}
} catch (IOException e) {
LOG.error("Failed to bulk upsert relationships.", e);
responseObserver.onError(e);
}
}
Aggregations