use of com.sequenceiq.freeipa.flow.freeipa.upscale.event.UpscaleStackRequest in project cloudbreak by hortonworks.
the class FreeIpaUpscaleActions method addInstancesAction.
@Bean(name = "UPSCALE_ADD_INSTANCES_STATE")
public Action<?, ?> addInstancesAction() {
return new AbstractUpscaleAction<>(StackEvent.class) {
@Override
protected void doExecute(StackContext context, StackEvent payload, Map<Object, Object> variables) {
Stack stack = context.getStack();
stackUpdater.updateStackStatus(stack.getId(), getInProgressStatus(variables), "Adding instances");
List<CloudInstance> newInstances = buildNewInstances(context.getStack(), getInstanceCountByGroup(variables));
if (newInstances.isEmpty()) {
skipAddingNewInstances(context, stack);
} else {
addNewInstances(context, stack, newInstances);
}
}
private void skipAddingNewInstances(StackContext context, Stack stack) {
List<CloudResourceStatus> list = resourceService.getAllAsCloudResourceStatus(stack.getId());
UpscaleStackRequest<UpscaleStackResult> request = new UpscaleStackRequest<>(context.getCloudContext(), context.getCloudCredential(), context.getCloudStack(), ResourceLists.transform(list), new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, 0L));
UpscaleStackResult result = new UpscaleStackResult(request.getResourceId(), ResourceStatus.CREATED, list);
sendEvent(context, result.selector(), result);
}
private void addNewInstances(StackContext context, Stack stack, List<CloudInstance> newInstances) {
Stack updatedStack = instanceMetaDataService.saveInstanceAndGetUpdatedStack(stack, newInstances);
List<CloudResource> cloudResources = resourceService.findAllByStackId(stack.getId()).stream().map(resource -> resourceConverter.convert(resource)).collect(Collectors.toList());
CloudStack updatedCloudStack = cloudStackConverter.convert(updatedStack);
UpscaleStackRequest<UpscaleStackResult> request = new UpscaleStackRequest<>(context.getCloudContext(), context.getCloudCredential(), updatedCloudStack, cloudResources, new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, (long) newInstances.size()));
sendEvent(context, request.selector(), request);
}
private List<CloudInstance> buildNewInstances(Stack stack, int instanceCountByGroup) {
long privateId = privateIdProvider.getFirstValidPrivateId(stack.getInstanceGroups());
List<CloudInstance> newInstances = new ArrayList<>();
for (InstanceGroup instanceGroup : stack.getInstanceGroups()) {
int remainingInstances = instanceCountByGroup - instanceGroup.getNotDeletedInstanceMetaDataSet().size();
for (long i = 0; i < remainingInstances; ++i) {
newInstances.add(cloudStackConverter.buildInstance(stack, null, instanceGroup, stack.getStackAuthentication(), privateId++, InstanceStatus.CREATE_REQUESTED));
}
}
return newInstances;
}
};
}
Aggregations