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

Test-TargetResource Streamlining #5787

Draft
wants to merge 3 commits into
base: Dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
FIXES [#5781](https://github.com/microsoft/Microsoft365DSC/issues/5781)
* DEPENDENCIES
* Updated ReverseDSC to version 2.0.0.27
* MISC
* Streamlining the Test-TargetResource of all resources.

# 1.25.212.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,6 @@ function Test-TargetResource
[System.String[]]
$AccessTokens
)

#Ensure the proper dependencies are installed in the current environment.
Confirm-M365DSCDependencies

#region Telemetry
$ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '')
$CommandName = $MyInvocation.MyCommand
Expand All @@ -673,49 +669,9 @@ function Test-TargetResource
Add-M365DSCTelemetryEvent -Data $data
#endregion

Write-Verbose -Message "Testing configuration of the Azure AD Access Review Definition with Id {$Id} and DisplayName {$DisplayName}"

$CurrentValues = Get-TargetResource @PSBoundParameters
$ValuesToCheck = ([Hashtable]$PSBoundParameters).clone()
$testResult = $true

#Compare Cim instances
foreach ($key in $PSBoundParameters.Keys)
{
$source = $PSBoundParameters.$key
$target = $CurrentValues.$key
if ($null -ne $source -and $source.GetType().Name -like '*CimInstance*')
{
$testResult = Compare-M365DSCComplexObject `
-Source ($source) `
-Target ($target)

if (-not $testResult)
{
break
}

$ValuesToCheck.Remove($key) | Out-Null
}
}

$ValuesToCheck.Remove('Id') | Out-Null
$ValuesToCheck = Remove-M365DSCAuthenticationParameter -BoundParameters $ValuesToCheck

Write-Verbose -Message "Current Values: $(Convert-M365DscHashtableToString -Hashtable $CurrentValues)"
Write-Verbose -Message "Target Values: $(Convert-M365DscHashtableToString -Hashtable $ValuesToCheck)"

if ($testResult)
{
$testResult = Test-M365DSCParameterState -CurrentValues $CurrentValues `
-Source $($MyInvocation.MyCommand.Source) `
-DesiredValues $PSBoundParameters `
-ValuesToCheck $ValuesToCheck.Keys
}

Write-Verbose -Message "Test-TargetResource returned $testResult"

return $testResult
$result = Test-M365DSCTargetResource -DesiredValues $PSBoundParameters `
-ResourceName $ResourceName
return $result
}

function Export-TargetResource
Expand Down Expand Up @@ -983,3 +939,4 @@ function Export-TargetResource
}

Export-ModuleMember -Function *-TargetResource

Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class MSFT_MicrosoftGraphAccessReviewStageSettings
[Write, Description("The duration of the stage. Required. NOTE: The cumulative value of this property across all stages 1. Will override the instanceDurationInDays setting on the accessReviewScheduleDefinition object. 2. Can't exceed the length of one recurrence. That is, if the review recurs weekly, the cumulative durationInDays can't exceed 7.")] UInt32 DurationInDays;
[Write, Description("Recommendation Insights Settings"), EmbeddedInstance("MSFT_MicrosoftGraphAccessReviewRecommendationInsightSetting")] String RecommendationInsightSettings[];
[Write, Description("Optional field. Indicates the time period of inactivity (with respect to the start date of the review instance) from which that recommendations will be configured. The recommendation is to deny if the user is inactive during the look back duration. For reviews of groups and Microsoft Entra roles, any duration is accepted. For reviews of applications, 30 days is the maximum duration. If not specified, the duration is 30 days. NOTE: The value of this property overrides the corresponding setting on the accessReviewScheduleDefinition object.")] String RecommendationLookBackDuration;
[Write, Description("Indicates whether showing recommendations to reviewers is enabled. Required. NOTE: The value of this property overrides the corresponding setting on the accessReviewScheduleDefinition object.")] Boolean RecommendationsEnabled;
[Required, Description("Indicates whether showing recommendations to reviewers is enabled. Required. NOTE: The value of this property overrides the corresponding setting on the accessReviewScheduleDefinition object.")] Boolean RecommendationsEnabled;
[Write, Description("Unique identifier of the accessReviewStageSettings. The stageId is used in dependsOn property to indicate the stage relationship. Required.")] String StageId;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,6 @@ function Test-TargetResource
[System.String[]]
$AccessTokens
)

