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

MSFT_SqlAgentOperator: Added resource for SQL Server Agent Operators #1255

Merged
merged 54 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
1f5c372
add SqlAgentOperator resource
jpomfret Oct 19, 2018
98fbb7e
start of tests
jpomfret Oct 19, 2018
e10b1cb
code fixes. change/check email only when passed in
jpomfret Oct 24, 2018
c67c82f
working on unit tests
jpomfret Oct 24, 2018
e6e25ea
removed write-host used for debugging tests
jpomfret Nov 21, 2018
f0d971a
Cleaned up test file
jpomfret Nov 21, 2018
849778d
added note to Changelog
jpomfret Nov 21, 2018
e325090
add docs
jpomfret Nov 21, 2018
f0defb6
add examples
jpomfret Nov 21, 2018
f2a7375
changed mock object, still can't get drop to work
jpomfret Dec 21, 2018
2c53107
add localized messages
jpomfret Jan 4, 2019
ed5ce1a
add ensure param to readme
jpomfret Jan 5, 2019
e38b0fa
resolve requested readme changes
jpomfret Jan 7, 2019
40015b1
resolve requested changes
jpomfret Jan 7, 2019
9fcc018
making requested changes
jpomfret Jan 7, 2019
f91cce8
updated examples
jpomfret Jan 7, 2019
cb222b1
typo
jpomfret Jan 7, 2019
b64557c
working on tests
jpomfret Jan 7, 2019
9159bba
email shouldn't be required
jpomfret Jan 7, 2019
3a7a7a2
changed parameters to not go over 80 characters
jpomfret Jan 23, 2019
55d776d
fixed tests!!
jpomfret Jan 24, 2019
94f5dc6
address reviewable comments
jpomfret Jan 25, 2019
75e875e
adding integration tests
jpomfret Jan 25, 2019
1c2c1b1
add SqlAgentOperator resource
jpomfret Oct 19, 2018
f0eb3e9
start of tests
jpomfret Oct 19, 2018
8e3a63b
code fixes. change/check email only when passed in
jpomfret Oct 24, 2018
4409a17
working on unit tests
jpomfret Oct 24, 2018
778fe40
removed write-host used for debugging tests
jpomfret Nov 21, 2018
3f62c31
Cleaned up test file
jpomfret Nov 21, 2018
ce49417
added note to Changelog
jpomfret Nov 21, 2018
f4ed19e
add docs
jpomfret Nov 21, 2018
0cd492a
add examples
jpomfret Nov 21, 2018
a8a97bf
changed mock object, still can't get drop to work
jpomfret Dec 21, 2018
0fc4621
add localized messages
jpomfret Jan 4, 2019
9f262d0
add ensure param to readme
jpomfret Jan 5, 2019
656b9cf
resolve requested readme changes
jpomfret Jan 7, 2019
09324b9
resolve requested changes
jpomfret Jan 7, 2019
bb22dab
making requested changes
jpomfret Jan 7, 2019
d39b8f8
updated examples
jpomfret Jan 7, 2019
769343e
typo
jpomfret Jan 7, 2019
05f1b39
working on tests
jpomfret Jan 7, 2019
2252418
email shouldn't be required
jpomfret Jan 7, 2019
02c6dba
changed parameters to not go over 80 characters
jpomfret Jan 23, 2019
172d5c0
fixed tests!!
jpomfret Jan 24, 2019
c3d1bef
address reviewable comments
jpomfret Jan 25, 2019
ad40fca
adding integration tests
jpomfret Jan 25, 2019
3cafabf
Merge branch 'SqlAgentOperator' of https://github.com/jpomfret/SqlSer…
jpomfret Jan 25, 2019
ea45848
rename integration tests file, fix indents in readme
jpomfret Jan 27, 2019
67e68e1
hopefully the last few fixes...
jpomfret Jan 27, 2019
90dae65
fix for absent test
jpomfret Jan 27, 2019
0da2c20
fix for absent test
jpomfret Jan 27, 2019
ef32daa
Merge branch 'SqlAgentOperator' of https://github.com/jpomfret/SqlSer…
jpomfret Jan 27, 2019
51188c7
mocking the agent.operator
jpomfret Jan 29, 2019
3876e66
Merge branch 'dev' into SqlAgentOperator
johlju Jan 29, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
used for the creation of the endpoint
([issue #1251](https://github.com/PowerShell/SqlServerDsc/issues/1251)).
[Maxime Daniou (@mdaniou)](https://github.com/mdaniou)
- Added SqlAgentOperator Resource

## 12.2.0.0

Expand Down
332 changes: 332 additions & 0 deletions DSCResources/MSFT_SqlAgentOperator/MSFT_SqlAgentOperator.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,332 @@
Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) `
-ChildPath 'SqlServerDscHelper.psm1') `
-Force

Import-Module -Name (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) `
-ChildPath 'CommonResourceHelper.psm1')

$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_SqlAgentOperator'

<#
.SYNOPSIS
This function gets the SQL Agent Operator.

.PARAMETER Name
The name of the SQL Agent Operator.

.PARAMETER ServerName
The host name of the SQL Server to be configured. Default is $env:COMPUTERNAME.

.PARAMETER InstanceName
The name of the SQL instance to be configured.

#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name,

[Parameter()]
[System.String]
$ServerName = $env:COMPUTERNAME,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$InstanceName

)

$returnValue = @{
Name = $null
Ensure = 'Absent'
ServerName = $ServerName
InstanceName = $InstanceName
EmailAddress = $null
}

$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName

if ($sqlServerObject)
{
Write-Verbose -Message (
$script:localizedData.GetSqlAgents
)
# Check operator exists
$sqlOperatorObject = $sqlServerObject.JobServer.Operators | Where-Object {$_.Name -eq $Name}
if ($sqlOperatorObject)
{
Write-Verbose -Message (
$script:localizedData.SqlAgentPresent `
-f $Name
)
$returnValue['Ensure'] = 'Present'
$returnValue['Name'] = $sqlOperatorObject.Name
$returnValue['EmailAddress'] = $sqlOperatorObject.EmailAddress
}
else
{
Write-Verbose -Message (
$script:localizedData.SqlAgentAbsent `
-f $Name
)
}
}
else
{

$errorMessage = $script:localizedData.ConnectServerFailed -f $ServerName, $InstanceName
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}

return $returnValue
}

<#
.SYNOPSIS
This function sets the SQL Agent Operator.

.PARAMETER Ensure
Specifies if the SQL Agent Operator should be present or absent. Default is Present

.PARAMETER Name
The name of the SQL Agent Operator.

.PARAMETER ServerName
The host name of the SQL Server to be configured. Default is $env:COMPUTERNAME.

.PARAMETER InstanceName
The name of the SQL instance to be configured.

.PARAMETER EmailAddress
The email address to be used for the SQL Agent Operator.
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter()]
[ValidateSet('Present', 'Absent')]
[ValidateNotNullOrEmpty()]
[System.String]
$Ensure = 'Present',

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name,

[Parameter()]
[System.String]
$ServerName = $env:COMPUTERNAME,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$InstanceName,

[Parameter()]
[System.String]
$EmailAddress
)

$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName

if ($sqlServerObject)
{
switch ($Ensure)
{
'Present'
{
$sqlOperatorObject = $sqlServerObject.JobServer.Operators | Where-Object {$_.Name -eq $Name}

if ($sqlOperatorObject)
{
if ($PSBoundParameters.ContainsKey('EmailAddress'))
{
try
{
Write-Verbose -Message (
$script:localizedData.UpdateEmailAddress `
-f $EmailAddress, $Name
)
$sqlOperatorObject.EmailAddress = $EmailAddress
$sqlOperatorObject.Alter()
}
catch
{
$errorMessage = $script:localizedData.UpdateOperatorSetError -f $ServerName, $InstanceName, $Name, $EmailAddress
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}
}
else
{
try
{
$sqlOperatorObjectToCreate = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.Operator -ArgumentList $sqlServerObject.JobServer, $Name

if ($sqlOperatorObjectToCreate)
{
Write-Verbose -Message (
$script:localizedData.AddSqlAgentOperator `
-f $Name
)
if ($PSBoundParameters.ContainsKey('EmailAddress'))
{
Write-Verbose -Message (
$script:localizedData.UpdateEmailAddress `
-f $EmailAddress, $Name
)
$sqlOperatorObjectToCreate.EmailAddress = $EmailAddress
}
$sqlOperatorObjectToCreate.Create()
}
}
catch
{
$errorMessage = $script:localizedData.CreateOperatorSetError -f $Name, $ServerName, $InstanceName
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}
}

