Skip to content

Commit 937f29d

Browse files
authored
Merge pull request #61 from Azure/feature/heinrich/autodoc
Fixing bugs with built-in versus custom assignments and autodoc
2 parents fb1cc5a + a3e8006 commit 937f29d

22 files changed

+1041
-930
lines changed

Scripts/Helpers/Build-AzPolicyAssignmentsPlan.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,15 @@ function Build-AzPolicyAssignmentsPlan {
130130
}
131131
else {
132132
$initiativeDefinition = $allInitiativeDefinitions[$name]
133-
$parametersInDefinition = $initiativeDefinition.Parameter
134133
if ($customInitiativeDefinitions.ContainsKey($name)) {
135134
# is custom
136135
$policyDefinitionId = $rootScopeId + "/providers/Microsoft.Authorization/policySetDefinitions/" + $name
136+
$parametersInDefinition = $initiativeDefinition.Parameter
137137
}
138138
else {
139139
# is built in
140140
$policyDefinitionId = "/providers/Microsoft.Authorization/policySetDefinitions/" + $name
141+
$parametersInDefinition = $initiativeDefinition.parameters
141142
}
142143
$policySpec = @{ initiativeId = $policyDefinitionId }
143144
if ($initiativeNeededRoleDefinitionIds.ContainsKey($name)) {
@@ -159,14 +160,15 @@ function Build-AzPolicyAssignmentsPlan {
159160
}
160161
else {
161162
$policyDefinition = $allPolicyDefinitions[$name]
162-
$parametersInDefinition = $policyDefinition.Parameter
163163
if ($customPolicyDefinitions.ContainsKey($name)) {
164164
# is custom
165165
$policyDefinitionId = $rootScopeId + "/providers/Microsoft.Authorization/policyDefinitions/" + $name
166+
$parametersInDefinition = $policyDefinition.Parameter
166167
}
167168
else {
168169
# is built in
169170
$policyDefinitionId = "/providers/Microsoft.Authorization/policyDefinitions/" + $name
171+
$parametersInDefinition = $policyDefinition.parameters
170172
}
171173
$policySpec = @{ policyId = $policyDefinitionId }
172174
if ($policyNeededRoleDefinitionIds.ContainsKey($name)) {
@@ -422,8 +424,8 @@ function Build-AzPolicyAssignmentsPlan {
422424
)
423425
}
424426
}
425-
}
426-
}
427+
428+
} }
427429
Write-Information ""
428430
Write-Information ""
429431

Scripts/Helpers/Build-AzPolicyDefinitionsPlan.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,24 @@ function Build-AzPolicyDefinitionsPlan {
7171
$Mode = $policyObject.properties.mode
7272
}
7373

74+
75+
7476
# Constructing policy definitions parameters for splatting
7577
$policyDefinitionConfig = @{
7678
Name = $name
7779
DisplayName = $displayName
7880
Policy = $policyObject.properties.policyRule
7981
Parameter = $policyObject.properties.parameters
80-
Metadata = $policyObject.properties.metadata
8182
Mode = $Mode
8283
}
84+
if ($null -ne $policyObject.properties.metadata) {
85+
$null = $policyDefinitionConfig.Add("Metadata", $policyObject.properties.metadata)
86+
}
8387

8488
# Calculate roleDefinitionIds for this Policy
8589
if ($policyObject.properties.policyRule.then.details -and $policyObject.properties.policyRule.then.details.roleDefinitionIds) {
8690
$roleDefinitionIdsInPolicy = $policyObject.properties.policyRule.then.details.roleDefinitionIds
87-
$policyNeededRoleDefinitionIds.Add($name, $roleDefinitionIdsInPolicy)
91+
$null = $policyNeededRoleDefinitionIds.Add($name, $roleDefinitionIdsInPolicy)
8892
}
8993

9094
# Adding SubscriptionId or ManagementGroupName value (depending on the parameter set in use)

Scripts/Helpers/Convert-AssignmentsInfoToFlatPolicyList.ps1

Lines changed: 0 additions & 87 deletions
This file was deleted.

Scripts/Helpers/Convert-EffectToShortForm.ps1

Lines changed: 0 additions & 14 deletions
This file was deleted.

Scripts/Helpers/Convert-EffectToString.ps1

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,26 @@ function Convert-EffectToString {
88
)
99

