-
Notifications
You must be signed in to change notification settings - Fork 95
/
UpdateWAFCategories.ps1
219 lines (174 loc) · 7.05 KB
/
UpdateWAFCategories.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#requires -Version 7
<#
.SYNOPSIS
Uses the published Well Architected Framework documentation to generate category names and descriptions for the WAF Category file.
The script preserves existing descriptions by default. To update existing descriptions run with the -Force parameter or delete the existing descriptions.
The script requires an internet connection and uses content from:
https://github.com/MicrosoftDocs/well-architected
.PARAMETER Force
The "Force" parameter will overwrite existing descriptions in the csv file.
.INPUTS
WAF Category Descriptions.csv file which must be in the same directory as this script.
If the file is not present, it will be created.
.OUTPUTS
Updated WAF Category Descriptions.csv file
.NOTES
You must be connected to the internet to run this script.
It takes a dependency on the Well-Architected documentation repo.
.LINK
https://github.com/Azure/WellArchitected-Tools/
#>
[CmdletBinding()]
param (
# Indicates CSV file for input
[Parameter()][string]
$WellArchitectedDocsRepo = "https://raw.githubusercontent.com/MicrosoftDocs/well-architected/main/well-architected/",
[Parameter()][string]
$WAFCategoryFileName = "WAF Category Descriptions.csv",
[Parameter()]
[switch] $Force
)
# Get the working directory from the script
$workingDirectory = (Get-Location).Path
Function EnsureRepoFile($fileName, $remoteFile) {
if([System.IO.File]::Exists($workingDirectory + "\" + $fileName)) {
#check if file has been updated in the last day
$fileAge = (Get-Date) - (Get-Item -Path ($workingDirectory + "\" + $fileName)).LastWriteTime
if ($fileAge.Days -gt 1) {
#file is older than a day, so update it
Remove-Item -Path ($workingDirectory + "\" + $fileName) -Force
}
}
if(![System.IO.File]::Exists($workingDirectory + "\" + $fileName)) {
$toc = Invoke-RestMethod -Uri ($WellArchitectedDocsRepo + $remoteFile)
$toc | Out-File -FilePath ($workingDirectory + "\" + $fileName)
}
}
Function GetWAF2CategoryCaption($lineData) {
$searchCode = $lineData.Substring(0,5).Trim()
$toc = ""
$categoryCaption = $searchCode
EnsureRepoFile -fileName "WAF-toc.yml" -remoteFile "TOC.yml"
$toc = Get-Content -Path ($workingDirectory + "\WAF-toc.yml")
foreach($line in $toc) {
if($line.Trim().StartsWith("- name: " + $searchCode)) {
if($categoryCaption.Length -gt 5) {
$categoryCaption = $categoryCaption + ","
}
$categoryArray = $line.Trim().Split(":")
$categoryCaption = $categoryCaption + $categoryArray[$categoryArray.Length - 1].Substring(2)
}
}
return $categoryCaption
}
Function GetWAF2CategoryDescription($lineData) {
$searchCode = $lineData.Substring(0,5).Trim()
$toc = ""
$description = $null
EnsureRepoFile -fileName "WAF-toc.yml" -remoteFile "TOC.yml"
$toc = Get-Content -Path ($workingDirectory + "\WAF-toc.yml")
foreach($line in $toc) {
if($line.Contains("checklist.md")) {
$array = $line.Split(":");
$filename = $array[$array.Length - 1].Trim();
$remoteFileName = $filename;
$localFileName = $filename.Replace("/", "-");
EnsureRepoFile -fileName $localFileName -remoteFile $remoteFileName
$checklist = Get-Content -Path ($workingDirectory + "\" + $localFileName)
foreach($checklistLine in $checklist) {
if($checklistLine.Contains($searchCode)) {
$checklistArray = $checklistLine.Split("|")
$descriptionRaw = $checklistArray[$checklistArray.Length - 2].Trim()
$description = $descriptionRaw.Replace("**", "")
break
}
}
}
if($description -ne $null) {
break
}
}
return $description
}
# Check that WAF Descriptions File exists
if(![System.IO.File]::Exists($workingDirectory + "\" + $WAFCategoryFileName)) {
# Create the file
$header = "Pillar,Category,Caption,Description"
$header | Out-File -FilePath ($workingDirectory + "\" + $WAFCategoryFileName)
}
# Get existing content from CSV file
$existingContent = @(Import-Csv -Path ($workingDirectory + "\" + $WAFCategoryFileName))
EnsureRepoFile -fileName "WAF-toc.yml" -remoteFile "TOC.yml"
# Go through the TOC and find category indicators
$toc = Get-Content -Path ($workingDirectory + "\WAF-toc.yml")
foreach($line in $toc) {
$pillar = "";
$category = "";
$caption = $null;
$description = $null;
if($line.Trim().StartsWith("- name: ")) {
$lineArray = $line.Trim().Split(":")
if($lineArray.Length -ne 3) {
# not the type of line we're looking for
continue
}
$pillarIndicator = $lineArray[1].Trim();
$recommendationIndicator = $lineArray[2].Trim().Substring(0,2);
if($pillarIndicator.Length -ne 2) {
# not the type of line we're looking for
continue
}
if($pillarIndicator.Equals("CO")) {
$pillar = "Cost Optimization"
} elseif ($pillarIndicator.Equals("OE")) {
$pillar = "Operational Excellence"
} elseif ($pillarIndicator.Equals("PE")) {
$pillar = "Performance Efficiency"
} elseif ($pillarIndicator.Equals("RE")) {
$pillar = "Reliability"
} elseif ($pillarIndicator.Equals("SE")) {
$pillar = "Security"
} else {
# not the type of line we're looking for
continue
}
$category = $pillarIndicator + ":" + $recommendationIndicator
$caption = GetWAF2CategoryCaption -lineData $category
$description = GetWAF2CategoryDescription -lineData $category
if($caption -eq $null) {
# this hasn't worked - let's skip
continue
}
if($description -eq $null) {
# this hasn't worked - let's skip
continue
}
$exists = $false
# does this category already exist
$existingContent | Where-Object {$_.Category -eq $category} | ForEach-Object {
if($Force) {
# overwrite existing description
$_.Pillar = $pillar
$_.Caption = $caption
$_.Description = $description
}
$exists = $true
}
if(!$exists) {
# add new category
$newLine = [PSCustomObject] @{
'Pillar' = $null
'Category' = $null
'Caption' = $null
'Description' = $null
}
$newLine.Pillar = $pillar
$newLine.Category = $category
$newLine.Caption = $caption
$newLine.Description = $description
$existingContent += $newLine
}
}
}
Remove-Item -Path ($workingDirectory + "\" + $WAFCategoryFileName) -Force
$existingContent | Export-Csv -Path ($workingDirectory + "\" + $WAFCategoryFileName) -NoTypeInformation -UseQuotes AsNeeded