Hi,
I have created my own variant of the SDK sample script "vmprovision.pl" to clone a vm template. My Perl script usually works but when cloning across datacenters I am getting the error message: "A specified parameter was not correct. spec.location.folder"
I am handing the Folder reference over to CloneVM after finding the folder reference with "find_entity_view" like this:
my $vm_folder = get_folder_view('name' => Opts::get_option('folder'));
...
my $relocate_spec = VirtualMachineRelocateSpec->new(
datastore => $ds_view,
pool => $cs_view->resourcePool
);
# Create CloneSpec corresponding to the RelocateSpec
# XXX Don't hardcode things here, like 'powerOn', 'template', etc.
my $clone_spec = VirtualMachineCloneSpec->new(
powerOn => 0,
template => 0,
location => $relocate_spec,
);
# Clone source vm, pass resource pool, folder and new vm name
$vm_view->CloneVM(folder => $vm_folder->parent,
name => Opts::get_option('vmname_destination'),
spec => $clone_spec) or die "Could not create new VM";
}
sub get_folder_view {
# input: folder name
# outupt: folder reference
my %args = @_;
my $folder_name = $args{name};
print "Folder name: $folder_name\n", if ($DEBUG);
my $vm_folder = Vim::find_entity_view(view_type => 'Folder',
filter => {'name' => $folder_name});
if(!defined($vm_folder)) {
die "Could not find a folder named \"$folder_name\"\n";
}
return $vm_folder;
}
Our setup is a vCenter with multiple datacenters which each contain multiple DRS clusters (not using storage-DRS). The templates I am trying to clone resides in another Datacenter and Cluster.
What am I doing wrong here?
Is there any way to get a reference to the root vm folder of a cluster?
Thanks for any hints or help you can provide!
Thomas Willert
The folder won't tied to the cluster, but to the datacenter. So you'd want to get the rootFolder from the datacenter object, or pick another folder by searching for the specific name or path.
$datacenter_view->{'vmFolder'};
The folder won't tied to the cluster, but to the datacenter. So you'd want to get the rootFolder from the datacenter object, or pick another folder by searching for the specific name or path.
$datacenter_view->{'vmFolder'};
Thanks for your fast reply. I will try it out as soon as I can, but we are having some other vCenter problems so it might take a few days before I am able to.
I have post my findings here.
/ Thomas
Your solution worked fine, thanks a lot!
Currently I have hardcoded my cluster-to-datacenter mapping, but I hope to replace that soon with a proper tree walk to find out which datacenter a cluster belongs to. Unless there a faster to way to find out which datacenter a cluster belongs to?
Cheers,
Thomas
There isn't a direct property you have to query, so you'll have to ascend (or descend) the tree. Probably the quickest way is to create a container view with the starting object as the rootFolder, getting just the parent properties of all objects. From there you can ascend the tree to get parent objects. This requires building a simple spec (not as complicated as a TraversalSpec) that will fetch the right data, then storing the results in a hash you can recurse on (or perhaps using a tree structure from CPAN).
You can also use the begin_entity parameter of the find_entity_views() call. You could do something like the following:
$datacenter_views = Vim::find_entity_views(view_type => 'Datacenter', properties => ['name']);
$cluster_views = [ ];
foreach my $dc (@$datacenter_views) {
my $child_clusters = Vim::find_entity_views(view_type => 'ClusterComputeResource', properties => ['name'], begin_entity => $dc);
foreach my $cluster (@$child_clusters) {
$cluster->{'_parent_datacenter'} = $dc;
push @$cluster_views, $cluster;
}
}
Then you'll be able to find your clusters by name (simple grep) from the cluster_views. Since we're keeping the properties array simple (just name), it should be pretty quick. You basically have a simple cluster to datacenter map that can be queried without hitting vCenter iteratively. If you're extra concerned about cycles and calls against the API for performance, then you may want to use the ContainerView method (which can leverage WaitForUpdatesEx -- this would let you do efficient change updates on the tree if you had a long running program).
I have done this before, but I'm not sure my examples are easy to copy and paste. If I find some cycles this afternoon, I'll see if can convert one of them into something you can leverage in your tool.
For the last part I did a tree walk like you suggested. I am building a hash of datacenters whose values contain an array of their clusters.
Thanks for your help, its much appriciated!
/ Thomas Willert
