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

feat: Reduce the number of web-request by prefetching more data #809

Merged
merged 82 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
20ac8b7
Added skeleton
AlexanderSehr Nov 7, 2023
4939b4d
Implemented first new tests
AlexanderSehr Nov 7, 2023
4e1d86f
Changed to warning
AlexanderSehr Nov 8, 2023
94fff0b
Expanded test verbosity & added additional tets
AlexanderSehr Nov 8, 2023
c18ce6b
Finalized further tests
AlexanderSehr Nov 8, 2023
a343694
Update to latest
AlexanderSehr Nov 8, 2023
3a6f0a2
Update to latest
AlexanderSehr Nov 8, 2023
4ab6a5b
Added module name + small fix
AlexanderSehr Nov 8, 2023
9bb64bd
Fixed kvlt
AlexanderSehr Nov 8, 2023
68e2bd2
Small caching fix
AlexanderSehr Nov 8, 2023
1a013c2
Small fix and enabled passthru of warnings to GH
AlexanderSehr Nov 8, 2023
1c2fc8a
More updates
AlexanderSehr Nov 8, 2023
d9c6dd5
Update to latest
AlexanderSehr Nov 8, 2023
dd0c343
Updated test names
AlexanderSehr Nov 8, 2023
2ebd458
JSON rollback
AlexanderSehr Nov 8, 2023
f6caf7e
Updated regex
AlexanderSehr Nov 8, 2023
f56f66a
Removed assertion
AlexanderSehr Nov 8, 2023
282b640
Update to latest
AlexanderSehr Nov 8, 2023
c025f52
Update avm/utilities/pipelines/staticValidation/compliance/module.tes…
AlexanderSehr Nov 9, 2023
405901a
Update avm/utilities/pipelines/staticValidation/compliance/module.tes…
AlexanderSehr Nov 9, 2023
904c82f
Merge branch 'main' into users/alsehr/testFileCompliance
AlexanderSehr Nov 9, 2023
ef24d1f
Rollback of unrelated changes
AlexanderSehr Nov 9, 2023
9393aa1
Merge branch 'Azure:main' into main
AlexanderSehr Nov 9, 2023
cb39086
Merge branch 'Azure:main' into main
AlexanderSehr Nov 9, 2023
89eac40
Merge branch 'Azure:main' into main
AlexanderSehr Nov 10, 2023
712a673
Merge branch 'Azure:main' into main
AlexanderSehr Nov 13, 2023
55e095f
Resolved conflict
AlexanderSehr Nov 14, 2023
3121335
Update to latest
AlexanderSehr Nov 14, 2023
e9d736c
Merge branch 'Azure:main' into main
AlexanderSehr Nov 15, 2023
abfa1b9
Merge branch 'Azure:main' into main
AlexanderSehr Nov 15, 2023
c6707f6
Merge branch 'Azure:main' into main
AlexanderSehr Nov 16, 2023
246f9ba
Merge branch 'Azure:main' into main
AlexanderSehr Nov 16, 2023
6e9729d
Merge branch 'Azure:main' into main
AlexanderSehr Nov 16, 2023
26ea9f6
Merge branch 'Azure:main' into main
AlexanderSehr Nov 17, 2023
c2d183b
Merge branch 'Azure:main' into main
AlexanderSehr Nov 18, 2023
0c6650e
Merge branch 'Azure:main' into main
AlexanderSehr Nov 19, 2023
65094a5
Merge branch 'Azure:main' into main
AlexanderSehr Nov 21, 2023
968cd9a
Merge branch 'Azure:main' into main
AlexanderSehr Nov 21, 2023
3c9589b
Merge branch 'Azure:main' into main
AlexanderSehr Nov 24, 2023
eb34ddf
Merge branch 'Azure:main' into main
AlexanderSehr Nov 25, 2023
3d4f5c6
Added pipeline to main for testing
AlexanderSehr Nov 26, 2023
70154f3
Merge branch 'Azure:main' into main
AlexanderSehr Nov 27, 2023
18bd2ad
Merge branch 'Azure:main' into main
AlexanderSehr Nov 27, 2023
940b34f
Merge branch 'Azure:main' into main
AlexanderSehr Nov 28, 2023
fbb840f
Merge branch 'Azure:main' into main
AlexanderSehr Nov 29, 2023
381e366
Merge branch 'Azure:main' into main
AlexanderSehr Nov 29, 2023
06bcda3
Merge branch 'Azure:main' into main
AlexanderSehr Dec 1, 2023
0858beb
Merge branch 'Azure:main' into main
AlexanderSehr Dec 1, 2023
72b5fbb
Merge branch 'Azure:main' into main
AlexanderSehr Dec 1, 2023
917a761
TEst disable pipe
AlexanderSehr Dec 4, 2023
1b89452
Merge branch 'Azure:main' into main
AlexanderSehr Dec 4, 2023
feb981e
Update to latest
AlexanderSehr Dec 4, 2023
dda85a8
Merge branch 'Azure:main' into main
AlexanderSehr Dec 4, 2023
0373f46
Merge branch 'Azure:main' into main
AlexanderSehr Dec 6, 2023
80247e4
Merge branch 'Azure:main' into main
AlexanderSehr Dec 9, 2023
2b5a791
Merge branch 'Azure:main' into main
AlexanderSehr Dec 10, 2023
f1bab34
Merge branch 'Azure:main' into main
AlexanderSehr Dec 19, 2023
ef12ee5
Merge branch 'Azure:main' into main
AlexanderSehr Dec 21, 2023
ec1722c
Merge branch 'Azure:main' into main
AlexanderSehr Dec 21, 2023
f4136ec
Added SA workflow
AlexanderSehr Dec 22, 2023
e55e732
Added workflow
AlexanderSehr Jan 1, 2024
5c6aa5e
Update to latest
AlexanderSehr Jan 1, 2024
086c1a8
Merge branch 'Azure:main' into main
AlexanderSehr Jan 2, 2024
a9821fb
Merge branch 'Azure:main' into main
AlexanderSehr Jan 3, 2024
62b326f
Merge branch 'Azure:main' into main
AlexanderSehr Jan 3, 2024
c159269
Merge branch 'Azure:main' into main
AlexanderSehr Jan 4, 2024
6e1f59a
Merge branch 'Azure:main' into main
AlexanderSehr Jan 5, 2024
fb7befc
Merge branch 'Azure:main' into main
AlexanderSehr Jan 5, 2024
cb72681
Merge branch 'Azure:main' into main
AlexanderSehr Jan 8, 2024
f3befb7
Merge branch 'Azure:main' into main
AlexanderSehr Jan 10, 2024
f938cd8
Merge branch 'Azure:main' into main
AlexanderSehr Jan 12, 2024
0e90ba0
Improved runtime
AlexanderSehr Jan 14, 2024
96b81a0
Update to latest
AlexanderSehr Jan 14, 2024
d92a1ab
Update to latest
AlexanderSehr Jan 14, 2024
25a887b
Fixed for ptn
AlexanderSehr Jan 14, 2024
f387359
Update to latest
AlexanderSehr Jan 14, 2024
d0924b6
Merge branch 'Azure:main' into main
AlexanderSehr Jan 14, 2024
0231efa
Merge branch 'main' into users/alsehr/avmRequestReduce
AlexanderSehr Jan 14, 2024
978252c
Fixed small test issue
AlexanderSehr Jan 14, 2024
20a24a3
Fixed typos
AlexanderSehr Jan 15, 2024
3c53941
Merge branch 'main' into users/alsehr/avmRequestReduce
AlexanderSehr Jan 15, 2024
906480a
Merge branch 'main' into users/alsehr/avmRequestReduce
ChrisSidebotham Jan 17, 2024
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
101 changes: 62 additions & 39 deletions avm/utilities/pipelines/sharedScripts/Set-ModuleReadMe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,12 @@ Mandatory. The readme file content array to update
.PARAMETER SectionStartIdentifier
Optional. The identifier of the section. Defaults to '## Data Collection'

