vmware-archive / powernsx

PowerShell module that abstracts the VMware NSX-v API to a set of easily used PowerShell functions
173 stars 90 forks source link

Set-NSXLogicalSwitch -Connection argument bug #640

Closed vireshov closed 3 years ago

vireshov commented 3 years ago

Hi guys,

I've been working on an NSX script recently, combining some PowerNSX outlets (Logical Switch related) with native NSX API calls (DFW related). However, upon testing, I noticed that one of the outlets is not working as expected (or as I am expecting it to work :)).

Finding: It seems that the Set-NSXLogicalSwitch does not handle the "-Connection" argument properly. I specify it as follows:

$LS | Set-NSXLogicalSwitch -Name "VVscriptrename" -Connection $NSXConn

Error: See error.txt attached. What it basically indicates is that the $NSXConn is not a valid PowerNSX connection object (it doesn't see Credential/uriprefix/protocol properties).

I know for a fact that my $NSXConn is a valid connection object however, as it works with other PowerNSX outlets. E.g., this works fine (with $defaultNSXConnection being empty, just to make sure it isn't falling back) :

$NSXConn = Connect-NSXServer -vCenterServer $vcFRA -Credential $cred -DefaultConnection:$false -VIDefaultConnection:$false -ErrorAction Stop

$LS = Get-NSXLogicalSwitch -Transportzone $nsxLocalTZ -Connection $NSXConn -Name "VVscripttest" -ErrorAction Stop

I've added the "-Defaultconnection" and "-VIDefaultConnection" arguments later, for testing purposes, but both with and without these arguments, the Set-NSXLogicalSwitch fails.

Workaround: as I'm using try/catch blocks, I will always enter the catch block and my script will terminate. A workaround is to have a default NSX connection set ($defaultNSXConnection), so that it will fallback to that (it works, I tested it), but in my case this is not workable, as my script is connecting to multiple NSX instances and I need to perform actions on specific NSX instances based on certain logic. I've stored these instances as objects in an array to loop over them to do operations on multiple NSX instances (based on specific logic).

Can you guys check it out?

Note: When reproducing, please make sure $defaultNSXConnection is empty, after connecting to your NSX instances and before u run the Set-NSXLogicalSwitch command with the -Connection argument.

Thanks and Regards,

Viresh error.txt

vireshov commented 3 years ago

forgot to mention PowerNSX version:

Script 3.0.1174 PowerNSX

vireshov commented 3 years ago

FYI, for now Ive worked around the issue, by using the native API code, instead of using the Set-NSXLogicalSwitch cmdlet. This works fine, with the same connection object that I was using for Set-NSXLogicalSwitch! See also attached screenshot.

1112

alagoutte commented 3 years ago

Hi @vireshov

From the code (PowerNSX.psm1)

[...]
function Set-NsxLogicalSwitch {
$uri = "/api/2.0/vdn/virtualwires/$($ObjectId)"
        $null = Invoke-NsxWebRequest -method put -Uri $uri -body $_virtualWire.OuterXml -connection $connection
        Get-NsxlogicalSwitch -ObjectId $ObjectId
[...]

I think the issue coming for last line, there is no connection parameter to Get-NSXLogicalSwitch it is a easy fix to implement (and try)

vireshov commented 3 years ago

Hi @alagoutte

Well damn, I did actually check the PowerNSX code for this Set-NSXLogicalSwitch function to see if I could find the bug myself, but I focussed on the line of the Invoke-WebRequest, which looked fine. But indeed, the Get-NSXLogicalSwitch doesn't have the "-Connection" argument specified! That must be it, especially since the actual rename of my LS does get executed (I forgot to mention that earlier), but then after that it throws the error. I will give it a try tomorrow morning, I've logged out of my work laptop now.

Cheers !

vireshov commented 3 years ago

Hi @alagoutte ,

I've changed the line in the PowerNSX code today and tested it, all good. Will this bug fix (-Connection $connection) be included in a next release, so that Set-NSXLogicalSwitch doesn't throw an error when $defaultNSXConnection is empty?

Thanks !

alagoutte commented 3 years ago

Hi @alagoutte ,

I've changed the line in the PowerNSX code today and tested it, all good. Will this bug fix (-Connection $connection) be included in a next release, so that Set-NSXLogicalSwitch doesn't throw an error when $defaultNSXConnection is empty?

Thanks !

No yet, but you can push the fix ! (if need i can push the fix too) but i don't known when there is the next release !

dcoghlan commented 3 years ago

If you make the changes and write a simple test for it, I can have it tested relatively quickly and merged into master. Not sure when a new version will be published, but it needs to be sometime soon to incorporate the latest changes made

alagoutte commented 3 years ago

If you make the changes and write a simple test for it, I can have it tested relatively quickly and merged into master. Not sure when a new version will be published, but it needs to be sometime soon to incorporate the latest changes made

For test, it will be complicated, there is not really test with multi connection actually... (or need to rewrite all tests for use multi connection...)