}
}
@Override
public void updateNodeCount(Long stackId, Integer scalingAdjustment) {
Stack stack = stackRepository.findOne(stackId);
MDCBuilder.buildMdcContext(stack);
if (!Status.AVAILABLE.equals(stack.getStatus())) {
throw new BadRequestException(String.format("Stack '%s' is currently in '%s' state. Node count can only be updated if it's running.", stackId,
stack.getStatus()));
}
if (0 == scalingAdjustment) {
throw new BadRequestException(String.format("Requested scaling adjustment on stack '%s' is 0. Nothing to do.", stackId));
}
if (0 > scalingAdjustment) {
if (-1 * scalingAdjustment > stack.getNodeCount()) {
throw new BadRequestException(String.format("There are %s instances in stack '%s'. Cannot remove %s instances.", stack.getNodeCount(), stackId,
-1 * scalingAdjustment));
}
int removeableHosts = 0;
for (InstanceMetaData metadataEntry : stack.getInstanceMetaData()) {
if (metadataEntry.isRemovable()) {
removeableHosts++;
}
}
if (removeableHosts < -1 * scalingAdjustment) {
throw new BadRequestException(
String.format("There are %s removable hosts on stack '%s' but %s were requested. Decomission nodes from the cluster first!",
removeableHosts, stackId, scalingAdjustment * -1));
}
}
stackUpdater.updateStackStatus(stack.getId(), Status.UPDATE_IN_PROGRESS);
LOGGER.info("Publishing {} event [scalingAdjustment: '{}']", ReactorConfig.UPDATE_INSTANCES_REQUEST_EVENT, scalingAdjustment);
reactor.notify(ReactorConfig.UPDATE_INSTANCES_REQUEST_EVENT,
Event.wrap(new UpdateInstancesRequest(stack.getTemplate().cloudPlatform(), stack.getId(), scalingAdjustment)));
}