From 87816b8a78b0b7c79bcece899e736248261b8026 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 13 Aug 2022 15:14:57 +0200 Subject: [PATCH] SqlServerDsc: Make class-based resources derive from `SqlResourceBase` (#1780) --- CHANGELOG.md | 8 ++ source/Classes/010.ResourceBase.ps1 | 4 +- source/Classes/020.SqlAudit.ps1 | 3 +- source/Classes/020.SqlDatabasePermission.ps1 | 73 +----------------- source/Classes/020.SqlPermission.ps1 | 80 +------------------- source/Public/Invoke-SqlDscQuery.ps1 | 1 - tests/Unit/Classes/ResourceBase.Tests.ps1 | 10 +-- 7 files changed, 20 insertions(+), 159 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02f3e3f9e..f8f562f31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -218,6 +218,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 The type `DatabasePermission` contains two properties; `State` and `Permission`. This fixes issue [issue #1555](https://github.com/dsccommunity/SqlServerDsc/issues/1555). - The resource was refactored into a class-based resource. + - Made the resource derive from `SqlResourceBase` to clean up the code + a bit. - SqlPermission - BREAKING CHANGE: The resource has been refactored. The parameters `Permissions` has been replaced by parameters `Permission`, @@ -229,6 +231,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [issue #1704](https://github.com/dsccommunity/SqlServerDsc/issues/1704), and [issue #752](https://github.com/dsccommunity/SqlServerDsc/issues/752). - The resource was refactored into a class-based resource. + - Made the resource derive from `SqlResourceBase` to clean up the code + a bit. +- Class `ResourceBase` + - Renamed the hidden property that derived classes can specify which properties + to not enforce when comparing desired state against current state. New name + of the hidden property is `ExcludeDscProperties`. ### Fixed diff --git a/source/Classes/010.ResourceBase.ps1 b/source/Classes/010.ResourceBase.ps1 index 642bd7b56..881c81fe9 100644 --- a/source/Classes/010.ResourceBase.ps1 +++ b/source/Classes/010.ResourceBase.ps1 @@ -17,7 +17,7 @@ class ResourceBase hidden [System.Collections.Hashtable] $localizedData = @{} # Property for derived class to set properties that should not be enforced. - hidden [System.String[]] $notEnforcedProperties = @() + hidden [System.String[]] $ExcludeDscProperties = @() # Default constructor ResourceBase() @@ -207,7 +207,7 @@ class ResourceBase CurrentValues = $currentState DesiredValues = $desiredState Properties = $desiredState.Keys - ExcludeProperties = ($excludeProperties + $this.notEnforcedProperties) | Select-Object -Unique + ExcludeProperties = ($excludeProperties + $this.ExcludeDscProperties) | Select-Object -Unique IncludeValue = $true # This is needed to sort complex types. SortArrayValues = $true diff --git a/source/Classes/020.SqlAudit.ps1 b/source/Classes/020.SqlAudit.ps1 index a0ac2bc44..ac9f6c786 100644 --- a/source/Classes/020.SqlAudit.ps1 +++ b/source/Classes/020.SqlAudit.ps1 @@ -190,9 +190,8 @@ class SqlAudit : SqlResourceBase SqlAudit () : base () { - # TODO:_Rename this to ExcludeDscProperties or ExcludeProperties # These properties will not be enforced. - $this.notEnforcedProperties = @( + $this.ExcludeDscProperties = @( 'ServerName' 'InstanceName' 'Name' diff --git a/source/Classes/020.SqlDatabasePermission.ps1 b/source/Classes/020.SqlDatabasePermission.ps1 index fa94dfbfa..5f8339500 100644 --- a/source/Classes/020.SqlDatabasePermission.ps1 +++ b/source/Classes/020.SqlDatabasePermission.ps1 @@ -57,20 +57,12 @@ + PSComputerName : localhost ``` - .PARAMETER InstanceName - The name of the _SQL Server_ instance to be configured. Default value is - `'MSSQLSERVER'`. - .PARAMETER DatabaseName The name of the database. .PARAMETER Name The name of the user that should be granted or denied the permission. - .PARAMETER ServerName - The host name of the _SQL Server_ to be configured. Default value is the - current computer name. - .PARAMETER Permission An array of database permissions to enforce. Any permission that is not part of the desired state will be revoked. @@ -104,15 +96,6 @@ This is an array of CIM instances of advanced type `DatabasePermission` from the namespace `root/Microsoft/Windows/DesiredStateConfiguration`. - .PARAMETER Credential - Specifies the credential to use to connect to the _SQL Server_ instance. - - If parameter **Credential'* is not provided then the resource instance is - run using the credential that runs the configuration. - - .PARAMETER Reasons - Returns the reason a property is not in desired state. - .EXAMPLE Invoke-DscResource -ModuleName SqlServerDsc -Name SqlDatabasePermission -Method Get -Property @{ ServerName = 'localhost' @@ -155,20 +138,8 @@ #> [DscResource(RunAsCredential = 'NotSupported')] -class SqlDatabasePermission : ResourceBase +class SqlDatabasePermission : SqlResourceBase { - <# - Property for holding the server connection object. - This should be an object of type [Microsoft.SqlServer.Management.Smo.Server] - but using that type fails the build process currently. - See issue https://github.com/dsccommunity/DscResource.DocGenerator/issues/121. - #> - hidden [System.Object] $sqlServerObject = $null - - [DscProperty(Key)] - [System.String] - $InstanceName - [DscProperty(Key)] [System.String] $DatabaseName @@ -177,10 +148,6 @@ class SqlDatabasePermission : ResourceBase [System.String] $Name - [DscProperty()] - [System.String] - $ServerName = (Get-ComputerName) - [DscProperty()] [DatabasePermission[]] $Permission @@ -193,18 +160,10 @@ class SqlDatabasePermission : ResourceBase [DatabasePermission[]] $PermissionToExclude - [DscProperty()] - [PSCredential] - $Credential - - [DscProperty(NotConfigurable)] - [Reason[]] - $Reasons - SqlDatabasePermission() : base () { # These properties will not be enforced. - $this.notEnforcedProperties = @( + $this.ExcludeDscProperties = @( 'ServerName' 'InstanceName' 'DatabaseName' @@ -231,34 +190,6 @@ class SqlDatabasePermission : ResourceBase ([ResourceBase] $this).Set() } - <# - Returns and reuses the server connection object. If the server connection - object does not exist a connection to the SQL Server instance will occur. - - This should return an object of type [Microsoft.SqlServer.Management.Smo.Server] - but using that type fails the build process currently. - See issue https://github.com/dsccommunity/DscResource.DocGenerator/issues/121. - #> - hidden [System.Object] GetServerObject() - { - if (-not $this.sqlServerObject) - { - $connectSqlDscDatabaseEngineParameters = @{ - ServerName = $this.ServerName - InstanceName = $this.InstanceName - } - - if ($this.Credential) - { - $connectSqlDscDatabaseEngineParameters.Credential = $this.Credential - } - - $this.sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters - } - - return $this.sqlServerObject - } - <# Base method Get() call this method to get the current state as a hashtable. The parameter properties will contain the key properties. diff --git a/source/Classes/020.SqlPermission.ps1 b/source/Classes/020.SqlPermission.ps1 index 696b7fcbe..fcbed3f22 100644 --- a/source/Classes/020.SqlPermission.ps1 +++ b/source/Classes/020.SqlPermission.ps1 @@ -59,17 +59,9 @@ + PSComputerName : localhost ``` - .PARAMETER InstanceName - The name of the _SQL Server_ instance to be configured. Default value is - `'MSSQLSERVER'`. - .PARAMETER Name The name of the user that should be granted or denied the permission. - .PARAMETER ServerName - The host name of the _SQL Server_ to be configured. Default value is the - current computer name. - .PARAMETER Permission An array of server permissions to enforce. Any permission that is not part of the desired state will be revoked. @@ -103,15 +95,6 @@ This is an array of CIM instances of advanced type `ServerPermission` from the namespace `root/Microsoft/Windows/DesiredStateConfiguration`. - .PARAMETER Credential - Specifies the credential to use to connect to the _SQL Server_ instance. - - If parameter **Credential'* is not provided then the resource instance is - run using the credential that runs the configuration. - - .PARAMETER Reasons - Returns the reason a property is not in desired state. - .EXAMPLE Invoke-DscResource -ModuleName SqlServerDsc -Name SqlPermission -Method Get -Property @{ ServerName = 'localhost' @@ -153,28 +136,12 @@ #> [DscResource(RunAsCredential = 'NotSupported')] -class SqlPermission : ResourceBase +class SqlPermission : SqlResourceBase { - <# - Property for holding the server connection object. - This should be an object of type [Microsoft.SqlServer.Management.Smo.Server] - but using that type fails the build process currently. - See issue https://github.com/dsccommunity/DscResource.DocGenerator/issues/121. - #> - hidden [System.Object] $sqlServerObject = $null - - [DscProperty(Key)] - [System.String] - $InstanceName - [DscProperty(Key)] [System.String] $Name - [DscProperty()] - [System.String] - $ServerName = (Get-ComputerName) - [DscProperty()] [ServerPermission[]] $Permission @@ -187,18 +154,10 @@ class SqlPermission : ResourceBase [ServerPermission[]] $PermissionToExclude - [DscProperty()] - [PSCredential] - $Credential - - [DscProperty(NotConfigurable)] - [Reason[]] - $Reasons - SqlPermission() : base () { # These properties will not be enforced. - $this.notEnforcedProperties = @( + $this.ExcludeDscProperties = @( 'ServerName' 'InstanceName' 'Name' @@ -224,41 +183,6 @@ class SqlPermission : ResourceBase ([ResourceBase] $this).Set() } - <# - TODO: This method can be moved to a parent class "SqlServerDscResource" that - instead inherits ResourceBase. Then this method does not need to be - duplicated. Make sure to create a localized strings file for the new - class. - The property 'sqlServerObject' should also be moved (but still be hidden). - #> - <# - Returns and reuses the server connection object. If the server connection - object does not exist a connection to the SQL Server instance will occur. - - This should return an object of type [Microsoft.SqlServer.Management.Smo.Server] - but using that type fails the build process currently. - See issue https://github.com/dsccommunity/DscResource.DocGenerator/issues/121. - #> - hidden [System.Object] GetServerObject() - { - if (-not $this.sqlServerObject) - { - $connectSqlDscDatabaseEngineParameters = @{ - ServerName = $this.ServerName - InstanceName = $this.InstanceName - } - - if ($this.Credential) - { - $connectSqlDscDatabaseEngineParameters.Credential = $this.Credential - } - - $this.sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters - } - - return $this.sqlServerObject - } - <# Base method Get() call this method to get the current state as a hashtable. The parameter properties will contain the key properties. diff --git a/source/Public/Invoke-SqlDscQuery.ps1 b/source/Public/Invoke-SqlDscQuery.ps1 index 4f687e043..221c18420 100644 --- a/source/Public/Invoke-SqlDscQuery.ps1 +++ b/source/Public/Invoke-SqlDscQuery.ps1 @@ -58,7 +58,6 @@ function Invoke-SqlDscQuery { [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')] [OutputType([System.Data.DataSet])] - # TODO: Should use ShouldProcess [CmdletBinding()] param ( diff --git a/tests/Unit/Classes/ResourceBase.Tests.ps1 b/tests/Unit/Classes/ResourceBase.Tests.ps1 index bbc6f5c18..c4319b0e2 100644 --- a/tests/Unit/Classes/ResourceBase.Tests.ps1 +++ b/tests/Unit/Classes/ResourceBase.Tests.ps1 @@ -141,7 +141,7 @@ class MyMockResource : ResourceBase MyMockResource() : base () { # These properties will not be enforced. - $this.notEnforcedProperties = @( + $this.ExcludeDscProperties = @( 'MyResourceKeyProperty1' ) } @@ -227,7 +227,7 @@ class MyMockResource : ResourceBase MyMockResource() : base () { # Test not to add the key property to the list of properties that are not enforced. - $this.notEnforcedProperties = @('MyResourceKeyProperty1') + $this.ExcludeDscProperties = @('MyResourceKeyProperty1') } [System.Collections.Hashtable] GetCurrentState([System.Collections.Hashtable] $properties) @@ -451,7 +451,7 @@ class MyMockResource : ResourceBase MyMockResource() : base () { # Test not to add the key property to the list of properties that are not enforced. - $this.notEnforcedProperties = @('MyResourceKeyProperty1') + $this.ExcludeDscProperties = @('MyResourceKeyProperty1') } [System.Collections.Hashtable] GetCurrentState([System.Collections.Hashtable] $properties) @@ -525,7 +525,7 @@ class MyMockResource : ResourceBase MyMockResource() : base () { # Test not to add the key property to the list of properties that are not enforced. - $this.notEnforcedProperties = @('MyResourceKeyProperty1') + $this.ExcludeDscProperties = @('MyResourceKeyProperty1') } [System.Collections.Hashtable] GetCurrentState([System.Collections.Hashtable] $properties) @@ -601,7 +601,7 @@ class MyMockResource : ResourceBase MyMockResource() : base () { # Test not to add the key property to the list of properties that are not enforced. - $this.notEnforcedProperties = @('MyResourceKeyProperty1') + $this.ExcludeDscProperties = @('MyResourceKeyProperty1') } [System.Collections.Hashtable] GetCurrentState([System.Collections.Hashtable] $properties)