Improve error handling. Added ref counting of server connections. Extended Add-ExternalDomainIdentitySource

This commit is contained in:
Dimitar Milov
2021-02-11 10:31:03 +02:00
parent dc5a755805
commit 775498aa8a
3 changed files with 31 additions and 20 deletions

View File

@@ -30,6 +30,7 @@ namespace VMware.vSphere.SsoAdminClient.DataTypes
password, password,
serverCertificateValidator); serverCertificateValidator);
RefCount = 1;
Id = $"/SsoAdminServer={NormalizeUserName()}@{Name}"; Id = $"/SsoAdminServer={NormalizeUserName()}@{Name}";
} }
@@ -50,10 +51,13 @@ namespace VMware.vSphere.SsoAdminClient.DataTypes
public string Id { get; set; } public string Id { get; set; }
public bool IsConnected => _client != null; public bool IsConnected => _client != null;
public SsoAdminClient Client => _client; public SsoAdminClient Client => _client;
public int RefCount { get; set; }
public void Disconnect() { public void Disconnect() {
if (--RefCount == 0) {
_client = null; _client = null;
} }
}
public override string ToString() { public override string ToString() {
return Name; return Name;

View File

@@ -627,9 +627,9 @@ namespace VMware.vSphere.SsoAdminClient
string baseDNUsers, string baseDNUsers,
string baseDNGroups, string baseDNGroups,
string authenticationUserName, string authenticationUserName,
string authenticationPassword) { string authenticationPassword,
string serverType) {
string serverType = "ActiveDirectory";
string authenticationType = "password"; string authenticationType = "password";
var authorizedInvocationContext = var authorizedInvocationContext =
CreateAuthorizedInvocationContext(); CreateAuthorizedInvocationContext();

View File

@@ -52,7 +52,8 @@ Describe "Connect-SsoAdminServer and Disconnect-SsoAdminServer Tests" {
-Server $VcAddress ` -Server $VcAddress `
-User $User ` -User $User `
-Password ($Password + "invalid") ` -Password ($Password + "invalid") `
-SkipCertificateCheck } | ` -SkipCertificateCheck `
-ErrorAction Stop } | `
Should Throw "Invalid credentials" Should Throw "Invalid credentials"
} }
@@ -62,7 +63,8 @@ Describe "Connect-SsoAdminServer and Disconnect-SsoAdminServer Tests" {
{ Connect-SsoAdminServer ` { Connect-SsoAdminServer `
-Server $VcAddress ` -Server $VcAddress `
-User $User ` -User $User `
-Password $Password} | ` -Password $Password `
-ErrorAction Stop } | `
Should Throw "The SSL connection could not be established, see inner exception." Should Throw "The SSL connection could not be established, see inner exception."
} }
} }
@@ -102,43 +104,48 @@ Describe "Connect-SsoAdminServer and Disconnect-SsoAdminServer Tests" {
It 'Diconnect-SsoAdminServer does not disconnect if connected to more than 1 SSO server' { It 'Diconnect-SsoAdminServer does not disconnect if connected to more than 1 SSO server' {
# Arrange # Arrange
$expected += @(Connect-SsoAdminServer ` $connection1 = Connect-SsoAdminServer `
-Server $VcAddress ` -Server $VcAddress `
-User $User ` -User $User `
-Password $Password ` -Password $Password `
-SkipCertificateCheck) -SkipCertificateCheck
$expected += @(Connect-SsoAdminServer ` $connection2 = Connect-SsoAdminServer `
-Server $VcAddress ` -Server $VcAddress `
-User $User ` -User $User `
-Password $Password ` -Password $Password `
-SkipCertificateCheck) -SkipCertificateCheck
# Act # Act
# Assert # Assert
{Disconnect-SsoAdminServer} | should -Throw 'Connected to more than 1 SSO server, please specify a SSO server via -Server parameter' $connection2 | Should Be $connection1
(Compare-Object $global:DefaultSsoAdminServers $expected -IncludeEqual).Count | Should Be 2 $connection2.RefCount | Should Be 2
$expected.IsConnected | Should -Contain $true
Disconnect-SsoAdminServer
$connection2.IsConnected | Should -Contain $true
$connection2.RefCount | Should Be 1
} }
It 'Diconnect-SsoAdminServer does disconnect via pipeline if connected to more than 1 SSO server' { It 'Diconnect-SsoAdminServer does disconnect via pipeline if connected to more than 1 SSO server' {
# Arrange # Arrange
$expected += @(Connect-SsoAdminServer ` $connection1 = Connect-SsoAdminServer `
-Server $VcAddress ` -Server $VcAddress `
-User $User ` -User $User `
-Password $Password ` -Password $Password `
-SkipCertificateCheck) -SkipCertificateCheck
$expected += @(Connect-SsoAdminServer ` $connection2 = Connect-SsoAdminServer `
-Server $VcAddress ` -Server $VcAddress `
-User $User ` -User $User `
-Password $Password ` -Password $Password `
-SkipCertificateCheck) -SkipCertificateCheck
# Act # Act
$expected | Disconnect-SsoAdminServer $connection1, $connection2 | Disconnect-SsoAdminServer
# Assert # Assert
$global:DefaultSsoAdminServers.count | Should Be 0 $global:DefaultSsoAdminServers.Count | Should Be 0
$expected.IsConnected | Should -not -Contain $true $connection1.IsConnected | Should Be $false
$connection2.IsConnected | Should Be $false
} }
It 'Disconnects disconnected object' { It 'Disconnects disconnected object' {