Skip to content

Commit

Permalink
SqlServerDsc: Make class-based resources derive from SqlResourceBase (
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju authored Aug 13, 2022
1 parent d741244 commit 87816b8
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 159 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions source/Classes/010.ResourceBase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions source/Classes/020.SqlAudit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
73 changes: 2 additions & 71 deletions source/Classes/020.SqlDatabasePermission.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -177,10 +148,6 @@ class SqlDatabasePermission : ResourceBase
[System.String]
$Name

[DscProperty()]
[System.String]
$ServerName = (Get-ComputerName)

[DscProperty()]
[DatabasePermission[]]
$Permission
Expand All @@ -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'
Expand All @@ -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.
Expand Down
80 changes: 2 additions & 78 deletions source/Classes/020.SqlPermission.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -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.
Expand Down
1 change: 0 additions & 1 deletion source/Public/Invoke-SqlDscQuery.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
(
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/Classes/ResourceBase.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class MyMockResource : ResourceBase
MyMockResource() : base ()
{
# These properties will not be enforced.
$this.notEnforcedProperties = @(
$this.ExcludeDscProperties = @(
'MyResourceKeyProperty1'
)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 87816b8

Please sign in to comment.