'Absent'
{
try
{
$sqlOperatorObjectToDrop = $sqlServerObject.JobServer.Operators | Where-Object {$_.Name -eq $Name}
if ($sqlOperatorObjectToDrop)
{
Write-Verbose -Message (
$script:localizedData.DeleteSqlAgentOperator `
-f $Name
)
$sqlOperatorObjectToDrop.Drop()
}
}
catch
{
$errorMessage = $script:localizedData.DropOperatorSetError -f $Name, $ServerName, $InstanceName
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}
}
}
else
{
$errorMessage = $script:localizedData.ConnectServerFailed -f $ServerName, $InstanceName
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}

<#
.SYNOPSIS
This function tests the SQL Agent Operator.

.PARAMETER Ensure
Specifies if the SQL Agent Operator should be present or absent. Default is Present

.PARAMETER Name
The name of the SQL Agent Operator.

.PARAMETER ServerName
The host name of the SQL Server to be configured. Default is $env:COMPUTERNAME.

.PARAMETER InstanceName
The name of the SQL instance to be configured.

.PARAMETER EmailAddress
The email address to be used for the SQL Agent Operator.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter()]
[ValidateSet('Present', 'Absent')]
[ValidateNotNullOrEmpty()]
[System.String]
$Ensure = 'Present',

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name,

[Parameter()]
[System.String]
$ServerName = $env:COMPUTERNAME,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$InstanceName,

[Parameter()]
[System.String]
$EmailAddress
)

Write-Verbose -Message (
$script:localizedData.TestSqlAgentOperator `
-f $Name
)

$getTargetResourceParameters = @{
Name = $Name
ServerName = $ServerName
InstanceName = $InstanceName
}

$getTargetResourceResult = Get-TargetResource @getTargetResourceParameters
$isOperatorInDesiredState = $true

switch ($Ensure)
{
'Absent'
{
if ($getTargetResourceResult.Ensure -ne 'Absent')
{
Write-Verbose -Message (
$script:localizedData.SqlAgentOperatorExistsButShouldNot `
-f $Name
)
$isOperatorInDesiredState = $false
}
}

'Present'
{
if ($getTargetResourceResult.EmailAddress -ne $EmailAddress -and $PSBoundParameters.ContainsKey('EmailAddress'))
{
Write-Verbose -Message (
$script:localizedData.SqlAgentOperatorExistsButEmailWrong `
-f $Name, $getTargetResourceResult.EmailAddress, $EmailAddress
)
$isOperatorInDesiredState = $false
}
elseif ($getTargetResourceResult.Ensure -ne 'Present')
{
Write-Verbose -Message (
$script:localizedData.SqlAgentOperatorDoesNotExistButShould `
-f $Name
)
$isOperatorInDesiredState = $false
}
}
}
$isOperatorInDesiredState
}

Export-ModuleMember -Function *-TargetResource
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[ClassVersion("1.0.0.0"), FriendlyName("SqlAgentOperator")]
class MSFT_SqlAgentOperator : OMI_BaseResource
{
[Key, Description("The name of the SQL Agent Operator.")] String Name;
[Write, Description("Specifies if the SQL Agent Operator should be present or absent. Default is Present."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
[Write, Description("The host name of the SQL Server to be configured. Default is $env:COMPUTERNAME.")] String ServerName;
[Key, Description("The name of the SQL instance to be configured.")] String InstanceName;
[Write, Description("The email address to be used for the SQL Agent Operator.")] String EmailAddress;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Localized resources for MSFT_SqlServerAgentOperator

ConvertFrom-StringData @'
GetSqlAgents = Getting SQL Agent Operators.
SqlAgentPresent = SQL Agent Operator '{0}' is present.
SqlAgentAbsent = SQL Agent Operator '{0}' is absent.
UpdateOperatorSetError = Unable to update the email address for '{2}' to '{3}' on {0}\\{1}.
AddSqlAgentOperator = Adding SQL Agent Operator '{0}'.
UpdateEmailAddress = Updating email address to '{0}' for SQL Agent Operator '{1}'.
CreateOperatorSetError = Unable to create the SQL Agent Operator '{0}' on {1}\\{2}.
DeleteSqlAgentOperator = Deleting SQL Agent Operator '{0}'.
DropOperatorSetError = Unable to drop the SQL Agent Operator '{0}' on {1}\\{2}.
TestSqlAgentOperator = Checking if SQL Agent Operator '{0}' is present or absent.
SqlAgentOperatorExistsButShouldNot = SQL Agent Operator exists but ensure is set to Absent. The SQL Agent Operator '{0}' should be deleted.
SqlAgentOperatorDoesNotExistButShould = SQL Agent Operator does not exist but Ensure is set to Present. The SQL Agent Operator '{0}' should be created.
SqlAgentOperatorExistsButEmailWrong = SQL Agent Operator '{0}' exists but has the wrong email address. Email address is currently '{1}' and should be updated to '{2}'.
ConnectServerFailed = Unable to connect to {0}\\{1}.
'@
1 change: 1 addition & 0 deletions Examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ These are the links to the examples for each individual resource.

- [SqlAG](Resources/SqlAG)
- [SqlAGDatabase](Resources/SqlAGDatabase)
- [SqlAgentOperator](Resources/SqlAgentOperator)
- [SqlAGListener](Resources/SqlAGListener)
- [SqlAGReplica](Resources/SqlAGReplica)
- [SqlAlias](Resources/SqlAlias)
Expand Down
21 changes: 21 additions & 0 deletions Examples/Resources/SqlAgentOperator/1-AddOperator.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<#
.EXAMPLE
This example shows how to ensure that the SQL Agent Operator
DbaTeam exists with the correct email address.
#>

Configuration Example
{

Import-DscResource -ModuleName SqlServerDsc

node localhost {
SqlAgentOperator Add_DbaTeam {
Ensure = 'Present'
Name = 'DbaTeam'
ServerName = 'TestServer'
InstanceName = 'MSSQLServer'
EmailAddress = '[email protected]'
}
}
}
Loading