Adding or removing single static routes using API on VMware NSX for vSphere 6.x Edge routers
search cancel

Adding or removing single static routes using API on VMware NSX for vSphere 6.x Edge routers

book

Article ID: 325446

calendar_today

Updated On:

Products

VMware NSX Networking

Issue/Introduction

This article provides information on the supported API method for adding and removing static routes on the VMware NSX for vSphere 6.x Edge routers. The same functionality is also exposed in the VMware NSX for vSphere 6.x Graphical User Interface (GUI).

Environment

VMware NSX for vSphere 6.0.x
VMware NSX for vSphere 6.1.x

Resolution

The VMware NSX for vSphere 6.x Application Programming Interface (API) and Graphical User Interface (GUI), allows the creation and deletion of static routes in the NSX Edge routers. However it is not possible, with the API, to reference to a single static route. To add or remove a single element, you need to query the static routing configuration first, modify it and then apply the new one.

Note: When using the VMware NSX for vSphere 6.x API with the PUT and POST methods, it is important that the encoding specified on the headers are configured with application/xml and not application/json. Specifying application/json may result in HTTP Status errors. Also, the NSX for vSphere API session must be authenticated using valid credentials for the NSX Manager endpoint.

To configure static routes on the Edge Gateway:

Working with Edge Routers

When working with the APIs, you need to know the edgeId of the corresponding Edge router you want to configure. The edgeId is shown in the Id column in the list of Edge routers in the NSX for vSphere GUI, or it can be retrieved using the API call:

Request:
GET https://<nsxmgr-ip>/api/4.0/edges/

Query Static Routing configuration
Retrieve the NSX Edge Static Route Configuration with the following API call, specifying the correct edgeId:

Request:

GET https://<nsxmgr-ip>/api/4.0/edges/<edgeId>/routing/config/static

The expected response body is similar to:

<?xml version="1.0" encoding="UTF-8"?>
<staticRouting>
<staticRoutes>
<route>
<description>route1</description>
<vnic>0</vnic>
<network>3.1.1.4/22</network>
<nextHop>172.16.1.14</nextHop>
<mtu>1500</mtu>
<type>user</type>
</route>
<route>
<description>route2</description>
<vnic>1</vnic>
<network>4.1.1.4/22</network>
<nextHop>10.112.196.118</nextHop>
<mtu>1500</mtu>
<type>user</type>
</route>
</staticRoutes>
<defaultRoute>
<description>defaultRoute</description>
<vnic>0</vnic>
<gatewayAddress>172.16.1.12</gatewayAddress>
<mtu>1500</mtu>
</defaultRoute>


In this example, two static routes and a default route are present.

Add a static route

To add a static route to an existing configuration, you need to add it to the XML response previously obtained, and update the overall static routing configuration on the Edge Gateway.

For example, to add a 100.100.100.0/24 static route with a next hop pointing to 172.16.1.20 on vNic 0, you need to:

<route>
<description>route3</description>
<vnic>0</vnic>
<network>100.100.100.0/24</network>
<nextHop>172.16.1.20</nextHop>
<mtu>1500</mtu>
<type>user</type>
</route>


The overall PUT request to the API endpoint will therefore look like this:

Request:

PUT https://<nsxmgr-ip>/api/4.0/edges/<edgeId>/routing/config/static

Request Body:

<staticRouting>
<staticRoutes>
<route>
<description>route1</description>
<vnic>0</vnic>
<network>3.1.1.4/22</network>
<nextHop>172.16.1.14</nextHop>
<mtu>1500</mtu>
<type>user</type>
</route>
<route>
<description>route2</description>
<vnic>1</vnic>
<network>4.1.1.4/22</network>
<nextHop>10.112.196.118</nextHop>
<mtu>1500</mtu>
<type>user</type>
</route>
<route>
<description>route3</description>
<vnic>0</vnic>
<network>100.100.100.0/24</network>
<nextHop>172.16.1.20</nextHop>
<mtu>1500</mtu>
<type>user</type>
</route>
</staticRoutes>
<defaultRoute>
<description>defaultRoute</description>
<vnic>0</vnic>
<gatewayAddress>172.16.1.12</gatewayAddress>
<mtu>1500</mtu>
</defaultRoute>


Remove a static route
To remove a specific static route from an existing configuration, you need to remove the corresponding part of the XML response previously obtained, and update the overall static routing configuration on the Edge Gateway. For example, to remove the second static route (4.1.1.4/22 via 10.112.196.118) on the previous configuration, you need to clear these from the XML request:

<route>
<description>route2</description>
<vnic>1</vnic>
<network>4.1.1.4/22</network>
<nextHop>10.112.196.118</nextHop>
<mtu>1500</mtu>
<type>user</type>
</route>


The HTTP PUT request to the API endpoint will therefore look similar to:

Request:

PUT https://<nsxmgr-ip>/api/4.0/edges/<edgeId>/routing/config/static

Request Body:

<staticRouting>
<staticRoutes>
<route>
<description>route1</description>
<vnic>0</vnic>
<network>3.1.1.4/22</network>
<nextHop>172.16.1.14</nextHop>
<mtu>1500</mtu>
<type>user</type>
</route>
<route>
<description>route3</description>
<vnic>0</vnic>
<network>100.100.100.0/24</network>
<nextHop>172.16.1.20</nextHop>
<mtu>1500</mtu>
<type>user</type>
</route>
</staticRoutes>
<defaultRoute>
<description>defaultRoute</description>
<vnic>0</vnic>
<gatewayAddress>172.16.1.12</gatewayAddress>
<mtu>1500</mtu>
</defaultRoute>


Clear static routing configuration

To completely delete the static routing configuration of the Edge gateway, including the default gateway configuration, use the API call:

Request:
DELETE https://<nsxmgr-ip>/api/4.0/edges/<edgeId>/routing/config/static

For more information, see the NSX for vSphere API Reference Guide.

Additional Information

VMware NSX for vSphere 6.x Edge ルータでの API を使用した単一の固定ルートの追加または削除
使用 API 在 VMware NSX for vSphere 6.x Edge 路由器上添加或移除单个静态路由