mccarthybri
Enthusiast
Enthusiast

Does anyone have a simple example of creating VM->VM affinities using the PERL APIs?

Jump to solution

The question pretty much says it all. Does somebody have a perl snippet of creating affinities between VMs.

1 Solution

Accepted Solutions
mccarthybri
Enthusiast
Enthusiast

Thank you, Vikas, for the example and the advice. Since I was abel to get it working, I thought I'd share the example code:

    my $vm_name_list;    # A list of the vm names.

    my @vm_morefs;      # Filled in with managed object refes to the VMs, this detail not included.

    my $cluster_name;    # Name of the cluster to create the rule in

#        Find the Cluster Compute Resource Object.

    my $cluster_views = Vim::find_entity_views( view_type => 'ClusterComputeResource',
                                                filter    => { name => $cluster_name } );
    if (!@$cluster_views) {
        die "No cluster compute resource named $cluster_name found\n";
    }

    my $cluster_view = $cluster_views->[0];

    my $rule_name = "Affinity for $vm_name_list";

#         Create the nest of objects required for ReconfigureCluster_Task:
#
#         ClusterConfigSpec
#             rulesSpec -> ClusterRuleSpec[]
#                              operation -> ArrayUpdateInformation
#                                               enum( add, edit, remove )
#                              info      -> ClusterAffinityRuleSpec

    my $aff_rule_spec = new ClusterAffinityRuleSpec( name      => $rule_name,
                                                     enabled   => "TRUE",
                                                     mandatory => "TRUE",
                                                     vm        => \@vm_morefs  );

    my $array_update_operation = new ArrayUpdateOperation( "add" );

    my $clus_rule_spec = new ClusterRuleSpec( operation => $array_update_operation, info => $aff_rule_spec );

    my @clus_rule_specs;
    push( @clus_rule_specs, $clus_rule_spec );

    my $cluster_config_spec = new ClusterConfigSpec( rulesSpec => \@clus_rule_specs );
    $cluster_view->ReconfigureCluster_Task( spec => $cluster_config_spec,

                                                                                    modify => "TRUE"  ) );

View solution in original post

0 Kudos
4 Replies
SG1234
Enthusiast
Enthusiast

you should probably try this in powercli forum

0 Kudos
vThinkBeyondVM
VMware Employee
VMware Employee

I assume you are talking about DRS affinity rules. I deal with DRS with JAVA vSphere SDKs. However, you can easily create it using perl also. (All APIs are same, just need to follow perl constructs etc. )

Please refer : Below java code for creating VM-VM affinity rule, refer the same & design using PERL.

http://sourceforge.net/p/vijava/code/HEAD/tree/v5.1/src/com/vmware/vim25/mo/samples/cluster/DrsAffRu...

Take a look at the ReconfigureComputeResource_Task()

http://vijava.sourceforge.net/vSphereAPIDoc/ver5/ReferenceGuide/vim.cluster.ConfigSpecEx.html

You need to concentrate on "ruleSpec" for VM-VM affinity rules. and for VM-host rule: Groupspec property.

If it is useful, plz mark answer as correct or helpful.
----------------------------------------------------------------
Thanks & Regards
Vikas, VCP50, MCTS on AD, SCJP6.0.
http://vThinkBeyondVM.com
-----------------------------------------------------------------
Disclaimer: Any views or opinions expressed here are strictly my own. I am solely responsible for all content published here. Content published here is not read, reviewed or approved in advance by VMware and does not necessarily represent or reflect the views or opinions of VMware.

mccarthybri
Enthusiast
Enthusiast

Thank you, Vikas, for the example and the advice. Since I was abel to get it working, I thought I'd share the example code:

    my $vm_name_list;    # A list of the vm names.

    my @vm_morefs;      # Filled in with managed object refes to the VMs, this detail not included.

    my $cluster_name;    # Name of the cluster to create the rule in

#        Find the Cluster Compute Resource Object.

    my $cluster_views = Vim::find_entity_views( view_type => 'ClusterComputeResource',
                                                filter    => { name => $cluster_name } );
    if (!@$cluster_views) {
        die "No cluster compute resource named $cluster_name found\n";
    }

    my $cluster_view = $cluster_views->[0];

    my $rule_name = "Affinity for $vm_name_list";

#         Create the nest of objects required for ReconfigureCluster_Task:
#
#         ClusterConfigSpec
#             rulesSpec -> ClusterRuleSpec[]
#                              operation -> ArrayUpdateInformation
#                                               enum( add, edit, remove )
#                              info      -> ClusterAffinityRuleSpec

    my $aff_rule_spec = new ClusterAffinityRuleSpec( name      => $rule_name,
                                                     enabled   => "TRUE",
                                                     mandatory => "TRUE",
                                                     vm        => \@vm_morefs  );

    my $array_update_operation = new ArrayUpdateOperation( "add" );

    my $clus_rule_spec = new ClusterRuleSpec( operation => $array_update_operation, info => $aff_rule_spec );

    my @clus_rule_specs;
    push( @clus_rule_specs, $clus_rule_spec );

    my $cluster_config_spec = new ClusterConfigSpec( rulesSpec => \@clus_rule_specs );
    $cluster_view->ReconfigureCluster_Task( spec => $cluster_config_spec,

                                                                                    modify => "TRUE"  ) );

View solution in original post

0 Kudos
vThinkBeyondVM
VMware Employee
VMware Employee

It is great that it worked. I will try this example using Perl as well.

If it is useful, plz mark answer as correct or helpful.
----------------------------------------------------------------
Thanks & Regards
Vikas, VCP50, MCTS on AD, SCJP6.0.
http://vThinkBeyondVM.com
-----------------------------------------------------------------
Disclaimer: Any views or opinions expressed here are strictly my own. I am solely responsible for all content published here. Content published here is not read, reviewed or approved in advance by VMware and does not necessarily represent or reflect the views or opinions of VMware.

0 Kudos