VMware {code} Community
CRaiford
Contributor
Contributor
Jump to solution

C#, enumerate hosts from a Datastore reference

*I need a report of datastores which lists the ESX hosts which access each LUN. I have been able to collect the summary information of the datastore reference (name, capacity, free space) but need help understanding how to enumerate the host property, which is an array of type DatastoreHoustMount.*

*My code (not shown) first obtains a reference to a datacenter, then passes the name as a parameter (dcName) to the following:*

ManagedObjectReference dc = vim_svc.FindByInventoryPath(vim_svc_content.searchIndex, dcName);

/// -- Datacenter to Datastore --

TraversalSpec dc2ds = new TraversalSpec();

dc2ds.type = "Datacenter";

dc2ds.name = "dc2ds";

dc2ds.path = "datastore";

dc2ds.selectSet = new SelectionSpec[] { new SelectionSpec() };

dc2ds.selectSet[0].name = dc2ds.name;

PropertySpec pspecDS = new PropertySpec();

pspecDS.type = "Datastore";

pspecDS.pathSet = new string[] { "host", "summary.name",

"summary.capacity", "summary.freeSpace", "summary.type", "summary.url" };

pspecDS.all = true;

ObjectSpec ospec = new ObjectSpec();

ospec.obj = dc;

ospec.skip = false;

ospec.selectSet = new SelectionSpec[] ;

PropertyFilterSpec pfspec = new PropertyFilterSpec();

pfspec.propSet = new PropertySpec[] ;

pfspec.objectSet = new ObjectSpec[] ;

Here's the routine I'm using to retrieve the datastore properties:

ObjectContent[] [occoll = vim_svc.RetrieveProperties(vim_svc_content.propertyCollector, new PropertyFilterSpec[http://] occoll = vim_svc.RetrieveProperties(vim_svc_content.propertyCollector, new PropertyFilterSpec[] );

if (occoll != null)

{

ManagedObjectReference mor = null;

DynamicProperty[] pcary = null;

DynamicProperty pc = null;

decimal diskSpace = 0;

/// create an array to concatenate the properties of a datastore

/// the properties are returned in an unpredictable order

string[] dsSummary = null;

// use the following for indexes of the array

int summaryName = 0;

int summaryCapacity = 1;

int summaryFreeSpace = 2;

int summaryType = 3;

int summaryUrl = 4;

string[] datastores = new string[[occoll.Length|http://occoll.Length]];

int datastoreCounter = 0;

foreach (ObjectContent oc in occoll)

{

mor = oc.obj;

pcary = oc.propSet;

tempString = null;

if (pcary != null)

{

dsSummary = new string[5];

for (int pci = 0; pci < pcary.Length; pci++)

{

pc = pcary[[pci]];

BuildOutput("name: " + pc.name + " (" + pc.val + ")\n");

if (pc.val.GetType().IsArray)

{

Array ipcary = (Array)pc.val;

for (int ii = 0; ii < ipcary.Length; ii++)

{

object oval = ipcary.GetValue(ii);

BuildOutput(" --> type: " + oval.GetType().ToString() + "\n");

if (oval.GetType().ToString() == "VimApi.ManagedObjectReference")

{

ManagedObjectReference imor = (ManagedObjectReference)oval;

BuildOutput(" --> imor.type: " + imor.type + "\n");

BuildOutput(" --> imor.value: " + imor.Value + "\n");

}

if (oval.GetType().ToString() == "VimApi.DatastoreHostMount")

{

/// -- DatastoreHostMount is a dynamic property,

///NOT a ManagedObject

// ==> loop through each DatastoreHostMount and print 'key' <==

????????????????????????????????????????????????????????????

}

}

}

else

{

switch (pc.name)

{

case "summary.name":

dsSummary[[summaryName]] = pc.val.ToString();

break;

case "summary.capacity":

diskSpace = (Int64)pc.val;

dsSummary[[summaryCapacity]] = ConvertToGB(diskSpace);

break;

case "summary.freeSpace":

diskSpace = (Int64)pc.val;

dsSummary[[summaryFreeSpace]] = ConvertToGB(diskSpace);

break;

case "summary.type":

dsSummary[[summaryType]] = pc.val.ToString();

break;

case "summary.url":

dsSummary[[summaryUrl]] = pc.val.ToString();

break;

}

In another routine (not shown), I build a hash table with each host name (human readable xxx.yy.com) and host (VMware's unique ID host-##). If I could just enumate the DatastoreHostMount array, I could associate the human readable host names with the unique IDs.

Thanks

0 Kudos
1 Solution

Accepted Solutions
ssurana
VMware Employee
VMware Employee
Jump to solution

Hi,

Your code looks okay. In fact you have done most of the things required. You just need to print the host MORs. You can do so as follows:

if (oval.GetType().ToString() == "VimApi.DatastoreHostMount")

{

VimApi.DatastoreHostMount dhm = (VimApi.DatastoreHostMount)oval;

System.Console.WriteLine(" --> dhm.key.type: " + dhm.key.type + "\n");

System.Console.WriteLine(" --> dhm.key.Value: " + dhm.key.Value + "\n";

}

Hope the above helps.

~ Sidharth

View solution in original post

0 Kudos
2 Replies
ssurana
VMware Employee
VMware Employee
Jump to solution

Hi,

Your code looks okay. In fact you have done most of the things required. You just need to print the host MORs. You can do so as follows:

if (oval.GetType().ToString() == "VimApi.DatastoreHostMount")

{

VimApi.DatastoreHostMount dhm = (VimApi.DatastoreHostMount)oval;

System.Console.WriteLine(" --> dhm.key.type: " + dhm.key.type + "\n");

System.Console.WriteLine(" --> dhm.key.Value: " + dhm.key.Value + "\n";

}

Hope the above helps.

~ Sidharth

0 Kudos
CRaiford
Contributor
Contributor
Jump to solution

That solved the issue. I just wasn't certain how to instantiate a DatastoreHoutMount object to retrieve the key properties.

Thanks

0 Kudos