private Datastore getDatastoreByName(final String datastoreName, ManagedEntity rootEntity)
      throws RemoteException, MalformedURLException {
    if (rootEntity == null) {
      rootEntity = getServiceInstance().getRootFolder();
    }

    Datastore datastore =
        (Datastore)
            new InventoryNavigator(rootEntity).searchManagedEntity("Datastore", datastoreName);
    if (datastore != null) {
      return datastore;
    }

    if (rootEntity == null || !(rootEntity instanceof ClusterComputeResource)) {
      return null;
    }

    // try to fetch data store directly from cluster if above approach doesn't work
    ClusterComputeResource clusterResource = (ClusterComputeResource) rootEntity;

    for (Datastore dataStore : clusterResource.getDatastores()) {
      if (dataStore.getName().equals(datastoreName)) {
        return dataStore;
      }
    }
    return null;
  }
 public SetStorageIOControl(Datastore datastore, boolean enabled) {
   this.datastore = datastore;
   this.enabled = enabled;
   if (enabled) {
     setDetail("SetStorageIOControl.detail.enable", datastore.getName());
   } else {
     setDetail("SetStorageIOControl.detail.disable", datastore.getName());
   }
 }
  private VirtualMachineRelocateSpec createRelocateSpec(
      PrintStream jLogger,
      boolean linkedClone,
      String resourcePoolName,
      String cluster,
      String datastoreName,
      boolean isResourcePoolRequired)
      throws RemoteException, MalformedURLException, VSphereException {
    VirtualMachineRelocateSpec rel = new VirtualMachineRelocateSpec();

    if (linkedClone) {
      rel.setDiskMoveType("createNewChildDiskBacking");
    } else {
      rel.setDiskMoveType("moveAllDiskBackingsAndDisallowSharing");
    }

    ClusterComputeResource clusterResource = getClusterByName(cluster);

    // probably only of interest if someone actually entered a cluster name
    if (clusterResource == null && StringUtils.isNotBlank(cluster)) {
      logMessage(
          jLogger,
          "Cluster resource "
              + cluster
              + " does not exist, root folder will be used for getting resource pool and datastore");
    }

    if (resourcePoolName != null && !resourcePoolName.isEmpty()) {
      ResourcePool resourcePool = getResourcePoolByName(resourcePoolName, clusterResource);

      if (resourcePool == null) {
        throw new VSphereException("Resource pool \"" + resourcePoolName + "\" not found");
      }

      rel.setPool(resourcePool.getMOR());
    } else if (isResourcePoolRequired) {
      throw new VSphereException("You must specify a resource  pool  when using a template");
    }

    if (datastoreName != null && !datastoreName.isEmpty()) {
      Datastore datastore = getDatastoreByName(datastoreName, clusterResource);
      if (datastore == null) {
        throw new VSphereException("Datastore \"" + datastoreName + "\" not found!");
      }
      rel.setDatastore(datastore.getMOR());
    }

    return rel;
  }