@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
  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);
  }