OK, this post will be a simple one. All I want to show is the exact API code with the body you can send to NSX manager to create Network Segments (aka Logical Switches).
This information is, of course, available in the NSX-T API guide but to be absolutely honest, API Explorer for NSX-T sucks. It is Just a huge plain html file. It takes so much time to find something there, especially if you are not a hard core Developer who spends his days working and documenting APIs.
Anyway, Policy API is a new API introduced in NSX-T 2.4. It is quite different from the Old API (aka NSX -T Management Plane API) which has been there and still is part of NSX-T. I don’t think I should go to the jungle of details to describe why and how those are different from each other. I am sure you can google that yourself.
The key idea is that Policy API is declarative, so you only have to specify the desired final state.
The code
OK, so in this example I will be executing an API call to create 2 Segments. One Overlay segment very creatively called “Overlay_Segment” and one VLAN based segment called “VLAN Segment”. To execute the call I will use Postman.
To start with, I will first need to find IDs of your Overlay Transport Zone and your VLAN Transport Zone. This can also be done using API but I am lazy so I will copy those from GUI.
Navigate to System>Fabric>Transport Zones, find the TZs you need, click on the ID so you can copy the whole string.
For me, ID for Overlay TZ is 2a1dd409-208f-465c-9e19-5b5f797523b6 and ID for VLAN TZ is a583737d-9180-4c22-9d48-78cada932364.
Based on this info lets construct the API call:
we will have to use PATCH method on https://{{NSX_MANAGER}}//policy/api/v1/infra (replace {{NSX_MANAGER}} with IP or Hostname of your actual NSX manager). Configure Basic Auth with your admin credentials. Set Content-Type to application/json.
The Code bellow with be our request body (aka payload) but make sure to replace the Transport Zone IDs with the ones you captured in previous step. You just need to replace the last part of the transport_zone_path.
Oh yeah, and if you want to use different VLAN ID make sure to replace that “0” in vlan_ids with actual VLAN ID you need.
{ "resource_type": "Infra", "children": [ { "resource_type": "ChildSegment", "Segment": { "resource_type": "Segment", "transport_zone_path": "/infra/sites/default/enforcement-points/default/transport-zones/2a1dd409-208f-465c-9e19-5b5f797523b6", "id": "Overlay_Segment", "display_name": "Overlay_Segment" } }, { "resource_type": "ChildSegment", "Segment": { "resource_type": "Segment", "transport_zone_path": "/infra/sites/default/enforcement-points/default/transport-zones/a583737d-9180-4c22-9d48-78cada932364", "id": "VLAN_segment", "display_name": "VLAN_segment", "vlan_ids": [ "0" ] } } ] }
The status response you are expecting to get is 200 OK. If you see it, it means everything is fine and your segments are created. Below some example screenshots.
That’s it, pretty simple. From here you can modify that JSON to create as many segments as you want. Or you can, for example, use it in Ansible playbook. Many, many opportunities.
What other API calls you would want to see described? Let me know in comments.
Latest posts by Aram Avetisyan (see all)
- Make Youtube Videos About Technology? Why not… The Cross-Cloud Guy - October 7, 2021
- Automating (NSX-T) REST API using Ansible URI module - December 29, 2020
- Quick Reference: Create Security Policy with Firewall Rules using NSX-T Policy API - May 4, 2020
Pingback: Create NSX-T Tier-0 Gateway using Policy API - The Virtualist
Pingback: Create NSX-T Tier-0 Gateway using Policy API - Free Help Tech
What if I want to specify a range for vlan_ids (0 – 4094)?
If you want to specify a range, just replace the VLAN ID with a range.
“vlan_ids”: [
“0-4094”
]
Hello Aram, neat tutorial here. I am using 2.5 and I am having some trouble getting rules and sections created, using the API guide from the NSX help page too (the HTML page) is there any way you could do something like that? thanks!
Hey Johnny,
Here you go: https://thevirtualist.org/quick-reference-create-security-policy-with-firewall-rules-using-nsx-t-policy-api/
Hope it helps.
yes sir it does and did! Been using these examples you have, getting better everyday. Thank you very much
Hello this is very helpul, do we have a script to automate the creation of LS on NSXT referring to the existing distributed port groups? like be referring to an existing RVtools looking at the existing DPGs and creating corresponding LS on NSXT, using the policy APIs, I think this is doable, but is there any readily available script?
Hello Navazish, it should be doable, but i do not have anything ready.
Hello Sir,
How we can create multiple Vlan Backed segment using postman ?