VMware Cloud Community
qc4vmware
Virtuoso
Virtuoso

xpath searches in the vCenter plugin for exact match and case insensitivity

I figured I would go ahead and post some updates to the community on the xpath searches that are working for me in 7.2 with the most current vSphere plugin.  They have changed quite a bit from what was working for me in the past.  It seems like recently some change in either the plugin or vRO has effected how the plugin responds to the search string.  In previous examples on here the translate portion of the expression was missing the numeric, dash, and period which seemed to work fine up until now.  Adding those characters end up in a correct translation.  Also the escape sequencing is needed now whereas before it would cause issues.  If you find this helpful give me a "like" I'm trying to level up on here!

if (vroVersion <= "6") {

  System.debug("vRO Version 6 detected... using old xpath expressions");

  if (exactMatch == true && caseSensitive == false) vms = VcPlugin.getAllVirtualMachines(null, "xpath:name[translate(.,'abcdefghijklmnopqrstuvwxyz-0123456789.','ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789.')='" + vmName.toUpperCase() + "']");

  if (exactMatch == true && caseSensitive == true) vms = VcPlugin.getAllVirtualMachines(null, "xpath:name='" + vmName + "'");

  if (exactMatch == false && caseSensitive == false) vms = VcPlugin.getAllVirtualMachines(null, "xpath:name[contains(translate(.,'abcdefghijklmnopqrstuvwxyz-0123456789.','ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789.'),'" + vmName.toUpperCase() + "')]");

  if (exactMatch == false && caseSensitive == true) vms = VcPlugin.getAllVirtualMachines(null, "xpath:name[contains(.,'" + vmName + "')]");

}

else {

  if (exactMatch == true && caseSensitive == false) vms = VcPlugin.getAllVirtualMachines(null, "xpath:name[translate(.,\'abcdefghijklmnopqrstuvwxyz-0123456789.\',\'ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789.\')]=\'" + vmName.toUpperCase() + "\'");

  if (exactMatch == true && caseSensitive == true) vms = VcPlugin.getAllVirtualMachines(null, "xpath:name=\'" + vmName + "\'");

  if (exactMatch == false && caseSensitive == false) vms = VcPlugin.getAllVirtualMachines(null, "xpath:contains(name[translate(.,\'abcdefghijklmnopqrstuvwxyz-0123456789.\',\'ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789.\')],\'" + vmName.toUpperCase() + "\')");

  if (exactMatch == false && caseSensitive == true) vms = VcPlugin.getAllVirtualMachines(null, "xpath:contains(name,\'" + vmName + "\')");

}

Reply
0 Kudos
0 Replies