Reputation: 167
I am writing an application that interfaces with VMWare vSphere client. It uses vijava library to do so. I need to find a NAS datastore on a host using IP Address of the NFS server and the name of export directory.
I can look up the datastores on that host and find out the ones that are NFS type, using HostFileSystemVolume.type, but how do I find the NASDatastoreInfo object for this Datastore now. Documentation is vast and I haven't gotten the time to read it all. I know that I need to get the NASDatastoreInfo object for the corresponding Datastore object I have found, but am not able to figure out any method that may return NASDatastoreInfo object. All you have is the DatastoreInfo object coming from the Datastore object.
Any help would be appreciated.
Thanks... footloose
Upvotes: 0
Views: 1220
Reputation: 167
This is how I managed to do it. The code may seem a little complex but here's the gist:
Look up all the volumes of this host and find the ones that have NFS file system built on them. For each of these volumes, iterate the Datastore list with the volume name = datastore name condition. Upon finding a match, get the NASDatastoreInfo object from the DatastoreInfo object and check for the remote NFS Server IP and export directory.
I checked HostFileSystemVolume.type=='NFS' for all the volumes on this host, and then check the Remote NFS Server IP and export directory for that volume.
Code already has a service instance 'si' to the vCenter and a HostSystem object named 'host'
HostDatastoreSystem hds = host.getHostDatastoreSystem();
HostDatastoreBrowser hdb = host.getDatastoreBrowser();
Datastore[] allDS = hdb.getDatastores();
HostConfigInfo hostConfigInfo = host.getConfig();
HostFileSystemVolumeInfo hostFSVolumeInfo = hostConfigInfo.getFileSystemVolume();
HostFileSystemMountInfo[] hostFSMountInfo= hostFSVolumeInfo.getMountInfo();
for (HostFileSystemMountInfo hfsmi : hostFSMountInfo) {
HostFileSystemVolume hfsv = hfsmi.getVolume();
if (hfsv.getType().equalsIgnoreCase("nfs")){
String dsName = hfsv.getName();
for(Datastore ds: allDS) {
DatastoreInfo di = ds.getInfo();
if (di.getName().equals(dsName)){
HostNasVolume nas = ((NasDatastoreInfo)di).getNas();
if (nas.getRemoteHost().equals(nfsServer) & nas.getRemotePath().equals(datastorePathOnNfsServer)) {
try {
} catch (HostConfigFault e) {
logger.error("ERROR : unmountNfsDatastore : Could not remove Datastore named: " + ds.getName() + " on " + "host: " + hostname , e);
ret = 1;
return ret;
} catch (ResourceInUse e) {
logger.error("ERROR : unmountNfsDatastore : Could not remove Datastore named: " + ds.getName() + " on " + "host: " + hostname , e);
ret = 1;
return ret;
} catch (NotFound e) {
logger.error("ERROR : unmountNfsDatastore : Could not remove Datastore named: " + ds.getName() + " on " + "host: " + hostname , e);
ret = 1;
return ret;
} catch (RuntimeFault e) {
logger.error("ERROR : unmountNfsDatastore : Could not remove Datastore named: " + ds.getName() + " on " + "host: " + hostname , e);
ret = 1;
return ret;
} catch (RemoteException e) {
logger.error("ERROR : unmountNfsDatastore : Could not remove Datastore named: " + ds.getName() + " on " + "host: " + hostname , e);
ret = 1;
return ret;
}
return 0;
}
}
}
}
}
logger.error("ERROR : unmountNfsDatastore : Could not find Datastore exported by " + nfsServer + " on " + "host: " + hostname);
ret = 0;
return ret;
hds.removeDatastore(ds);
Upvotes: 0
Reputation: 121609
You wouldn't be able to get any volume information about the NAS unless it was mounted.
I doubt you have that many mounted drives on your host.
SUGGESTION:
Just grab all mounted volumes
Check for a NASDataStoreInfo property on each of them
Simply ignore the volumes that don't have it.
Upvotes: 1