NSX-T PowerCLI Search option

Submitted by Robin van Altena on Mon, 02/21/2022 - 12:48
 
 
Follow your favourite author

Leave us your email address and be the first to receive a notification when Robin posts a new blog.

NSX-T PowerCLI Search option
Mon 21 Feb, 2022
When scripting with NSX-T there are multiple ways to retrieve an item that needs to be modified. For example: a group can be retrieved using an API call that lists all the groups, but it is also possible to search for a group. Which is the best method and how do they work?
Textarea

A couple of weeks ago I was working for a customer that needed to add multiple VM’s to several different groups. So, I started to look at the scripts or function we created previously. In our repository I found a function to retrieve all the groups from NSX-T and add them to global array. I also found a function that checks the array to see if a group exists and return its ID.

This function uses the PowerCLI command to retrieve all groups:

Code (new)

Get-NsxtPolicyService -Name com.vmware.nsx_policy.infra.domains.groups

Textarea

Or if you compare it to a direct API call, it will be:

Code (new)

GET https://<NSX Manager>/policy/api/v1/infra/domains/default/groups

Textarea

Since the functions I found were created with PowerCLI I continued on that path. Together with my colleague Robert we created a script to add VM's to NSX-T security groups. With some limited testing in our lab the script worked like a charm. So, we handed the script over to the customer and at first it did seem to do the trick. Until the customer mentioned he was unable to find several groups with the script that he could see in the NSX-T Manager through the GUI. Fortunately, he also found the reason and a quick solution.

I’ll create an additional blog on adding a VM to a group, but here I want to show how the groups can be found.

What went wrong

Why did the script work in our lab and not in the customers production environment? The reason was the total number of groups. In our lab we only had a couple of groups, at least less than 100. But in the production environment there were more than 1000. The reason the script fails is because the result is limited to 1000 results by default. To demonstrate this, I created 1500 test groups in our lab.

Image
NSX-T List Group results using PowerCLI.
Textarea

As you can see in the screen shot the result_count from the call to list all groups is 1591, but the count of the ID in the result is only 1000. This can also be viewed in the documentation using $Groups.help.list where it says: default to 1000. Also pay attention to the cursor value, as I’ll come back to that later in this blog.

Image
NSX-T List() help infromation
Textarea

How can this be solved?

As always there are multiple ways of solving this, but you can’t increase the page_size for the API call of the list() command. See the alternatives at the bottom of this blog post.

And as always, we were creating the script with some time constrains so, the script was adjusted as suggested in the results we found on the internet. We used the Search option. Hopefully I can find some time to also test the method to increase the page_size.

The API call we used was: 

Code (new)

$getGroupsURL = https://$nsxtmanager/policy/api/v1/search?query=display_name:$($NSXTFindGroup)

Textarea

Naturally I would like to show you how this can be done with the NsxtPolicyService module. Once a connection has been made towards the NSX-T manager. You can use the following to search for a group called Test:

Code (new)

$NSXGroup = ‘Test’
$Search = Get-NsxtPolicyService -Name com.vmware.nsx_policy.search.query
$SearchGrp = $Search.list($NSXGroup).results

Image
Results for the Search Test
Textarea

The screenshot shows that the search has multiple results of different types, not just groups. This wasn’t an issue in the script we needed for our customer, since all their groups start with SG_* and there was no possibility that a group with the same display name existed twice. But for this blog I wanted to show how to find the correct group named Test. Using the search function in the example above, NSX-T searches for *Test* and returns everything containing ‘test’ in the name, regardless of type.

Back to finding just the group named ‘Test’ (and nothing else). The first clue is already shown in the screenshot above. Each result in the search query has a property named resource_type. The next script can be used to filter the results and display the groups with the display name Test.

Code (new)

$NSXGroup = 'Test'
$Search = Get-NsxtPolicyService -Name com.vmware.nsx_policy.search.query
$SearchGrp = $search.list($NSXGroup).results
foreach($result in $SearchGrp)
{
    if ($result.resource_type -eq 'Group' -and $result.display_name -eq $NSXGroup)
    {
        write-host "Group displayname " $result.display_name "with id" $result.id
    }
}

Image
Groups with the display name Test
Textarea

As you can see, I have two groups in our lab with the name (display_name) Test, but each with a different ID. We got two groups with the same display name by creating them with a unique name (which is used for the ID) and then renaming one of them. Just to show you that it is possible and might be something you need to prepare your script for – the so-called edge cases.

Alternatives

As mentioned, there are always multiple options to come to a result. Though sometimes it takes a little bit longer to figure them all out. I’ll try to explain some of the other solutions with the help of the screenshot below.

Image
Alternatives for manipulating the number of results
Textarea

Again, we are retrieving the groups into $Groups, as we have seen in the screenshot(2) above there are 1591 groups, but the results contain only 1000. With the help function we can display the Definition for the list function.

list(string domain_id, string? cursor, boolean? include_mark_for_delete_objects, string? included_fields, string? member_types, long? page_size, boolean? sort_ascending, string? sort_by)

The cursor option is used for getting next page of records (supplied by current result page) and is optional. The cursor can be added to a consecutive command to retrieve the remaining groups. As shown in the second red square in the screenshot above. The first command retrieves the first 1000 groups, the second command the remaining 591. With some logic around this you should be able to retrieve all groups.

Code (new)

$Group1 = $Groups.list('default')
$Group2 = $Groups.list('default',$Group1.cursor)
$Group1.results.id.count
$Group2.results.id.count

Textarea

The documentation also mentions page_size that defaults 1000. If you try to increase that to command fails. Looking at the yellow square in the screenshot above you can see the first call returning all the groups. If the page_size is limited to 500 the result count is also 500. But if the page_size is increased to 1500 there is an error. So, it appears the default is also the maximum.

Image
Manipulating the number of search results
Textarea

Probably there is a method of setting the page_size directly in the command, but again I’m still learning and there isn’t much to find about this on the internet. Hopefully this helps you on your scripting adventures. If you have any comments or additional questions, please let me know.

Tags

Questions, Remarks & Comments

If you have any questions and need more clarification, we are more than happy to dig deeper. Any comments are also appreciated. You can either post it online or send it directly to the author, it’s your choice.
Let us know  

 
 
Questions, Remarks & Comments

Message Robin directly, in order to receive a quick response.

More about RedLogic