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

Support for groups as part of policy Initiatives #364

Merged
merged 2 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions .github/scripts/Invoke-PolicyToBicep-China.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@ function New-PolicySetDefinitionsBicepInputTxtFile {
# Loop through child Policy Set/Initiative Definitions if HashTable not == 0
if (($policyDefinitions.Count) -ne 0) {
$policyDefinitions | Sort-Object | ForEach-Object {
$policySetDefinitionsOutputForBicep.Add($_.policyDefinitionReferenceId, $_.policyDefinitionId)
if ($null -ne $_.groupNames -and $_.groupNames.Count -ne 0) {
$joinedGroupNames = "'" + ($_.groupNames -join "','" ) + "'"
$policySetDefinitionsOutputForBicep.Add($_.policyDefinitionReferenceId, @($_.policyDefinitionId, $joinedGroupNames))
}
else {
$policySetDefinitionsOutputForBicep.Add($_.policyDefinitionReferenceId, @($_.policyDefinitionId, ""))
}
}
}

Expand All @@ -173,7 +179,8 @@ function New-PolicySetDefinitionsBicepInputTxtFile {
$policySetDefinitionsOutputForBicep.Keys | Sort-Object | ForEach-Object {
$definitionReferenceId = $_
$definitionReferenceIdForParameters = $_
$definitionId = $($policySetDefinitionsOutputForBicep[$_])
$definitionId = $($policySetDefinitionsOutputForBicep[$_][0])
$groups = $($policySetDefinitionsOutputForBicep[$_][1])

# If definitionReferenceId or definitionReferenceIdForParameters contains apostrophes, replace that apostrophe with a backslash and an apostrohphe for Bicep string escaping
if ($definitionReferenceId.Contains("'")) {
Expand All @@ -189,11 +196,11 @@ function New-PolicySetDefinitionsBicepInputTxtFile {
$definitionReferenceIdForParameters = "['$definitionReferenceIdForParameters']"

# Add nested array of objects to each Policy Set/Initiative Definition in the Bicep variable, without the '.' before the definitionReferenceId to make it an accessor
Add-Content -Path "$rootPath/$definitionsSetLongPath/$defintionsSetTxtFileName" -Encoding "utf8" -Value "`t`t`t{`r`n`t`t`t`tdefinitionReferenceId: '$definitionReferenceId'`r`n`t`t`t`tdefinitionId: '$definitionId'`r`n`t`t`t`tdefinitionParameters: $policySetDefParamVarCreation$definitionReferenceIdForParameters.parameters`r`n`t`t`t}"
Add-Content -Path "$rootPath/$definitionsSetLongPath/$defintionsSetTxtFileName" -Encoding "utf8" -Value "`t`t`t{`r`n`t`t`t`tdefinitionReferenceId: '$definitionReferenceId'`r`n`t`t`t`tdefinitionId: '$definitionId'`r`n`t`t`t`tdefinitionParameters: $policySetDefParamVarCreation$definitionReferenceIdForParameters.parameters`r`n`t`t`t`tdefinitionGroups: [$groups]`r`n`t`t`t}"
}
else {
# Add nested array of objects to each Policy Set/Initiative Definition in the Bicep variable
Add-Content -Path "$rootPath/$definitionsSetLongPath/$defintionsSetTxtFileName" -Encoding "utf8" -Value "`t`t`t{`r`n`t`t`t`tdefinitionReferenceId: '$definitionReferenceId'`r`n`t`t`t`tdefinitionId: '$definitionId'`r`n`t`t`t`tdefinitionParameters: $policySetDefParamVarCreation.$definitionReferenceIdForParameters.parameters`r`n`t`t`t}"
Add-Content -Path "$rootPath/$definitionsSetLongPath/$defintionsSetTxtFileName" -Encoding "utf8" -Value "`t`t`t{`r`n`t`t`t`tdefinitionReferenceId: '$definitionReferenceId'`r`n`t`t`t`tdefinitionId: '$definitionId'`r`n`t`t`t`tdefinitionParameters: $policySetDefParamVarCreation.$definitionReferenceIdForParameters.parameters`r`n`t`t`t`tdefinitionGroups: [$groups]`r`n`t`t`t}"
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions .github/scripts/Invoke-PolicyToBicep.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@ function New-PolicySetDefinitionsBicepInputTxtFile {
# Loop through child Policy Set/Initiative Definitions if HashTable not == 0
if (($policyDefinitions.Count) -ne 0) {
$policyDefinitions | Sort-Object | ForEach-Object {
$policySetDefinitionsOutputForBicep.Add($_.policyDefinitionReferenceId, $_.policyDefinitionId)
if ($null -ne $_.groupNames -and $_.groupNames.Count -ne 0) {
$joinedGroupNames = "'" + ($_.groupNames -join "','" ) + "'"
$policySetDefinitionsOutputForBicep.Add($_.policyDefinitionReferenceId, @($_.policyDefinitionId, $joinedGroupNames))
}
else {
$policySetDefinitionsOutputForBicep.Add($_.policyDefinitionReferenceId, @($_.policyDefinitionId, ""))
}
}
}

Expand All @@ -173,7 +179,8 @@ function New-PolicySetDefinitionsBicepInputTxtFile {
$policySetDefinitionsOutputForBicep.Keys | Sort-Object | ForEach-Object {
$definitionReferenceId = $_
$definitionReferenceIdForParameters = $_
$definitionId = $($policySetDefinitionsOutputForBicep[$_])
$definitionId = $($policySetDefinitionsOutputForBicep[$_][0])
$groups = $($policySetDefinitionsOutputForBicep[$_][1])

# If definitionReferenceId or definitionReferenceIdForParameters contains apostrophes, replace that apostrophe with a backslash and an apostrohphe for Bicep string escaping
if ($definitionReferenceId.Contains("'")) {
Expand All @@ -189,11 +196,11 @@ function New-PolicySetDefinitionsBicepInputTxtFile {
$definitionReferenceIdForParameters = "['$definitionReferenceIdForParameters']"

# Add nested array of objects to each Policy Set/Initiative Definition in the Bicep variable, without the '.' before the definitionReferenceId to make it an accessor
Add-Content -Path "$rootPath/$definitionsSetLongPath/$defintionsSetTxtFileName" -Encoding "utf8" -Value "`t`t`t{`r`n`t`t`t`tdefinitionReferenceId: '$definitionReferenceId'`r`n`t`t`t`tdefinitionId: '$definitionId'`r`n`t`t`t`tdefinitionParameters: $policySetDefParamVarCreation$definitionReferenceIdForParameters.parameters`r`n`t`t`t}"
Add-Content -Path "$rootPath/$definitionsSetLongPath/$defintionsSetTxtFileName" -Encoding "utf8" -Value "`t`t`t{`r`n`t`t`t`tdefinitionReferenceId: '$definitionReferenceId'`r`n`t`t`t`tdefinitionId: '$definitionId'`r`n`t`t`t`tdefinitionParameters: $policySetDefParamVarCreation$definitionReferenceIdForParameters.parameters`r`n`t`t`t`tdefinitionGroups: [$groups]`r`n`t`t`t}"
}
else {
# Add nested array of objects to each Policy Set/Initiative Definition in the Bicep variable
Add-Content -Path "$rootPath/$definitionsSetLongPath/$defintionsSetTxtFileName" -Encoding "utf8" -Value "`t`t`t{`r`n`t`t`t`tdefinitionReferenceId: '$definitionReferenceId'`r`n`t`t`t`tdefinitionId: '$definitionId'`r`n`t`t`t`tdefinitionParameters: $policySetDefParamVarCreation.$definitionReferenceIdForParameters.parameters`r`n`t`t`t}"
Add-Content -Path "$rootPath/$definitionsSetLongPath/$defintionsSetTxtFileName" -Encoding "utf8" -Value "`t`t`t{`r`n`t`t`t`tdefinitionReferenceId: '$definitionReferenceId'`r`n`t`t`t`tdefinitionId: '$definitionId'`r`n`t`t`t`tdefinitionParameters: $policySetDefParamVarCreation.$definitionReferenceIdForParameters.parameters`r`n`t`t`t`tdefinitionGroups: [$groups]`r`n`t`t`t}"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,7 @@ resource resPolicySetDefinitions 'Microsoft.Authorization/policySetDefinitions@2
policyDefinitionReferenceId: policySetDef.definitionReferenceId
policyDefinitionId: policySetDef.definitionId
parameters: policySetDef.definitionParameters
groupNames: policySetDef.definitionGroups
}]
policyDefinitionGroups: policySet.libSetDefinition.properties.policyDefinitionGroups
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,7 @@ resource resPolicySetDefinitions 'Microsoft.Authorization/policySetDefinitions@2
policyDefinitionReferenceId: policySetDef.definitionReferenceId
policyDefinitionId: policySetDef.definitionId
parameters: policySetDef.definitionParameters
groupNames: policySetDef.definitionGroups
}]
policyDefinitionGroups: policySet.libSetDefinition.properties.policyDefinitionGroups
}
Expand Down