Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 252940

Re: using VMware.Vim

$
0
0

More forward progress today, it feels as though I'm very close to having a working POC. Going to post up a couple of links that I've found that have helped. The last part of this that I'm working on is the cloning, so these links are related to that.

 

http://www.vmdev.info/?p=202

 

http://communities.vmware.com/thread/140504

 

 

string selectedVMname = cboClones.SelectedItem.ToString();
string selectedDatastore = cboDatastores.SelectedItem.ToString();
string SelectedDataCenter = cboDatacenters.SelectedItem.ToString();
string selectedPortGroupsmoRef = cboPortGroups.SelectedValue.ToString();
string selectedCustomization = cboCustomizations.SelectedItem.ToString();
List<ManagedObjectReference> hostCollection = new List<ManagedObjectReference>();
Random rand = new Random();
ManagedObjectReference randomHost; 
sViServer = txtViServer.Text.ToString();
sUsername = txtUsername.Text.ToString();
sPassword = txtPassword.Text.ToString(); 
vimClient.Connect("https://" + this.sViServer + "/sdk");
vimSession = vimClient.Login(this.sUsername, this.sPassword);
vimServiceContent = vimClient.ServiceContent;       //// Connect to datacenter// 
NameValueCollection dcFilter = new NameValueCollection();
dcFilter.Add("name", SelectedDataCenter); 
EntityViewBase appDC = vimClient.FindEntityView(typeof(Datacenter), null, dcFilter, null);
Datacenter thisDC = (Datacenter)appDC; //// Get a list of hosts in the cluster// 
ManagedObjectReference hostFolderMoRef = thisDC.HostFolder;
Folder hostFolder = (Folder)vimClient.GetView(hostFolderMoRef, null);
ManagedObjectReference[] childEntityMoRefs = hostFolder.ChildEntity;
foreach (ManagedObjectReference childEntityMoRef in childEntityMoRefs){    ClusterComputeResource thisCluster = (ClusterComputeResource)vimClient.GetView(childEntityMoRef, null);    ManagedObjectReference[] clusterHostMoRefs = thisCluster.Host;    foreach (ManagedObjectReference clusterHostMoRef in clusterHostMoRefs)    {        HostSystem hostSystem = (HostSystem)vimClient.GetView(clusterHostMoRef, null);        hostCollection.Add(hostSystem.MoRef);    }} //// Connect to selected vm to clone// 
NameValueCollection vmFilter = new NameValueCollection();
vmFilter.Add("Name", selectedVMname);
EntityViewBase thisVm = vimClient.FindEntityView(typeof(VirtualMachine), null, vmFilter, null);
VirtualMachine sourceVm = (VirtualMachine)thisVm; //// Randomly pick host from collection for folder// 
randomHost = hostCollection[rand.Next(0,hostCollection.Count)];
HostSystem selectedHost = (HostSystem)vimClient.GetView(randomHost, null); //// Connect to the datastore// 
NameValueCollection dsFilter = new NameValueCollection();
dsFilter.Add("name", selectedDatastore);
EntityViewBase myDs = vimClient.FindEntityView(typeof(Datastore), null, dsFilter, null);
Datastore selectedStore = (Datastore)myDs; //// Connect to the customizationspec// 
CustomizationSpecManager specManager = (CustomizationSpecManager)vimClient.GetView(vimServiceContent.CustomizationSpecManager, null);
CustomizationSpecItem thisSpec = specManager.GetCustomizationSpec(selectedCustomization);        
VirtualMachineCloneSpec mySpec = new VirtualMachineCloneSpec();//mySpec.Location.Datastore = selectedStore.MoRef;
mySpec.Location = new VirtualMachineRelocateSpec();
mySpec.Location.Host = randomHost;
mySpec.Customization = thisSpec.Spec;        //// Perform the clone// //sourceVm.CloneVM_Task(sourceVm.Parent, "my-sample-vm", mySpec); 
vimClient.Disconnect();

 

The line

 

//mySpec.Location.Datastore = selectedStore.MoRef;

 

Appears to be sticking point at the moment, I thought it was expecting a moref of the datastore, so when I pass that in I get an error

 

Object reference not set to an instance of an object, oh well it's quite close though.


Viewing all articles
Browse latest Browse all 252940

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>