VMware Cloud Community
aslk5
Enthusiast
Enthusiast

How to create folder in datastore using datastoreNamespaceManager

I want to create a folder at the root of my datastore and to ensure I properly handle a vsan datastore (I don't have one to test against at the moment), this page https://www.virtuallyghetto.com/2014/03/exploring-vsan-apis-part-7-vsan-datastore-folder-management....  says I should be using datastoreNamespaceManager but I can't figure out how to use it in vRO.

I'm doing this find my datastore

var mo;

for each (ds in host.datastore){

     if (ds.name == "mydatastore")

          mo = ds.moref;

}

and then I think I need to do something like

sc = something.retrieveServiceContent();

sc.datastoreNamespaceManager.createDirectory(mo, "myfolder" ,null);

but I can't figure out what "something" should be to access the function.

Anyone have any ideas?

Reply
0 Kudos
3 Replies
iiliev
VMware Employee
VMware Employee

var connection = host.datastore.sdkConnection; // you can also use some other way to get the vCenter connection, for example as input parameter

var moref = new VcManagedObjectReference("ServiceInstance", "ServiceInstance"); 

var serviceInstance = VcPlugin.convertToVimManagedObject(connection, moref); 

var serviceContent = serviceInstance.retrieveServiceContent(); 

var result = serviceContent.datastoreNamespaceManager.createDirectory(...);

Reply
0 Kudos
aslk5
Enthusiast
Enthusiast

I'm getting an error:

Cannot convert ManagedObjectReference: type = Datastore, value = datastore-637, serverGuid = null to com.vmware.o11n.sdk.modeldriven.ModelWrapper

i changed from the managed object reference to the datastore object and it goes through but nothing is created:

var myDs;

for each (ds in host.datastore){

     if (ds.name == "mydatastore")

          myDs = ds;

}

var sdkConn = host.sdkConnection;

var moref = new VcManagedObjectReference("ServiceInstance", "ServiceInstance"); 

var serviceInstance = VcPlugin.convertToVimManagedObject(sdkConn, moref); 

var serviceContent = serviceInstance.retrieveServiceContent(); 

var result = serviceContent.datastoreNamespaceManager.createDirectory(myDs, "abc" ,null);

With the code above I now get the error: Cannot complete file creation operation and if I try at the mob browser I also see: Datastore not supported for directory creation by DatastoreNamespaceManager

Does that mean the datastoreNamespaceManager is really only for vSAN (and vvols??) but I need to continue using fileManager for non vsan? At least I seem to have working vsan capable code now so i' future ready Smiley Happy

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

No, it is not a matter of vSAN vs non-vSAN.

The documentation for DatastoreNamespaceManager says that "The DatastoreNamespaceManager managed object exposes APIs for manipulating top-level directories of datastores which do not support the traditional top-level directory creation."

Which means you have to check the value of the flag topLevelDirectoryCreateSupported (accessible via myDs.capability.topLevelDirectoryCreateSupported) to decide whether to use DatastoreNamespaceManager or FileManager.

Reply
0 Kudos