in streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/connect/AdapterResource.java [214:293]
public ResponseEntity<?> deleteAdapter(
@PathVariable("id") String elementId,
@RequestParam(value = "deleteAssociatedPipelines", defaultValue = "false")
boolean deleteAssociatedPipelines
) {
List<String> pipelinesUsingAdapter = getPipelinesUsingAdapter(elementId);
IPipelineStorage pipelineStorageAPI = StorageDispatcher.INSTANCE.getNoSqlStore()
.getPipelineStorageAPI();
if (pipelinesUsingAdapter.isEmpty()) {
try {
managementService.deleteAdapter(elementId);
return ok(Notifications.success("Adapter with id: " + elementId + " is deleted."));
} catch (AdapterException e) {
LOG.error("Error while deleting adapter with id {}", elementId, e);
return ok(Notifications.error(e.getMessage()));
}
} else if (!deleteAssociatedPipelines) {
List<String> namesOfPipelinesUsingAdapter = pipelinesUsingAdapter
.stream()
.map(pipelineId -> pipelineStorageAPI.getElementById(
pipelineId)
.getName())
.collect(Collectors.toList());
return ResponseEntity.status(HttpStatus.SC_CONFLICT)
.body(String.join(", ", namesOfPipelinesUsingAdapter));
} else {
PermissionResourceManager permissionResourceManager = new PermissionResourceManager();
// find out the names of pipelines that have an owner and the owner is not the current user
List<String> namesOfPipelinesNotOwnedByUser = pipelinesUsingAdapter
.stream()
.filter(pipelineId ->
!permissionResourceManager.findForObjectId(
pipelineId)
.stream()
.findFirst()
.map(
Permission::getOwnerSid)
// if a pipeline has no owner, pretend the owner
// is the user so the user can delete it
.orElse(
this.getAuthenticatedUserSid())
.equals(
this.getAuthenticatedUserSid()))
.map(pipelineId -> pipelineStorageAPI.getElementById(
pipelineId)
.getName())
.collect(Collectors.toList());
boolean isAdmin = SecurityContextHolder.getContext()
.getAuthentication()
.getAuthorities()
.stream()
.anyMatch(r -> r.getAuthority()
.equals(
DefaultRole.ROLE_ADMIN.name()));
// if the user is admin or owns all pipelines using this adapter,
// the user can delete all associated pipelines and this adapter
if (isAdmin || namesOfPipelinesNotOwnedByUser.isEmpty()) {
try {
for (String pipelineId : pipelinesUsingAdapter) {
PipelineManager.stopPipeline(pipelineId, false);
PipelineManager.deletePipeline(pipelineId);
}
managementService.deleteAdapter(elementId);
return ok(Notifications.success("Adapter with id: " + elementId
+ " and all pipelines using the adapter are deleted."));
} catch (Exception e) {
LOG.error(
"Error while deleting adapter with id "
+ elementId + " and all pipelines using the adapter", e
);
return ok(Notifications.error(e.getMessage()));
}
} else {
// otherwise, hint the user the names of pipelines using the adapter but not owned by the user
return ResponseEntity.status(HttpStatus.SC_CONFLICT)
.body(String.join(", ", namesOfPipelinesNotOwnedByUser));
}
}
}