VMware Cloud Community
mccarthybri
Enthusiast
Enthusiast
Jump to solution

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

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
Jump to solution

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

Reply
0 Kudos
4 Replies
SG1234
Enthusiast
Enthusiast
Jump to solution

you should probably try this in powercli forum

Reply
0 Kudos
vThinkBeyondVM
VMware Employee
VMware Employee
Jump to solution

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.


----------------------------------------------------------------
Thanks & Regards
Vikas, VCP70, MCTS on AD, SCJP6.0, VCF, vSphere with Tanzu specialist.
https://vThinkBeyondVM.com/about
-----------------------------------------------------------------
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
Jump to solution

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"  ) );

Reply
0 Kudos
vThinkBeyondVM
VMware Employee
VMware Employee
Jump to solution

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


----------------------------------------------------------------
Thanks & Regards
Vikas, VCP70, MCTS on AD, SCJP6.0, VCF, vSphere with Tanzu specialist.
https://vThinkBeyondVM.com/about
-----------------------------------------------------------------
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.

Reply
0 Kudos