VMware Networking Community
sivakumarss
Enthusiast
Enthusiast
Jump to solution

NSX plugin API to get list of edges

Hello.

Is there any method (scripting class) to get the list of edges for a given NSX connection?

I am using the NSX HTTP REST method to get the list of edges. But was wondering if I have missed any method in say NSXEdgeManager class. There is a method to get an edge provided an id is given. But none to list ALL the edges. This is VRA 7.3. Thanks

Tags (3)
1 Solution

Accepted Solutions
jasnyder
Hot Shot
Hot Shot
Jump to solution

The page size is 20 and isn't configurable in this case.  You can set the index of the first edge to receive on the first page in the getEdges(connection, index) call.  It appears that if you specify an index higher than the total number of edges, it will restart at index 0.  This means that if the total # of edges in your environment is a non-zero multiple of 20 (i.e. totalEdges % 20 = 0), the edge page returned will always be full. 

Here is some code that should cover this case.  Basically, we're paging through the returned edges 20 at a time.  As long as the amount returned is equal to 20, we'll grab another page by incrementing index by 20 and going back to the server with the new index.  If the number of edges returned is less than 20, then the page wasn't full and we reached the end of the list.  To cover the case where the total edges is a multiple of 20, which would otherwise cause an infinite loop, we keep a dictionary of all unique edge IDs and perform a lookup on each new edge to make sure it's not in the dictionary.  If a duplicate is found, assume we've started over at index = 0 and break out of the loop.

Change maxIndex = 400 to a higher number if you think you will be pulling more than 400 edges.  I don't know what the NSX config maximum is on edges... but I wanted to make sure this code doesn't execute for too long or get into an infinite loop scenario.

var keepLooping = true;

var index = 0;

var fullEdgeList = new Array();

var maxIndex = 400;

var edgeIDDictionary = {};

//maxIndex will limit the total number edges to return and prevent an infinite loop

//  in case a condition exists that would cause one

while(keepLooping && index < maxIndex) {

    //get edges starting at current index

    var edges = NSXEdgeManager.getEdges(conn, index);

    var duplicateFound = false;

   

    //add each edge to the full edge list for consumption later

    for(var e in edges) {

        //Check for a duplicate by seeing if the edge ID is already in the dictionary

        if(edgeIDDictionary[edges[e].id.toString()] == edges[e].id.toString()) {

            duplicateFound = true;

            System.log("Found duplicate; exiting loop - " + edges[e].id);

            break;

        }

       

        //add the edge ID to the dictionary so we can find duplicates later

        edgeIDDictionary[edges[e].id.toString()] = edges[e].id.toString()

        fullEdgeList.push(edges[e]);

    }

   

    //Only continue looping if the page was full and a duplicate wasn't found

    keepLooping = (edges.length == 20) && (!duplicateFound);

   

    //increase the index if we're continuing

    if(keepLooping) index += 20;

}

for(var e in fullEdgeList) {

    System.log(e + ". " + fullEdgeList[e].name + " - " + fullEdgeList[e].id);

}


I think it would be possible for an edge to be added while this code is executing (the more edges there are, the more round trips are required, and the longer it takes to fully execute).  In that case, I don't know exactly how it would respond.  My assumption is that any new edges are appended to the list in the order they were added.  This has held true during my testing, but I don't know this is always the case.

In any case, we're quitting the loop as soon as the first duplicate is found, so if the list is modified in some kind of way while we're pulling pages, it shouldn't be an issue except that it may not be up to date, especially for larger lists of edges and in cases where edges are being created and destroyed often.  This would be true of any solution however - the data is basically stale the second you grab it from the source.

The problem is probably academic, but it's maybe something to keep in mind.

View solution in original post

7 Replies
Mparayil
Enthusiast
Enthusiast
Jump to solution

Hi Sivakumar,

what is the version of NSX you have deployed.

GET

https://nsxmgr-IP/api/4.0/edges/?

This should list out all the ESG and DLR managed by the NSX manager

let me know if you are looking for this information

pastedImage_1.png

