@Override
 public List<CloudResourceStatus> update(
     AuthenticatedContext authenticatedContext, CloudStack stack, List<CloudResource> resources) {
   String stackName = authenticatedContext.getCloudContext().getName();
   boolean existingNetwork = isExistingNetwork(stack);
   boolean assignFloatingIp = assignFloatingIp(stack);
   String existingSubnetCidr = getExistingSubnetCidr(authenticatedContext, stack);
   String heatTemplate =
       heatTemplateBuilder.build(
           stackName,
           stack.getGroups(),
           stack.getSecurity(),
           stack.getImage(),
           existingNetwork,
           existingSubnetCidr != null,
           assignFloatingIp);
   Map<String, String> parameters =
       heatTemplateBuilder.buildParameters(
           authenticatedContext,
           stack.getNetwork(),
           stack.getImage(),
           existingNetwork,
           existingSubnetCidr);
   return updateHeatStack(authenticatedContext, resources, heatTemplate, parameters);
 }
  @SuppressWarnings("unchecked")
  @Override
  public List<CloudResourceStatus> launch(
      AuthenticatedContext authenticatedContext,
      CloudStack stack,
      PersistenceNotifier notifier,
      AdjustmentType adjustmentType,
      Long threshold) {
    String stackName = authenticatedContext.getCloudContext().getName();
    boolean existingNetwork = isExistingNetwork(stack);
    boolean assignFloatingIp = assignFloatingIp(stack);
    String existingSubnetCidr = getExistingSubnetCidr(authenticatedContext, stack);
    String heatTemplate =
        heatTemplateBuilder.build(
            stackName,
            stack.getGroups(),
            stack.getSecurity(),
            stack.getImage(),
            existingNetwork,
            existingSubnetCidr != null,
            assignFloatingIp);
    Map<String, String> parameters =
        heatTemplateBuilder.buildParameters(
            authenticatedContext,
            stack.getNetwork(),
            stack.getImage(),
            existingNetwork,
            existingSubnetCidr);

    OSClient client = openStackClient.createOSClient(authenticatedContext);

    Stack heatStack =
        client
            .heat()
            .stacks()
            .create(
                Builders.stack()
                    .name(stackName)
                    .template(heatTemplate)
                    .disableRollback(false)
                    .parameters(parameters)
                    .timeoutMins(OPERATION_TIMEOUT)
                    .build());

    CloudResource cloudResource =
        new CloudResource.Builder().type(ResourceType.HEAT_STACK).name(heatStack.getId()).build();
    try {
      notifier.notifyAllocation(cloudResource, authenticatedContext.getCloudContext());
    } catch (Exception e) {
      // Rollback
      terminate(authenticatedContext, stack, Lists.newArrayList(cloudResource));
    }
    List<CloudResourceStatus> resources =
        check(authenticatedContext, Lists.newArrayList(cloudResource));
    LOGGER.debug("Launched resources: {}", resources);
    return resources;
  }
 private CloudStack removeDeleteRequestedInstances(CloudStack stack) {
   List<Group> groups = new ArrayList<>();
   for (Group group : stack.getGroups()) {
     List<CloudInstance> instances = new ArrayList<>(group.getInstances());
     for (CloudInstance instance : group.getInstances()) {
       if (InstanceStatus.DELETE_REQUESTED == instance.getTemplate().getStatus()) {
         instances.remove(instance);
       }
     }
     groups.add(new Group(group.getName(), group.getType(), instances));
   }
   return new CloudStack(
       groups, stack.getNetwork(), stack.getSecurity(), stack.getImage(), stack.getParameters());
 }