@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);
    }
  }
  @Override
  public List<CloudResourceStatus> check(
      AuthenticatedContext authenticatedContext, List<CloudResource> resources) {
    List<CloudResourceStatus> result = new ArrayList<>();
    AzureRMClient access = armClient.createAccess(authenticatedContext.getCloudCredential());
    String stackName = armUtils.getStackName(authenticatedContext.getCloudContext());

    for (CloudResource resource : resources) {
      switch (resource.getType()) {
        case ARM_TEMPLATE:
          LOGGER.info("Checking Arm group stack status of: {}", stackName);
          try {
            Map<String, Object> resourceGroup = access.getTemplateDeployment(stackName, stackName);
            CloudResourceStatus templateResourceStatus =
                armUtils.templateStatus(resource, resourceGroup, access, stackName);
            result.add(templateResourceStatus);
          } catch (HttpResponseException e) {
            if (e.getStatusCode() == NOT_FOUND) {
              result.add(new CloudResourceStatus(resource, ResourceStatus.DELETED));
            } else {
              throw new CloudConnectorException(e.getResponse().getData().toString(), e);
            }
          } catch (Exception e) {
            throw new CloudConnectorException(
                String.format("Invalid resource exception: %s", e.getMessage()), e);
          }
          break;
        default:
          throw new CloudConnectorException(
              String.format("Invalid resource type: %s", resource.getType()));
      }
    }

    return result;
  }
 private void deallocateVirtualMachine(
     AuthenticatedContext authenticatedContext,
     AzureRMClient client,
     String stackName,
     String privateInstanceId)
     throws CloudConnectorException {
   try {
     client.deallocateVirtualMachine(stackName, privateInstanceId);
     PollTask<Boolean> task =
         armPollTaskFactory.newVirtualMachineStatusCheckerTask(
             authenticatedContext,
             armClient,
             new VirtualMachineCheckerContext(
                 new ArmCredentialView(authenticatedContext.getCloudCredential()),
                 stackName,
                 privateInstanceId,
                 "Succeeded"));
     syncPollingScheduler.schedule(task);
   } catch (HttpResponseException e) {
     if (e.getStatusCode() != NOT_FOUND) {
       throw new CloudConnectorException(e.getResponse().getData().toString(), e);
     }
   } catch (Exception e) {
     throw new CloudConnectorException(
         String.format("Could not deallocate machine: %s", privateInstanceId), e);
   }
 }
  private void deleteNetworkInterfaces(
      AuthenticatedContext authenticatedContext,
      AzureRMClient client,
      String stackName,
      List<String> networkInterfacesNames)
      throws CloudConnectorException {
    for (String networkInterfacesName : networkInterfacesNames) {
      try {
        client.deleteNetworkInterface(stackName, networkInterfacesName);
        PollTask<Boolean> task =
            armPollTaskFactory.newNetworkInterfaceDeleteStatusCheckerTask(
                authenticatedContext,
                armClient,
                new NetworkInterfaceCheckerContext(
                    new ArmCredentialView(authenticatedContext.getCloudCredential()),
                    stackName,
                    networkInterfacesName));

        syncPollingScheduler.schedule(task);

      } catch (HttpResponseException e) {
        if (e.getStatusCode() != NOT_FOUND) {
          throw new CloudConnectorException(e.getResponse().getData().toString(), e);
        }
      } catch (Exception e) {
        throw new CloudConnectorException(
            String.format("Could not delete network interface: %s", networkInterfacesName), e);
      }
    }
  }
 @Override
 public List<CloudResourceStatus> terminate(
     AuthenticatedContext authenticatedContext, CloudStack stack, List<CloudResource> resources) {
   AzureRMClient azureRMClient = armClient.createAccess(authenticatedContext.getCloudCredential());
   for (CloudResource resource : resources) {
     try {
       azureRMClient.deleteResourceGroup(resource.getName());
       PollTask<Boolean> task =
           armPollTaskFactory.newResourceGroupDeleteStatusCheckerTask(
               authenticatedContext,
               armClient,
               new ResourceGroupCheckerContext(
                   new ArmCredentialView(authenticatedContext.getCloudCredential()),
                   resource.getName()));
       Boolean statePollerResult = task.call();
       if (!task.completed(statePollerResult)) {
         syncPollingScheduler.schedule(task);
       }
       if (armUtils.isPersistentStorage()) {
         CloudContext cloudCtx = authenticatedContext.getCloudContext();
         String storageName =
             armUtils.getStorageName(
                 authenticatedContext.getCloudCredential(), cloudCtx, stack.getRegion());
         String imageStorageGroup = armUtils.getImageResourceGroupName(cloudCtx);
         String diskContainer = armUtils.getDiskContainerName(cloudCtx);
         deleteContainer(azureRMClient, imageStorageGroup, storageName, diskContainer);
       }
     } catch (HttpResponseException e) {
       if (e.getStatusCode() != NOT_FOUND) {
         throw new CloudConnectorException(e.getResponse().getData().toString(), e);
       } else {
         return check(authenticatedContext, new ArrayList<CloudResource>());
       }
     } catch (Exception e) {
       throw new CloudConnectorException(
           String.format("Could not delete resource group: %s", resource.getName()), e);
     }
   }
   return check(authenticatedContext, resources);
 }
 @Override
 protected boolean checkStatus(
     OpenStackContext context, AuthenticatedContext auth, CloudResource resource) {
   CloudContext cloudContext = auth.getCloudContext();
   OSClient osClient = createOSClient(auth);
   Router osRouter = osClient.networking().router().get(resource.getReference());
   if (osRouter != null && context.isBuild()) {
     State routerStatus = osRouter.getStatus();
     if (State.ERROR == routerStatus) {
       throw new OpenStackResourceException(
           "Router in failed state",
           resource.getType(),
           cloudContext.getStackName(),
           cloudContext.getStackId(),
           resource.getName());
     }
     return routerStatus == State.ACTIVE;
   } else if (osRouter == null && !context.isBuild()) {
     return true;
   }
   return false;
 }
 @Override
 public List<CloudResource> build(
     OpenStackContext context,
     long privateId,
     AuthenticatedContext auth,
     Group group,
     Image image,
     List<CloudResource> buildableResource)
     throws Exception {
   CloudResource resource = buildableResource.get(0);
   try {
     OSClient osClient = createOSClient(auth);
     List<CloudResource> computeResources = context.getComputeResources(privateId);
     CloudResource instance = getInstance(computeResources);
     String pool = osClient.compute().floatingIps().getPoolNames().get(0);
     FloatingIP unusedIp = osClient.compute().floatingIps().allocateIP(pool);
     ActionResponse response =
         osClient
             .compute()
             .floatingIps()
             .addFloatingIP(
                 instance.getParameter(OpenStackConstants.SERVER, Server.class),
                 unusedIp.getFloatingIpAddress());
     if (!response.isSuccess()) {
       throw new OpenStackResourceException(
           "Add floating-ip to server failed",
           resourceType(),
           resource.getName(),
           auth.getCloudContext().getId(),
           response.getFault());
     }
     return Collections.singletonList(createPersistedResource(resource, unusedIp.getId()));
   } catch (OS4JException ex) {
     throw new OpenStackResourceException(
         "Add floating-ip to server failed", resourceType(), resource.getName(), ex);
   }
 }
  @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;
  }
  @Override
  public List<CloudResourceStatus> downscale(
      AuthenticatedContext auth,
      CloudStack stack,
      List<CloudResource> resources,
      List<CloudInstance> vms) {
    AzureRMClient client = armClient.createAccess(auth.getCloudCredential());
    String stackName = armUtils.getStackName(auth.getCloudContext());
    String storageName =
        armUtils.getStorageName(
            auth.getCloudCredential(), auth.getCloudContext(), stack.getRegion());
    String imageStorageGroup = armUtils.getImageResourceGroupName(auth.getCloudContext());
    String diskContainer = armUtils.getDiskContainerName(auth.getCloudContext());

    for (CloudInstance instance : vms) {
      List<String> networkInterfacesNames = new ArrayList<>();
      List<String> storageProfileDiskNames = new ArrayList<>();
      String instanceId = instance.getInstanceId();

      try {
        Map<String, Object> virtualMachine = client.getVirtualMachine(stackName, instanceId);

        Map properties = (Map) virtualMachine.get("properties");

        Map networkProfile = (Map) properties.get("networkProfile");

        List<Map> networkInterfaces = (List<Map>) networkProfile.get("networkInterfaces");
        for (Map networkInterface : networkInterfaces) {
          networkInterfacesNames.add(
              getNameFromConnectionString(networkInterface.get("id").toString()));
        }

        Map storageProfile = (Map) properties.get("storageProfile");

        Map osDisk = (Map) storageProfile.get("osDisk");
        List<Map> dataDisks = (List<Map>) storageProfile.get("dataDisks");

        for (Map datadisk : dataDisks) {
          Map vhds = (Map) datadisk.get("vhd");
          storageProfileDiskNames.add(getNameFromConnectionString(vhds.get("uri").toString()));
        }
        Map vhds = (Map) osDisk.get("vhd");
        storageProfileDiskNames.add(getNameFromConnectionString(vhds.get("uri").toString()));
      } catch (HttpResponseException e) {
        if (e.getStatusCode() != NOT_FOUND) {
          throw new CloudConnectorException(e.getResponse().getData().toString(), e);
        }
      } catch (Exception e) {
        throw new CloudConnectorException(String.format("Could not downscale: %s", stackName), e);
      }
      try {
        deallocateVirtualMachine(auth, client, stackName, instanceId);
        deleteVirtualMachine(auth, client, stackName, instanceId);
        deleteNetworkInterfaces(auth, client, stackName, networkInterfacesNames);
        deleteDisk(storageProfileDiskNames, client, imageStorageGroup, storageName, diskContainer);
      } catch (CloudConnectorException e) {
        throw e;
      }
    }
    return check(auth, resources);
  }