sivakumarss
Enthusiast
Enthusiast
Jump to solution

I am using the below REST API only.

What I was looking for an equivalent using NSXEdgeManager.

If I know the edge id, I can use 'NSXEdgeManager.getEdge(NSXConn, edgeId)' to get the NSXEdge object.

Similarly, I want to use 'NSXEdgeManager.getAllEdges(NSXConn)' or somthing similar.

Thanks

0 Kudos
jasnyder
Hot Shot
Hot Shot
Jump to solution

Yes, you can use the NSXEdgeManager.getEdges(conn, startIndex) method, as such:

var edges = NSXEdgeManager.getEdges(conn, 0);

for(var e in edges) {

    System.log(edges[e].name + " - " + edges[e].id);

}

(conn is an object of type NSX:Connection; getEdges returns an Array/NSXEdge object)

Sample Output:

[2017-11-24 15:06:32.051] [I] backup-b7c9fc26-c86f - b9d6c87f-63ff-421c-9212-b61820333abb/edge-3

[2017-11-24 15:06:32.055] [I] dhcp-04ef360f-e278-4 - b9d6c87f-63ff-421c-9212-b61820333abb/edge-2

[2017-11-24 15:06:32.057] [I] backup-937a9889-c609 - b9d6c87f-63ff-421c-9212-b61820333abb/edge-4

[2017-11-24 15:06:32.059] [I] metadata_proxy_router-cb3435d3-9538-4593-a97f-3ba6b20c969e - b9d6c87f-63ff-421c-9212-b61820333abb/edge-5

[2017-11-24 15:06:32.061] [I] metadata_proxy_router-b3751366-5a8b-473c-bb2c-f79106fee33e - b9d6c87f-63ff-421c-9212-b61820333abb/edge-6

[2017-11-24 15:06:32.062] [I] dhcp-08c47b58-0e14-4 - b9d6c87f-63ff-421c-9212-b61820333abb/edge-8

[2017-11-24 15:06:32.064] [I] k8s-router-1a89e9d4-c14a-49b6-b88c-79419d-669303d1-6abc-4e3b-a56d-07b50b0cf353 - b9d6c87f-63ff-421c-9212-b61820333abb/edge-9

[2017-11-24 15:06:32.066] [I] backup-d5569779-64d8 - b9d6c87f-63ff-421c-9212-b61820333abb/edge-10

[2017-11-24 15:06:32.068] [I] k8s-router-13e5a729-9377-48b7-84d2-07f922-e99c36c1-2812-4cc7-afc7-ae5c1729624b - b9d6c87f-63ff-421c-9212-b61820333abb/edge-11

[2017-11-24 15:06:32.070] [I] backup-aa6c2c14-4493 - b9d6c87f-63ff-421c-9212-b61820333abb/edge-12

[2017-11-24 15:06:32.072] [I] backup-e2f2ba2d-d1a6 - b9d6c87f-63ff-421c-9212-b61820333abb/edge-16

[2017-11-24 15:06:32.074] [I] dhcp-6280b85a-075f-4 - b9d6c87f-63ff-421c-9212-b61820333abb/edge-17

[2017-11-24 15:06:32.077] [I] backup-17dcc2a4-bdd2 - b9d6c87f-63ff-421c-9212-b61820333abb/edge-15

[2017-11-24 15:06:32.078] [I] metadata_proxy_router-997fa687-782e-4245-83d0-e37f7b90c885 - b9d6c87f-63ff-421c-9212-b61820333abb/edge-18

[2017-11-24 15:06:32.080] [I] metadata_proxy_router-a095276b-b5ad-4238-acf9-243a0bd8c515 - b9d6c87f-63ff-421c-9212-b61820333abb/edge-19

[2017-11-24 15:06:32.084] [I] k8s-router-5cf4a8e0-3fd8-472c-a8c1-deff4c-804a6f41-091b-426e-b7f5-518678d0d69a - b9d6c87f-63ff-421c-9212-b61820333abb/edge-20

