NSX Automation Part 6 – Delete Logical Switches

In part 5 of this series, we looked at how to remove LIFs from an existing DLR.  In this post, we will look at removing Logical Switches or VXLANs from NSX.

 

From the API Guide:

Delete a VXLAN Virtual Wire
You can delete a VXLAN virtual Wire.
Example 6-26. Delete virtual wire
Request:
DELETE https://<vsm-ip>/api/2.0/vdn/virtualwires/virtualWireID

 

Delete Certain Logical Switches

First step in this process is to have a CSV file that has all the Logical Switches that we want to remove.  The only piece of information we need to be able to delete a Logical Switch is the virtualWireId.  If you remember, in NSX Automation Part 3 – Create CSV from Existing Logical Switches Partial Data, we covered how to extract partial data from the NSX manager to build a Logical Switch CSV.  In this case, all we need are name and objectId.  Here are the commands that we need to run.  I have already filled in the correct field set.

ndaher@ubuntu:~$ curl -k -u admin:Password! -H 'content-type: application/xml'-X GET https://nsxman.local/api/2.0/vdn/virtualwires | xmllint --xpath "//virtualWire/objectId|//virtualWire/name" - | sed -e 's/<objectId>/<virtualWire><objectId>/g' -e 's/<\/name>/<\/name><\/virtualWire>/g' | sed -e 's/^/<virtualWires>/' -e 's/$/<\/virtualWires>/' >logswitches.xml

ndaher@ubuntu:~$ xml2csv --input "logswitches.xml" --output "logswitches.out" --tag "virtualWire"
ndaher@ubuntu:~$ sed s/\"//g logswitches.out > logswitches.csv

Sample Output of logswitches.csv

objectId,name
virtualwire-8,TEST-DLR-Uplink-A
virtualwire-9,DEV-DLR-Uplink-A
virtualwire-10,DB-QA-10.2.13.0
virtualwire-11,SQL-QA-10.2.14.0

Edit the logswitches.csv and remove any LSs that you do not want to delete. Now delete the LSs remaining in the CSV file.

ndaher@ubuntu:~$ tail -n+2 logswitches.csv | awk -F, '{print "curl -k -u admin:Password! -X DELETE https://nsxman.local/api/2.0/vdn/virtualwires/" $1}' | sh


Permanent link to this article: https://daherlabs.mywire.org/wordpress/?p=529

Leave a Reply

Your email address will not be published.