1010
[string] $text = ""
11-
$effectShort = Convert-EffectToShortForm -effect $effect
12-
if ($Markdown.IsPresent) {
13-
if ($allowedValues.Count -eq 1) {
14-
$text = "***$effectShort***"
15-
}
16-
else {
17-
$text = "**$effectShort**"
18-
}
19-
foreach ($allowed in $allowedValues) {
20-
if ($allowed -cne $effect) {
21-
$effectShort = Convert-EffectToShortForm -effect $allowed
22-
$text += "<br/>*$effectShort*"
11+
if ($null -ne $effect) {
12+
if ($Markdown.IsPresent) {
13+
if ($allowedValues.Count -eq 1) {
14+
$text = "***$effect***"
15+
}
16+
else {
17+
$text = "**$effect**"
18+
}
19+
foreach ($allowed in $allowedValues) {
20+
if ($allowed -cne $effect) {
21+
$text += "<br/>*$allowed*"
22+
}
2323
}
2424
}
25-
}
26-
else {
27-
$text += $effectShort
28-
foreach ($allowed in $allowedValues) {
29-
if ($allowed -cne $effect) {
30-
$effectShort = Convert-EffectToShortForm -effect $allowed
31-
$text += ", $effectShort"
25+
else {
26+
$text += $effect
27+
foreach ($allowed in $allowedValues) {
28+
if ($allowed -cne $effect) {
29+
$text += ", $allowed"
30+
}
3231
}
3332
}
3433
}

Scripts/Helpers/Convert-ParametersToString.ps1

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,70 @@
44
function Convert-ParametersToString {
55
param (
66
[hashtable] $parameters,
7-
[switch] $Markdown
7+
[string] $outputType
88
)
99

1010
[string] $text = ""
11+
[hashtable] $csvParametersHt = @{}
1112
if ($parameters.Count -gt 0) {
12-
[string[]] $parameterList = @()
1313
foreach ($parameterName in $parameters.Keys) {
1414
$parameter = $parameters.$parameterName
15+
$multiUse = $parameter.multiUse
16+
$isEffect = $parameter.isEffect
1517
$value = $parameter.value
16-
if ($null -eq $value) {
17-
$value = "undefined"
18-
if ($parameter.defaultValue) {
19-
$value = $parameter.defaultValue
20-
}
21-
18+
$defaultValue = $parameter.defaultValue
19+
$definition = $parameter.definition
20+
$initiativeDisplayNames = $parameter.initiatives
21+
if ($null -eq $value -and $null -eq $defaultValue) {
22+
$noDefault = $true
23+
$value = "++ no default ++"
2224
}
23-
$value = ConvertTo-Json $value -Compress
24-
$parameterList += "$parameterName=``$value``"
25-
}
26-
if ($Markdown.IsPresent) {
27-
foreach ($parameterText in $parameterList) {
28-
$text += "<br/>&nbsp;&nbsp;&nbsp;&nbsp;*$parameterText*"
25+
elseif ($null -eq $value) {
26+
$value = $defaultValue
2927
}
30-
}
31-
else {
32-
$newLine = ""
33-
foreach ($parameterText in $parameterList) {
34-
$text += "$newLine$parameterText"
35-
$newLine = "; "
28+
switch ($outputType) {
29+
markdown {
30+
if ($value -is [string]) {
31+
$text += "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*$parameterName = ``$value``*"
32+
}
33+
else {
34+
$json = ConvertTo-Json $value -Depth 100 -Compress
35+
$jsonTruncated = $json
36+
if ($json.length -gt 40) {
37+
$jsonTruncated = $json.substring(0, 40) + "..."
38+
}
39+
$text += "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*$parameterName = ``$jsonTruncated``*"
40+
}
41+
}
42+
csvValues {
43+
if (-not ($multiUse -or $isEffect)) {
44+
$null = $csvParametersHt.Add($parameterName, $value)
45+
}
46+
}
47+
csvDefinitions {
48+
if (-not $multiUse) {
49+
$null = $csvParametersHt.Add($parameterName, $definition)
50+
}
51+
}
52+
jsonc {
53+
$parameterString = "`"$($parameterName)`": $(ConvertTo-Json $value -Depth 100 -Compress), // '$($initiativeDisplayNames -Join "', '")'"
54+
if ($multiUse) {
55+
$text += "`n // Multi-use: ($parameterString)"
56+
}
57+
elseif ($noDefault) {
58+
$text += "`n // No-default: ($parameterString)"
59+
}
60+
else {
61+
$text += "`n $($parameterString),"
62+
}
63+
}
64+
Default {
65+
Write-Error "Convert-ParametersToString: unknown outputType '$outputType'" -ErrorAction Stop
66+
}
3667
}
3768
}
38-
}
39-
else {
40-
if (-not $Markdown.IsPresent) {
41-
$text = "n/a"
69+
if (($outputType -eq "csvValues" -or $outputType -eq "csvDefinitions") -and $csvParametersHt.Count -gt 0) {
70+
$text = ConvertTo-Json $csvParametersHt -Depth 100 -Compress
4271
}
4372
}
4473
return $text

0 commit comments

Comments
 (0)