Skip to content

Commit

Permalink
SqlServerLogin: Set default database (#1485)
Browse files Browse the repository at this point in the history
- SqlServerLogin
  - Added `DefaultDatabase` parameter (issue #1474]).
  • Loading branch information
bozho authored Apr 11, 2020
1 parent e49d4d0 commit 65d9a13
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 172 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)

## [Unreleased]

### Added

- SqlServerLogin
- Added `DefaultDatabase` parameter ([issue #1474](https://github.com/dsccommunity/SqlServerDsc/issues/1474)).

### Changed

- SqlServerDsc
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,8 @@ No description.
policy. Only applies to SQL Logins. Default is $true.
* **`[Boolean]` Disabled** _(Write)_: Specifies if the login is disabled. Default
is $false.
* **`[String]` DefaultDatabase** _(Write)_: Default database name. If not specified,
default database is not changed.

#### Examples

Expand Down
53 changes: 45 additions & 8 deletions source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ function Get-TargetResource
)

$returnValue = @{
Ensure = $ensure
Name = $Name
LoginType = $login.LoginType
ServerName = $ServerName
InstanceName = $InstanceName
Disabled = $login.IsDisabled
Ensure = $ensure
Name = $Name
LoginType = $login.LoginType
ServerName = $ServerName
InstanceName = $InstanceName
Disabled = $login.IsDisabled
DefaultDatabase = $login.DefaultDatabase
}

if ($login.LoginType -eq 'SqlLogin')
Expand Down Expand Up @@ -113,6 +114,9 @@ function Get-TargetResource
.PARAMETER Disabled
Specifies if the login is disabled. Default is $false.
.PARAMETER DefaultDatabase
Specifies the default database for the login.
#>
function Set-TargetResource
{
Expand Down Expand Up @@ -167,7 +171,11 @@ function Set-TargetResource

[Parameter()]
[System.Boolean]
$Disabled
$Disabled,

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

$serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
Expand Down Expand Up @@ -232,6 +240,12 @@ function Set-TargetResource
$login.Enable()
}
}

if ( $PSBoundParameters.ContainsKey('DefaultDatabase') -and ($login.DefaultDatabase -ne $DefaultDatabase) )
{
$login.DefaultDatabase = $DefaultDatabase
Update-SQLServerLogin -Login $login
}
}
else
{
Expand Down Expand Up @@ -295,6 +309,13 @@ function Set-TargetResource

$login.Disable()
}

# set the default database if specified
if ( $PSBoundParameters.ContainsKey('DefaultDatabase') )
{
$login.DefaultDatabase = $DefaultDatabase
Update-SQLServerLogin -Login $login
}
}
}

Expand Down Expand Up @@ -345,6 +366,9 @@ function Set-TargetResource
.PARAMETER Disabled
Specifies if the login is disabled. Default is $false.
.PARAMETER DefaultDatabase
Specifies the default database for the login.
#>
function Test-TargetResource
{
Expand Down Expand Up @@ -399,7 +423,11 @@ function Test-TargetResource

[Parameter()]
[System.Boolean]
$Disabled
$Disabled,

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

Write-Verbose -Message (
Expand Down Expand Up @@ -455,6 +483,15 @@ function Test-TargetResource
$testPassed = $false
}

if ( $PSBoundParameters.ContainsKey('DefaultDatabase') -and ($loginInfo.DefaultDatabase -ne $DefaultDatabase) )
{
Write-Verbose -Message (
$script:localizedData.WrongDefaultDatabase -f $Name, $loginInfo.DefaultDatabase, $DefaultDatabase
)

$testPassed = $false
}

if ( $LoginType -eq 'SqlLogin' )
{
if ( $LoginPasswordExpirationEnabled -ne $loginInfo.LoginPasswordExpirationEnabled )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ class MSFT_SqlServerLogin : OMI_BaseResource
[Write, Description("Specifies if the login password is required to expire in accordance to the operating system security policy. Only applies to SQL Logins. Default is $true.")] Boolean LoginPasswordExpirationEnabled;
[Write, Description("Specifies if the login password is required to conform to the password policy specified in the system security policy. Only applies to SQL Logins. Default is $true.")] Boolean LoginPasswordPolicyEnforced;
[Write, Description("Specifies if the login is disabled. Default is $false.")] Boolean Disabled;
[Write, Description("Default database name.")] String DefaultDatabase;
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ConvertFrom-StringData @'
WrongLoginType = The login '{0}' has the login type '{1}', but expected it to have the login type '{2}'.
ExpectedDisabled = Expected the login '{0}' to be disabled, but it is enabled.
ExpectedEnabled = Expected the login '{0}' to be enabled, but it is disabled.
WrongDefaultDatabase = The login '{0}' has the default database '{1}', but expected it to have the default database '{2}'.
ExpectedLoginPasswordExpirationDisabled = The login '{0}' has the password expiration enabled, but expected it to be disabled.
ExpectedLoginPasswordExpirationEnabled = The login '{0}' has the password expiration disabled, but expected it to be enabled.
ExpectedLoginPasswordPolicyEnforcedDisabled = The login '{0}' has the password policy enforced enabled, but expected it to be disabled.
Expand Down
11 changes: 11 additions & 0 deletions source/Examples/Resources/SqlServerLogin/1-AddLogin.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ Configuration Example
Disabled = $true
}

SqlServerLogin 'Add_WindowsUser_Set_Default_Database'
{
Ensure = 'Present'
Name = 'CONTOSO\WindowsUser3'
LoginType = 'WindowsUser'
ServerName = 'TestServer.company.local'
InstanceName = 'DSC'
DefaultDatabase = 'contoso'
PsDscRunAsCredential = $SqlAdministratorCredential
}

SqlServerLogin 'Add_WindowsGroup'
{
Ensure = 'Present'
Expand Down
28 changes: 28 additions & 0 deletions tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ try
$resourceCurrentState.Ensure | Should -Be 'Present'
$resourceCurrentState.Name | Should -Be $ConfigurationData.AllNodes.DscUser2Name
$resourceCurrentState.LoginType | Should -Be $ConfigurationData.AllNodes.DscUser2Type
$resourceCurrentState.DefaultDatabase | Should -Be $ConfigurationData.AllNodes.DefaultDbName
$resourceCurrentState.Disabled | Should -Be $false
}

Expand Down Expand Up @@ -358,6 +359,33 @@ try
Test-DscConfiguration -Verbose | Should -Be 'True'
}
}

