Leveraging the VI3 SDK with .NET, part three - Exploring the SDK

The preferred mechanism for programmatically interacting with VI3 is the SDK. .NET can help you to harness the power of the SDK. In part three of this series, Andrew Kutz describes the beginning steps you can take on your path to leveraging the VI3 SDK with .NET using C#.

The SDK is the preferred mechanism for programmatically interacting with VI3 is the SDK, a powerful tool that can be harnessed with another equally powerful tool, .NET. In this tip, I will describe the beginning steps you can take on your path to leveraging the VI3 SDK with .NET using C#.

This article is the third in a series designed to help developers understand how to leverage the VI3 SDK with .NET and C#. If you haven't already, it would be best to go back and read part one and part two before you proceed.

We will begin right where we left off by expanding on the hostshow program. This new program, pgadd, will allow users to add port groups to existing VLANs on multiple ESX servers. I wrote this program about two months ago; and now, I know it was a great idea because another program was just released that does the same thing. Thanks to the guys at Run-Virtual for this awesome GUI app; it will be a mainstay in every VI3 administrator's arsenal. And those admins that prefer the command-line can still write their own app to the tune of the framework I provide in this article.

The source code for pgadd starts to differ from the hostshow code at line 300 where we previously set the property specification object to collect the name property of the HostSystem object. For this program, we need to tell the property specification object to also collect the "config" and "configManager" properties.

 <code> // Collect the name property from this object, as well as the config and // configManager properties. The config property is the HostConfigInfo // object associated with the host. It includes useful information about // the ESX server -- including information about the host's virtual switches. // The configManager is the HostConfigManager object for the host. // It is what is used to get access to the ESX server's network system // configuration.
 pspec.pathSet = new string[] { "name","config", "configManager" };

 

 // Build the PropertyFilterSpec and set its PropertySpecificiation (propSet) // and ObjectSpecification (objectset) attributes to pspec and ospec // respectively.
 PropertyFilterSpec pfspec = new PropertyFilterSpec(); pfspec.propSet = new PropertySpec[] { pspec }; pfspec.objectSet = new ObjectSpec[] { ospec };

 

 // Retrieve the property values from the VI3 SDK Web service.
 ObjectContent[] occoll = vim_svc.RetrieveProperties( vim_svc_content.propertyCollector, new PropertyFilterSpec[] { pfspec } );

 

 // Iterate through the results of the search. In this case, all the // objects we will be looking at are ESX host servers.
 foreach ( ObjectContent oc in occoll ) {

 

 // We only care about ESX hosts, because those are the only objects // that can have networks associated with them.
 if ( oc.obj.type == "HostSystem" ) {

 

 // Get the host config info object.
 HostConfigInfo host_cfg_info = ( HostConfigInfo ) oc.propSet[ 0 ].val;

 

 // Get the host configuration manager so that we can use it // to get the a managed object reference to the host's // networkSystem object.
 HostConfigManager host_cfg_mgr = ( HostConfigManager ) oc.propSet[ 1 ].val;

 

 // Get the name of the ESX host.
 string host_name = Convert.ToString( oc.propSet[ 2 ].val, CultureInfo.CurrentCulture );

 

 // Get a managed object reference to the host's networkSystem // object from the host configuration manager.
 ManagedObjectReference host_net_sys = ( ManagedObjectReference ) host_cfg_mgr.networkSystem;

 

 // Iterate through all the port groups specified in the // command-line options
 foreach ( PortGroup clpgv in cmd_line_opts.PortGroups ) {

 

 // Check to see if we should add the current port group // to the current host by comparing the name of the current // host to the name of the host associated with the current // port group from the command line options.
 bool cmd_line_host_name_matches_host_name = false;

 

 // If the host name from the command line options is // a star character, then we have a match.
 if ( clpgv.HostName == "*" ) { cmd_line_host_name_matches_host_name = true; }

 

 // Match the current host against the host from the // command-line options by first matching on only the // the hostname part of the FQDN, and if there is still // no match, then by matching against the entire FQDN.
 else {

 

 // Match the hostname part.
 Regex rx_cmd_line_host_name = new Regex( @"(?
 
  [^.]+)\." ); Match m_cmd_line_host_name = rx_cmd_line_host_name.Match( host_name ); if ( Regex.IsMatch( m_cmd_line_host_name.Groups[ "hn" ].Value, clpgv.HostName, RegexOptions.IgnoreCase ) ) { cmd_line_host_name_matches_host_name = true; }

 

 

 // Match the FQDN.
 else if ( Regex.IsMatch( host_name, clpgv.HostName, RegexOptions.IgnoreCase ) ) { cmd_line_host_name_matches_host_name = true; } }

 

 // If the current host does not match the host name associated // with the current port group, then continute to the next iteration // of the loop with the next host.
 if ( !cmd_line_host_name_matches_host_name ) { continue; }

 

 // Iterate through all of the virtual switches on the current host.
 foreach ( HostVirtualSwitch host_vswitch in host_cfg_info.network.vswitch ) {

 

 // Compare the name of the current virtual switch with the virtual // switch associated with the current port group from the // command line options.
 if ( clpgv.VSwitchName == "*" || Regex.IsMatch( host_vswitch.name, clpgv.VSwitchName, RegexOptions.IgnoreCase ) ) {

 

 // Check to see if a port group with the same name already // exists; if it does, we cannot add another port group with // the same name because that would cause an error.
 bool host_port_group_exists = false; foreach ( string host_pgname in host_vswitch.portgroup ) { string cmd_line_pgname_formatted = string.Format( CultureInfo.CurrentCulture, "key-vim.host.PortGroup-{0}", clpgv.PortGroupName ); if ( Regex.IsMatch( cmd_line_pgname_formatted, host_pgname ) ) { host_port_group_exists = true; continue; } }

 

 // Do not add the port group if it already exists; instead, // print out the port group's information preceding it with // a tilde character, ~, to show that the port group already // exists.
 if ( host_port_group_exists ) { Console.WriteLine( "~,{0},{1},{2},{3}", host_name, host_vswitch.name, clpgv.PortGroupName, clpgv.VlanID ); }

 

 // Add the port group.
 else {

 

 // To add a port group, we must build a aHostPortGroupSpec // object. This is easy -- we just instantiate // the class and give it a name, a blank network // policy, a VLAN ID and most importantly, we set // the name of the virtual switch it is to be associated // with.
 HostPortGroupSpec hpgs = new HostPortGroupSpec(); hpgs.name = clpgv.PortGroupName; hpgs.policy = new HostNetworkPolicy(); hpgs.vlanId = clpgv.VlanID; hpgs.vswitchName = host_vswitch.name; if ( !cmd_line_opts.Debug ) { vim_svc.AddPortGroup( host_net_sys, hpgs ); } Console.WriteLine( "+,{0},{1},{2},{3}", host_name, host_vswitch.name, clpgv.PortGroupName, clpgv.VlanID ); } } } } } } </code>

You can download the complete source code for pgadd at my Website. Between hostshow and pgadd, I hope that I have shown you how easy it is to use the .NET framework in combination with C# to create some fantastically simple and yet powerful applications that you can use to interface with your VI3 systems.

This is the last part in my series on how to leverage the VI3 SDK with .NET and C#. If you have any questions about the VI3 SDK, .NET or C# please feel free to e-mail me at akutz@lostcreations.com. And be sure to check out the vish2 project, which is filled with examples that expand on what we have covered in this series.

Andrew Kutz is deeply embedded in the dark, dangerous world of virtualization. Andrew is an avid fan of .NET, open source, Terminal Services, coding and comics. He is a Microsoft Certified Solutions Developer (MCSD), a SANS/GIAC Certified Windows Security Administrator (GCWN) and a VMware Certified Professional (VCP) in VI3. Andrew graduated from the University of Texas at Austin with a BA in Ancient History and Classical Civilization and currently lives in Austin with his wife Mandy and their two puppies, Lucy and CJ.
 

This was first published in December 2006

Dig deeper on Scripting administrative tasks

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchServerVirtualization

SearchVirtualDesktop

SearchDataCenter

SearchCloudComputing

Close