VMware Cloud Community
m200
Enthusiast
Enthusiast
Jump to solution

Autoselection of correct portgroup for deployment

I'm making an deployment workflow where the workflow itself decides several of the inputparameters for the actual deployment, based on presentation-inputs selections like "Security Zone", "Type of Enviroment", Required Redundant Storage etc.

Based on all of theese inputs, the scripts "knows" what cluster to deploy to (hardcoded). It automaticly then selects a host (at random) from that cluster, verifies it's state, and if it's in maintenance, it selects another one. It find's a suitable datastore of correct type, selects the one with the most free space, but also takes into concideration that datastores should not be filled above 80%. It then provisons the template, adds more ram (if selected), more cpu (if selected), adds additional harddrives, partitions then inside the VM etc etc.

The script works great and saves ut tons of time on deployment. But there is one annoying litte information I still have to provide, and that is the portgroup. I would like the script to select the portgroup based on just a VLAN ID given during the start of the workflow (presentation) because this would make the script more "selfservice" friendly and availlable to other people in the organization, and not just the vmware admins because they are the ones knowing what portgroups goes with wich hosts/clusters etc. The other parts of organizations like to focus more on placement into security zones and vlans, type of environment etc, not physical cluster placement, and virtual networking......

I have several clusters/hosts, some of wich are connected to vSwitches and some are connected to dvSwitches, and some connected to both, and I also have different dvSwitches. VLAN 600 could be part of several dvSwitches, but ofcourse under different PG-name for each dvswitch. It could be called "P_600" on one dvSwitch and PRD_MGMT on another dvSwitch for other clusters/hosts. And It's very easy to make selections during presentation indicating deployment to CLUSTER D1 and then by mistake or confusion (atleast for nonadmins) select PRD_MGMT instead of P_600, causing the deployment to hook up the network to a switch not visible to the host, causing the deployment to fail on jondomain operations etc. This is the reason for me wanting the script to do this part itself.

What I need is a piece of code using an cluster or a host as input, finding all switches to which this host is connected (both standard and distributed). Then loop through all portgroups on theese switches, and find a portgroup with the specified VLAN ID and return this portgroup as either VC:Network or VC:DistributedVirtualPortGroup based on what switch it was found in.  But I could find no way to do what I wanted using the default API's availlable.

I also tried to modify my wishes, and let some of the automation be "hardcoded" which would also work, just like the cluster's part (just not as fancy) and make a pice of code taking a specified hardcoded dvSwitch or vSwitch, loop trough all portgroups and find the portgroup with correct VLAN ID, returning it ito a VC:Network variable or a VC:DistributedVirtualPortGroup variable, but I couldn't get that to work either. Atleast not for dvSwitches, which is used for 90% of all deployments.... I just can't seem to find a working way to do this.

Anyone know how this could be done? In short, how do I loop through all portgroups on a dvswitch and return one with a specified VLAN ID? Any help appreciated!

Reply
0 Kudos
1 Solution

Accepted Solutions
m200
Enthusiast
Enthusiast
Jump to solution

From the code above, I modified it to this:

var dvPGS = VcPlugin.getAllDistributedVirtualPortgroups();

for each (pg in dvPGS)

                {

                               if (pg.config.distributedVirtualSwitch.name == dvSwitch)

                                               {

                                                               if (pg.config.defaultPortConfig.vlan.vlanId == vLAN)

                                                                              {

                                                                                              var SelectedPort=pg

                                                                              }

                                               }

                             

                }

  1. System.log ("The port "+SelectedPort.name+" belongs to "+dvSwitch+" and has vlan id: "+SelectedPort.config.defaultPortConfig.vlan.vlanId)

This code takes an input parameter of string (dvSwitch) and number (vLAN) and successfully locates a portgroup with the given vLAN on the dvswitch with it's name set like the dvswitch string, This script does exactly what I am looking for when it comes to dvSwitches, given that I hardcode what dvSwitch belongs to each cluster. It might be possible to rework this even further to actually look at connected host/parent etc instead of hardcoding the dvswitch name, but with this code I can actually get the job done, so that will probably have to wait for the next release of the workflow 🙂

Now I just have to make a working solution for vSwitches too 🙂

View solution in original post

Reply
0 Kudos
3 Replies
Burke-
VMware Employee
VMware Employee
Jump to solution

Lines 40-58 of the code Joerg posted in this thread may be of use for checking the VLAN of portgroups: how to get VLAN Id of Organization network.. I don't have time or environment to provide a more full answer, but hopefully that thread helps you out. It sounds like you have a pretty good grasp on vCO's capability Smiley Happy

ooh.. I just did a search in the vCO client for vlan and see that there are actually several actions that get vlan ids from stuff -- I suggest you do the same, maybe there is something there that can save you some time... com.vmware.library.vc.networking seems to have several to choose from.

Message was edited by: Burke Azbill

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
m200
Enthusiast
Enthusiast
Jump to solution

After googling the entire day, looking at other peoples codesnippets doing partly what I was looking for, reading though Vmware API-refererence docs etc, I managed to come up with some working code that I'm sure I can use for my script:

var dvPG = VcPlugin.getAllDistributedVirtualPortgroups();

var dvs = new Array();

for each (pg in dvPG)

                {

                               if (pg.config.distributedVirtualSwitch.name == dvSwitch)

                                               {

                                                               dvs.push(pg)

                                               }

                             

                }

for each (pg in dvs)

                {

                               System.log ("A Port belonging to: "+dvSwitch+" is "+pg.name+" and it has vlan id: "+pg.config.defaultPortConfig.vlan.vlanId)

                }

This codesnippet has an input of type string, called dvSwitch (given during start of Workflow, at "Presentation"). It loops through ALL dvportgroups on the system, and if it's connected to the dvSwitch I'm looking for (the one stored in the input-string) the portgroup get's pushed to a new array called pg

It then lists all portgroups and their respective vlan ID's to the System log.

From this, to a working code, presenting ONLY one specific portgroup with one specific VLAN ID, instead of all portgroups of the dvswitch is just a matter of an extra if-block during the first for each (pg in dvPG) loop i think 🙂 - Atleast I have gotten somewhere, and have some working code to modify further 🙂

Reply
0 Kudos
m200
Enthusiast
Enthusiast
Jump to solution

From the code above, I modified it to this:

var dvPGS = VcPlugin.getAllDistributedVirtualPortgroups();

for each (pg in dvPGS)

                {

                               if (pg.config.distributedVirtualSwitch.name == dvSwitch)

                                               {

                                                               if (pg.config.defaultPortConfig.vlan.vlanId == vLAN)

                                                                              {

                                                                                              var SelectedPort=pg

                                                                              }

                                               }

                             

                }

  1. System.log ("The port "+SelectedPort.name+" belongs to "+dvSwitch+" and has vlan id: "+SelectedPort.config.defaultPortConfig.vlan.vlanId)

This code takes an input parameter of string (dvSwitch) and number (vLAN) and successfully locates a portgroup with the given vLAN on the dvswitch with it's name set like the dvswitch string, This script does exactly what I am looking for when it comes to dvSwitches, given that I hardcode what dvSwitch belongs to each cluster. It might be possible to rework this even further to actually look at connected host/parent etc instead of hardcoding the dvswitch name, but with this code I can actually get the job done, so that will probably have to wait for the next release of the workflow 🙂

Now I just have to make a working solution for vSwitches too 🙂

Reply
0 Kudos