Skip to content

Commit

Permalink
Merge pull request #24 from X-Guardian/Messaging-and-Test-Standardiza…
Browse files Browse the repository at this point in the history
…tion

AdfsDsc: Standardise Messaging and Tests
  • Loading branch information
X-Guardian authored Nov 25, 2019
2 parents 122e8d3 + 6f57158 commit cc543fd
Show file tree
Hide file tree
Showing 47 changed files with 2,186 additions and 755 deletions.
168 changes: 133 additions & 35 deletions DSCResources/MSFT_AdfsApplicationGroup/MSFT_AdfsApplicationGroup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ function Get-TargetResource
Get-TargetResource
.NOTES
Used Resource PowerShell Cmdlets:
- Get-AdfsApplicationGroup - https://docs.microsoft.com/en-us/powershell/module/adfs/get-adfsapplicationgroup
Used Cmdlets/Functions:
Name | Module
-------------------------|----------------
Get-AdfsApplicationGroup | Adfs
Assert-Module | AdfsDsc.Common
Assert-Command | AdfsDsc.Common
Assert-AdfsService | AdfsDsc.Common
#>

[CmdletBinding()]
Expand All @@ -61,22 +67,38 @@ function Get-TargetResource
$Name
)

# Set Verbose and Debug parameters
$commonParms = @{
Verbose = $VerbosePreference
Debug = $DebugPreference
}

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

# Check of the Resource PowerShell module is installed
Assert-Module -ModuleName $script:psModuleName

# Check if the Get-AdfsApplicationGroup command is available
Assert-Command -Module $script:psModuleName -Command 'Get-AdfsApplicationGroup'

# Check if the ADFS Service is present and running
Assert-AdfsService -Verbose

Write-Verbose ($script:localizedData.GettingResourceMessage -f $Name)
Assert-AdfsService @commonParms

$targetResource = Get-AdfsApplicationGroup -Name $Name
try
{
$targetResource = Get-AdfsApplicationGroup -Name $Name
}
catch
{
$errorMessage = $script:localizedData.GettingResourceErrorMessage -f $Name
New-InvalidOperationException -Message $errorMessage -Error $_
}

