@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);
 }
 private String getExistingSubnetCidr(
     AuthenticatedContext authenticatedContext, CloudStack stack) {
   Network network = stack.getNetwork();
   return utils.isExistingSubnet(network)
       ? utils.getExistingSubnetCidr(authenticatedContext, network)
       : null;
 }
  @Override
  public List<CloudResourceStatus> upscale(
      AuthenticatedContext authenticatedContext, CloudStack stack, List<CloudResource> resources) {
    AzureRMClient azureRMClient = armClient.createAccess(authenticatedContext.getCloudCredential());

    String stackName = armUtils.getStackName(authenticatedContext.getCloudContext());
    String template =
        armTemplateBuilder.build(
            stackName,
            authenticatedContext.getCloudCredential(),
            authenticatedContext.getCloudContext(),
            stack);
    String parameters =
        armTemplateBuilder.buildParameters(
            authenticatedContext.getCloudCredential(), stack.getNetwork(), stack.getImage());

    try {
      azureRMClient.createTemplateDeployment(stackName, stackName, template, parameters);
      List<CloudResourceStatus> check = new ArrayList<>();
      check.add(new CloudResourceStatus(resources.get(0), ResourceStatus.IN_PROGRESS));
      return check;
    } catch (HttpResponseException e) {
      throw new CloudConnectorException(e.getResponse().getData().toString(), e);
    } catch (Exception e) {
      throw new CloudConnectorException(String.format("Could not upscale: %s", stackName), e);
    }
  }
  @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());
 }
  @Override
  public List<CloudResourceStatus> launch(
      AuthenticatedContext authenticatedContext,
      CloudStack stack,
      PersistenceNotifier notifier,
      AdjustmentType adjustmentType,
      Long threshold) {
    String stackName = armUtils.getStackName(authenticatedContext.getCloudContext());
    String resourceGroupName =
        armUtils.getResourceGroupName(authenticatedContext.getCloudContext());
    String template =
        armTemplateBuilder.build(
            stackName,
            authenticatedContext.getCloudCredential(),
            authenticatedContext.getCloudContext(),
            stack);
    String parameters =
        armTemplateBuilder.buildParameters(
            authenticatedContext.getCloudCredential(), stack.getNetwork(), stack.getImage());

    AzureRMClient access = armClient.createAccess(authenticatedContext.getCloudCredential());
    try {
      access.createTemplateDeployment(resourceGroupName, stackName, template, parameters);
    } catch (HttpResponseException e) {
      throw new CloudConnectorException(
          String.format(
              "Error occured when creating stack: %s", e.getResponse().getData().toString()));
    } catch (Exception e) {
      throw new CloudConnectorException(String.format("Invalid provisiong type: %s", stackName));
    }

    CloudResource cloudResource =
        new CloudResource.Builder().type(ResourceType.ARM_TEMPLATE).name(stackName).build();
    List<CloudResourceStatus> resources = check(authenticatedContext, Arrays.asList(cloudResource));
    LOGGER.debug("Launched resources: {}", resources);
    return resources;
  }
 private boolean assignFloatingIp(CloudStack stack) {
   return utils.assignFloatingIp(stack.getNetwork());
 }
 private boolean isExistingNetwork(CloudStack stack) {
   return utils.isExistingNetwork(stack.getNetwork());
 }