VMware Cloud Community
vSteveR
Contributor
Contributor

get vcac:vm from vc:vm

HI,

I know this has been discussed before but I seem to be missing something (probably obvious!) and I'm going around in circles. This script works ok on a test WF, with a scriptable task to convert the vc:vm parameter to a vcac:vm parameter, iI'm then passing this value to another WF to import it to vRA.

My problem is when I use the same sript in a different WF (to clone a machine). For some reason it generates a random machine name when it has searches the vCACvms in the "IF". I added the UUID piece as I have teo vCenters and thought it may be a duplicate of some sort.

Any advide would be greatly appreciated.

Thanks in advance and regards

Steve

Input is "newVM1" type vc:vm

Output is "vm" type vcac:vc

var vCACvms = Server.findAllForType("vCAC:VirtualMachine");

var uuid = newVM1.config.uuid

var vcName = newVM1.name

System.log ("vcName = " + vcName);

for each (var vCACvm in vCACvms){

//System.log(vCACvm)

  if (vcName == vCACvm.VirtualMachineName && vCACvm.uuid){

  System.log("Resolved vCAC VM: " + vCACvm.VirtualMachineName);

  System.log("vCACvm = " + vCACvm);

  break;

  }

}

vm=vCACvm;

System.log ("Output vm = " + vm);

Reply
0 Kudos
11 Replies
eoinbyrne
Expert
Expert

Hi

You have a few problems in your code there. I've updated and added some comments to explain

var vCACvms = Server.findAllForType("vCAC:VirtualMachine");

var uuid = newVM1.config.uuid

var vcName = newVM1.name

System.log ("vcName = " + vcName);

for each (var vCACvm in vCACvms){

//System.log(vCACvm)

// corrected the case on the member var name here, i.e., 'virtualMachineName' not 'VirtualMachineName'

// corrected the second clause to include the equality check.

// Previously it was a side-effect existence check (which IS valid but not what you wanted)

// also, there  is no member with name 'uuid' on a vCACVirtualMachine object so changed the target of the check

// also, added braces to surround the clauses

if ((vcName == vCACvm.virtualMachineName) && (uuid == vCACvm.virtualMachineID)){

System.log("Resolved vCAC VM: " + vCACvm.virtualMachineName);

System.log("vCACvm = " + vCACvm);

// moved this line as the var 'vCACVm' is only in-scope inside this loop

vm = vCACvm;

break;

}

}

System.log ("Output vm = " + vm);

System.log ("Output vm = " + vm);

Try it again and see how you go

Also, worth looking at this API guide -  it's a great help to sort through object structures

vRO API Explorer by Dr Ruurd and Flores of ITQ

- HTH

Reply
0 Kudos
vSteveR
Contributor
Contributor

Thanks for the response, I've edited the script now but the output is null. There was nothing returned on the two logs from within the loop fyi.

Log below:

2018-07-26 12:49:04.039] [I] newVM1 = DynamicWrapper (Instance) : [VcVirtualMachine]-[class com.vmware.o11n.plugin.vsphere_gen.VirtualMachine_Wrapper] -- VALUE : Stub: moRef = (ManagedObjectReference: type = VirtualMachine, value = vm-221683, serverGuid = null), binding =****:443/sdk

[2018-07-26 12:49:10.360] [I] vcName = 75clone08

[2018-07-26 12:49:10.374] [I] Output vm = null

Thanks again for your help

Steve

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

Does it work if on line# 24 you replace:

vCACvm.virtualMachineID

with:

vCACvm.vmUniqueID

Also, comparing UUIDs on this line is probably enough (no need to compare also names).

qc4vmware
Virtuoso
Virtuoso

A couple of other things... If you are not passing vm into and out of your scriptable task make sure you include "var" before it.

You might also try this code...

var Uuid = vCenterVm.config.instanceUuid;

var vCACVm = Server.findAllForType("vCAC:VirtualMachine", "VMUniqueID eq '" + Uuid + "'");

I've got workflows that pull all the vCAC:VirtualMachine types down and loop through and also this one that tries to filter in the query.  I forget which was faster... I think in varying versions of the plugin they may have flip flopped.  Anyway, you might want to be mindful of the speed depending on how heavily you look things up.

Paul

vSteveR
Contributor
Contributor