$configurationName = "$($script:dscResourceName)_CleanupDependencies_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}
}
}
}
finally
Expand Down
41 changes: 41 additions & 0 deletions tests/Integration/MSFT_SqlServerLogin.config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ else
ServerName = $env:COMPUTERNAME
InstanceName = 'DSCSQLTEST'

DefaultDbName = 'DefaultDb'

DscUser1Name = ('{0}\{1}' -f $env:COMPUTERNAME, 'DscUser1')
DscUser1Type = 'WindowsUser'

Expand Down Expand Up @@ -52,6 +54,7 @@ else
Configuration MSFT_SqlServerLogin_CreateDependencies_Config
{
Import-DscResource -ModuleName 'PSDscResources' -ModuleVersion '2.12.0.0'
Import-DscResource -ModuleName 'SqlServerDsc'

node $AllNodes.NodeName
{
Expand Down Expand Up @@ -99,6 +102,18 @@ Configuration MSFT_SqlServerLogin_CreateDependencies_Config
'[User]CreateDscUser2'
)
}

SqlDatabase 'DefaultDb_Test'
{
Ensure = 'Present'
ServerName = $Node.ServerName
InstanceName = $Node.InstanceName
Name = $Node.DefaultDbName

PsDscRunAsCredential = New-Object `
-TypeName System.Management.Automation.PSCredential `
-ArgumentList @($Node.Admin_UserName, (ConvertTo-SecureString -String $Node.Admin_Password -AsPlainText -Force))
}
}
}

Expand Down Expand Up @@ -143,6 +158,7 @@ Configuration MSFT_SqlServerLogin_AddLoginDscUser2_Config
Ensure = 'Present'
Name = $Node.DscUser2Name
LoginType = $Node.DscUser2Type
DefaultDatabase = $Node.DefaultDbName

ServerName = $Node.ServerName
InstanceName = $Node.InstanceName
Expand Down Expand Up @@ -264,3 +280,28 @@ Configuration MSFT_SqlServerLogin_RemoveLoginDscUser3_Config
}
}
}

<#
.SYNOPSIS
Clean up test resources so they are not interfering with
the other integration tests.
#>
Configuration MSFT_SqlServerLogin_CleanupDependencies_Config
{
Import-DscResource -ModuleName 'SqlServerDsc'

node $AllNodes.NodeName
{
SqlDatabase 'Remove_DefaultDb_Test'
{
Ensure = 'Absent'
ServerName = $Node.ServerName
InstanceName = $Node.InstanceName
Name = $Node.DefaultDbName

PsDscRunAsCredential = New-Object `
-TypeName System.Management.Automation.PSCredential `
-ArgumentList @($Node.Admin_UserName, (ConvertTo-SecureString -String $Node.Admin_Password -AsPlainText -Force))
}
}
}
Loading

0 comments on commit 65d9a13

Please sign in to comment.