Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlServerDsc: Change type for property Reasons #1858

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Set-SqlDscStartupParameter`
- Added class `StartupParameters` which can parse the startup parameters
of a manged computer service object.
- Added class `SqlReason` to be used as the type of the DSC property `Reasons`
for class-based resources.
- New GitHub issue templates for proposing new public commands, proposing
an enhancement to an existing command, or having a problem with an existing
command.
Expand Down Expand Up @@ -143,6 +145,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
output a warning to install any of the dependent modules.
- Add empty constructor to classes to be able to use Pester's new code
coverage method. See more information can be found in [pester/Pester#2306](https://github.com/pester/Pester/issues/2306).
- The type of the property `Reasons` was changed in the class-based resources.
This resolves a problem when using two DSC resource modules that was
using the same class-type for the property `Reasons`. Resolves the issues
[issue #1831](https://github.com/dsccommunity/SqlServerDsc/issues/1831),
[issue #1832](https://github.com/dsccommunity/SqlServerDsc/issues/1832),
and [issue #1833](https://github.com/dsccommunity/SqlServerDsc/issues/1833).
- `Install-SqlServerDsc`
- No longer throws an exception when parameter `AgtSvcAccount` is not specified.
- SqlAgReplica
Expand Down Expand Up @@ -230,6 +238,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
current state ([issue #1834](https://github.com/dsccommunity/SqlServerDsc/issues/1834)).
- `Set-TargetResource` was updated to correctly include or exclude a single
flag ([issue #1834](https://github.com/dsccommunity/SqlServerDsc/issues/1834)).
- SqlAudit
- Return the correct type for parameter `LogType` when calling method `Get()`.

## [16.0.0] - 2022-09-09

Expand Down
21 changes: 21 additions & 0 deletions source/Classes/001.SqlReason.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<#
.SYNOPSIS
The reason a property of a DSC resource is not in desired state.

.DESCRIPTION
A DSC resource can have a read-only property `Reasons` that the compliance
part (audit via Azure Policy) of Azure AutoManage Machine Configuration
uses. The property Reasons holds an array of SqlReason. Each SqlReason
explains why a property of a DSC resource is not in desired state.
#>

class SqlReason
{
[DscProperty()]
[System.String]
$Code

[DscProperty()]
[System.String]
$Phrase
}
2 changes: 1 addition & 1 deletion source/Classes/011.SqlResourceBase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SqlResourceBase : ResourceBase
$Credential

[DscProperty(NotConfigurable)]
[Reason[]]
[SqlReason[]]
$Reasons

# Passing the module's base directory to the base constructor.
Expand Down
2 changes: 1 addition & 1 deletion source/Classes/020.SqlAudit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class SqlAudit : SqlResourceBase

if ($auditObject.DestinationType -in @('ApplicationLog', 'SecurityLog'))
{
$currentState.LogType = $auditObject.DestinationType
$currentState.LogType = $auditObject.DestinationType.ToString()
}

if ($auditObject.FilePath)
Expand Down
80 changes: 80 additions & 0 deletions tests/Unit/Classes/SqlReason.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
param ()

BeforeDiscovery {
try
{
if (-not (Get-Module -Name 'DscResource.Test'))
{
# Assumes dependencies has been resolved, so if this module is not available, run 'noop' task.
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
{
# Redirect all streams to $null, except the error stream (stream 2)
& "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null
}

# If the dependencies has not been resolved, this will throw an error.
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
}
}
catch [System.IO.FileNotFoundException]
{
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.'
}
}

BeforeAll {
$script:dscModuleName = 'SqlServerDsc'

$env:SqlServerDscCI = $true

Import-Module -Name $script:dscModuleName

$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName
$PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName
$PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName
}

AfterAll {
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName')
$PSDefaultParameterValues.Remove('Mock:ModuleName')
$PSDefaultParameterValues.Remove('Should:ModuleName')

# Unload the module being tested so that it doesn't impact any other tests.
Get-Module -Name $script:dscModuleName -All | Remove-Module -Force

Remove-Item -Path 'env:SqlServerDscCI'
}

Describe 'SqlReason' -Tag 'SqlReason' {
Context 'When instantiating the class' {
It 'Should not throw an error' {
$script:mockSqlReasonInstance = InModuleScope -ScriptBlock {
[SqlReason]::new()
}
}

It 'Should be of the correct type' {
$mockSqlReasonInstance | Should -Not -BeNullOrEmpty
$mockSqlReasonInstance.GetType().Name | Should -Be 'SqlReason'
}
}

Context 'When setting an reading values' {
It 'Should be able to set value in instance' {
$script:mockSqlReasonInstance = InModuleScope -ScriptBlock {
$sqlReasonInstance = [SqlReason]::new()

$sqlReasonInstance.Code = 'SqlAudit:SqlAudit:Ensure'
$sqlReasonInstance.Phrase = 'The property Ensure should be "Present", but was "Absent"'

return $sqlReasonInstance
}
}

It 'Should be able read the values from instance' {
$mockSqlReasonInstance.Code | Should -Be 'SqlAudit:SqlAudit:Ensure'
$mockSqlReasonInstance.Phrase = 'The property Ensure should be "Present", but was "Absent"'
}
}
}