VMware {code} Community
MR-Z
VMware Employee
VMware Employee
Jump to solution

create directory on a datastore...

Anyone has a sample code on how to use the datastoreNamespaceManager API to create a directory on a datastore?

Reply
0 Kudos
1 Solution

Accepted Solutions
stumpr
Virtuoso
Virtuoso
Jump to solution

use strict;

use warnings;

use VMware::VIRuntime;

Opts::parse();

Opts::validate();

Util::connect();

my ($sc, $datastoreNS, $ds, $path, $supported);

$sc = Vim::get_service_content();

$ds = Vim::find_entity_view(view_type => "Datastore", filter => { 'name' => 'ds-nfs-01' });

$datastoreNS = Vim::get_view(mo_ref => $sc->datastoreNamespaceManager);

$supported = $ds->{'capability'}->{'topLevelDirectoryCreateSupported'};

die "datastoreNamespaceManager is not supported on this datastore (topLevelDirectoryCreateSupported=1)" if $supported;

$path = $datastoreNS->CreateDirectory(datastore => $ds, displayName => "TestDir/");

print "New directory created: $path\n";

But bear in mind this call is only for datastores that DO NOT support topLevelDirectoryCreateSupported capabilities (vSphere 5.5 Documentation Center )

You'll need to use FileManager otherwise:

use strict;

use warnings;

use VMware::VIRuntime;

Opts::parse();

Opts::validate();

Util::connect();

my ($sc, $fm, $dc, $path);

$sc = Vim::get_service_content();

$fm = Vim::get_view(mo_ref => $sc->fileManager);

$dc = Vim::find_entity_view(view_type => "Datacenter", filter => { 'name' => 'DC01' });

$path = "[ds-nfs-01] TestDir";

$fm->MakeDirectory(name => $path, datacenter => $dc, createParentDirectories => 1);

print "Directory successfully created\n";

Reuben Stump | http://www.virtuin.com | @ReubenStump

View solution in original post

Reply
0 Kudos
4 Replies
stumpr
Virtuoso
Virtuoso
Jump to solution

use strict;

use warnings;

use VMware::VIRuntime;

Opts::parse();

Opts::validate();

Util::connect();

my ($sc, $datastoreNS, $ds, $path, $supported);

$sc = Vim::get_service_content();

$ds = Vim::find_entity_view(view_type => "Datastore", filter => { 'name' => 'ds-nfs-01' });

$datastoreNS = Vim::get_view(mo_ref => $sc->datastoreNamespaceManager);

$supported = $ds->{'capability'}->{'topLevelDirectoryCreateSupported'};

die "datastoreNamespaceManager is not supported on this datastore (topLevelDirectoryCreateSupported=1)" if $supported;

$path = $datastoreNS->CreateDirectory(datastore => $ds, displayName => "TestDir/");

print "New directory created: $path\n";

But bear in mind this call is only for datastores that DO NOT support topLevelDirectoryCreateSupported capabilities (vSphere 5.5 Documentation Center )

You'll need to use FileManager otherwise:

use strict;

use warnings;

use VMware::VIRuntime;

Opts::parse();

Opts::validate();

Util::connect();

my ($sc, $fm, $dc, $path);

$sc = Vim::get_service_content();

$fm = Vim::get_view(mo_ref => $sc->fileManager);

$dc = Vim::find_entity_view(view_type => "Datacenter", filter => { 'name' => 'DC01' });

$path = "[ds-nfs-01] TestDir";

$fm->MakeDirectory(name => $path, datacenter => $dc, createParentDirectories => 1);

print "Directory successfully created\n";

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos
MR-Z
VMware Employee
VMware Employee
Jump to solution

Thanks you very much, Stumpr!

I did have to use the fileManager call, instead of the datastorenamespacemanager. Thanks for pointing out the different application of those two API calls. Wondering why the datastoreNamespacemanager only supports the creation on datastore that does NOT support top level directory creation?

Reply
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

The datastoreNamespaceManager API is specifically built for VSAN (Virtual SAN) based datastores. For regular Datstores, you should use the FileManager for directory creation/deletion and for VSAN Datastores, it is recommend you use the datastoreNamespaceManager.

MR-Z
VMware Employee
VMware Employee
Jump to solution

Got it. Thanks William.

Reply
0 Kudos