Skip to content

Commit

Permalink
SqlDatabaseRole: Added localization (#1338)
Browse files Browse the repository at this point in the history
- Changes to SqlDatabaseRole
  - Added en-US localization (issue #610).
  • Loading branch information
johlju authored Apr 27, 2019
1 parent 1fd9a8a commit e2a37be
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 76 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
- Added en-US localization ([issue #612](https://github.com/PowerShell/SqlServerDsc/issues/612)).
- Changes to SqlServerEndpointState
- Added en-US localization ([issue #613](https://github.com/PowerShell/SqlServerDsc/issues/613)).
- Changes to SqlDatabaseRole
- Added en-US localization ([issue #610](https://github.com/PowerShell/SqlServerDsc/issues/610)).

## 12.4.0.0

Expand Down
100 changes: 57 additions & 43 deletions DSCResources/MSFT_SqlDatabaseRole/MSFT_SqlDatabaseRole.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Import-Module -Name (Join-Path -Path $script:localizationModulePath -ChildPath '
$script:resourceHelperModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'DscResource.Common'
Import-Module -Name (Join-Path -Path $script:resourceHelperModulePath -ChildPath 'DscResource.Common.psm1')

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

<#
.SYNOPSIS
Returns the current state of the user memberships in the role(s).
Expand Down Expand Up @@ -61,37 +63,35 @@ function Get-TargetResource
$Role
)

Write-Verbose -Message "Getting SQL Database role for $Name"
Write-Verbose -Message (
$script:localizedData.GetDatabaseRole -f $Name, $Database, $InstanceName
)

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

if ($sqlServerObject)
{
# Check database exists
if ( -not ($sqlDatabaseObject = $sqlServerObject.Databases[$Database]) )
{
throw New-TerminatingError -ErrorType NoDatabase `
-FormatArgs @($Database, $ServerName, $InstanceName) `
-ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.DatabaseNotFound -f $Database
New-ObjectNotFoundException -Message $errorMessage
}

# Check role exists
foreach ($currentRole in $Role)
{
if ( -not ($sqlDatabaseObject.Roles[$currentRole]) )
{
throw New-TerminatingError -ErrorType RoleNotFound `
-FormatArgs @($currentRole, $Database, $ServerName, $InstanceName) `
-ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.RoleNotFound -f $currentRole, $Database
New-ObjectNotFoundException -Message $errorMessage
}
}

# Check login exists
if ( -not ($sqlServerObject.Logins[$Name]) )
{
throw New-TerminatingError -ErrorType LoginNotFound `
-FormatArgs @($Name, $ServerName, $InstanceName) `
-ErrorCategory ObjectNotFound
$errorMessage = $script:localizedData.LoginNotFound -f $Name
New-ObjectNotFoundException -Message $errorMessage
}

$ensure = 'Absent'
Expand All @@ -103,15 +103,17 @@ function Get-TargetResource
{
if ($sqlDatabaseUser.IsMember($currentRole))
{
New-VerboseMessage -Message ("The login '$Name' is a member of the role '$currentRole' on the " + `
"database '$Database', on the instance $ServerName\$InstanceName")
Write-Verbose -Message (
$script:localizedData.IsMember -f $Name, $currentRole, $Database
)

$grantedRole += $currentRole
}
else
{
New-VerboseMessage -Message ("The login '$Name' is not a member of the role '$currentRole' on the " + `
"database '$Database', on the instance $ServerName\$InstanceName")
Write-Verbose -Message (
$script:localizedData.IsNotMember -f $Name, $currentRole, $Database
)
}
}

Expand All @@ -122,8 +124,9 @@ function Get-TargetResource
}
else
{
New-VerboseMessage -Message ("The login '$Name' is not a user of the database " + `
"'$Database' on the instance $ServerName\$InstanceName")
Write-Verbose -Message (
$script:localizedData.LoginIsNotUser -f $Name, $Database
)
}
}

Expand Down Expand Up @@ -201,10 +204,7 @@ function Set-TargetResource
$Role
)

Write-Verbose -Message "Setting SQL Database role for $Name"

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

if ($sqlServerObject)
{
$sqlDatabaseObject = $sqlServerObject.Databases[$Database]
Expand All @@ -218,20 +218,21 @@ function Set-TargetResource
{
try
{
New-VerboseMessage -Message ("Adding the login '$Name' as a user of the database " + `
"'$Database', on the instance $ServerName\$InstanceName")
Write-Verbose -Message (
'{0} {1}' -f
($script:localizedData.LoginIsNotUser -f $Name, $Database),
$script:localizedData.AddingLoginAsUser
)

$sqlDatabaseUser = New-Object -TypeName Microsoft.SqlServer.Management.Smo.User `
$sqlDatabaseUser = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.User' `
-ArgumentList $sqlDatabaseObject, $Name
$sqlDatabaseUser.Login = $Name
$sqlDatabaseUser.Create()
}
catch
{
throw New-TerminatingError -ErrorType AddLoginDatabaseSetError `
-FormatArgs @($ServerName, $InstanceName, $Name, $Database) `
-ErrorCategory InvalidOperation `
-InnerException $_.Exception
$errorMessage = $script:localizedData.FailedToAddUser -f $Name, $Database
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}

Expand All @@ -240,18 +241,17 @@ function Set-TargetResource
{
try
{
New-VerboseMessage -Message ("Adding the login '$Name' to the role '$currentRole' on the " + `
"database '$Database', on the instance $ServerName\$InstanceName")
Write-Verbose -Message (
$script:localizedData.AddUserToRole -f $Name, $currentRole, $Database
)

$sqlDatabaseRole = $sqlDatabaseObject.Roles[$currentRole]
$sqlDatabaseRole.AddMember($Name)
}
catch
{
throw New-TerminatingError -ErrorType AddMemberDatabaseSetError `
-FormatArgs @($ServerName, $InstanceName, $Name, $Role, $Database) `
-ErrorCategory InvalidOperation `
-InnerException $_.Exception
$errorMessage = $script:localizedData.FailedToAddUserToRole -f $Name, $currentRole, $Database
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}
}
Expand All @@ -262,19 +262,18 @@ function Set-TargetResource
{
foreach ($currentRole in $Role)
{
New-VerboseMessage -Message ("Removing the login '$Name' to the role '$currentRole' on the " + `
"database '$Database', on the instance $ServerName\$InstanceName")
Write-Verbose -Message (
$script:localizedData.DropUserFromRole -f $Name, $currentRole, $Database
)

$sqlDatabaseRole = $sqlDatabaseObject.Roles[$currentRole]
$sqlDatabaseRole.DropMember($Name)
}
}
catch
{
throw New-TerminatingError -ErrorType DropMemberDatabaseSetError `
-FormatArgs @($ServerName, $InstanceName, $Name, $Role, $Database) `
-ErrorCategory InvalidOperation `
-InnerException $_.Exception
$errorMessage = $script:localizedData.FailedToDropUserFromRole -f $Name, $currentRole, $Database
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}
}
Expand Down Expand Up @@ -341,7 +340,9 @@ function Test-TargetResource
$Role
)

Write-Verbose -Message "Testing SQL Database role for $Name"
Write-Verbose -Message (
$script:localizedData.TestingConfiguration -f $Name, $Database, $InstanceName
)

$getTargetResourceParameters = @{
InstanceName = $PSBoundParameters.InstanceName
Expand All @@ -361,7 +362,10 @@ function Test-TargetResource
{
if ($getTargetResourceResult.Ensure -ne 'Absent')
{
New-VerboseMessage -Message "Ensure is set to Absent. The existing role for $Name should be dropped"
Write-Verbose -Message (
$script:localizedData.NotInDesiredStateAbsent -f $Name, $Database
)

$isDatabaseRoleInDesiredState = $false
}
}
Expand All @@ -370,13 +374,23 @@ function Test-TargetResource
{
if ($getTargetResourceResult.Ensure -ne 'Present')
{
New-VerboseMessage -Message "Ensure is set to Present. The missing role for $Name should be added"
Write-Verbose -Message (
$script:localizedData.NotInDesiredStatePresent -f $Name, $Database
)

$isDatabaseRoleInDesiredState = $false
}
}
}

$isDatabaseRoleInDesiredState
if ($isDatabaseRoleInDesiredState)
{
Write-Verbose -Message (
$script:localizedData.InDesiredState -f $Name, $Database
)
}

return $isDatabaseRoleInDesiredState
}

Export-ModuleMember -Function *-TargetResource
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ConvertFrom-StringData @'
GetDatabaseRole = Getting current role(s) for the user '{0}' of the database '{1}' on the instance '{2}'.
DatabaseNotFound = The database '{0}' does not exist.
RoleNotFound = The role '{0}' does not exist in the database '{1}'.
LoginNotFound = The login '{0}' does not exist on the instance.
IsMember = The login '{0}' is a member of the role '{1}' in the database '{2}'.
IsNotMember = The login '{0}' is not a member of the role '{1}' in the database '{2}'.
LoginIsNotUser = The login '{0}' is not a user in the database '{1}'.
AddingLoginAsUser = Adding the login as a user of the database.
FailedToAddUser = Failed to add the login '{0}' as a user of the database '{1}'.
AddUserToRole = Adding the user (login) '{0}' to the role '{1}' in the database '{2}'.
FailedToAddUserToRole = Failed to add the user {0} to the role {1} in the database {2}.
DropUserFromRole = Removing the user (login) '{0}' from the role '{1}' in the database '{2}'.
FailedToDropUserFromRole = Failed to remove the login {0} from the role {1} in the database {2}.
TestingConfiguration = Determines if the the user '{0}' of the database '{1}' on the instance '{2}' is a member of the desired role(s).
InDesiredState = The user '{0}' of the database '{1}' is member of the specified role(s).
NotInDesiredStateAbsent = Expected the user '{0}' to not be a member of the specified role(s) in the database '{1}', but the user was member of at least one of the roles.
NotInDesiredStatePresent = Expected the user '{0}' to be a member of the specified role(s) in the database '{1}', but the user was not a member of at least one of the roles.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ ConvertFrom-StringData @'
# Database Role
AddLoginDatabaseSetError = Failed adding the login {2} as a user of the database {3}, on the instance {0}\\{1}.
DropMemberDatabaseSetError = Failed removing the login {2} from the role {3} on the database {4}, on the instance {0}\\{1}.
AddMemberDatabaseSetError = Failed adding the login {2} to the role {3} on the database {4}, on the instance {0}\\{1}.
# AvailabilityGroupListener
AvailabilityGroupListenerNotFound = Trying to make a change to a listener that does not exist.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ ConvertFrom-StringData @'
# Database Role
AddLoginDatabaseSetError = Failed adding the login {2} as a user of the database {3}, on the instance {0}\\{1}.
DropMemberDatabaseSetError = Failed removing the login {2} from the role {3} on the database {4}, on the instance {0}\\{1}.
AddMemberDatabaseSetError = Failed adding the login {2} to the role {3} on the database {4}, on the instance {0}\\{1}.
# AvailabilityGroupListener
AvailabilityGroupListenerNotFound = Trying to make a change to a listener that does not exist.
Expand Down
Loading

0 comments on commit e2a37be

Please sign in to comment.