#Ensure the proper dependencies are installed in the current environment.
Confirm-M365DSCDependencies

#region Telemetry
$ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '')
$CommandName = $MyInvocation.MyCommand
Expand All @@ -216,20 +212,9 @@ function Test-TargetResource
Add-M365DSCTelemetryEvent -Data $data
#endregion

$CurrentValues = Get-TargetResource @PSBoundParameters
$ValuesToCheck = ([Hashtable]$PSBoundParameters).Clone()

Write-Verbose -Message "Current Values: $(Convert-M365DscHashtableToString -Hashtable $CurrentValues)"
Write-Verbose -Message "Target Values: $(Convert-M365DscHashtableToString -Hashtable $ValuesToCheck)"

$testResult = Test-M365DSCParameterState -CurrentValues $CurrentValues `
-Source $($MyInvocation.MyCommand.Source) `
-DesiredValues $PSBoundParameters `
-ValuesToCheck $ValuesToCheck.Keys

Write-Verbose -Message "Test-TargetResource returned $testResult"

return $testResult
$result = Test-M365DSCTargetResource -DesiredValues $PSBoundParameters `
-ResourceName $ResourceName
return $result
}

function Export-TargetResource
Expand Down Expand Up @@ -346,3 +331,4 @@ function Export-TargetResource
}

Export-ModuleMember -Function *-TargetResource

Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,6 @@ function Test-TargetResource
[System.String[]]
$AccessTokens
)

#Ensure the proper dependencies are installed in the current environment.
Confirm-M365DSCDependencies

#region Telemetry
$ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '')
$CommandName = $MyInvocation.MyCommand
Expand All @@ -388,55 +384,9 @@ function Test-TargetResource
Add-M365DSCTelemetryEvent -Data $data
#endregion

Write-Verbose -Message "Testing configuration of the Azure AD Activity Based Timeout Policy with Id {$Id} and DisplayName {$DisplayName}"

$CurrentValues = Get-TargetResource @PSBoundParameters
$ValuesToCheck = ([Hashtable]$PSBoundParameters).clone()
$testResult = $true

#Compare Cim instances
foreach ($key in $PSBoundParameters.Keys)
{
$source = $PSBoundParameters.$key
$target = $CurrentValues.$key
if ($source.getType().Name -like '*CimInstance*')
{
$source = Get-M365DSCDRGComplexTypeToHashtable -ComplexObject $source

$testResult = Compare-M365DSCComplexObject `
-Source ($source) `
-Target ($target)

if (-Not $testResult)
{
$testResult = $false
break
}

$ValuesToCheck.Remove($key) | Out-Null
}
}

$ValuesToCheck.remove('Id') | Out-Null
$ValuesToCheck.Remove('Credential') | Out-Null
$ValuesToCheck.Remove('ApplicationId') | Out-Null
$ValuesToCheck.Remove('TenantId') | Out-Null
$ValuesToCheck.Remove('ApplicationSecret') | Out-Null

Write-Verbose -Message "Current Values: $(Convert-M365DscHashtableToString -Hashtable $CurrentValues)"
Write-Verbose -Message "Target Values: $(Convert-M365DscHashtableToString -Hashtable $ValuesToCheck)"

if ($testResult)
{
$testResult = Test-M365DSCParameterState -CurrentValues $CurrentValues `
-Source $($MyInvocation.MyCommand.Source) `
-DesiredValues $PSBoundParameters `
-ValuesToCheck $ValuesToCheck.Keys
}

Write-Verbose -Message "Test-TargetResource returned $testResult"

return $testResult
$result = Test-M365DSCTargetResource -DesiredValues $PSBoundParameters `
-ResourceName $ResourceName
return $result
}

function Export-TargetResource
Expand Down Expand Up @@ -563,3 +513,4 @@ function Export-TargetResource
}

Export-ModuleMember -Function *-TargetResource

Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,6 @@ function Test-TargetResource
[System.String[]]
$AccessTokens
)

#Ensure the proper dependencies are installed in the current environment.
Confirm-M365DSCDependencies

#region Telemetry
$ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '')
$CommandName = $MyInvocation.MyCommand
Expand All @@ -356,47 +352,9 @@ function Test-TargetResource
Add-M365DSCTelemetryEvent -Data $data
#endregion

$CurrentValues = Get-TargetResource @PSBoundParameters
$ValuesToCheck = ([Hashtable]$PSBoundParameters).Clone()
$testTargetResource = $true

