throw new PermissionException("User [" + subject.getName()
+ "] does not have permission to delete resource [" + resource + "]");
}
// Persist in separate transaction so it is committed immediately, before the request is sent to the agent
DeleteResourceHistory persistedHistory = resourceFactoryManager.persistDeleteHistory(subject, resourceId);
// Package into transfer object
DeleteResourceRequest request = new DeleteResourceRequest(persistedHistory.getId(), resourceId);
try {
AgentClient agentClient = agentManager.getAgentClient(agent);
ResourceFactoryAgentService resourceFactoryAgentService = agentClient.getResourceFactoryAgentService();
resourceFactoryAgentService.deleteResource(request);
return persistedHistory;
} catch (CannotConnectException e) {
LOG.error("Error while sending delete resource request to agent service", e);
// Submit the error as a failure response
String errorMessage = ThrowableUtil.getAllMessages(e);
DeleteResourceResponse response = new DeleteResourceResponse(persistedHistory.getId(),
DeleteResourceStatus.FAILURE, errorMessage);
resourceFactoryManager.completeDeleteResourceRequest(response);
throw new CannotConnectToAgentException("Error while sending delete resource request to agent service", e);
} catch (Exception e) {
LOG.error("Error while sending delete resource request to agent service", e);
// Submit the error as a failure response
String errorMessage = ThrowableUtil.getAllMessages(e);
DeleteResourceResponse response = new DeleteResourceResponse(persistedHistory.getId(),
DeleteResourceStatus.FAILURE, errorMessage);
resourceFactoryManager.completeDeleteResourceRequest(response);
throw new RuntimeException("Error while sending delete resource request to agent service", e);
}