diff --git a/CHANGELOG.md b/CHANGELOG.md index 2acf66b1f..02e9a531e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -95,6 +95,9 @@ - Changes to SqlServerEndpoint - BREAKING CHANGE: Parameters SQLServer and SQLInstanceName has been renamed to ServerName and InstanceName respectivly ([issue #308](https://github.com/PowerShell/xSQLServer/issues/)). +- Changes to SqlServerOwner + - BREAKING CHANGE: Parameters SQLServer and SQLInstanceName has been renamed + to ServerName and InstanceName respectivly ([issue #308](https://github.com/PowerShell/xSQLServer/issues/)). ## 8.2.0.0 diff --git a/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.psm1 b/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.psm1 index a947278d4..fd0c03a1d 100644 --- a/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.psm1 +++ b/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.psm1 @@ -11,10 +11,10 @@ Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Pare .PARAMETER Name The name of the login that will become a owner of the desired sql database. - .PARAMETER SQLServer + .PARAMETER ServerName The host name of the SQL Server to be configured. - .PARAMETER SQLInstanceName + .PARAMETER InstanceName The name of the SQL instance to be configured. #> function Get-TargetResource @@ -36,16 +36,16 @@ function Get-TargetResource [Parameter()] [ValidateNotNullOrEmpty()] [System.String] - $SQLServer = $env:COMPUTERNAME, + $ServerName = $env:COMPUTERNAME, [Parameter()] [ValidateNotNullOrEmpty()] [System.String] - $SQLInstanceName = 'MSSQLSERVER' + $InstanceName = 'MSSQLSERVER' ) Write-Verbose -Message "Getting owner of database $Database" - $sqlServerObject = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName + $sqlServerObject = Connect-SQL -SQLServer $ServerName -SQLInstanceName $InstanceName if ($sqlServerObject) { @@ -53,7 +53,7 @@ function Get-TargetResource if ( -not ($sqlDatabaseObject = $sqlServerObject.Databases[$Database]) ) { throw New-TerminatingError -ErrorType NoDatabase ` - -FormatArgs @($Database, $SQLServer, $SQLInstanceName) ` + -FormatArgs @($Database, $ServerName, $InstanceName) ` -ErrorCategory ObjectNotFound } @@ -65,16 +65,16 @@ function Get-TargetResource catch { throw New-TerminatingError -ErrorType FailedToGetOwnerDatabase ` - -FormatArgs @($Database, $SQLServer, $SQLInstanceName) ` + -FormatArgs @($Database, $ServerName, $InstanceName) ` -ErrorCategory InvalidOperation } } $returnValue = @{ - Database = $Database - Name = $sqlDatabaseOwner - SQLServer = $SQLServer - SQLInstanceName = $SQLInstanceName + Database = $Database + Name = $sqlDatabaseOwner + ServerName = $ServerName + InstanceName = $InstanceName } $returnValue @@ -90,10 +90,10 @@ function Get-TargetResource .PARAMETER Name The name of the login that will become a owner of the desired sql database. - .PARAMETER SQLServer + .PARAMETER ServerName The host name of the SQL Server to be configured. - .PARAMETER SQLInstanceName + .PARAMETER InstanceName The name of the SQL instance to be configured. #> function Set-TargetResource @@ -114,29 +114,29 @@ function Set-TargetResource [Parameter()] [ValidateNotNullOrEmpty()] [System.String] - $SQLServer = $env:COMPUTERNAME, + $ServerName = $env:COMPUTERNAME, [Parameter()] [ValidateNotNullOrEmpty()] [System.String] - $SQLInstanceName = 'MSSQLSERVER' + $InstanceName = 'MSSQLSERVER' ) Write-Verbose -Message "Setting owner $Name of database $Database" - $sqlServerObject = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName + $sqlServerObject = Connect-SQL -SQLServer $ServerName -SQLInstanceName $InstanceName if ($sqlServerObject) { # Check database exists if ( -not ($sqlDatabaseObject = $sqlServerObject.Databases[$Database]) ) { - throw New-TerminatingError -ErrorType NoDatabase -FormatArgs @($Database, $SQLServer, $SQLInstanceName) -ErrorCategory ObjectNotFound + throw New-TerminatingError -ErrorType NoDatabase -FormatArgs @($Database, $ServerName, $InstanceName) -ErrorCategory ObjectNotFound } # Check login exists if ( -not ($sqlServerObject.Logins[$Name]) ) { - throw New-TerminatingError -ErrorType LoginNotFound -FormatArgs @($Name, $SQLServer, $SQLInstanceName) -ErrorCategory ObjectNotFound + throw New-TerminatingError -ErrorType LoginNotFound -FormatArgs @($Name, $ServerName, $InstanceName) -ErrorCategory ObjectNotFound } try @@ -147,7 +147,7 @@ function Set-TargetResource catch { throw New-TerminatingError -ErrorType FailedToSetOwnerDatabase ` - -FormatArgs @($Name, $Database, $SQLServer, $SQLInstanceName) ` + -FormatArgs @($Name, $Database, $ServerName, $InstanceName) ` -ErrorCategory InvalidOperation ` -InnerException $_.Exception } @@ -164,10 +164,10 @@ function Set-TargetResource .PARAMETER Name The name of the login that will become a owner of the desired sql database. - .PARAMETER SQLServer + .PARAMETER ServerName The host name of the SQL Server to be configured. - .PARAMETER SQLInstanceName + .PARAMETER InstanceName The name of the SQL instance to be configured. #> function Test-TargetResource @@ -189,12 +189,12 @@ function Test-TargetResource [Parameter()] [ValidateNotNullOrEmpty()] [System.String] - $SQLServer = $env:COMPUTERNAME, + $ServerName = $env:COMPUTERNAME, [Parameter()] [ValidateNotNullOrEmpty()] [System.String] - $SQLInstanceName = 'MSSQLSERVER' + $InstanceName = 'MSSQLSERVER' ) Write-Verbose -Message "Testing owner $Name of database $Database" diff --git a/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.schema.mof b/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.schema.mof index 1d970c749..c488b2e42 100644 --- a/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.schema.mof +++ b/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.schema.mof @@ -3,6 +3,6 @@ class MSFT_SqlDatabaseOwner : OMI_BaseResource { [Key, Description("The name of database to be configured.")] String Database; [Required, Description("The name of the login that will become a owner of the desired sql database.")] String Name; - [Write, Description("The host name of the SQL Server to be configured.")] String SQLServer; - [Write, Description("The name of the SQL instance to be configured.")] String SQLInstanceName; + [Write, Description("The host name of the SQL Server to be configured.")] String ServerName; + [Write, Description("The name of the SQL instance to be configured.")] String InstanceName; }; diff --git a/Examples/Resources/SqlDatabaseOwner/1-SetDatabaseOwner.ps1 b/Examples/Resources/SqlDatabaseOwner/1-SetDatabaseOwner.ps1 index c07ad3b57..a847d17ea 100644 --- a/Examples/Resources/SqlDatabaseOwner/1-SetDatabaseOwner.ps1 +++ b/Examples/Resources/SqlDatabaseOwner/1-SetDatabaseOwner.ps1 @@ -31,7 +31,7 @@ Configuration Example { Name = 'CONTOSO\SQLAdmin' Database = 'AdventureWorks' - SQLServer = 'SQLServer' + ServerName = 'SQLServer' InstanceName = 'DSC' PsDscRunAsCredential = $SysAdminAccount } diff --git a/Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1 b/Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1 index 6390c5d31..a3930a6c7 100644 --- a/Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1 @@ -1,14 +1,14 @@ -$script:DSCModuleName = 'SqlServerDsc' -$script:DSCResourceName = 'MSFT_SqlDatabaseOwner' +$script:DSCModuleName = 'SqlServerDsc' +$script:DSCResourceName = 'MSFT_SqlDatabaseOwner' #region HEADER # Unit Test Template Version: 1.2.0 $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` - (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) + (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) { - & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\')) + & git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\')) } Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force @@ -20,10 +20,12 @@ $TestEnvironment = Initialize-TestEnvironment ` #endregion HEADER -function Invoke-TestSetup { +function Invoke-TestSetup +{ } -function Invoke-TestCleanup { +function Invoke-TestCleanup +{ Restore-TestEnvironment -TestEnvironment $TestEnvironment } @@ -33,19 +35,19 @@ try Invoke-TestSetup InModuleScope $script:DSCResourceName { - $mockSqlServerName = 'localhost' - $mockSqlServerInstanceName = 'MSSQLSERVER' - $mockSqlDatabaseName = 'AdventureWorks' - $mockSqlServerLogin = 'Zebes\SamusAran' - $mockSqlServerLoginType = 'WindowsUser' - $mockDatabaseOwner = 'Elysia\Chozo' - $mockInvalidOperationForSetOwnerMethod = $false - $mockExpectedDatabaseOwner = 'Elysia\Chozo' + $mockServerName = 'localhost' + $mockInstanceName = 'MSSQLSERVER' + $mockSqlDatabaseName = 'AdventureWorks' + $mockSqlServerLogin = 'Zebes\SamusAran' + $mockSqlServerLoginType = 'WindowsUser' + $mockDatabaseOwner = 'Elysia\Chozo' + $mockInvalidOperationForSetOwnerMethod = $false + $mockExpectedDatabaseOwner = 'Elysia\Chozo' # Default parameters that are used for the It-blocks $mockDefaultParameters = @{ - SQLInstanceName = $mockSqlServerInstanceName - SQLServer = $mockSqlServerName + InstanceName = $mockInstanceName + ServerName = $mockServerName } #region Function mocks @@ -53,43 +55,43 @@ try return @( ( New-Object Object | - Add-Member -MemberType NoteProperty -Name InstanceName -Value $mockSqlServerInstanceName -PassThru | - Add-Member -MemberType NoteProperty -Name ComputerNamePhysicalNetBIOS -Value $mockSqlServerName -PassThru | + Add-Member -MemberType NoteProperty -Name InstanceName -Value $mockInstanceName -PassThru | + Add-Member -MemberType NoteProperty -Name ComputerNamePhysicalNetBIOS -Value $mockServerName -PassThru | Add-Member -MemberType ScriptProperty -Name Databases -Value { - return @{ - $mockSqlDatabaseName = @(( + return @{ + $mockSqlDatabaseName = @(( New-Object Object | Add-Member -MemberType NoteProperty -Name Name -Value $mockSqlDatabaseName -PassThru | Add-Member -MemberType NoteProperty -Name Owner -Value $mockDatabaseOwner -PassThru | Add-Member -MemberType ScriptMethod -Name SetOwner -Value { - if ($mockInvalidOperationForSetOwnerMethod) - { - throw 'Mock of method SetOwner() was called with invalid operation.' - } - - if ( $this.Owner -ne $mockExpectedDatabaseOwner ) - { - throw "Called mocked SetOwner() method without setting the right login. Expected '{0}'. But was '{1}'." ` - -f $mockExpectedDatabaseOwner, $this.Owner - } - } -PassThru -Force - )) - } - } -PassThru -Force | + if ($mockInvalidOperationForSetOwnerMethod) + { + throw 'Mock of method SetOwner() was called with invalid operation.' + } + + if ( $this.Owner -ne $mockExpectedDatabaseOwner ) + { + throw "Called mocked SetOwner() method without setting the right login. Expected '{0}'. But was '{1}'." ` + -f $mockExpectedDatabaseOwner, $this.Owner + } + } -PassThru -Force + )) + } + } -PassThru -Force | Add-Member -MemberType ScriptProperty -Name Logins -Value { - return @{ - $mockSqlServerLogin = @(( + return @{ + $mockSqlServerLogin = @(( New-Object Object | Add-Member -MemberType NoteProperty -Name LoginType -Value $mockSqlServerLoginType -PassThru )) - } - } -PassThru -Force + } + } -PassThru -Force ) ) } #endregion - Describe "MSFT_SqlDatabaseOwner\Get-TargetResource" -Tag 'Get'{ + Describe "MSFT_SqlDatabaseOwner\Get-TargetResource" -Tag 'Get' { BeforeEach { Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable } @@ -98,12 +100,12 @@ try It 'Should throw the correct error' { $testParameters = $mockDefaultParameters $testParameters += @{ - Database = 'unknownDatabaseName' - Name = $mockSqlServerLogin + Database = 'unknownDatabaseName' + Name = $mockSqlServerLogin } $throwInvalidOperation = ("Database 'unknownDatabaseName' does not exist " + ` - "on SQL server 'localhost\MSSQLSERVER'.") + "on SQL server 'localhost\MSSQLSERVER'.") { Get-TargetResource @testParameters } | Should -Throw $throwInvalidOperation } @@ -117,8 +119,8 @@ try It 'Should not throw' { $testParameters = $defaultParameters $testParameters += @{ - Database = $mockSqlDatabaseName - Name = $mockSqlServerLogin + Database = $mockSqlDatabaseName + Name = $mockSqlServerLogin } $result = Get-TargetResource @testParameters @@ -129,8 +131,8 @@ try } It 'Should return the same values as passed as parameters' { - $result.SQLServer | Should -Be $testParameters.SQLServer - $result.SQLInstanceName | Should -Be $testParameters.SQLInstanceName + $result.ServerName | Should -Be $testParameters.ServerName + $result.InstanceName | Should -Be $testParameters.InstanceName $result.Database | Should -Be $testParameters.Database } @@ -142,7 +144,7 @@ try Assert-VerifiableMock } - Describe "MSFT_SqlDatabaseOwner\Test-TargetResource" -Tag 'Test'{ + Describe "MSFT_SqlDatabaseOwner\Test-TargetResource" -Tag 'Test' { BeforeEach { Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable } @@ -151,8 +153,8 @@ try It 'Should return the state as false when desired login is not the database owner' { $testParameters = $defaultParameters $testParameters += @{ - Database = $mockSqlDatabaseName - Name = $mockSqlServerLogin + Database = $mockSqlDatabaseName + Name = $mockSqlServerLogin } $result = Test-TargetResource @testParameters @@ -170,8 +172,8 @@ try $mockSqlServerLogin = 'Zebes\SamusAran' $testParameters = $defaultParameters $testParameters += @{ - Database = $mockSqlDatabaseName - Name = $mockSqlServerLogin + Database = $mockSqlDatabaseName + Name = $mockSqlServerLogin } $result = Test-TargetResource @testParameters @@ -186,7 +188,7 @@ try Assert-VerifiableMock } - Describe "MSFT_SqlDatabaseOwner\Set-TargetResource" -Tag 'Set'{ + Describe "MSFT_SqlDatabaseOwner\Set-TargetResource" -Tag 'Set' { BeforeEach { Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable } @@ -195,12 +197,12 @@ try It 'Should throw the correct error' { $testParameters = $mockDefaultParameters $testParameters += @{ - Database = 'unknownDatabaseName' - Name = $mockSqlServerLogin + Database = 'unknownDatabaseName' + Name = $mockSqlServerLogin } $throwInvalidOperation = ("Database 'unknownDatabaseName' does not exist " + ` - "on SQL server 'localhost\MSSQLSERVER'.") + "on SQL server 'localhost\MSSQLSERVER'.") { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation } @@ -214,12 +216,12 @@ try It 'Should throw the correct error' { $testParameters = $mockDefaultParameters $testParameters += @{ - Database = $mockSqlDatabaseName - Name = 'John' + Database = $mockSqlDatabaseName + Name = 'John' } $throwInvalidOperation = ("Login 'John' does not exist on " + ` - "SQL server 'localhost\MSSQLSERVER'.") + "SQL server 'localhost\MSSQLSERVER'.") { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation } @@ -235,8 +237,8 @@ try $mockDatabaseOwner = $mockSqlServerLogin $testParameters = $mockDefaultParameters $testParameters += @{ - Database = $mockSqlDatabaseName - Name = $mockSqlServerLogin + Database = $mockSqlDatabaseName + Name = $mockSqlServerLogin } { Set-TargetResource @testParameters } | Should -Not -Throw @@ -252,15 +254,15 @@ try $mockExpectedDatabaseOwner = $mockSqlServerLogin $testParameters = $mockDefaultParameters $testParameters += @{ - Database = $mockSqlDatabaseName - Name = $mockSqlServerLogin + Database = $mockSqlDatabaseName + Name = $mockSqlServerLogin } $throwInvalidOperation = ('Failed to set owner named Zebes\SamusAran of the database ' + ` - 'named AdventureWorks on localhost\MSSQLSERVER. InnerException: ' + ` - 'Exception calling "SetOwner" with "1" argument(s): "Called mocked ' + ` - 'SetOwner() method without setting the right login. ' + ` - "Expected 'Zebes\SamusAran'. But was 'Elysia\Chozo'.") + 'named AdventureWorks on localhost\MSSQLSERVER. InnerException: ' + ` + 'Exception calling "SetOwner" with "1" argument(s): "Called mocked ' + ` + 'SetOwner() method without setting the right login. ' + ` + "Expected 'Zebes\SamusAran'. But was 'Elysia\Chozo'.") { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation } @@ -272,18 +274,18 @@ try Context 'When the system is not in the desired state' { It 'Should throw the correct error when the method SetOwner() was called' { - $mockInvalidOperationForSetOwnerMethod = $true + $mockInvalidOperationForSetOwnerMethod = $true $mockExpectedDatabaseOwner = $mockSqlServerLogin $testParameters = $mockDefaultParameters $testParameters += @{ - Database = $mockSqlDatabaseName - Name = $mockSqlServerLogin + Database = $mockSqlDatabaseName + Name = $mockSqlServerLogin } $throwInvalidOperation = ('Failed to set owner named Zebes\SamusAran of the database ' + ` - 'named AdventureWorks on localhost\MSSQLSERVER. InnerException: ' + ` - 'Exception calling "SetOwner" with "1" argument(s): "Mock ' + ` - 'of method SetOwner() was called with invalid operation.') + 'named AdventureWorks on localhost\MSSQLSERVER. InnerException: ' + ` + 'Exception calling "SetOwner" with "1" argument(s): "Mock ' + ` + 'of method SetOwner() was called with invalid operation.') { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation }