VMware {code} Community
MikeFriberg
Contributor
Contributor

Using Perl to create a nested resource pool

I've used William Lam's "createResourcepool.pl" script as a guide and am trying to convert it to where it will create a child/nested resource pool.  The group I work for has a Resource Pool assigned to us (IHPS), and I'd like to be able to create child/nested Resource Pools under that to help group project team work.

Below is the code I've leveraged and modified:

#!/usr/bin/perl -w

# William Lam 11/28/09

# http://engineering.ucsb.edu/~duonglt/vmware

use strict;

use warnings;

use VMware::VILib;

use VMware::VIRuntime;

my %opts = (

    resourcepool  => {

        type => "=s",

        help => "Name of Resource Pool to create",

    required => 1,

        },

);

Opts::add_options(%opts);

Opts::parse();

Opts::validate();

Util::connect();

my ($folder, $resourcepool, $parent_pool_view, $parentresourcepool);

$resourcepool = Opts::get_option('resourcepool');

my $parent_pool = "IHPS";

$parent_pool_view = Vim::find_entity_view(view_type => 'ResourcePool', filter => { name => $parent_pool});

unless($parent_pool_view) {

        Util::disconnect();

        die "Unable to find parent Resource    Pool: \"$parent_pool\"\n";

}

$parentresourcepool = Vim::get_view(mo_ref => $parent_pool_view->resourcePool);

eval {

    my $sharesLevel = SharesLevel->new('normal');

    my $cpuShares = SharesInfo->new(shares => 0, level => $sharesLevel);

    my $memShares = SharesInfo->new(shares => 0, level => $sharesLevel);

    my $cpuAllocation = ResourceAllocationInfo->new(expandableReservation => 'true', limit => -1, reservation => 0, shares => $cpuShares);

    my $memoryAllocation = ResourceAllocationInfo->new(expandableReservation => 'true', limit => -1, reservation => 0, shares => $memShares);

    my $rp_spec = ResourceConfigSpec->new(cpuAllocation => $cpuAllocation, memoryAllocation => $memoryAllocation);

    my $newRP = $parentresourcepool->CreateResourcePool(name => $resourcepool, spec => $rp_spec);

    if($newRP->type eq 'ResourcePool') {

        print "Successfully created new ResourcePool: \"" . $resourcepool . "\"\n";

    } else {

        print "Error: Unable to create new ResourcePool: \"" . $resourcepool . "\"\n";

    }

};

if($@) { print "Error: " . $@ . "\n"; }

Util::disconnect();

When I run this from (from a Windows Server 2008 R2 system with the 5.1 Perl SDK installed), I get this error:

Can't call method "type" on unblessed reference at C:\Program Files (x86)/VMware/VMware vSphere CLI/Perl/lib/VMware/VICommon.pm line 1501.

I'm new enough to Perl that I'm not sure how to resolve this - any ideas/hints?

Thanks,
Mike

Reply
0 Kudos
2 Replies
MikeFriberg
Contributor
Contributor

After a quick break, I added a couple debug statements to determine where the error was being thrown and it turns out it wasn't where I first thought.

I also found a couple other similar posts that I hadn't found before that led me in the right direction - here is the modified code that now works!

The basic issue was that I thought the CreateResourcePool was called with a mo_ref to the parent Resource Pool where instead it is called with the result of the Vim::find_entity_view (please excuse the layman's terms).

#!/usr/bin/perl -w

# William Lam 11/28/09

# http://engineering.ucsb.edu/~duonglt/vmware

use strict;

use warnings;

use VMware::VILib;

use VMware::VIRuntime;

my %opts = (

    resourcepool  => {

        type => "=s",

        help => "Name of Resource Pool to create",

    required => 1,

        },

);

Opts::add_options(%opts);

Opts::parse();

Opts::validate();

Util::connect();

my ($folder, $resourcepool, $parent_pool_view, $parent_pool, $parentresourcepool);

$resourcepool = Opts::get_option('resourcepool');

# Parent Resource Pool that all child/nested Resource Pools must be created under

$parent_pool = "IHPS";

$parent_pool_view = Vim::find_entity_view(view_type => 'ResourcePool', filter => { name => $parent_pool});

unless($parent_pool_view) {

        Util::disconnect();

        die "Unable to find parent Resource    Pool: \"$parent_pool\"\n";

}

eval {

    my $sharesLevel = SharesLevel->new('normal');

    my $cpuShares = SharesInfo->new(shares => 0, level => $sharesLevel);

    my $memShares = SharesInfo->new(shares => 0, level => $sharesLevel);

    my $cpuAllocation = ResourceAllocationInfo->new(expandableReservation => 'true', limit => -1, reservation => 0, shares => $cpuShares);

    my $memoryAllocation = ResourceAllocationInfo->new(expandableReservation => 'true', limit => -1, reservation => 0, shares => $memShares);

    my $rp_spec = ResourceConfigSpec->new(cpuAllocation => $cpuAllocation, memoryAllocation => $memoryAllocation);

    my $newRP = $parent_pool_view->CreateResourcePool(name => $resourcepool, spec => $rp_spec);

    if($newRP->type eq 'ResourcePool') {

        print "Successfully created new ResourcePool: \"" . $resourcepool . "\"\n";

    } else {

        print "Error: Unable to create new ResourcePool: \"" . $resourcepool . "\"\n";

    }

};

if($@) { print "Error: " . $@ . "\n"; }

Util::disconnect();

Thanks again to William Lam for the code base to start this!

Thanks,

Mike

Reply
0 Kudos
burnbrighter
Contributor
Contributor

What was the outcome of this?  I am trying to use this script and running in to the same:

Unable to find parent Resource    Pool: "IHPS"

Reply
0 Kudos