-
Notifications
You must be signed in to change notification settings - Fork 226
SqlAgentAlert: Refactor to class-bases resource
#2150
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
Merged
johlju
merged 37 commits into
dsccommunity:main
from
johlju:f/refactor-sqlagentalert-resource
Aug 29, 2025
Merged
Changes from 13 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
4d5b237
`SqlAgentAlert`: Refactor to class-bases resource
johlju 4897a08
Fix typo in DSC class resource guidelines for ExcludeDscProperties co…
johlju 11a2f00
Remove unit tests for DSC_SqlAgentAlert resource
johlju e62febd
Update SqlServerDsc guidelines to clarify resource inheritance and pr…
johlju 4dfbc14
Update ExcludeDscProperties in SqlAgentAlert constructor to include I…
johlju 642a742
Remove unnecessary blank lines from DSC_SqlAgentAlert_Remove_Config c…
johlju d74eedd
Update integration test to verify resource name against Configuration…
johlju 9398b1d
Update AssertProperties method to validate Severity and MessageId par…
johlju 9a5cba3
Fix AssertProperties method to validate Ensure property from input pa…
johlju 017f3a8
Enhance SqlAgentAlert to restrict Severity and MessageId when Ensure …
johlju 0afbd30
Clarify guideline on nullable property types in DSC class-based resou…
johlju c4050a5
Clarify guidelines for nullable property types in DSC class-based res…
johlju 207ce9c
Add support for MessageId in SqlAgentAlert configuration and tests
johlju 5856af5
Update module name placeholders in integration, localization, and uni…
johlju 1c74791
Refactor AI instructions header to generalize project guidance
johlju 670f853
Clarify hashtable formatting guideline for multi-line properties
johlju 6d1226d
Add "RAISERROR" to cSpell words list in settings.json
johlju d75a5da
Enhance SQL Agent alert configurations to utilize MessageId with RAIS…
johlju 2d0a7da
Add optional encryption parameter to SQL Agent alert configurations
johlju 8a08ce4
Enhance PowerShell guidelines with output stream usage and backtick l…
johlju c4263f1
Correct capitalization in PowerShell guidelines for Write-Verbose usage
johlju 54a02bf
Fix variable name in BeforeAll block for consistency in integration t…
johlju b3a7de8
Fix variable name in BeforeAll block for consistency in unit test gui…
johlju 8fceb2a
Refactor naming section in Pester guidelines for clarity and consistency
johlju 809953f
Enhance SqlAgentAlert validation logic in AssertProperties method and…
johlju 05fe912
Update SqlAgentAlert documentation and adjust validation ranges for S…
johlju cd6e952
Update SqlAgentAlert parameter documentation to correct valid ranges …
johlju b60438e
Fix constructor definition in DSC class guidelines to include $PSScri…
johlju f898566
Update SqlServerDsc guidelines to clarify resource inheritance and co…
johlju 51175ed
Refine documentation for SqlAgentAlert to improve clarity on alert ty…
johlju 3354094
Refactor SqlAgentAlert to streamline absence validation for Severity …
johlju 5c97552
Fix formatting in changelog guidelines for issue references
johlju 0a2fba5
Add assertion for bound parameters in SqlAgentAlert Set method
johlju a245ba2
Add tests for Modify() method in SqlAgentAlert to validate alert updates
johlju 250b7f0
Update build and test workflow instructions for clarity and consistency
johlju 87fec7e
Refactor test descriptions in SqlAgentAlert.Tests.ps1 for clarity and…
johlju 2f86af9
Refactor test assertions in SqlAgentAlert.Tests.ps1 to use assignment…
johlju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,304 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| The `SqlAgentAlert` DSC resource is used to create, modify, or remove | ||
| _SQL Server Agent_ alerts. | ||
|
|
||
| .DESCRIPTION | ||
| The `SqlAgentAlert` DSC resource is used to create, modify, or remove | ||
| _SQL Server Agent_ alerts. | ||
|
|
||
| The built-in parameter **PSDscRunAsCredential** can be used to run the resource | ||
| as another user. The resource will then authenticate to the _SQL Server_ | ||
| instance as that user. It also possible to instead use impersonation by the | ||
| parameter **Credential**. | ||
|
|
||
| ## Requirements | ||
|
|
||
| * Target machine must be running Windows Server 2012 or later. | ||
| * Target machine must be running SQL Server Database Engine 2012 or later. | ||
| * Target machine must have access to the SQLPS PowerShell module or the SqlServer | ||
| PowerShell module. | ||
|
|
||
| ## Known issues | ||
|
|
||
| All issues are not listed here, see [here for all open issues](https://github.com/dsccommunity/SqlServerDsc/issues?q=is%3Aissue+is%3Aopen+in%3Atitle+SqlAgentAlert). | ||
|
|
||
| ### Property **Reasons** does not work with **PSDscRunAsCredential** | ||
|
|
||
| When using the built-in parameter **PSDscRunAsCredential** the read-only | ||
| property **Reasons** will return empty values for the properties **Code** | ||
| and **Phrase**. The built-in property **PSDscRunAsCredential** does not work | ||
| together with class-based resources that using advanced type like the parameter | ||
| **Reasons** have. | ||
|
|
||
| ### Using **Credential** property | ||
|
|
||
| SQL Authentication and Group Managed Service Accounts is not supported as | ||
| impersonation credentials. Currently only Windows Integrated Security is | ||
| supported to use as credentials. | ||
|
|
||
| For Windows Authentication the username must either be provided with the User | ||
| Principal Name (UPN), e.g. `username@domain.local` or if using non-domain | ||
| (for example a local Windows Server account) account the username must be | ||
| provided without the NetBIOS name, e.g. `username`. Using the NetBIOS name, e.g | ||
| using the format `DOMAIN\username` will not work. | ||
|
|
||
| See more information in [Credential Overview](https://github.com/dsccommunity/SqlServerDsc/wiki/CredentialOverview). | ||
|
|
||
| .PARAMETER Name | ||
| The name of the _SQL Server Agent_ alert. | ||
|
|
||
| .PARAMETER Ensure | ||
| Specifies if the _SQL Server Agent_ alert should be present or absent. | ||
| Default value is `'Present'`. | ||
|
|
||
| .PARAMETER Severity | ||
| The severity of the _SQL Server Agent_ alert. Valid range is 0 to 25. | ||
| Cannot be used together with **MessageId**. | ||
|
|
||
| .PARAMETER MessageId | ||
| The message id of the _SQL Server Agent_ alert. Valid range is 0 to 2147483647. | ||
| Cannot be used together with **Severity**. | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .EXAMPLE | ||
| Invoke-DscResource -ModuleName SqlServerDsc -Name SqlAgentAlert -Method Get -Property @{ | ||
| InstanceName = 'MSSQLSERVER' | ||
| Name = 'Alert1' | ||
| } | ||
|
|
||
| This example shows how to get the current state of the _SQL Server Agent_ | ||
| alert named **Alert1**. | ||
|
|
||
| .EXAMPLE | ||
| Invoke-DscResource -ModuleName SqlServerDsc -Name SqlAgentAlert -Method Test -Property @{ | ||
| InstanceName = 'MSSQLSERVER' | ||
| Name = 'Alert1' | ||
| Ensure = 'Present' | ||
| Severity = 16 | ||
| } | ||
|
|
||
| This example shows how to test if the _SQL Server Agent_ alert named | ||
| **Alert1** is in the desired state. | ||
|
|
||
| .EXAMPLE | ||
| Invoke-DscResource -ModuleName SqlServerDsc -Name SqlAgentAlert -Method Set -Property @{ | ||
| InstanceName = 'MSSQLSERVER' | ||
| Name = 'Alert1' | ||
| Ensure = 'Present' | ||
| Severity = 16 | ||
| } | ||
|
|
||
| This example shows how to set the desired state for the _SQL Server Agent_ | ||
| alert named **Alert1** with severity level 16. | ||
|
|
||
| .EXAMPLE | ||
| Invoke-DscResource -ModuleName SqlServerDsc -Name SqlAgentAlert -Method Set -Property @{ | ||
| InstanceName = 'MSSQLSERVER' | ||
| Name = 'Alert1' | ||
| Ensure = 'Present' | ||
| MessageId = 50001 | ||
| } | ||
|
|
||
| This example shows how to set the desired state for the _SQL Server Agent_ | ||
| alert named **Alert1** with message ID 50001. | ||
|
|
||
| .EXAMPLE | ||
| Invoke-DscResource -ModuleName SqlServerDsc -Name SqlAgentAlert -Method Set -Property @{ | ||
| InstanceName = 'MSSQLSERVER' | ||
| Name = 'Alert1' | ||
| Ensure = 'Absent' | ||
| } | ||
|
|
||
| This example shows how to remove the _SQL Server Agent_ alert named | ||
| **Alert1**. | ||
| #> | ||
| [DscResource(RunAsCredential = 'Optional')] | ||
| class SqlAgentAlert : SqlResourceBase | ||
| { | ||
| [DscProperty(Key)] | ||
| [System.String] | ||
| $Name | ||
|
|
||
| [DscProperty()] | ||
| [ValidateSet('Present', 'Absent')] | ||
| [System.String] | ||
| $Ensure = 'Present' | ||
|
|
||
| [DscProperty()] | ||
| [ValidateRange(0, 25)] | ||
| [Nullable[System.Int32]] | ||
| $Severity | ||
|
|
||
| [DscProperty()] | ||
| [ValidateRange(0, 2147483647)] | ||
| [Nullable[System.Int32]] | ||
| $MessageId | ||
|
|
||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| SqlAgentAlert () : base () | ||
| { | ||
| # Property names that cannot be enforced | ||
| $this.ExcludeDscProperties = @( | ||
| 'InstanceName', | ||
| 'ServerName', | ||
| 'Credential' | ||
| 'Name' | ||
| ) | ||
| } | ||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| [SqlAgentAlert] Get() | ||
| { | ||
| # Call base implementation to get current state | ||
| $currentState = ([ResourceBase] $this).Get() | ||
|
|
||
| return $currentState | ||
| } | ||
|
|
||
| [System.Boolean] Test() | ||
| { | ||
| # Call base implementation to test current state | ||
| $inDesiredState = ([ResourceBase] $this).Test() | ||
|
|
||
| return $inDesiredState | ||
| } | ||
|
|
||
| [void] Set() | ||
| { | ||
| # Call base implementation to set desired state | ||
| ([ResourceBase] $this).Set() | ||
| } | ||
|
|
||
| hidden [void] AssertProperties([System.Collections.Hashtable] $properties) | ||
| { | ||
| # TODO: Waiting for issue: https://github.com/dsccommunity/DscResource.Common/issues/160 | ||
| if ($properties.Ensure -eq 'Present') | ||
| { | ||
| # Validate that at least one of Severity or MessageId is specified | ||
| # TODO: Waiting for issue: https://github.com/dsccommunity/DscResource.Common/issues/161 | ||
| #Assert-BoundParameter -BoundParameterList $properties -AtLeastOneList @('Severity', 'MessageId') | ||
|
|
||
| # Validate that both Severity and MessageId are not specified | ||
| Assert-BoundParameter -BoundParameterList $properties -MutuallyExclusiveList1 @('Severity') -MutuallyExclusiveList2 @('MessageId') | ||
| } | ||
| else | ||
| { | ||
| # When Ensure is 'Absent', Severity and MessageId must not be set | ||
| if ($properties.ContainsKey('Severity') -or $properties.ContainsKey('MessageId')) | ||
| { | ||
| $errorMessage = $this.localizedData.SqlAgentAlert_SeverityOrMessageIdNotAllowedWhenAbsent | ||
|
|
||
| New-InvalidArgumentException -ArgumentName 'Severity, MessageId' -Message $errorMessage | ||
| } | ||
| } | ||
| } | ||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| hidden [System.Collections.Hashtable] GetCurrentState([System.Collections.Hashtable] $properties) | ||
| { | ||
| $serverObject = $this.GetServerObject() | ||
|
|
||
| Write-Verbose -Message ($this.localizedData.SqlAgentAlert_GettingCurrentState -f $this.Name, $this.InstanceName) | ||
|
|
||
| $currentState = @{ | ||
| InstanceName = $this.InstanceName | ||
| ServerName = $this.ServerName | ||
| Name = $this.Name | ||
| Ensure = 'Absent' | ||
| } | ||
|
|
||
| $alertObject = $serverObject | Get-SqlDscAgentAlert -Name $this.Name -ErrorAction 'SilentlyContinue' | ||
|
|
||
| if ($alertObject) | ||
| { | ||
| Write-Verbose -Message ($this.localizedData.SqlAgentAlert_AlertExists -f $this.Name) | ||
|
|
||
| $currentState.Ensure = 'Present' | ||
|
|
||
| # Get the current severity and message ID | ||
| if ($alertObject.Severity -gt 0) | ||
| { | ||
| $currentState.Severity = $alertObject.Severity | ||
| } | ||
|
|
||
| if ($alertObject.MessageId -gt 0) | ||
| { | ||
| $currentState.MessageId = $alertObject.MessageId | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Write-Verbose -Message ($this.localizedData.SqlAgentAlert_AlertDoesNotExist -f $this.Name) | ||
| } | ||
|
|
||
| return $currentState | ||
| } | ||
|
|
||
| hidden [void] Modify([System.Collections.Hashtable] $properties) | ||
| { | ||
| $serverObject = $this.GetServerObject() | ||
|
|
||
| if ($this.Ensure -eq 'Present') | ||
| { | ||
| $alertObject = $serverObject | Get-SqlDscAgentAlert -Name $this.Name -ErrorAction 'SilentlyContinue' | ||
|
|
||
| if ($null -eq $alertObject) | ||
| { | ||
| Write-Verbose -Message ($this.localizedData.SqlAgentAlert_CreatingAlert -f $this.Name) | ||
|
|
||
| $newAlertParameters = @{ | ||
| ServerObject = $serverObject | ||
| Name = $this.Name | ||
| ErrorAction = 'Stop' | ||
| } | ||
|
|
||
| if ($properties.ContainsKey('Severity')) | ||
| { | ||
| $newAlertParameters.Severity = $properties.Severity | ||
| } | ||
|
|
||
| if ($properties.ContainsKey('MessageId')) | ||
| { | ||
| $newAlertParameters.MessageId = $properties.MessageId | ||
| } | ||
|
|
||
| $null = New-SqlDscAgentAlert @newAlertParameters | ||
| } | ||
| else | ||
| { | ||
| Write-Verbose -Message ($this.localizedData.SqlAgentAlert_UpdatingAlert -f $this.Name) | ||
|
|
||
| $setAlertParameters = @{ | ||
| AlertObject = $alertObject | ||
| ErrorAction = 'Stop' | ||
| } | ||
|
|
||
| $needsUpdate = $false | ||
|
|
||
| if ($properties.ContainsKey('Severity') -and $alertObject.Severity -ne $properties.Severity) | ||
| { | ||
| $setAlertParameters.Severity = $properties.Severity | ||
| $needsUpdate = $true | ||
| } | ||
|
|
||
| if ($properties.ContainsKey('MessageId') -and $alertObject.MessageId -ne $properties.MessageId) | ||
| { | ||
| $setAlertParameters.MessageId = $properties.MessageId | ||
| $needsUpdate = $true | ||
| } | ||
|
|
||
| if ($needsUpdate) | ||
| { | ||
| $null = Set-SqlDscAgentAlert @setAlertParameters | ||
| } | ||
| else | ||
| { | ||
| Write-Verbose -Message ($this.localizedData.SqlAgentAlert_NoChangesNeeded -f $this.Name) | ||
| } | ||
| } | ||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| else # Ensure = 'Absent' | ||
| { | ||
| Write-Verbose -Message ($this.localizedData.SqlAgentAlert_RemovingAlert -f $this.Name) | ||
|
|
||
| $null = $serverObject | Remove-SqlDscAgentAlert -Name $this.Name -Force -ErrorAction 'Stop' | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.