Thanks for this suggestion "llian lliev", I have made those changes, but no joy on the output =null. I have removed the name check anyway.

I'll try the next suggestion, if you think of anything else I'd appreciate it.

Cheers

Steve

Reply
0 Kudos
qc4vmware
Virtuoso
Virtuoso

I'm attaching the action I use all the time... never fails... if it returns a null then something else might be going on.  I've found when doing this sort of thing and looking up new objects sometimes I need to inject a wait or a retry.  Anyway, give a call to this action a shot.  Maybe we're all glossing over some other tweak to your code that is needed and just not identifying it.

vSteveR
Contributor
Contributor

Thanks for this, I was thinking along the same lines today. I actually put a breakpoint in WF before the conversation script but it didn't help. I'll test this action next and let you know.

Thanks and regards

Steve

Reply
0 Kudos
vSteveR
Contributor
Contributor

Hi, to keep things as simple as possible I created a new WF with simply this action and the relevant I/O's and I get "Too many matches for UUID,there can be only one!". I'm guessing it's my multiple vCenters?

## Update - I ran a powerCLI query for every VM & UUID and no duplicates, assuming "instanceuuid" is the same.

Error log is as follows, notice that the "resolved vCAC VM:" line is repeated.

[2018-07-30 15:04:12.220] [I] 2 match uuid: 5015c1d8-0e58-2923-9dfc-42059c8bddbb

[2018-07-30 15:04:12.222] [I] Resolved vCAC VM: 75clone26

[2018-07-30 15:04:12.224] [I] Resolved vCAC VM: 75clone26

[2018-07-30 15:04:12.226] [E] (Rev.vCloud/QCgetvCACVmFromvCenterVmUuid) Error in (Dynamic Script Module name : QCgetvCACVmFromvCenterVmUuid#9) Too many matches for UUID: 5015c1d8-0e58-2923-9dfc-42059c8bddbb, there can be only one!

[2018-07-30 15:04:12.236] [E] Workflow execution stack:

***

item: 'SR_test convert element vc:vm to vcac:vm and import to vRA/item1', state: 'failed', business state: 'null', exception: 'Too many matches for UUID: 5015c1d8-0e58-2923-9dfc-42059c8bddbb, there can be only one! (Dynamic Script Module name : QCgetvCACVmFromvCenterVmUuid#9)'

workflow: 'SR_test convert element vc:vm to vcac:vm and import to vRA' (e87fb2bf-f9f4-49d0-8e9f-a3eabc1c513e)

|  'input': name=vCenterVm type=VC:VirtualMachine value=dunes://service.dunes.ch/CustomSDKObject?id='#####%2Cid:vm-221881'&dunesName='VC:VirtualMachine'

|  'output': name=actionResult type=vCAC:VirtualMachine value=null

|  'no attributes'

*** End of execution stack.

Thanks again for your help on this.

Steve

Reply
0 Kudos
qc4vmware
Virtuoso
Virtuoso

It sounds like you've added the same vCenter server twice to vRO maybe? I'm not sure how else you'd get duplicates.  The bios uuid might not be unique but instanceuuid should be.

Paul

vSteveR
Contributor
Contributor

I double checked that there isn't a duplicate vCenter, removed the vC instances and added again but no joy. Strange results as I don't always get duplicates, some times I don't get any match. Could I have a more serious issue?

I've been doing some testing with the following script also, which is the name comparison only i appreciate but it seems to work well (so far). I've yet to get the result I want from the master WF, but that could be a different issue.

var Uuid = newVM1.config.instanceUuid;

var vmname = newVM1.config.Name;

var vCACVms = Server.findAllForType("vCAC:VirtualMachine", "substringof('" + vmname + "',VirtualMachineName) eq true")

System.log("vCACObj: " + vCACVms);

return vCACVms[0];

Regards

Steve

Reply
0 Kudos
qc4vmware
Virtuoso
Virtuoso

The only other thing I can think of is a race condition of some sort.  You could try looping through a few times around getting no result and too many results.  I've run into these sorts of situations pretty frequently.  Sometimes there is a call you can make to force a refresh, sync, etc but for the instances where you just need to wait looping or inserting a wait state is the only way to deal with it. Start off trying some tests where you loop until it works and watch the logs so you can get a feel for how long it takes and then work backward from there.

Reply
0 Kudos