Create a Segment using Policy API on NSX-T 2.5

Share this:

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.

Screenshot of API call from postman
List of Segments

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.

The following two tabs change content below.
Aram Avetisyan is an IT specialist with more than 18 years experience. He has rich background in various IT related fields like Cloud, Virtualization and SDN. He holds several industry level certifications including but not limited to VCIX-DCV, VCIX-NV. He is also a vEXPERT in years 2014-2021.

About Aram Avetisyan

Aram Avetisyan is an IT specialist with more than 18 years experience. He has rich background in various IT related fields like Cloud, Virtualization and SDN. He holds several industry level certifications including but not limited to VCIX-DCV, VCIX-NV. He is also a vEXPERT in years 2014-2021.
Bookmark the permalink.

10 Comments

  1. Pingback: Create NSX-T Tier-0 Gateway using Policy API - The Virtualist

  2. Pingback: Create NSX-T Tier-0 Gateway using Policy API - Free Help Tech

  3. What if I want to specify a range for vlan_ids (0 – 4094)?

  4. 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!

  5. 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?

  6. Hello Sir,

    How we can create multiple Vlan Backed segment using postman ?

Leave a Reply

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