}
@Override
public void updateStatus(Long stackId, StatusRequest statusRequest) {
Stack stack = stackRepository.findOne(stackId);
Cluster cluster = stack.getCluster();
MDCBuilder.buildMdcContext(stack.getCluster());
long clusterId = cluster.getId();
Status clusterStatus = cluster.getStatus();
Status stackStatus = stack.getStatus();
if (statusRequest.equals(StatusRequest.STARTED)) {
if (Status.START_IN_PROGRESS.equals(stackStatus)) {
LOGGER.info("Stack is starting, set cluster state to: {}", Status.START_REQUESTED);
cluster.setStatus(Status.START_REQUESTED);
clusterRepository.save(cluster);
} else {
if (!Status.STOPPED.equals(clusterStatus)) {
throw new BadRequestException(
String.format("Cannot update the status of cluster '%s' to STARTED, because it isn't in STOPPED state.", clusterId));
}
if (!Status.AVAILABLE.equals(stackStatus)) {
throw new BadRequestException(
String.format("Cannot update the status of cluster '%s' to STARTED, because the stack is not AVAILABLE", clusterId));
}
cluster.setStatus(Status.START_IN_PROGRESS);
clusterRepository.save(cluster);
LOGGER.info("Publishing {} event", ReactorConfig.CLUSTER_STATUS_UPDATE_EVENT);
reactor.notify(ReactorConfig.CLUSTER_STATUS_UPDATE_EVENT,
Event.wrap(new ClusterStatusUpdateRequest(stack.getId(), statusRequest)));
}
} else {
if (!Status.AVAILABLE.equals(clusterStatus)) {
throw new BadRequestException(
String.format("Cannot update the status of cluster '%s' to STOPPED, because it isn't in AVAILABLE state.", clusterId));
}
if (!Status.AVAILABLE.equals(stackStatus) && !Status.STOP_REQUESTED.equals(stackStatus)) {
throw new BadRequestException(
String.format("Cannot update the status of cluster '%s' to STARTED, because the stack is not AVAILABLE", clusterId));
}
cluster.setStatus(Status.STOP_IN_PROGRESS);
clusterRepository.save(cluster);
LOGGER.info("Publishing {} event", ReactorConfig.CLUSTER_STATUS_UPDATE_EVENT);
reactor.notify(ReactorConfig.CLUSTER_STATUS_UPDATE_EVENT,
Event.wrap(new ClusterStatusUpdateRequest(stack.getId(), statusRequest)));
}