private void deleteInstance(@NotNull final VmwareCloudInstance instance) {
   if (instance.getErrorInfo() == null) {
     LOG.info("Will delete instance " + instance.getName());
     final VmwareInstance vmInstance;
     try {
       vmInstance = myApiConnector.getInstanceDetails(instance.getName());
       myAsyncTaskExecutor.executeAsync(
           vmInstance.deleteInstance(),
           new ImageStatusTaskWrapper(instance) {
             @Override
             public void onSuccess() {
               removeInstance(instance.getName());
             }
           });
     } catch (VmwareCheckedCloudException e) {
       LOG.warn("An exception during deleting instance " + instance.getName(), e);
       instance.updateErrors(TypedCloudErrorInfo.fromException(e));
     }
   } else {
     LOG.warn(
         String.format(
             "Won't delete instance %s with error: %s (%s)",
             instance.getName(),
             instance.getErrorInfo().getMessage(),
             instance.getErrorInfo().getDetailedMessage()));
   }
 }
 @Override
 public void onError(final Throwable th) {
   myInstance.setStatus(InstanceStatus.ERROR);
   if (th != null) {
     myInstance.updateErrors(TypedCloudErrorInfo.fromException(th));
     LOG.warn(
         "An error occurred: "
             + th.getLocalizedMessage()
             + " during processing "
             + myInstance.getName());
   } else {
     myInstance.updateErrors(
         new TypedCloudErrorInfo(
             "Unknown error during processing instance " + myInstance.getName()));
     LOG.warn("Unknown error during processing " + myInstance.getName());
   }
 }
  public VmwareCloudImage(
      @NotNull final VMWareApiConnector apiConnector,
      @NotNull final VmwareCloudImageDetails imageDetails,
      @NotNull final CloudAsyncTaskExecutor asyncTaskExecutor,
      @NotNull final File idxStorage) {
    super(imageDetails.getNickname(), imageDetails.getNickname());
    myImageDetails = imageDetails;
    myApiConnector = apiConnector;
    myAsyncTaskExecutor = asyncTaskExecutor;
    myInstances.clear();
    myIdxFile = new File(idxStorage, imageDetails.getNickname() + ".idx");
    if (!myIdxFile.exists()) {
      try {
        FileUtil.writeFileAndReportErrors(myIdxFile, "1");
      } catch (IOException e) {
        LOG.warn(
            String.format(
                "Unable to write idx file '%s': %s", myIdxFile.getAbsolutePath(), e.toString()));
      }
    }

    Map<String, VmwareInstance> realInstances = null;
    try {
      realInstances = myApiConnector.listImageInstances(this);
    } catch (VmwareCheckedCloudException e) {
      updateErrors(TypedCloudErrorInfo.fromException(e));
      return;
    }
    if (imageDetails.getBehaviour().isUseOriginal()) {
      final VmwareCloudInstance imageInstance =
          new VmwareCloudInstance(
              this, imageDetails.getSourceName(), VmwareConstants.CURRENT_STATE);
      myInstances.put(myImageDetails.getSourceName(), imageInstance);

      final VmwareInstance vmwareInstance = realInstances.get(imageDetails.getSourceName());
      if (vmwareInstance != null) {
        imageInstance.setStatus(vmwareInstance.getInstanceStatus());
      } else {
        imageInstance.setStatus(InstanceStatus.UNKNOWN);
        imageInstance.updateErrors(
            new TypedCloudErrorInfo("NoVM", "VM doesn't exist: " + imageDetails.getSourceName()));
      }
    } else {
      for (String instanceName : realInstances.keySet()) {
        final VmwareInstance instance = realInstances.get(instanceName);
        final String snapshotName = instance.getSnapshotName();
        VmwareCloudInstance cloudInstance =
            new VmwareCloudInstance(this, instanceName, snapshotName);
        cloudInstance.setStatus(instance.getInstanceStatus());
        myInstances.put(instanceName, cloudInstance);
      }
    }
  }