Skip to content

Commit

Permalink
Wider fix for property round-trip through Update-
Browse files Browse the repository at this point in the history
Improve global test cleanup
  • Loading branch information
mentat9 committed Jun 28, 2024
1 parent ead709b commit 0941a51
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 182 deletions.
35 changes: 14 additions & 21 deletions src/Resources/Policy.Autorest/custom/Update-AzPolicyAssignment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,8 @@ process {
}
}

if (!$NotScope) {
if ($_.NotScope) {
$calledParameters.NotScope = $_.NotScope
}
elseif ($existing.NotScope) {
$calledParameters.NotScope = $existing.NotScope
}
if (!$NotScope -and !($NotScope -is [array])) {
$calledParameters.NotScope = $existing.NotScope
}

if (!$Location) {
Expand All @@ -337,22 +332,20 @@ process {
}
}

if (!$DisplayName) {
if ($_.DisplayName) {
$calledParameters.DisplayName = $_.DisplayName
}
elseif ($existing.DisplayName) {
$calledParameters.DisplayName = $existing.DisplayName
}
if (!$calledParameters.DisplayName) {
$calledParameters.DisplayName = $existing.DisplayName
}

if (!$Description) {
if ($_.Description) {
$calledParameters.Description = $_.Description
}
elseif ($existing.Description) {
$calledParameters.Description = $existing.Description
}
if (!$calledParameters.Description) {
$calledParameters.Description = $existing.Description
}

if (!$calledParameters.Metadata) {
$calledParameters.Metadata = $existing.Metadata
}

if (!$calledParameters.EnforcementMode -and $calledParameters.EnforcementMode) {
$calledParameters.EnforcementMode = $existing.EnforcementMode
}

if ($BackwardCompatible) {
Expand Down
12 changes: 12 additions & 0 deletions src/Resources/Policy.Autorest/custom/Update-AzPolicyDefinition.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,18 @@ process {
$calledParameters.Parameter = ConvertTo-Json $existing.Parameter -Depth 100
}

if (!$calledParameters.DisplayName) {
$calledParameters.DisplayName = $existing.DisplayName
}

if (!$calledParameters.Description) {
$calledParameters.Description = $existing.Description
}

if (!$calledParameters.Metadata) {
$calledParameters.Metadata = $existing.Metadata
}

if ($BackwardCompatible) {
$calledParameters.BackwardCompatible = $true
}
Expand Down
20 changes: 20 additions & 0 deletions src/Resources/Policy.Autorest/custom/Update-AzPolicyExemption.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,26 @@ process {
$null = $calledParameters.Remove('ClearExpiration')
}

if (!$calledParameters.DisplayName) {
$calledParameters.DisplayName = $existing.DisplayName
}

if (!$calledParameters.Description) {
$calledParameters.Description = $existing.Description
}

if (!$calledParameters.PolicyDefinitionReferenceId -and !($calledParameters.PolicyDefinitionReferenceId -is [array])) {
$calledParameters.PolicyDefinitionReferenceId = $existing.PolicyDefinitionReferenceId
}

if (!$calledParameters.Metadata) {
$calledParameters.Metadata = $existing.Metadata
}

if (!$calledParameters.AssignmentScopeValidation -and $existing.AssignmentScopeValidation) {
$calledParameters.AssignmentScopeValidation = $existing.AssignmentScopeValidation
}

if ($BackwardCompatible) {
$calledParameters.BackwardCompatible = $true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ process {
$calledParameters.PolicyDefinitionGroup = ConvertTo-Json -Depth 100 $existing.PolicyDefinitionGroup
}

if (!$calledParameters.DisplayName) {
$calledParameters.DisplayName = $existing.DisplayName
}

if (!$calledParameters.Description) {
$calledParameters.Description = $existing.Description
}

if (!$calledParameters.Metadata) {
$calledParameters.Metadata = $existing.Metadata
}

if ($BackwardCompatible) {
$calledParameters.BackwardCompatible = $true
}
Expand Down
25 changes: 25 additions & 0 deletions src/Resources/Policy.Autorest/test/PolicyAssignmentCRUD.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,31 @@ Describe 'PolicyAssignmentCRUD' {
$update.NonComplianceMessage[1].PolicyDefinitionReferenceId | Should -Be $policyDefinitionReferenceId1
}

It 'Validate parameter round-trip' {
# get the definition, do an update with no changes, validate nothing is changed in response or backend
$expected = Get-AzPolicyAssignment -Name $testPA -Scope $rgScope
$response = Update-AzPolicyAssignment -Name $testPA -Scope $rgScope
$response.DisplayName | Should -Be $expected.DisplayName
$response.Description | Should -Be $expected.Description
$response.Metadata.$metadataName | Should -Be $expected.Metadata.$metadataName
$response.NonComplianceMessage[0] | Should -BeLike $expected.NonComplianceMessage[0]
$response.NonComplianceMessage[1] | Should -BeLike $expected.NonComplianceMessage[1]
$response.Parameter | Should -BeLike $expected.Parameter
$response.NotScope | Should -BeLike $expected.NotScope
$response.Location | Should -BeLike $expected.Location
$response.EnforcementMode | Should -BeLike $expected.EnforcementMode
$actual = Get-AzPolicyAssignment -Name $testPA -Scope $rgScope
$actual.DisplayName | Should -Be $expected.DisplayName
$actual.Description | Should -Be $expected.Description
$actual.Metadata.$metadataName | Should -Be $expected.Metadata.$metadataName
$actual.NonComplianceMessage[0] | Should -BeLike $expected.NonComplianceMessage[0]
$actual.NonComplianceMessage[1] | Should -BeLike $expected.NonComplianceMessage[1]
$actual.Parameter | Should -BeLike $expected.Parameter
$actual.NotScope | Should -Be $expected.NotScope
$actual.Location | Should -BeLike $expected.Location
$actual.EnforcementMode | Should -BeLike $expected.EnforcementMode
}

It 'Update the policy assignment to have a single non-compliance message' {
# get original assignment back again
$actual = Get-AzPolicyAssignment -Name $testPA -Scope $rgScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,26 @@ Describe 'PolicyDefinitionCRUD' -Tag 'LiveOnly' {
$expected.DisplayName | Should -Be $actual.DisplayName
$expected.Description | Should -Be $actual.Description
$actual.Metadata | Should -Not -BeNullOrEmpty
$actual.Metadata.$metadataName| Should -Be $metadataValue
$actual.Metadata.$metadataName | Should -Be $metadataValue
}

It 'Validate parameter round-trip' {
# get the definition, do an update with no changes, validate nothing is changed in response or backend
$expected = Get-AzPolicyDefinition -Name $policyName
$response = Update-AzPolicyDefinition -Name $policyName
$response.DisplayName | Should -Be $expected.DisplayName
$response.Description | Should -Be $expected.Description
$response.Metadata.$metadataName | Should -Be $expected.Metadata.$metadataName
$response.PolicyDefinition | Should -BeLike $expected.PolicyDefinition
$response.Parameter | Should -BeLike $expected.Parameter
$response.Mode | Should -BeLike $expected.Mode
$actual = Get-AzPolicyDefinition -Name $policyName
$actual.DisplayName | Should -Be $expected.DisplayName
$actual.Description | Should -Be $expected.Description
$actual.Metadata.$metadataName | Should -Be $expected.Metadata.$metadataName
$actual.PolicyDefinition | Should -BeLike $expected.PolicyDefinition
$actual.Parameter | Should -BeLike $expected.Parameter
$actual.Mode | Should -BeLike $expected.Mode
}

It 'Make policy definition from command line rule' {
Expand Down
23 changes: 23 additions & 0 deletions src/Resources/Policy.Autorest/test/PolicyExemptionCRUD.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ Describe 'PolicyExemptionCRUD' {
$exemption.Metadata.$metadataName | Should -BeNull
}

It 'Validate parameter round-trip' {
# get the definition, do an update with no changes, validate nothing is changed in response or backend
$expected = Get-AzPolicyExemption -Name $testExemption -Scope $rgScope
$response = Update-AzPolicyExemption -Name $testExemption -Scope $rgScope
$response.DisplayName | Should -Be $expected.DisplayName
$response.Description | Should -Be $expected.Description
$response.Metadata.$metadataName | Should -Be $expected.Metadata.$metadataName
$response.ExemptionCategory | Should -BeLike $expected.ExemptionCategory
$response.Parameter | Should -BeLike $expected.Parameter
$response.PolicyDefinitionReferenceId | Should -BeLike $expected.PolicyDefinitionReferenceId
$response.ExpiresOn | Should -BeLike $expected.ExpiresOn
$response.AssignmentScopeValidation | Should -BeLike $expected.AssignmentScopeValidation
$actual = Get-AzPolicyExemption -Name $testExemption -Scope $rgScope
$actual.DisplayName | Should -Be $expected.DisplayName
$actual.Description | Should -Be $expected.Description
$actual.Metadata.$metadataName | Should -Be $expected.Metadata.$metadataName
$actual.ExemptionCategory | Should -BeLike $expected.ExemptionCategory
$actual.Parameter | Should -BeLike $expected.Parameter
$actual.PolicyDefinitionReferenceId | Should -BeLike $expected.PolicyDefinitionReferenceId
$actual.ExpiresOn | Should -BeLike $expected.ExpiresOn
$actual.AssignmentScopeValidation | Should -BeLike $expected.AssignmentScopeValidation
}

It 'Update policy exemption by clearing the expiration' {
# get the exemption by name first
$exemption = Get-AzPolicyExemption -Name $testExemption -Scope $rgScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ Describe 'PolicySetDefinitionCRUD' {
$actual.Metadata.$metadataName | Should -Be $metadataValue
}

It 'Validate parameter round-trip' {
# get the set definition, do an update with no changes, validate nothing is changed in response or backend
$expected = Get-AzPolicySetDefinition -Name $policySetDefName
$response = Update-AzPolicySetDefinition -Name $policySetDefName
$response.DisplayName | Should -Be $expected.DisplayName
$response.Description | Should -Be $expected.Description
$response.Metadata.$metadataName | Should -Be $expected.Metadata.$metadataName
$response.PolicyRule | Should -BeLike $expected.PolicyRule
$response.Parameter | Should -BeLike $expected.Parameter
$response.PolicyDefinitionGroup | Should -BeLike $expected.PolicyDefinitionGroup
$actual = Get-AzPolicySetDefinition -Name $policySetDefName
$actual.DisplayName | Should -Be $expected.DisplayName
$actual.Description | Should -Be $expected.Description
$actual.Metadata.$metadataName | Should -Be $expected.Metadata.$metadataName
$actual.PolicyRule | Should -BeLike $expected.PolicyRule
$actual.Parameter | Should -BeLike $expected.Parameter
$actual.PolicyDefinitionGroup | Should -BeLike $expected.PolicyDefinitionGroup
}

It 'List builtin and custom' {
# ensure that only custom set definitions are returned using the custom flag
$list = Get-AzPolicySetDefinition -Custom
Expand Down
Loading

0 comments on commit 0941a51

Please sign in to comment.