using vi toolkit (for windows) from .net

15
VI Toolkit (for Windows) and .NET development Carter Shanklin VMware, Inc. July 2008

Upload: carter-shanklin

Post on 26-Jan-2015

126 views

Category:

Technology


5 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Using VI Toolkit (For Windows) From .NET

VI Toolkit (for Windows)and .NET development

Carter Shanklin

VMware, Inc.

July 2008

Page 2: Using VI Toolkit (For Windows) From .NET

22

The VI Toolkit (for Windows)

We’ve built a lot of very useful PowerShell functionality in to the VI Toolkit (for Windows).

If you’re developing a compiled application, you should consider using PowerShell via runspaces to leverage all our hard work.

If you’ve evaluated this option and are convinced it’s not for you, this slide show will show you another option.

Page 3: Using VI Toolkit (For Windows) From .NET

33

Not planning on using PowerShell?

The VI Toolkit (for Windows) provides an assembly that allows simplified development, VMware.Vim.dll from a .NET environment.

28 methods are provided that make programming VI easy.

Documentation on programming using this assembly is provided within the VI Toolkit (for Windows) install.

The library provides access to the entire set of VMware Web Services.

Page 4: Using VI Toolkit (For Windows) From .NET

44

Some background.

This assembly provides a wrapper to the VMware VI Web Services APIs, along with some helper functions to simplify things.

Before we can talk about the assembly more, we need to take a look at VI Web Services.

Page 5: Using VI Toolkit (For Windows) From .NET

55

Some background: VMware web services

VMware uses an extensive database to store information about objects like hosts and virtual machines.

These objects are exposed through the web services interface.

The objects have properties and methods associated with them.

Examples:

The VirtualMachine object has a property called snapshot.

The VirtualMachine object has a method called ReconfigVM.

The HostSystem object has a method called RebootHost_Task.

By loading an object into memory you can access any of its properties, or call any of its methods.

Page 6: Using VI Toolkit (For Windows) From .NET

66

Some background: Managed entities and managed objects.

Not all objects are equal, some special objects are called “managed entities”.

Managed entities include:

ComputeResource, Datacenter, Folder, HostSystem, ResourcePool, VirtualMachine

Managed entities can be loaded directly.

To load other types of objects, you must first load a managed entity that contains that object.

Determining the correct entity to load requires referring to the API reference.

Page 7: Using VI Toolkit (For Windows) From .NET

77

The basic model of using these Web Services.

Follow these steps to determine how to

Identify the goal you wish to achieve.

Identify the persistent object(s) associated with this goal.

Example: If you wish to snapshot virtual machine X, you will need to first load the virtual machine in question.

Refer to the VI API Reference guide to determine what properties or methods to use.

Example: In this case we will want to use the CreateSnapshot_Task method.

VI API Reference Guide:

http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/index.html

Page 8: Using VI Toolkit (For Windows) From .NET

88

The basic model (continued).

Follow these steps to implement your solution.

If the object identified before is a managed entity, load it using find_entity_view.

Otherwise, determine which managed entity contains the object. Load this object using find_entity_view, then load the object you want using get_view.

This requires consulting the VI API reference.

From this point it is simply a matter of accessing the properties you want, or calling the methods you want.

Page 9: Using VI Toolkit (For Windows) From .NET

99

Getting Started

Step 1:

In Visual Studio, under Project add a reference.

Step 2:

Navigate to

%PROGRAMFILES%

VMware

Infrastructure

VIToolkitForWindows

Select VMware.Vim.dll and add.

Page 10: Using VI Toolkit (For Windows) From .NET

1010

Full list of methods provided by this assembly

CertificateErrorEventArg() GetTaskCollectorView()

Connect() GetView()

Disconnect() GetViews()

EntityViewBase() GetViewType()

FindEntityView() LoadSession()

FindEntityViews() Login()

GetAllEventsView() Logout()

GetAllTasksView() SaveSession()

GetEntityOnlyEventsCollectorView() SetViewData()

GetEntityOnlyTasksCollectorView() UpdateViewData()

GetEventCollectorView() ViewBase()

GetPropertyFilterSpec() VimClient()

GetPropertySpec() VimException()

GetSearchFilterSpec() WaitForTask()

Page 11: Using VI Toolkit (For Windows) From .NET

1111

Program Skeleton

using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Text;

using VMware.Vim;

namespace YourApplication

{

class Program

{

static void Main(string[] args)

{

VimClient client = new VimClient();

client.Login(“<your server>", “<username>", “<password>");

// From this point, load objects, access properties and invoke methods.

}

}

}

Page 12: Using VI Toolkit (For Windows) From .NET

1212

C# .NET examples using the toolkit.using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Text;

using VMware.Vim;

namespace ConsoleApplication4

{

class Program

{

static void Main(string[] args)

{

VimClient client = new VimClient();

client.Login("https://192.168.217.130/sdk", "root", "pass");

NameValueCollection filter = new NameValueCollection();

filter.Add("config.name", "vm1");

VirtualMachine virtualMachine = (VirtualMachine)client.FindEntityView(typeof(VirtualMachine), null, filter, null);

Console.WriteLine(virtualMachine.Config.Name, "\n");

}

}

}

This “Hello World” exampleconnects to a host, finds a VMby name, then prints that name.

Page 13: Using VI Toolkit (For Windows) From .NET

1313

C# .NET examples using the toolkit.namespace ConsoleApplication4

{

class Program

{

static void Main(string[] args)

{

VimClient client = new VimClient();

client.Login("https://192.168.217.130/sdk", "root", "pass");

NameValueCollection filter = new NameValueCollection();

filter.Add("config.name", "vm1");

VirtualMachine virtualMachine = (VirtualMachine)client.FindEntityView(typeof(VirtualMachine), null, filter, null);

ManagedObjectReference task = virtualMachine.CreateSnapshot_Task("mysnap", "This is my snapshot", false, false);

// Wait for this task to complete.

ViewBase vbase = new ViewBase(client, task);

vbase.WaitForTask(task);

Console.WriteLine("Snapshot completed.");

}

}

}

This example snapshotsa VM named “myvm” andwaits for it to complete.

Page 14: Using VI Toolkit (For Windows) From .NET

1414

C# .NET examples using the toolkit.using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Text;

using VMware.Vim;

namespace ConsoleApplication4

{

class Program

{

static void Main(string[] args)

{

VimClient client = new VimClient();

client.Login("https://192.168.217.130/sdk", "root", "pass");

HostSystem host = (HostSystem)client.FindEntityView(typeof(HostSystem), null, null, null);

Console.WriteLine("Networks for this host:");

foreach (ManagedObjectReference mor in host.Network)

{

Network net = (Network)client.GetView(mor, null);

Console.WriteLine(" " + net.Name);

}

}

}

}

This example illustratesloading views from withinan entity view.

Page 15: Using VI Toolkit (For Windows) From .NET

1515

Need help?

Visit our developer forum at:

http://communities.vmware.com/community/developer/managementapi