Write-Verbose -Message "Current Values: $(Convert-M365DscHashtableToString -Hashtable $CurrentValues)"
Write-Verbose -Message "Target Values: $(Convert-M365DscHashtableToString -Hashtable $ValuesToCheck)"
#Compare Cim instances
foreach ($key in $PSBoundParameters.Keys)
{
$source = $PSBoundParameters.$key
$target = $CurrentValues.$key
if ($null -ne $source -and $source.GetType().Name -like '*CimInstance*')
{
$testResult = Compare-M365DSCComplexObject `
-Source ($source) `
-Target ($target)

if (-not $testResult)
{
Write-Verbose "TestResult returned False for $source"
$testTargetResource = $false
}
else
{
$ValuesToCheck.Remove($key) | Out-Null
}
}
}

$testResult = Test-M365DSCParameterState -CurrentValues $CurrentValues `
-Source $($MyInvocation.MyCommand.Source) `
-DesiredValues $PSBoundParameters `
-ValuesToCheck $ValuesToCheck.Keys

if (-not $TestResult)
{
$testTargetResource = $false
}
Write-Verbose -Message "Test-TargetResource returned $testTargetResource"

return $testTargetResource
$result = Test-M365DSCTargetResource -DesiredValues $PSBoundParameters `
-ResourceName $ResourceName
return $result
}

function Export-TargetResource
Expand Down Expand Up @@ -561,3 +519,4 @@ function Get-M365DSCAzureADAAdminConsentPolicyReviewerAsString
}

Export-ModuleMember -Function *-TargetResource

Original file line number Diff line number Diff line change
Expand Up @@ -873,10 +873,6 @@ function Test-TargetResource
[System.String[]]
$AccessTokens
)

#Ensure the proper dependencies are installed in the current environment.
Confirm-M365DSCDependencies

#region Telemetry
$ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '')
$CommandName = $MyInvocation.MyCommand
Expand All @@ -886,62 +882,9 @@ function Test-TargetResource
Add-M365DSCTelemetryEvent -Data $data
#endregion

Write-Verbose -Message "Testing configuration of the Azure AD Administrative Unit with Id {$Id} and DisplayName {$DisplayName}"

$CurrentValues = Get-TargetResource @PSBoundParameters
$ValuesToCheck = ([Hashtable]$PSBoundParameters).clone()
$testResult = $true

#Compare Cim instances
foreach ($key in $PSBoundParameters.Keys)
{
$source = $PSBoundParameters.$key
$target = $CurrentValues.$key
if ($source.getType().Name -like '*CimInstance*')
{
$source = Get-M365DSCDRGComplexTypeToHashtable -ComplexObject $source

$testResult = Compare-M365DSCComplexObject `
-Source ($source) `
-Target ($target)

if (-Not $testResult)
{
Write-Verbose -Message "Difference found for $key"
$testResult = $false
break
}

$ValuesToCheck.Remove($key) | Out-Null

}
}

$ValuesToCheck.Remove('Id') | Out-Null

# Visibility is currently not returned by Get-TargetResource
$ValuesToCheck.Remove('Visibility') | Out-Null

if ($ValuesToCheck.ContainsKey('MembershipType') -and $MembershipType -ne 'Dynamic' -and $CurrentValues.MembershipType -ne 'Dynamic')
{
# MembershipType may be returned as null or Assigned with same effect. Only compare if Dynamic is specified or returned
$ValuesToCheck.Remove('MembershipType') | Out-Null
}

Write-Verbose -Message "Current Values: $(Convert-M365DscHashtableToString -Hashtable $CurrentValues)"
Write-Verbose -Message "Target Values: $(Convert-M365DscHashtableToString -Hashtable $ValuesToCheck)"

if ($testResult)
{
$testResult = Test-M365DSCParameterState -CurrentValues $CurrentValues `
-Source $($MyInvocation.MyCommand.Source) `
-DesiredValues $PSBoundParameters `
-ValuesToCheck $ValuesToCheck.Keys
}

Write-Verbose -Message "Test-TargetResource returned $testResult"

return $testResult
$result = Test-M365DSCTargetResource -DesiredValues $PSBoundParameters `
-ResourceName $ResourceName
return $result
}

function Export-TargetResource
Expand Down Expand Up @@ -1152,3 +1095,4 @@ function Export-TargetResource
}

Export-ModuleMember -Function *-TargetResource

Loading
Loading