if ($targetResource)
{
# Resource exists
# Resource is Present
Write-Debug -Message ($script:localizedData.TargetResourcePresentDebugMessage -f $Name)

$returnValue = @{
Name = $targetResource.Name
Description = $targetResource.Description
Expand All @@ -85,7 +107,9 @@ function Get-TargetResource
}
else
{
# Resource does not exist
# Resource is Absent
Write-Debug -Message ($script:localizedData.TargetResourceAbsentDebugMessage -f $Name)

$returnValue = @{
Name = $Name
Description = $null
Expand All @@ -103,10 +127,14 @@ function Set-TargetResource
Set-TargetResource
.NOTES
Used Resource PowerShell Cmdlets:
- New-AdfsApplicationGroup - https://docs.microsoft.com/en-us/powershell/module/adfs/new-adfsapplicationgroup
- Set-AdfsApplicationGroup - https://docs.microsoft.com/en-us/powershell/module/adfs/set-adfsapplicationgroup
- Remove-AdfsApplicationGroup - https://docs.microsoft.com/en-us/powershell/module/adfs/remove-adfsapplicationgroup
Used Cmdlets/Functions:
Name | Module
------------------------------|----------------
New-AdfsApplicationGroup | AdfsDsc
Set-AdfsApplicationGroup | AdfsDsc
Remove-AdfsApplicationGroup | AdfsDsc
Compare-ResourcePropertyState | AdfsDsc.Common
#>

[CmdletBinding()]
Expand All @@ -126,11 +154,19 @@ function Set-TargetResource
$Ensure = 'Present'
)

# Set Verbose and Debug parameters
$commonParms = @{
Verbose = $VerbosePreference
Debug = $DebugPreference
}

# Remove any parameters not used in Splats
$parameters = $PSBoundParameters
$parameters.Remove('Ensure')
$parameters.Remove('Verbose')

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

$GetTargetResourceParms = @{
Name = $Name
}
Expand All @@ -139,42 +175,81 @@ function Set-TargetResource
if ($targetResource.Ensure -eq 'Present')
{
# Resource is Present
Write-Debug -Message ($script:localizedData.TargetResourcePresentDebugMessage -f $Name)

if ($Ensure -eq 'Present')
{
# Resource should be Present
Write-Debug -Message ($script:localizedData.TargetResourceShouldBePresentDebugMessage -f $Name)

$propertiesNotInDesiredState = (
Compare-ResourcePropertyState -CurrentValues $targetResource -DesiredValues $parameters |
Where-Object -Property InDesiredState -eq $false)
Compare-ResourcePropertyState -CurrentValues $targetResource -DesiredValues $parameters `
@commonParms | Where-Object -Property InDesiredState -eq $false)

$SetParameters = @{ }
foreach ($property in $propertiesNotInDesiredState)
{
Write-Verbose -Message ($script:localizedData.SettingResourceMessage -f
Write-Verbose -Message ($script:localizedData.SettingResourcePropertyMessage -f
$Name, $property.ParameterName, ($property.Expected -join ', '))

$SetParameters.add($property.ParameterName, $property.Expected)
}

Set-AdfsApplicationGroup -TargetName $Name @SetParameters
try
{
Set-AdfsApplicationGroup -TargetName $Name @SetParameters
}
catch
{
$errorMessage = $script:localizedData.SettingResourceErrorMessage -f $Name
New-InvalidOperationException -Message $errorMessage -Error $_
}
}
else
{
# Resource should be Absent
Write-Debug -Message ($script:localizedData.TargetResourceShouldBeAbsentDebugMessage -f $Name)

Write-Verbose -Message ($script:localizedData.RemovingResourceMessage -f $Name)
Remove-AdfsApplicationGroup -TargetName $Name

try
{
Remove-AdfsApplicationGroup -TargetName $Name
}
catch
{
$errorMessage = $script:localizedData.RemovingResourceErrorMessage -f $Name
New-InvalidOperationException -Message $errorMessage -Error $_
}
}
}
else
{
# Resource is Absent
Write-Debug -Message ($script:localizedData.TargetResourceAbsentDebugMessage -f $Name)

if ($Ensure -eq 'Present')
{
# Resource should be Present
Write-Debug -Message ($script:localizedData.TargetResourceShouldBePresentDebugMessage -f $Name)

Write-Verbose -Message ($script:localizedData.AddingResourceMessage -f $Name)
New-AdfsApplicationGroup @parameters

try
{
New-AdfsApplicationGroup @parameters
}
catch
{
$errorMessage = $script:localizedData.AddingResourceErrorMessage -f $Name
New-InvalidOperationException -Message $errorMessage -Error $_
}
}
else
{
# Resource should be Absent
Write-Debug -Message ($script:localizedData.TargetResourceShouldBeAbsentDebugMessage -f $Name)

Write-Verbose -Message ($script:localizedData.ResourceInDesiredStateMessage -f $Name)
}
}
Expand All @@ -185,6 +260,13 @@ function Test-TargetResource
<#
.SYNOPSIS
Test-TargetResource
.NOTES
Used Cmdlets/Functions:
Name | Module
------------------------------|------------------
Compare-ResourcePropertyState | AdfsDsc.Common
#>

[CmdletBinding()]
Expand All @@ -205,6 +287,14 @@ function Test-TargetResource
$Ensure = 'Present'
)

# Set Verbose and Debug parameters
$commonParms = @{
Verbose = $VerbosePreference
Debug = $DebugPreference
}

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

$getTargetResourceParms = @{
Name = $Name
}
Expand All @@ -213,55 +303,63 @@ function Test-TargetResource
if ($targetResource.Ensure -eq 'Present')
{
# Resource is Present
Write-Debug -Message ($script:localizedData.TargetResourcePresentDebugMessage -f $Name)

if ($Ensure -eq 'Present')
{
# Resource should be Present
Write-Debug -Message ($script:localizedData.TargetResourceShouldBePresentDebugMessage -f $Name)

$propertiesNotInDesiredState = (
Compare-ResourcePropertyState -CurrentValues $targetResource -DesiredValues $PSBoundParameters |
Where-Object -Property InDesiredState -eq $false)
Compare-ResourcePropertyState -CurrentValues $targetResource -DesiredValues $PSBoundParameters `
@commonParms | Where-Object -Property InDesiredState -eq $false)

if ($propertiesNotInDesiredState)
{
# Resource is not in desired state
foreach ($property in $propertiesNotInDesiredState)
{
Write-Verbose -Message (
$script:localizedData.ResourcePropertyNotInDesiredStateMessage -f
$targetResource.Name, $property.ParameterName, `
$property.Expected, $property.Actual)
}
Write-Verbose -Message ($script:localizedData.ResourceNotInDesiredStateMessage -f $Name)

$inDesiredState = $false
}
else
{
# Resource is in desired state
Write-Verbose -Message ($script:localizedData.ResourceInDesiredStateMessage -f
$targetResource.Name)
Write-Verbose -Message ($script:localizedData.ResourceInDesiredStateMessage -f $Name)

$inDesiredState = $true
}
}
else
{
# Resource should be Absent
Write-Verbose -Message ($script:localizedData.ResourceIsPresentButShouldBeAbsentMessage -f
$targetResource.Name)
Write-Debug -Message ($script:localizedData.TargetResourceShouldBeAbsentDebugMessage -f $Name)

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

$inDesiredState = $false
}
}
else
{
# Resource is Absent
Write-Debug -Message ($script:localizedData.TargetResourceAbsentDebugMessage -f $Name)

if ($Ensure -eq 'Present')
{
# Resource should be Present
Write-Verbose -Message ($script:localizedData.ResourceIsAbsentButShouldBePresentMessage -f
$targetResource.Name)
Write-Debug -Message ($script:localizedData.TargetResourceShouldBePresentDebugMessage -f $Name)

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

$inDesiredState = $false
}
else
{
# Resource should be Absent
Write-Verbose ($script:localizedData.ResourceInDesiredStateMessage -f
$targetResource.Name)
Write-Debug -Message ($script:localizedData.TargetResourceShouldBeAbsentDebugMessage -f $Name)

Write-Verbose ($script:localizedData.ResourceInDesiredStateMessage -f $Name)

$inDesiredState = $true
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
# culture="en-US"
ConvertFrom-StringData @'
GettingResourceMessage = Getting '{0}'. (AG0001)
SettingResourceMessage = Setting '{0}' property '{1}' to '{2}'. (AG0002)
AddingResourceMessage = Adding '{0}'. (AG0003)
RemovingResourceMessage = Removing '{0}'. (AG0004)
ResourceInDesiredStateMessage = '{0}' is in the desired state. (AG0005)
ResourcePropertyNotInDesiredStateMessage = '{0}' Property '{1}' is not in the desired state. Expected: '{2}', Actual: '{3}'. (AG0006)
ResourceIsPresentButShouldBeAbsentMessage = '{0}' is present but should be absent. (AG0007)
ResourceIsAbsentButShouldBePresentMessage = '{0}' is absent but should be present. (AG0008)
GettingResourceMessage = Getting '{0}'. (AG001)
TestingResourceMessage = Testing '{0}'. (AG002)
SettingResourceMessage = Setting '{0}'. (AG003)
SettingResourcePropertyMessage = Setting '{0}' property '{1}' to '{2}'. (AG004)
AddingResourceMessage = Adding '{0}'. (AG005)
RemovingResourceMessage = Removing '{0}'. (AG006)
ResourceInDesiredStateMessage = '{0}' is in the desired state. (AG007)
ResourceNotInDesiredStateMessage = '{0}' is not in the desired state. (AG008)
ResourceIsPresentButShouldBeAbsentMessage = '{0}' is present but should be absent. (AG009)
ResourceIsAbsentButShouldBePresentMessage = '{0}' is absent but should be present. (AG010)
GettingResourceErrorMessage = Error getting '{0}'. (AGERR001)
SettingResourceErrorMessage = Error setting '{0}'. (AGERR002)
RemovingResourceErrorMessage = Error removing '{0}'. (AGERR003)
AddingResourceErrorMessage = Error adding '{0}'. (AGERR004)
TargetResourcePresentDebugMessage = '{0}' is Present. (AGDBG001)
TargetResourceAbsentDebugMessage = '{0}' is Absent. (AGDBG002)
TargetResourceShouldBePresentDebugMessage = '{0}' should be Present. (AGDBG003)
TargetResourceShouldBeAbsentDebugMessage = '{0}' should be Absent. (AGDBG004)
'@
Loading

0 comments on commit cc543fd

Please sign in to comment.