@Override
  public Void apply(IMachine machine) {
    List<IMedium> mediaToBeDeleted = ImmutableList.of();
    try {
      mediaToBeDeleted = machine.unregister(vmSpec.getCleanupMode());
    } catch (VBoxException e) {
      ErrorCode errorCode = ErrorCode.valueOf(e);
      switch (errorCode) {
        case VBOX_E_OBJECT_NOT_FOUND:
          logger.debug("Machine %s does not exists, cannot unregister", vmSpec.getVmName());
          break;
        default:
          throw e;
      }
    }

    List<IMedium> filteredMediaToBeDeleted =
        Lists.newArrayList(
            transform(
                filter(mediaToBeDeleted, new AutoDeleteHardDiskPredicate(vmSpec)),
                new DeleteChildrenOfMedium()));

    if (!filteredMediaToBeDeleted.isEmpty()) {
      try {
        IProgress deletion = machine.delete(filteredMediaToBeDeleted);
        deletion.waitForCompletion(-1);
      } catch (Exception e) {
        logger.error(e, "Problem in deleting the media attached to %s", machine.getName());
        Throwables.propagate(e);
      }
    }

    return null;
  }
  @Override
  @BeforeClass(groups = "live")
  public void setupContext() {
    super.setupContext();
    instanceName =
        VIRTUALBOX_IMAGE_PREFIX
            + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());

    StorageController ideController =
        StorageController.builder()
            .name("IDE Controller")
            .bus(StorageBus.IDE)
            .attachISO(0, 0, operatingSystemIso)
            .attachHardDisk(
                HardDisk.builder()
                    .diskpath(adminDisk(instanceName))
                    .controllerPort(0)
                    .deviceSlot(1)
                    .autoDelete(true)
                    .build())
            .attachISO(1, 1, guestAdditionsIso)
            .build();

    VmSpec instanceVmSpec =
        VmSpec.builder()
            .id(instanceName)
            .name(instanceName)
            .osTypeId("")
            .memoryMB(512)
            .cleanUpMode(CleanupMode.Full)
            .controller(ideController)
            .forceOverwrite(true)
            .build();

    Injector injector = view.utils().injector();
    Function<String, String> configProperties =
        injector.getInstance(ValueOfConfigurationKeyOrNull.class);
    IsoSpec isoSpec =
        IsoSpec.builder()
            .sourcePath(operatingSystemIso)
            .installationScript(
                configProperties
                    .apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE)
                    .replace("HOSTNAME", instanceVmSpec.getVmName()))
            .build();

    NetworkAdapter networkAdapter =
        NetworkAdapter.builder()
            .networkAttachmentType(NetworkAttachmentType.NAT)
            .tcpRedirectRule("127.0.0.1", 2222, "", 22)
            .build();
    NetworkInterfaceCard networkInterfaceCard =
        NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter).build();

    NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build();
    machineSpec = MasterSpec.builder().iso(isoSpec).vm(instanceVmSpec).network(networkSpec).build();
  }
 @Override
 public boolean apply(IMedium medium) {
   for (StorageController controller : vmSpec.getControllers()) {
     if (controller.getHardDisk(medium.getName()).isAutoDelete()) return true;
   }
   return false;
 }
 private IMachine getMasterNode(
     VirtualBoxManager manager, ComputeServiceContext localHostContext) {
   try {
     Predicate<IPSocket> socketTester =
         new RetryablePredicate<IPSocket>(new InetSocketAddressConnect(), 10, 1, TimeUnit.SECONDS);
     String workingDir = PropertyUtils.getWorkingDirFromProperty();
     StorageController ideController =
         StorageController.builder()
             .name(controllerIDE)
             .bus(StorageBus.IDE)
             .attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso")
             .attachHardDisk(
                 HardDisk.builder()
                     .diskpath(workingDir + "/testadmin.vdi")
                     .controllerPort(0)
                     .deviceSlot(1)
                     .build())
             .attachISO(1, 1, workingDir + "/VBoxGuestAdditions_4.1.2.iso")
             .build();
     VmSpec vmSpecification =
         VmSpec.builder()
             .id(vmId)
             .name(vmName)
             .osTypeId(osTypeId)
             .memoryMB(512)
             .cleanUpMode(CleanupMode.Full)
             .controller(ideController)
             .forceOverwrite(true)
             .build();
     return new CreateAndInstallVm(
             manager, guestId, localHostContext, hostId, socketTester, "127.0.0.1", 8080, HEADLESS)
         .apply(vmSpecification);
   } catch (IllegalStateException e) {
     // already created
     return manager.getVBox().findMachine(vmName);
   }
 }