To create a DLR is not the most time consuming task and frankly, if you are only creating 2 or 3, doing it through the GUI is faster than trying to automate it. What is very time consuming though is the task of attaching Logical Switches to a DLR (LIFs or SVIs). A DLR might have hundreds of LIFs, and it is very slow to add these through the GUI. In this post, we will see how to add these LIFs to a DLR using a CSV file. Some of the data required for the CSV file could be retrieved by an earlier post (NSX Automation Part 3 – Create CSV from Existing Logical Switches Partial Data).
From the API Guide:
Add Interfaces
Configures one or more interface for an NSX Edge Router. The specified configuration is stored in the
database. If any appliance(s) is associated with this Edge Edge instance, the specified configuration is applied
to the appliance as well.
You should not define a index for the new addition of interfaces. The indexes are system-generated To update
the existing interfaces, include them in the XML with the system-generated indexes (can be obtained by a GET
call).
Example 7-7. Add an interface
Request:
POST https://<nsxmgr-ip>/api/4.0/edges/<edgeId>/interfaces/?action=patch
120 VMware, Inc.
Request Body:
<interfaces>
<interface>
<name>interface1</name>
<addressGroups>
<addressGroup>
<primaryAddress>192.168.10.1</primaryAddress>
<subnetMask>255.255.255.0</subnetMask>
</addressGroup>
</addressGroups>
<mtu>1500</mtu>
<type>uplink</type>
<isConnected>true</isConnected>
<connectedToId>dvportgroup-39</connectedToId>
</interface>
</interfaces>
The CSV File:
name,primaryAddress,subnetMask,mtu,type,isConnected,connectedToId
ls-1,10.10.10.1,255.255.255.0,8900,internal,true,virtualwire-10
ls-2,10.10.10.2,255.255.255.0,8900,internal,true,virtualwire-11
ls-3,10.10.10.3,255.255.255.0,8900,internal,true,virtualwire-12
The AWK Script, csv2xml2.awk:
NR == 1 {for (i = 1; i <=NF; i++)
tag[i]=$i
}
NR != 1 {
printf "curl -k -u admin:Password! -H 'content-type: application/xml' -X " xtype " -d '"
if (node == "interface") printf "<interfaces>"
printf "<" node ">"
for (i = 1; i <= NF; i++) {
if (node == "interface" && tag[i] == "primaryAddress")
printf "<addressGroups><addressGroup>"
if (node == "interface" && tag[i] == "mtu")
printf "</addressGroup></addressGroups>"
printf "<" tag[i] ">" $i "</" tag[i] ">"
}
printf "</" node ">"
if (node == "interface") printf "</interfaces>"
printf "' "
print "https://" nsxmanager "/api/" api ".0" scope
}
How to execute, change “edge-15” to the correct edge name:
ndaher@ubuntu:~$ awk -F, -v node=interface -v nsxmanager=nsxman.local -v scope=/edges/edge-15/interfaces/?action=patch -v api=4 -v xtype=POST -f csv2xml2.awk dlr.csv | sh
The Output that is piped to SH
curl -k -u admin:Password! -H ‘content-type: application/xml’ -X POST -d ‘<interfaces><interface><name>ls-1</name><addressGroups><addressGroup><primaryAddress>10.10.10.1</primaryAddress><subnetMask>255.255.255.0</subnetMask></addressGroup></addressGroups><mtu>8900</mtu><type>internal</type><isConnected>true</isConnected><connectedToId>virtualwire-10</connectedToId></interface></interfaces>’ https://nsxman.local/api/4.0/edges/edge-15/interfaces/?action=patch
curl -k -u admin:Password! -H ‘content-type: application/xml’ -X POST -d ‘<interfaces><interface><name>ls-2</name><addressGroups><addressGroup><primaryAddress>10.10.10.2</primaryAddress><subnetMask>255.255.255.0</subnetMask></addressGroup></addressGroups><mtu>8900</mtu><type>internal</type><isConnected>true</isConnected><connectedToId>virtualwire-11</connectedToId></interface></interfaces>’ https://nsxman.local/api/4.0/edges/edge-15/interfaces/?action=patch
curl -k -u admin:Password! -H ‘content-type: application/xml’ -X POST -d ‘<interfaces><interface><name>ls-3</name><addressGroups><addressGroup><primaryAddress>10.10.10.3</primaryAddress><subnetMask>255.255.255.0</subnetMask></addressGroup></addressGroups><mtu>8900</mtu><type>internal</type><isConnected>true</isConnected><connectedToId>virtualwire-12</connectedToId></interface></interfaces>’ https://nsxman.local/api/4.0/edges/edge-15/interfaces/?action=patch