I am trying to write a VI Perl script which removes the "snap-00000001-" from the beginning of a datastore name for an automated recovery script I am writing.
For example, I have a recovered datastore named "snap-00000003-lun1-templates-iso" and I want to rename that datastore to "lun1-templates-iso" automatically using a script:
\----
my $host = Opts::get_option('host');
my $host_view = Vim::find_entity_view(view_type => 'HostSystem',
filter=> \{name => $host});
print "\nThe name of this host is ", $host_view->name . "\n\n";
my $mountinfo = $host_view->config->fileSystemVolume->mountInfo;
foreach (@$mountinfo) {
if ($_->volume->name =~ m/snap/)
{
print "Old Datastore Name: " . $_->volume->name . "\n";
my $volumename = $_->volume->name;
substr($volumename, 0, 14) = "";
print "New Datastore Name: " . $volumename . "\n";
$_->volume->name->property ($volumename);
}
}
\----
every time I execute this script using:
perl snapvolrename.pl --server 192.168.1.10 --username Administrator --password MyPassword --host esxserver1.mydomain.com
I get the following result:
The name of this host is esxserver1.mydomain.com
Old Datastore Name: snap-00000003-lun1-templates-iso
New Datastore Name: lun1-templates-iso
Can't locate object method "property" via package "snap-00000003-lun1-templates-iso" (perhaps you forgot to load "snap-00000003-lun1-templates-iso"?) at sn
apvollist.pl line 46.
Can anyone help me figure out how to rename a datastore?
Thanks,
-Darin
You have to make a get_view using the ManagedObjectReferences from the datastore branch: $datastore_view = Vim::get_view(mo_ref => $datastore_moref); The datastore_view contains all attributes of a datastore.
You can now iterate the array, search for the desired datastore and invoke the rename method.
I don't know if it is possible to rename a datastore that way.
You should better get the datastore-array of the host via $host_view->datastore, search for the desired datastore and then use the method RenameDatastore on that datastore.
Yeah, I thought of that, but the datastore branch only shows the API datastore name... here is the dumper result from that tree:
\----
'datastore' => [
bless( {
'type' => 'Datastore',
'value' => 'datastore-8236'
}, 'ManagedObjectReference' ),
bless( {
'type' => 'Datastore',
'value' => 'datastore-8246'
}, 'ManagedObjectReference' ),
bless( {
'type' => 'Datastore',
'value' => 'datastore-8248'
}, 'ManagedObjectReference' )
]
\----
Now, the problem is that I cant find a way to correlate which datastore is named which... for example: Is "datastore-8246" is really named "snap-00000003-lun1-templates-iso"?
The "HostSystem.config.fileSystemVolume.mountInfo.volume.name" object is the only place where I can find the string name of the datastore...
Any thoughts?
You have to make a get_view using the ManagedObjectReferences from the datastore branch: $datastore_view = Vim::get_view(mo_ref => $datastore_moref); The datastore_view contains all attributes of a datastore.
You can now iterate the array, search for the desired datastore and invoke the rename method.
Thanks! I appreciate your help... That did the trick!
For all those interested here is the final code:
\----
my $host = Opts::get_option('host');
my $host_view = Vim::find_entity_view(view_type => 'HostSystem',
filter=> \{name => $host});
my $datastore_view = Vim::get_views(mo_ref_array => $host_view->datastore);
foreach (@$datastore_view) {
if ($_->info->name =~ m/snap/)
{
print "Old Datastore Name: " . $_->info->name . "\n";
my $volumename = $_->info->name;
substr($volumename, 0, 14) = "";
print "New Datastore Name: " . $volumename . "\n";
$_->RenameDatastore(newName => $volumename);
}
}