[2017-11-24 15:06:32.086] [I] backup-268aa62b-0327 - b9d6c87f-63ff-421c-9212-b61820333abb/edge-21

It's not documented, but it's there (tested on vRA 7.3/vRO 7.3/NSX Plugin 1.1.0)

sivakumarss
Enthusiast
Enthusiast
Jump to solution

Thanks. This is what I was looking for.

It will be good if this is properly documented in future.

sivakumarss
Enthusiast
Enthusiast
Jump to solution

Hello.

The method 'getEdges' is not returning all the edges. I see it returns only 20 edges. My customer site has more than 50 edges.

How to get them all?

Thanks

Siva

0 Kudos
jasnyder
Hot Shot
Hot Shot
Jump to solution

The page size is 20 and isn't configurable in this case.  You can set the index of the first edge to receive on the first page in the getEdges(connection, index) call.  It appears that if you specify an index higher than the total number of edges, it will restart at index 0.  This means that if the total # of edges in your environment is a non-zero multiple of 20 (i.e. totalEdges % 20 = 0), the edge page returned will always be full. 

Here is some code that should cover this case.  Basically, we're paging through the returned edges 20 at a time.  As long as the amount returned is equal to 20, we'll grab another page by incrementing index by 20 and going back to the server with the new index.  If the number of edges returned is less than 20, then the page wasn't full and we reached the end of the list.  To cover the case where the total edges is a multiple of 20, which would otherwise cause an infinite loop, we keep a dictionary of all unique edge IDs and perform a lookup on each new edge to make sure it's not in the dictionary.  If a duplicate is found, assume we've started over at index = 0 and break out of the loop.

Change maxIndex = 400 to a higher number if you think you will be pulling more than 400 edges.  I don't know what the NSX config maximum is on edges... but I wanted to make sure this code doesn't execute for too long or get into an infinite loop scenario.

var keepLooping = true;

var index = 0;

var fullEdgeList = new Array();

var maxIndex = 400;

var edgeIDDictionary = {};

//maxIndex will limit the total number edges to return and prevent an infinite loop

//  in case a condition exists that would cause one

while(keepLooping && index < maxIndex) {

    //get edges starting at current index

    var edges = NSXEdgeManager.getEdges(conn, index);

    var duplicateFound = false;

   

    //add each edge to the full edge list for consumption later

    for(var e in edges) {

        //Check for a duplicate by seeing if the edge ID is already in the dictionary

        if(edgeIDDictionary[edges[e].id.toString()] == edges[e].id.toString()) {

            duplicateFound = true;

            System.log("Found duplicate; exiting loop - " + edges[e].id);

            break;

        }

       

        //add the edge ID to the dictionary so we can find duplicates later

        edgeIDDictionary[edges[e].id.toString()] = edges[e].id.toString()

        fullEdgeList.push(edges[e]);

    }

   

    //Only continue looping if the page was full and a duplicate wasn't found

    keepLooping = (edges.length == 20) && (!duplicateFound);

   

    //increase the index if we're continuing

    if(keepLooping) index += 20;

}

for(var e in fullEdgeList) {

    System.log(e + ". " + fullEdgeList[e].name + " - " + fullEdgeList[e].id);

}


I think it would be possible for an edge to be added while this code is executing (the more edges there are, the more round trips are required, and the longer it takes to fully execute).  In that case, I don't know exactly how it would respond.  My assumption is that any new edges are appended to the list in the order they were added.  This has held true during my testing, but I don't know this is always the case.

In any case, we're quitting the loop as soon as the first duplicate is found, so if the list is modified in some kind of way while we're pulling pages, it shouldn't be an issue except that it may not be up to date, especially for larger lists of edges and in cases where edges are being created and destroyed often.  This would be true of any solution however - the data is basically stale the second you grab it from the source.

The problem is probably academic, but it's maybe something to keep in mind.

sivakumarss
Enthusiast
Enthusiast
Jump to solution

Fantastic. Thanks a lot. I was about to rewrite using API which is okay for a single Nsx setup. But this one has multiple vcenters.

0 Kudos