Using VI Toolkit (For Windows) From .NET

10,036 views

Published on

This slideshow shows you one way of simplifying .NET development for VMware VI using the VI Toolkit (for Windows)

Published in: Technology
  • Be the first to comment

Using VI Toolkit (For Windows) From .NET

  1. 1. VI Toolkit (for Windows) and .NET development Carter Shanklin VMware, Inc. July 2008
  2. 2. The VI Toolkit (for Windows) <ul><ul><li>We’ve built a lot of very useful PowerShell functionality in to the VI Toolkit (for Windows). </li></ul></ul><ul><ul><li>If you’re developing a compiled application, you should consider using PowerShell via runspaces to leverage all our hard work. </li></ul></ul><ul><ul><li>If you’ve evaluated this option and are convinced it’s not for you, this slide show will show you another option. </li></ul></ul>
  3. 3. Not planning on using PowerShell? <ul><ul><li>The VI Toolkit (for Windows) provides an assembly that allows simplified development, VMware.Vim.dll from a .NET environment. </li></ul></ul><ul><ul><li>28 methods are provided that make programming VI easy. </li></ul></ul><ul><ul><li>Documentation on programming using this assembly is provided within the VI Toolkit (for Windows) install. </li></ul></ul><ul><ul><li>The library provides access to the entire set of VMware Web Services. </li></ul></ul>
  4. 4. Some background. <ul><ul><li>This assembly provides a wrapper to the VMware VI Web Services APIs, along with some helper functions to simplify things. </li></ul></ul><ul><ul><li>Before we can talk about the assembly more, we need to take a look at VI Web Services. </li></ul></ul>
  5. 5. Some background: VMware web services <ul><ul><li>VMware uses an extensive database to store information about objects like hosts and virtual machines. </li></ul></ul><ul><ul><li>These objects are exposed through the web services interface. </li></ul></ul><ul><ul><li>The objects have properties and methods associated with them. </li></ul></ul><ul><ul><li>Examples: </li></ul></ul><ul><ul><ul><li>The VirtualMachine object has a property called snapshot. </li></ul></ul></ul><ul><ul><ul><li>The VirtualMachine object has a method called ReconfigVM. </li></ul></ul></ul><ul><ul><ul><li>The HostSystem object has a method called RebootHost_Task. </li></ul></ul></ul><ul><ul><li>By loading an object into memory you can access any of its properties, or call any of its methods. </li></ul></ul>
  6. 6. Some background: Managed entities and managed objects. <ul><ul><li>Not all objects are equal, some special objects are called “managed entities”. </li></ul></ul><ul><ul><li>Managed entities include: </li></ul></ul><ul><ul><ul><li>ComputeResource, Datacenter, Folder, HostSystem, ResourcePool, VirtualMachine </li></ul></ul></ul><ul><ul><li>Managed entities can be loaded directly. </li></ul></ul><ul><ul><li>To load other types of objects, you must first load a managed entity that contains that object. </li></ul></ul><ul><ul><ul><li>Determining the correct entity to load requires referring to the API reference . </li></ul></ul></ul>
  7. 7. The basic model of using these Web Services. <ul><ul><li>Follow these steps to determine how to </li></ul></ul><ul><ul><ul><li>Identify the goal you wish to achieve. </li></ul></ul></ul><ul><ul><ul><li>Identify the persistent object(s) associated with this goal. </li></ul></ul></ul><ul><ul><ul><ul><li>Example: If you wish to snapshot virtual machine X, you will need to first load the virtual machine in question. </li></ul></ul></ul></ul><ul><ul><ul><li>Refer to the VI API Reference guide to determine what properties or methods to use. </li></ul></ul></ul><ul><ul><ul><ul><li>Example: In this case we will want to use the CreateSnapshot_Task method. </li></ul></ul></ul></ul><ul><ul><ul><li>VI API Reference Guide: </li></ul></ul></ul><ul><ul><ul><ul><li>http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/index.html </li></ul></ul></ul></ul>
  8. 8. The basic model (continued). <ul><ul><li>Follow these steps to implement your solution. </li></ul></ul><ul><ul><ul><li>If the object identified before is a managed entity, load it using find_entity_view. </li></ul></ul></ul><ul><ul><ul><li>Otherwise, determine which managed entity contains the object. Load this object using find_entity_view, then load the object you want using get_view. </li></ul></ul></ul><ul><ul><ul><ul><li>This requires consulting the VI API reference. </li></ul></ul></ul></ul><ul><ul><ul><li>From this point it is simply a matter of accessing the properties you want, or calling the methods you want. </li></ul></ul></ul>
  9. 9. Getting Started <ul><ul><li>Step 1: </li></ul></ul><ul><ul><ul><li>In Visual Studio, under Project add a reference. </li></ul></ul></ul><ul><ul><li>Step 2: </li></ul></ul><ul><ul><ul><li>Navigate to </li></ul></ul></ul><ul><ul><ul><ul><li>%PROGRAMFILES% </li></ul></ul></ul></ul><ul><ul><ul><ul><li>VMware </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Infrastructure </li></ul></ul></ul></ul><ul><ul><ul><ul><li>VIToolkitForWindows </li></ul></ul></ul></ul><ul><ul><ul><li>Select VMware.Vim.dll and add. </li></ul></ul></ul>
  10. 10. Full list of methods provided by this assembly WaitForTask() GetSearchFilterSpec() VimException() GetPropertySpec() VimClient() GetPropertyFilterSpec() ViewBase() GetEventCollectorView() UpdateViewData() GetEntityOnlyTasksCollectorView() SetViewData() GetEntityOnlyEventsCollectorView() SaveSession() GetAllTasksView() Logout() GetAllEventsView() Login() FindEntityViews() LoadSession() FindEntityView() GetViewType() EntityViewBase() GetViews() Disconnect() GetView() Connect() GetTaskCollectorView() CertificateErrorEventArg()
  11. 11. Program Skeleton <ul><ul><li>using System; </li></ul></ul><ul><ul><li>using System.Collections.Generic; </li></ul></ul><ul><ul><li>using System.Collections.Specialized; </li></ul></ul><ul><ul><li>using System.Text; </li></ul></ul><ul><ul><li>using VMware.Vim; </li></ul></ul><ul><ul><li>namespace YourApplication </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>class Program </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>static void Main(string[] args) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>VimClient client = new VimClient(); </li></ul></ul><ul><ul><li>client.Login( “ <your server> &quot;, “ <username> &quot;, “ <password> &quot;); </li></ul></ul><ul><ul><li>// From this point, load objects, access properties and invoke methods. </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  12. 12. C# .NET examples using the toolkit. <ul><ul><li>using System; </li></ul></ul><ul><ul><li>using System.Collections.Generic; </li></ul></ul><ul><ul><li>using System.Collections.Specialized; </li></ul></ul><ul><ul><li>using System.Text; </li></ul></ul><ul><ul><li>using VMware.Vim; </li></ul></ul><ul><ul><li>namespace ConsoleApplication4 </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>class Program </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>static void Main(string[] args) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>VimClient client = new VimClient(); </li></ul></ul><ul><ul><li>client.Login( &quot;https://192.168.217.130/sdk&quot;, &quot;root&quot;, &quot;pass&quot;); </li></ul></ul><ul><ul><li>NameValueCollection filter = new NameValueCollection(); </li></ul></ul><ul><ul><li>filter.Add( &quot;config.name&quot;, &quot;vm1&quot;); </li></ul></ul><ul><ul><li>VirtualMachine virtualMachine = (VirtualMachine)client.FindEntityView( typeof( VirtualMachine), null, filter, null); </li></ul></ul><ul><ul><li>Console.WriteLine(virtualMachine.Config.Name, &quot; &quot;); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>This “Hello World” example connects to a host, finds a VM by name, then prints that name.
  13. 13. C# .NET examples using the toolkit. <ul><ul><li>namespace ConsoleApplication4 </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>class Program </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>static void Main(string[] args) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>VimClient client = new VimClient(); </li></ul></ul><ul><ul><li>client.Login( &quot;https://192.168.217.130/sdk&quot;, &quot;root&quot;, &quot;pass&quot;); </li></ul></ul><ul><ul><li>NameValueCollection filter = new NameValueCollection(); </li></ul></ul><ul><ul><li>filter.Add( &quot;config.name&quot;, &quot;vm1&quot;); </li></ul></ul><ul><ul><li>VirtualMachine virtualMachine = (VirtualMachine)client.FindEntityView( typeof( VirtualMachine), null, filter, null); </li></ul></ul><ul><ul><li>ManagedObjectReference task = virtualMachine.CreateSnapshot_Task( &quot;mysnap&quot;, &quot;This is my snapshot&quot;, false, false); </li></ul></ul><ul><ul><li>// Wait for this task to complete. </li></ul></ul><ul><ul><li>ViewBase vbase = new ViewBase(client, task); </li></ul></ul><ul><ul><li>vbase.WaitForTask(task); </li></ul></ul><ul><ul><li>Console.WriteLine( &quot;Snapshot completed.&quot;); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>This example snapshots a VM named “myvm” and waits for it to complete.
  14. 14. C# .NET examples using the toolkit. <ul><ul><li>using System; </li></ul></ul><ul><ul><li>using System.Collections.Generic; </li></ul></ul><ul><ul><li>using System.Collections.Specialized; </li></ul></ul><ul><ul><li>using System.Text; </li></ul></ul><ul><ul><li>using VMware.Vim; </li></ul></ul><ul><ul><li>namespace ConsoleApplication4 </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>class Program </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>static void Main(string[] args) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>VimClient client = new VimClient(); </li></ul></ul><ul><ul><li>client.Login( &quot;https://192.168.217.130/sdk&quot;, &quot;root&quot;, &quot;pass&quot;); </li></ul></ul><ul><ul><li>HostSystem host = (HostSystem)client.FindEntityView( typeof( HostSystem), null, null, null); </li></ul></ul><ul><ul><li>Console.WriteLine( &quot;Networks for this host:&quot;); </li></ul></ul><ul><ul><li>foreach ( ManagedObjectReference mor in host.Network) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>Network net = (Network)client.GetView(mor, null); </li></ul></ul><ul><ul><li>Console.WriteLine( &quot; &quot; + net.Name); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>This example illustrates loading views from within an entity view.
  15. 15. Need help? <ul><ul><li>Visit our developer forum at: </li></ul></ul>http://communities.vmware.com/community/developer/managementapi

×