-
Notifications
You must be signed in to change notification settings - Fork 2
/
CfnResSpecDocParser.ps1
232 lines (198 loc) · 8.42 KB
/
CfnResSpecDocParser.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
220
221
222
223
224
225
226
227
228
229
230
231
232
$AWS_CFN_RES_SPEC_DOC_URL = "http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-resource-specification.html"
$DEFAULT_REGION_KEY = "US East (N. Virginia)"
function Resolve-ResourceSpecificationLinks {
<#
.SYNOPSIS
Pulls down the starting page of the Resource Specification documentation from the CFN User's Guide and
parses out the locations of the different specs (single file and ZIP of multi files) for each AWS Region.
#>
param(
[string]$DocUrl=$AWS_CFN_RES_SPEC_DOC_URL
)
$docResp = Invoke-WebRequest -Uri $AWS_CFN_RES_SPEC_DOC_URL
$docHtml = $docResp.ParsedHtml
$docTableDiv = $docHtml.getElementsByTagName('div') | ? { $_.className -eq 'table-contents' } | select -First 1
$docTableBody = $docTableDiv.getElementsByTagName('tbody') | select -First 1
$docRegionRows = $docTableBody.getElementsByTagName('tr')
$specLinksByRegion = [ordered]@{}
foreach ($tr in $docRegionRows) {
$regionName = $tr.children[0].innerText
$singleFileLink = $tr.children[1].getElementsByTagName('a')[0]
$multiFilesLink = $tr.children[2].getElementsByTagName('a')[0]
$specLinksByRegion.$regionName = [ordered]@{
SingleFileName = $singleFileLink.innerText
SingleFileLink = $singleFileLink.attributes['href'].value
MultiFilesName = $multiFilesLink.innerText
MultiFilesLink = $multiFilesLink.attributes['href'].value
}
}
return $specLinksByRegion
}
function ConvertFrom-PropertyTypeHtmlDocs {
<#
.SYNOPSIS
Pulls down the documentation page of a single Property Type from the CFN User's
Guide and parses out the first paragraph of the main description of the Property
Type, as well as the first paragraph of the description of any of its sub-properties.
#>
param(
[string]$DocUrl
)
$docResult = Invoke-WebRequest -Uri $DocUrl
$docHtml = $docResult.ParsedHtml
$docTitle = $docHtml.getElementsByTagName('h1')[0].innerText
$mainTitle = $docHtml.getElementsByTagName("div") | ? { $_.className -eq 'titlepage' } | select -First 1
$mainDesc = $mainTitle.nextSibling.innerText
$propDefList = $docHtml.getElementsByTagName("dl")[0]
$propDescs = [ordered]@{}
foreach ($dt in $propDefList.getElementsByTagName("dt")) {
$propKey = $dt.innerText
$propVal = $dt.nextSibling.getElementsByTagName("p")[0].innerText
$propDescs[$propKey] = $propVal
}
$ret = New-Object psobject
$ret | Add-Member -NotePropertyMembers ([ordered]@{
MainDescription = $mainDesc
PropertyDescriptions = $propDescs
})
return $ret
}
function ConvertFrom-ResourceTypeHtmlDocs {
<#
.SYNOPSIS
Pulls down the documentation page of a single Resource Type from the CFN User's
Guide and parses out the first paragraph of the main description of the Resource
Type, as well as the first paragraph of the description of any of its sub-properties.
#>
param(
[string]$DocUrl
)
$docResult = Invoke-WebRequest -Uri $DocUrl
$docHtml = $docResult.ParsedHtml
$docTitle = $docHtml.getElementsByTagName('h1')[0].innerText
$mainTitle = $docHtml.getElementsByTagName("div") | ? { $_.className -eq 'titlepage' } | select -First 1
$mainDesc = $mainTitle.nextSibling.innerText
$propDefList = $docHtml.getElementsByTagName("dl")[0]
$propDescs = [ordered]@{}
foreach ($dt in $propDefList.getElementsByTagName("dt")) {
$propKey = $dt.innerText
$propVal = $dt.nextSibling.getElementsByTagName("p")[0].innerText
$propDescs[$propKey] = $propVal
}
$ret = New-Object psobject
$ret | Add-Member -NotePropertyMembers ([ordered]@{
MainDescription = $mainDesc
PropertyDescriptions = $propDescs
})
return $ret
}
function Export-PropertyTypeDocItems {
<#
Given a Resource Specification description object (e.g. parsed out of JSON),
extracts all the Property Types and extracts all the summary
documentation for each Property Type and each of its sub-properties.
#>
param(
[object]$ResourcesSpecification,
[string[]]$PropTypeNames
)
$propTypeDocs = [ordered]@{}
$index = 0
foreach ($propTypeName in $PropTypeNames) {
++$index
Write-Verbose "Processing Property Type (#$index) [$propTypeName]"
$propTypeSpec = $ResourcesSpecification.PropertyTypes.$propTypeName
$docItems = ConvertFrom-PropertyTypeHtmlDocs -DocUrl $propTypeSpec.Documentation
$docJson = [ordered]@{
Name = $propTypeName
Index = $index
DocumentationUrl = $propTypeSpec.Documentation
DocumentationSummary = $docItems.MainDescription
Properties = [ordered]@{}
}
foreach ($p in ($propTypeSpec.Properties | Get-Member -MemberType NoteProperty | select -ExpandProperty Name)) {
$docJson.Properties.$p = [ordered]@{
Name = $p
DocumentationUrl = $propTypeSpec.Properties.$p.Documentation
DocumentationSummary = $docItems.PropertyDescriptions[$p]
}
}
$propTypeDocs[$propTypeName] = $docJson
}
return $propTypeDocs
}
function Export-ResourceTypeDocItems {
<#
Given a Resource Specification description object (e.g. parsed out of JSON),
extracts all the Resource Types and extracts all the summary
documentation for each Resource Type and each of its sub-properties.
#> param(
[object]$ResourcesSpecification,
[string[]]$ResTypeNames
)
$resTypeDocs = [ordered]@{}
$index = 0
foreach ($resTypeName in $ResTypeNames) {
++$index
Write-Verbose "Processing Resource Type (#$index) [$resTypeName]"
$resTypeSpec = $ResourcesSpecification.ResourceTypes.$resTypeName
$docItems = ConvertFrom-ResourceTypeHtmlDocs -DocUrl $resTypeSpec.Documentation
$docJson = [ordered]@{
Name = $resTypeName
Index = $index
DocumentationUrl = $resTypeSpec.Documentation
DocumentationSummary = $docItems.MainDescription
Properties = [ordered]@{}
}
foreach ($p in ($resTypeSpec.Properties | Get-Member -MemberType NoteProperty | select -ExpandProperty Name)) {
$docJson.Properties.$p = [ordered]@{
Name = $p
DocumentationUrl = $resTypeSpec.Properties.$p.Documentation
DocumentationSummary = $docItems.PropertyDescriptions[$p]
}
}
$resTypeDocs.$resTypeName = $docJson
}
return $resTypeDocs
}
function Export-ResourceSpecificationDocItems {
<#
.SYNOPSIS
Resolves the current AWS CloudFormationb Resource Specification and extracts
the current summary documentation for each Property Type and Resource Type.
#>
[CmdletBinding()]
param(
[string]$ResSpecFile="$PSScriptRoot\CfnResSpec.json",
[string]$DocUrl=$AWS_CFN_RES_SPEC_DOC_URL,
[string]$DocRegion=$DEFAULT_REGION_KEY,
[switch]$ForceDocFetch
)
if ($ForceDocFetch -or -not (Test-Path $ResSpecFile)) {
Write-Verbose "Fetching latest Resource Specification for Region [$DocRegion]"
$resSpecLinks = Resolve-ResourceSpecificationLinks -DocUrl $DocUrl
Write-Verbose " saving to local file [$ResSpecFile]"
Invoke-WebRequest $resSpecLinks[$DocRegion].SingleFileLink -OutFile $ResSpecFile
}
else {
Write-Verbose "Found and using locally cached Resource Specification [$ResSpecFile]"
}
$resSpecRaw = [System.IO.File]::ReadAllText($ResSpecFile)
$resSpec = ConvertFrom-Json $resSpecRaw
$resSpecVersion = $resSpec.ResourceSpecificationVersion
$propTypeNames = $resSpec.PropertyTypes | Get-Member -MemberType NoteProperty | select -ExpandProperty Name
$resTypeNames = $resSpec.ResourceTypes | Get-Member -MemberType NoteProperty | select -ExpandProperty Name
Write-Verbose "Found [$($propTypeNames.Count)] Property Types"
Write-Verbose "Found [$($resTypeNames.Count)] Resource Types"
$propTypeDocs = Export-PropertyTypeDocItems -ResourcesSpecification $resSpec -PropTypeNames $propTypeNames
$resTypeDocs = Export-ResourceTypeDocItems -ResourcesSpecification $resSpec -ResTypeNames $resTypeNames
return [ordered]@{
ResourceSpecificationVersion = $resSpecVersion
ResourceSpecificationDocumentationUrl = $DocUrl
PropertyTypes = $propTypeDocs
ResourceTypes = $resTypeDocs
}
}
## For testing:
#Export-ResourceSpecificationDocItems -Verbose | ConvertTo-Json -Depth 100