From b03c1a536dad532e162a0994bc732fe54febcf0e Mon Sep 17 00:00:00 2001 From: Grzegorz Kulikowski Date: Mon, 5 Oct 2020 20:16:36 +0200 Subject: [PATCH 1/5] Add pester tests for Disconnect-SsoaAminServer --- .../src/test/ConnectDisconnect.Tests.ps1 | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/Modules/VMware.vSphere.SsoAdmin/src/test/ConnectDisconnect.Tests.ps1 b/Modules/VMware.vSphere.SsoAdmin/src/test/ConnectDisconnect.Tests.ps1 index fee3610..02d8793 100644 --- a/Modules/VMware.vSphere.SsoAdmin/src/test/ConnectDisconnect.Tests.ps1 +++ b/Modules/VMware.vSphere.SsoAdmin/src/test/ConnectDisconnect.Tests.ps1 @@ -84,6 +84,62 @@ Describe "Connect-SsoAdminServer and Disconnect-SsoAdminServer Tests" { $expected.IsConnected | Should Be $false } + It 'Diconnect-SsoAdminServer disconnects the currently connected SSO in case there is 1 SSO server' { + # Arrange + $expected = Connect-SsoAdminServer ` + -Server $VcAddress ` + -User $User ` + -Password $Password ` + -SkipCertificateCheck + + # Act + Disconnect-SsoAdminServer -server $expected + + # Assert + $global:DefaultSsoAdminServers | Should Not Contain $expected + $expected.IsConnected | Should Be $false + } + + It 'Diconnect-SsoAdminServer does not disconnect if connected to more than 1 SSO server' { + # Arrange + $expected += @(Connect-SsoAdminServer ` + -Server $VcAddress ` + -User $User ` + -Password $Password ` + -SkipCertificateCheck) + $expected += @(Connect-SsoAdminServer ` + -Server $VcAddress ` + -User $User ` + -Password $Password ` + -SkipCertificateCheck) + + # Act + {Disconnect-SsoAdminServer} | should -Throw + # Assert + (Compare-Object $global:DefaultSsoAdminServers $expected -IncludeEqual).Count | Should Be 2 + $expected.IsConnected | Should -Contain $true + } + + It 'Diconnect-SsoAdminServer does disconnect via pipeline if connected to more than 1 SSO server' { + # Arrange + $expected += @(Connect-SsoAdminServer ` + -Server $VcAddress ` + -User $User ` + -Password $Password ` + -SkipCertificateCheck) + $expected += @(Connect-SsoAdminServer ` + -Server $VcAddress ` + -User $User ` + -Password $Password ` + -SkipCertificateCheck) + + # Act + $expected | Disconnect-SsoAdminServer + # Assert + $global:DefaultSsoAdminServers.count | Should Be 0 + $expected.IsConnected | Should -not -Contain $true + } + It 'Disconnects disconnected object' { # Arrange $expected = Connect-SsoAdminServer ` From ca508570fba07d03ef9d19a364d9b606ce8ff436 Mon Sep 17 00:00:00 2001 From: Grzegorz Kulikowski Date: Mon, 5 Oct 2020 20:28:40 +0200 Subject: [PATCH 2/5] Add support for quick disconnect to Disconnect-SsoAdminServer In case one is connected just to one SSO server the cmdlet does not need to take the -server parameter as it will be discovered from $DefaultSsoAdminServers. In case there are more than 1 SSO servers it will not disconnect any connection, instead it will ask to be more precise using the Server parameter. --- .../VMware.vSphere.SsoAdmin.psm1 | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 b/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 index f2d89f6..9d0898b 100644 --- a/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 +++ b/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 @@ -147,7 +147,7 @@ function Connect-SsoAdminServer { } function Disconnect-SsoAdminServer { -<# + <# .NOTES =========================================================================== Created on: 9/29/2020 @@ -167,18 +167,45 @@ function Disconnect-SsoAdminServer { Disconnect a SSO Admin connection stored in 'mySsoAdminConnection' varaible #> -[CmdletBinding()] - param( - [Parameter( - Mandatory=$true, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$false, - HelpMessage='SsoAdminServer object')] - [ValidateNotNull()] - [VMware.vSphere.SsoAdminClient.DataTypes.SsoAdminServer] - $Server) + [CmdletBinding()] + param( + [Parameter( + ValueFromPipeline = $true, + ValueFromPipelineByPropertyName = $false, + HelpMessage = 'SsoAdminServer object')] + [ValidateNotNull()] + [VMware.vSphere.SsoAdminClient.DataTypes.SsoAdminServer] + $Server + ) Process { + if (-not $PSBoundParameters['Server']) { + switch (@($global:DefaultSsoAdminServers).count) { + { $_ -eq 1 } { $server = ($global:DefaultSsoAdminServers).ToArray()[0] ; break } + { $_ -gt 1 } { + $PSCmdlet.ThrowTerminatingError( + [System.Management.Automation.ErrorRecord]::new( + ([System.ApplicationException]"Connected to more than 1 SSO server, please specify a SSO server via -Server parameter"), + 'Disconnect-SsoAdminServer.ConnectedToMoreThanOneSSO', + [System.Management.Automation.ErrorCategory]::InvalidOperation, + $global:DefaultSsoAdminServers + ) + ) + break + } + Default { + $PSCmdlet.ThrowTerminatingError( + [System.Management.Automation.ErrorRecord]::new( + ([System.ApplicationException]"Not connected to SSO server."), + 'Disconnect-SsoAdminServer.NotConnectedToSSO', + [System.Management.Automation.ErrorCategory]::ConnectionError, + $global:DefaultSsoAdminServers + ) + ) + } + } + } + if ($global:DefaultSsoAdminServers.Contains($Server)) { $global:DefaultSsoAdminServers.Remove($Server) | Out-Null } From d6565f9e2629b57a5b9d7aaea3ceb6ad097e018c Mon Sep 17 00:00:00 2001 From: Grzegorz Kulikowski Date: Mon, 5 Oct 2020 20:38:37 +0200 Subject: [PATCH 3/5] fix test for Disconnect-SsoAdminServer --- .../src/test/ConnectDisconnect.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/VMware.vSphere.SsoAdmin/src/test/ConnectDisconnect.Tests.ps1 b/Modules/VMware.vSphere.SsoAdmin/src/test/ConnectDisconnect.Tests.ps1 index 02d8793..9a8243e 100644 --- a/Modules/VMware.vSphere.SsoAdmin/src/test/ConnectDisconnect.Tests.ps1 +++ b/Modules/VMware.vSphere.SsoAdmin/src/test/ConnectDisconnect.Tests.ps1 @@ -114,8 +114,9 @@ Describe "Connect-SsoAdminServer and Disconnect-SsoAdminServer Tests" { -SkipCertificateCheck) # Act - {Disconnect-SsoAdminServer} | should -Throw + # Assert + {Disconnect-SsoAdminServer} | should -Throw (Compare-Object $global:DefaultSsoAdminServers $expected -IncludeEqual).Count | Should Be 2 $expected.IsConnected | Should -Contain $true } From 7832d6e7ae968009feeeb2dae56c5034ba28a60e Mon Sep 17 00:00:00 2001 From: Grzegorz Kulikowski Date: Tue, 6 Oct 2020 08:50:50 +0200 Subject: [PATCH 4/5] Use Throw instead of ThrowTerminatingError() --- .../VMware.vSphere.SsoAdmin.psm1 | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 b/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 index 9d0898b..92e4915 100644 --- a/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 +++ b/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 @@ -183,25 +183,11 @@ function Disconnect-SsoAdminServer { switch (@($global:DefaultSsoAdminServers).count) { { $_ -eq 1 } { $server = ($global:DefaultSsoAdminServers).ToArray()[0] ; break } { $_ -gt 1 } { - $PSCmdlet.ThrowTerminatingError( - [System.Management.Automation.ErrorRecord]::new( - ([System.ApplicationException]"Connected to more than 1 SSO server, please specify a SSO server via -Server parameter"), - 'Disconnect-SsoAdminServer.ConnectedToMoreThanOneSSO', - [System.Management.Automation.ErrorCategory]::InvalidOperation, - $global:DefaultSsoAdminServers - ) - ) + Throw 'Connected to more than 1 SSO server, please specify a SSO server via -Server parameter' break } Default { - $PSCmdlet.ThrowTerminatingError( - [System.Management.Automation.ErrorRecord]::new( - ([System.ApplicationException]"Not connected to SSO server."), - 'Disconnect-SsoAdminServer.NotConnectedToSSO', - [System.Management.Automation.ErrorCategory]::ConnectionError, - $global:DefaultSsoAdminServers - ) - ) + Throw 'Not connected to SSO server.' } } } From 2c1eafde1826ce5af85cda367f53e401115e361f Mon Sep 17 00:00:00 2001 From: Grzegorz Kulikowski Date: Tue, 6 Oct 2020 08:59:32 +0200 Subject: [PATCH 5/5] Modify test disconnect-ssoadminserver with throw msg --- .../src/test/ConnectDisconnect.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/VMware.vSphere.SsoAdmin/src/test/ConnectDisconnect.Tests.ps1 b/Modules/VMware.vSphere.SsoAdmin/src/test/ConnectDisconnect.Tests.ps1 index 9a8243e..53ef770 100644 --- a/Modules/VMware.vSphere.SsoAdmin/src/test/ConnectDisconnect.Tests.ps1 +++ b/Modules/VMware.vSphere.SsoAdmin/src/test/ConnectDisconnect.Tests.ps1 @@ -116,7 +116,7 @@ Describe "Connect-SsoAdminServer and Disconnect-SsoAdminServer Tests" { # Act # Assert - {Disconnect-SsoAdminServer} | should -Throw + {Disconnect-SsoAdminServer} | should -Throw 'Connected to more than 1 SSO server, please specify a SSO server via -Server parameter' (Compare-Object $global:DefaultSsoAdminServers $expected -IncludeEqual).Count | Should Be 2 $expected.IsConnected | Should -Contain $true }