.PARAMETER PreLoadedContent
Optional. Pre-Loaded content. May be used to reuse the same data for multiple invocations. For example:
@{
TelemetryFileContent = @() // Optional. The text of the telemetry notice to add to each readme.
}

.EXAMPLE
Set-DataCollectionSection -ReadMeFileContent @('# Title', '', '## Section 1', ...)

Expand All @@ -518,26 +524,34 @@ function Set-DataCollectionSection {
[Parameter(Mandatory)]
[object[]] $ReadMeFileContent,

[Parameter(Mandatory = $false)]
[hashtable] $PreLoadedContent = @{},

[Parameter(Mandatory = $false)]
[string] $SectionStartIdentifier = '## Data Collection'
)

# Load content, if required
if ($PreLoadedContent.Keys -notcontains 'TelemetryFileContent') {

$telemetryUrl = 'https://aka.ms/avm/static/telemetry'
try {
$rawReponse = Invoke-WebRequest -Uri $telemetryUrl
if (($rawReponse.Headers['Content-Type'] | Out-String) -like "*text/plain*") {
$telemetryInfoContent = $rawReponse.Content -split '\n'
} else {
throw "Failed to telemetry information from [$telemetryUrl]." # Incorrect Url (e.g., points to HTML)
$telemetryUrl = 'https://aka.ms/avm/static/telemetry'
try {
$rawResponse = Invoke-WebRequest -Uri $telemetryUrl
if (($rawResponse.Headers['Content-Type'] | Out-String) -like "*text/plain*") {
$telemetryFileContent = $rawResponse.Content -split '\n'
} else {
throw "Failed to telemetry information from [$telemetryUrl]." # Incorrect Url (e.g., points to HTML)
}
} catch {
throw "Failed to telemetry information from [$telemetryUrl]." # Invalid url
}
} catch {
throw "Failed to telemetry information from [$telemetryUrl]." # Invalid url
} else {
$telemetryFileContent = $PreLoadedContent.TelemetryFileContent
}

# Build result
if ($PSCmdlet.ShouldProcess('Original file with new output content', 'Merge')) {
$updatedFileContent = Merge-FileWithNewContent -oldContent $ReadMeFileContent -newContent $telemetryInfoContent -SectionStartIdentifier $SectionStartIdentifier -contentType 'nextH2'
$updatedFileContent = Merge-FileWithNewContent -oldContent $ReadMeFileContent -newContent $telemetryFileContent -SectionStartIdentifier $SectionStartIdentifier -contentType 'nextH2'
}
return $updatedFileContent
}
Expand All @@ -564,11 +578,14 @@ Mandatory. The readme file content array to update
.PARAMETER SectionStartIdentifier
Optional. The identifier of the 'outputs' section. Defaults to '## Cross-referenced modules'

.PARAMETER CrossReferencedModuleList
Required. The Cross Module References to consider when refreshing the readme.
.PARAMETER PreLoadedContent
Optional. Pre-Loaded content. May be used to reuse the same data for multiple invocations. For example:
@{
CrossReferencedModuleList = @{} // Optional. Cross Module References to consider when refreshing the readme. Can be provided to speed up the generation. If not provided, is fetched by this script.
}

.EXAMPLE
Set-CrossReferencesSection -ModuleRoot 'C:/key-vault/vault' -FullModuleIdentifier 'key-vault/vault' -TemplateFileContent @{ resource = @{}; ... } -ReadMeFileContent @('# Title', '', '## Section 1', ...) -CrossReferencedModuleList @{}
Set-CrossReferencesSection -ModuleRoot 'C:/key-vault/vault' -FullModuleIdentifier 'key-vault/vault' -TemplateFileContent @{ resource = @{}; ... } -ReadMeFileContent @('# Title', '', '## Section 1', ...) -PreLoadedContent @{ CrossReferencedModuleList = @{ ... } }
Update the given readme file's 'Cross-referenced modules' section based on the given template file content
#>
function Set-CrossReferencesSection {
Expand All @@ -587,13 +604,20 @@ function Set-CrossReferencesSection {
[Parameter(Mandatory)]
[object[]] $ReadMeFileContent,

[Parameter(Mandatory)]
[hashtable] $CrossReferencedModuleList,
[Parameter(Mandatory = $false)]
[hashtable] $PreLoadedContent = @{},

[Parameter(Mandatory = $false)]
[string] $SectionStartIdentifier = '## Cross-referenced modules'
)

# Load content, if required
if ($PreLoadedContent.Keys -notcontains 'CrossReferencedModuleList') {
$CrossReferencedModuleList = Get-CrossReferencedModuleList
} else {
$CrossReferencedModuleList = $PreLoadedContent.CrossReferencedModuleList
}

# Process content
$SectionContent = [System.Collections.ArrayList]@(
'This section gives you an overview of all local-referenced module files (i.e., other CARML modules that are referenced in this module) and all remote-referenced files (i.e., Bicep modules that are referenced from a Bicep Registry or Template Specs).',
Expand Down Expand Up @@ -1574,19 +1598,20 @@ Supports both ARM & bicep templates.
.PARAMETER TemplateFilePath
Mandatory. The path to the template to update

.PARAMETER TemplateFileContent
Optional. The template file content to process. If not provided, the template file content will be read from the TemplateFilePath file.
Using this property is useful if you already compiled the bicep template before invoking this function and want to avoid re-compiling it.

.PARAMETER ReadMeFilePath
Optional. The path to the readme to update. If not provided assumes a 'README.md' file in the same folder as the template

.PARAMETER SectionsToRefresh
Optional. The sections to update. By default it refreshes all that are supported.
Currently supports: 'Resource Types', 'Parameters', 'Outputs', 'Template references'

.PARAMETER CrossReferencedModuleList
Optional. Cross Module References to consider when refreshing the readme. Can be provided to speed up the generation. If not provided, is fetched by this script.
.PARAMETER PreLoadedContent
Optional. Pre-Loaded content. May be used to reuse the same data for multiple invocations. For example:
@{
CrossReferencedModuleList = @{} // Optional. Cross Module References to consider when refreshing the readme. Can be provided to speed up the generation. If not provided, is fetched by this script.
TemplateFileContent = @{} // Optional. The template file content to process. If not provided, the template file content will be read from the TemplateFilePath file.
TelemetryFileContent = @() // Optional. The text of the telemetry notice to add to each readme.
}

.EXAMPLE
Set-ModuleReadMe -TemplateFilePath 'C:\main.bicep'
Expand All @@ -1599,9 +1624,9 @@ Set-ModuleReadMe -TemplateFilePath 'C:/network/load-balancer/main.bicep' -Sectio
Generate the Module ReadMe only for specific sections. Updates only the sections `Parameters` & `Outputs`. Other sections remain untouched.

.EXAMPLE
Set-ModuleReadMe -TemplateFilePath 'C:/network/load-balancer/main.bicep' -TemplateFileContent @{...}
Set-ModuleReadMe -TemplateFilePath 'C:/network/load-balancer/main.bicep' -PreLoadedContent @{ TemplateFileContent = @{...} }

(Re)Generate the readme file for template 'loadBalancer' based on the content provided in the TemplateFileContent parameter
(Re)Generate the readme file for template 'loadBalancer' based on the content provided in the PreLoadedContent.TemplateFileContent parameter

.EXAMPLE
Set-ModuleReadMe -TemplateFilePath 'C:/network/load-balancer/main.bicep' -ReadMeFilePath 'C:/differentFolder'
Expand All @@ -1618,17 +1643,14 @@ function Set-ModuleReadMe {

[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Mandatory)]
[Parameter(Mandatory = $true)]
[string] $TemplateFilePath,

[Parameter(Mandatory = $false)]
[hashtable] $TemplateFileContent,

[Parameter(Mandatory = $false)]
[string] $ReadMeFilePath = (Join-Path (Split-Path $TemplateFilePath -Parent) 'README.md'),

[Parameter(Mandatory = $false)]
[hashtable] $CrossReferencedModuleList = @{},
[hashtable] $PreLoadedContent = @{},

[Parameter(Mandatory = $false)]
[ValidateSet(
Expand Down Expand Up @@ -1660,6 +1682,7 @@ function Set-ModuleReadMe {
. (Join-Path $PSScriptRoot 'helper' 'Get-SpecsAlignedResourceName.ps1')
. (Join-Path $PSScriptRoot 'helper' 'ConvertTo-OrderedHashtable.ps1')
. (Join-Path $PSScriptRoot 'Get-BRMRepositoryName.ps1')
. (Join-Path $PSScriptRoot 'helper' 'Get-CrossReferencedModuleList.ps1')

# Check template & make full path
$TemplateFilePath = Resolve-Path -Path $TemplateFilePath -ErrorAction Stop
Expand All @@ -1668,20 +1691,23 @@ function Set-ModuleReadMe {
throw "[$TemplateFilePath] is no valid file path."
}

if (-not $TemplateFileContent) {
# Build template, if required
if ($PreLoadedContent.Keys -notcontains 'TemplateFileContent') {
if ((Split-Path -Path $TemplateFilePath -Extension) -eq '.bicep') {
$templateFileContent = bicep build $TemplateFilePath --stdout | ConvertFrom-Json -AsHashtable
} else {
$templateFileContent = ConvertFrom-Json (Get-Content $TemplateFilePath -Encoding 'utf8' -Raw) -ErrorAction 'Stop' -AsHashtable
}
} else {
$templateFileContent = $PreLoadedContent.TemplateFileContent
}

if (-not $templateFileContent) {
throw "Failed to compile [$TemplateFilePath]"
}

$moduleRoot = Split-Path $TemplateFilePath -Parent
$fullModuleIdentifier = $moduleRoot.Replace('\', '/').split('res/')[-1]
$fullModuleIdentifier = ($moduleRoot -split '[\/|\\]{1}avm[\/|\\]{1}(res|ptn)[\/|\\]{1}')[2] -replace '\\', '/'
# Custom modules are modules having the same resource type but different properties based on the name
# E.g., web/site/config--appsetting vs web/site/config--authsettingv2
$customModuleSeparator = '--'
Expand Down Expand Up @@ -1769,16 +1795,12 @@ function Set-ModuleReadMe {
if ($SectionsToRefresh -contains 'CrossReferences') {
# Handle [CrossReferences] section
# ========================
if ($CrossReferencedModuleList.Count -eq 0) {
. (Join-Path $PSScriptRoot 'helper' 'Get-CrossReferencedModuleList.ps1')
$CrossReferencedModuleList = Get-CrossReferencedModuleList
}
$inputObject = @{
ModuleRoot = $ModuleRoot
FullModuleIdentifier = $fullModuleIdentifier
ReadMeFileContent = $readMeFileContent
TemplateFileContent = $templateFileContent
CrossReferencedModuleList = $CrossReferencedModuleList
ModuleRoot = $ModuleRoot
FullModuleIdentifier = $fullModuleIdentifier
ReadMeFileContent = $readMeFileContent
TemplateFileContent = $templateFileContent
PreLoadedContent = $PreLoadedContent
}
$readMeFileContent = Set-CrossReferencesSection @inputObject
}
Expand All @@ -1795,6 +1817,7 @@ function Set-ModuleReadMe {
# ========================
$inputObject = @{
ReadMeFileContent = $readMeFileContent
PreLoadedContent = $PreLoadedContent
}
$readMeFileContent = Set-DataCollectionSection @inputObject
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ Describe 'Module tests' -Tag 'Module' {
$templateFilePath = Join-Path $moduleFolderPath 'main.bicep'

$readmeFileTestCases += @{
moduleFolderName = $resourceTypeIdentifier
templateContent = $builtTestFileMap[$templateFilePath]
templateFilePath = $templateFilePath
readMeFilePath = Join-Path -Path $moduleFolderPath 'README.md'
moduleFolderName = $resourceTypeIdentifier
templateFileContent = $builtTestFileMap[$templateFilePath]
templateFilePath = $templateFilePath
readMeFilePath = Join-Path -Path $moduleFolderPath 'README.md'
}
}

Expand All @@ -307,7 +307,7 @@ Describe 'Module tests' -Tag 'Module' {
. (Join-Path $repoRootPath 'avm' 'utilities' 'pipelines' 'sharedScripts' 'Set-ModuleReadMe.ps1')

# Apply update with already compiled template content
Set-ModuleReadMe -TemplateFilePath $templateFilePath -TemplateFileContent $templateFileContent
Set-ModuleReadMe -TemplateFilePath $templateFilePath -PreLoadedContent @{ TemplateFileContent = $templateFileContent }

# Get hash after 'update'
$fileHashAfter = (Get-FileHash $readMeFilePath).Hash
Expand Down Expand Up @@ -704,9 +704,9 @@ Describe 'Module tests' -Tag 'Module' {
$implementedSchema = $templateFileContentBicep[$implementedSchemaStartIndex..$implementedSchemaEndIndex]

try {
$rawReponse = Invoke-WebRequest -Uri $expectedUdtUrl
if (($rawReponse.Headers['Content-Type'] | Out-String) -like "*text/plain*") {
$expectedSchemaFull = $rawReponse.Content -split '\n'
$rawResponse = Invoke-WebRequest -Uri $expectedUdtUrl
if (($rawResponse.Headers['Content-Type'] | Out-String) -like "*text/plain*") {
$expectedSchemaFull = $rawResponse.Content -split '\n'
} else {
throw "Failed to fetch schema from [$expectedUdtUrl]. Skipping schema check"
}
Expand Down
29 changes: 25 additions & 4 deletions avm/utilities/tools/Set-AVMModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ function Set-AVMModule {
# load cross-references
$crossReferencedModuleList = Get-CrossReferencedModuleList

# load AVM references (done to reduce WebRequests to GitHub repository)
# Telemetry
$telemetryUrl = 'https://aka.ms/avm/static/telemetry'
try {
$rawResponse = Invoke-WebRequest -Uri $telemetryUrl
if (($rawResponse.Headers['Content-Type'] | Out-String) -like "*text/plain*") {
$TelemetryFileContent = $rawResponse.Content -split '\n'
} else {
throw "Failed to telemetry information from [$telemetryUrl]." # Incorrect Url (e.g., points to HTML)
}
} catch {
throw "Failed to telemetry information from [$telemetryUrl]." # Invalid url
}

# create reference as it must be loaded in the thread to work
$ReadMeScriptFilePath = (Join-Path (Get-Item $PSScriptRoot).Parent.FullName 'pipelines' 'sharedScripts' 'Set-ModuleReadMe.ps1')
} else {
Expand Down Expand Up @@ -137,11 +151,18 @@ function Set-AVMModule {
if (-not $using:SkipReadMe) {
Write-Output "Generating readme for [$resourceTypeIdentifier]"

# If the template was just build, we can pass the JSON into the readme script to be more efficient
$readmeTemplateFilePath = (-not $using:SkipBuild) ? (Join-Path (Split-Path $_ -Parent) 'main.json') : $_

. $using:ReadMeScriptFilePath
Set-ModuleReadMe -TemplateFilePath $readmeTemplateFilePath -CrossReferencedModuleList $using:crossReferencedModuleList
$readmeInputObject = @{
TemplateFilePath = $_
PreLoadedContent = @{
CrossReferencedModuleList = $using:crossReferencedModuleList
TelemetryFileContent = $using:TelemetryFileContent
} + (-not $using:SkipBuild ? @{
# If the template was just build, we can pass the JSON into the readme script to be more efficient
TemplateFileContent = ConvertFrom-Json (Get-Content (Join-Path (Split-Path $_ -Parent) 'main.json') -Encoding 'utf8' -Raw) -ErrorAction 'Stop' -AsHashtable
} : @{})
}
Set-ModuleReadMe @readmeInputObject
}
}

Expand Down