From c26c06f2cbeeefe94d5a104d21656c8b2d6cc98e Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Fri, 19 Jan 2024 15:34:20 +0100 Subject: [PATCH] DatabasePermission/ServerPermission: Fix ToString() method (#1997) --- CHANGELOG.md | 4 ++++ source/Classes/002.DatabasePermission.ps1 | 27 +++++++++++++++++++++++ source/Classes/002.ServerPermission.ps1 | 27 ++++++++++++++++++++++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07e06d7d1..694a43484 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix unit test FailedRemoveAvailabilityGroupReplica - SqlAgentOperator - Integration test for changing e-mail address on an existing operator. +- `DatabasePermission` + - New method ToString() for making verbose output better. +- `ServerPermission` + - New method ToString() for making verbose output better. ### Changed diff --git a/source/Classes/002.DatabasePermission.ps1 b/source/Classes/002.DatabasePermission.ps1 index 09915de7c..7ae923417 100644 --- a/source/Classes/002.DatabasePermission.ps1 +++ b/source/Classes/002.DatabasePermission.ps1 @@ -24,6 +24,13 @@ evaluate during runtime so that no two states are enforcing the same permission. + This class cannot inherit a parent class. If it would have, then the + DSC resource (e.g. SqlDatabasePermission) that uses the complex type fail + with the error: + + "The 'Permission' property with type 'DatabasePermission' of DSC resource + class 'SqlDatabasePermission' is not supported." + The method Equals() returns $false if type is not the same on both sides of the comparison. There was a thought to throw an exception if the object being compared was of another type, but since there was issues with using @@ -32,6 +39,19 @@ the for example [ServerPermission] to the right side, then the left side array is filtered with the matching values on the right side. This is the normal behavior for other types. + + .EXAMPLE + [DatabasePermission] @{} + + Initializes a new instance of the DatabasePermission class without any + property values. + + .EXAMPLE + [DatabasePermission] @{ State = 'Grant'; Permission = @('Connect', 'Select') } + + Initializes a new instance of the DatabasePermission class with property + values. + #> class DatabasePermission : IComparable, System.IEquatable[Object] { @@ -245,4 +265,11 @@ class DatabasePermission : IComparable, System.IEquatable[Object] return $returnValue } + + [System.String] ToString() + { + $concatenatedPermission = ($this.Permission | Sort-Object) -join ', ' + + return ('{0}: {1}' -f $this.State, $concatenatedPermission) + } } diff --git a/source/Classes/002.ServerPermission.ps1 b/source/Classes/002.ServerPermission.ps1 index 56eb06d43..b6601feca 100644 --- a/source/Classes/002.ServerPermission.ps1 +++ b/source/Classes/002.ServerPermission.ps1 @@ -24,6 +24,13 @@ evaluate during runtime so that no two states are enforcing the same permission. + This class cannot inherit a parent class. If it would have, then the + DSC resource (e.g. SqlServerPermission) that uses the complex type fail + with the error: + + "The 'Permission' property with type 'ServerPermission' of DSC resource + class 'SqlServerPermission' is not supported." + The method Equals() returns $false if type is not the same on both sides of the comparison. There was a thought to throw an exception if the object being compared was of another type, but since there was issues with using @@ -32,6 +39,18 @@ the for example [ServerPermission] to the right side, then the left side array is filtered with the matching values on the right side. This is the normal behavior for other types. + + .EXAMPLE + [ServerPermission] @{} + + Initializes a new instance of the ServerPermission class without any + property values. + + .EXAMPLE + [ServerPermission] @{ State = 'Grant'; Permission = @('ConnectSql', 'ViewServerState') } + + Initializes a new instance of the ServerPermission class with property + values. #> class ServerPermission : IComparable, System.IEquatable[Object] { @@ -81,7 +100,6 @@ class ServerPermission : IComparable, System.IEquatable[Object] [System.String[]] $Permission - ServerPermission () { } @@ -184,4 +202,11 @@ class ServerPermission : IComparable, System.IEquatable[Object] return $returnValue } + + [System.String] ToString() + { + $concatenatedPermission = ($this.Permission | Sort-Object) -join ', ' + + return ('{0}: {1}' -f $this.State, $concatenatedPermission) + } }