VRO – XML work

Sooner or later you are going to have to work with XML in Ochestrator.  Orchestrator can be challenging with XML because it’s based upon E4X plugin not more current plugins.   You can find some specific details for now here.

I have been doing a lot of XML with NSX so let me explain with a real world example.  I have used the RestAPI to NSX to return the following XML:

<edge>
	<datacenterMoid>datacenter-21</datacenterMoid>
	<type>distributedRouter</type>
		<appliances>
			<appliance>
				<resourcePoolId>domain-c861</resourcePoolId>
				<datastoreId>datastore-998</datastoreId>
			</appliance>
		</appliances>
		<mgmtInterface>
			<connectedToId>virtualwire-1045</connectedToId>
				<addressGroups>
					<addressGroup>
						<primaryAddress>192.168.10.222</primaryAddress>
						<subnetMask>255.255.255.0</subnetMask>
					</addressGroup>
				</addressGroups>
		</mgmtInterface>
		<interfaces>
			<interface>
				<type>uplink</type>
				<mtu>1500</mtu>
				<isConnected>true</isConnected>
				<addressGroups>
					<addressGroup>
						<primaryAddress>172.16.1.2</primaryAddress>
						<subnetMask>255.255.255.0</subnetMask>
					</addressGroup>
				</addressGroups>
				<connectedToId>virtualwire-1036</connectedToId>
			</interface>
			<interface>
				<type>internal</type>
				<mtu>1500</mtu>
				<isConnected>true</isConnected>
				<addressGroups>
					<addressGroup>
						<primaryAddress>172.16.0.1</primaryAddress>
						<subnetMask>255.255.255.0</subnetMask>
					</addressGroup>
				</addressGroups>
				<connectedToId>virtualwire-1033</connectedToId>
			</interface>
			<interface>
				<type>internal</type>
				<mtu>1500</mtu>
				<isConnected>true</isConnected>
				<addressGroups>
					<addressGroup>
						<primaryAddress>172.16.20.1</primaryAddress>
						<subnetMask>255.255.255.0</subnetMask>
					</addressGroup>
				</addressGroups>
				<connectedToId>virtualwire-1035</connectedToId>
			</interface>
			<interface>
				<type>internal</type>
				<mtu>1500</mtu>
				<isConnected>true</isConnected>
				<addressGroups>
					<addressGroup>
						<primaryAddress>172.16.10.1</primaryAddress>
						<subnetMask>255.255.255.0</subnetMask>
					</addressGroup>
				</addressGroups>
				<connectedToId>virtualwire-1034</connectedToId>
			</interface>
		</interfaces>
</edge>

As you can see it’s got a lot of information.  This provides the basic build for a distributed logical router.  Let’s assume that I want to get all the ip addresses in use on this DLR.   First I need to convert this return from the RESTAPI into a XML object.  (It’s already in XML format but we need it as an object so we can interact with XML) Let’s assume all the content above is in the variable called mydata.

//Convert into XML object
var myXML = new XML(mydata);

 

Reading Data

Now that it’s an XML object we can interact with the specific nodes with ease.   If I wanted to return the management interface primary address I could use the following code:

System.log(myXML.mgmtInterface);

 

It’s a common mistake to include the edge on the node list which would fail to return results.   Lets do something more complex like return the first interface ip address:

System.log(myXML.interfaces.interface[0].addressGroups.addressGroup[0].primaryAddress);

 

As you can see I know that both interface and addressGroup might have multiple entries so I using the 0 to designate the first entry.  In reality it would be better to use a loop so we can get all interface IP addresses like this:

for each (a in myXML.interfaces.interface)
{
     for each (b in a.addressGroups.addressGroup)
     {
        System.log(b.primaryAddress);
     }

}

As you can see this will iterate through all interface entries (a) and then though all addressGroup entries(b) on a then print out the primaryAddress.

 

Deleting Data

Removing nodes from XML is really easy for example if I wanted to remove the first interface I would do:

delete myXML.interfaces.interface[0];

I hope this helps you a little on your journey.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.