Skip to content

Commit

Permalink
Simpliefied reference logic
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderSehr committed Sep 22, 2023
1 parent f77cc66 commit 566064e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 90 deletions.
4 changes: 4 additions & 0 deletions avm/res/key-vault/vault/secret/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ resource secretVault_roleAssignments 'Microsoft.Authorization/roleAssignments@20
scope: secret
}]

module dummy 'br/public:ai/cognitiveservices:1.1.1' = {
name: 'test'
}

@description('The name of the secret.')
output name string = secret.name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ function Publish-ModuleFromPathToPBR {
Tokens = @{
'moduleVersion' = $targetVersion
}
TokenPrefix = '#_'
TokenSuffix = '_#'
}
$null = Convert-TokensInFileList @tokenConfiguration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ function Publish-ModuleFromTagToPBR {
Tokens = @{
'moduleVersion' = $targetVersion
}
TokenPrefix = '#_'
TokenSuffix = '_#'
}
$null = Convert-TokensInFileList @tokenConfiguration

Expand Down
4 changes: 2 additions & 2 deletions avm/utilities/pipelines/sharedScripts/Set-ModuleReadMe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ function Set-UsageExamplesSection {
)

# Load used function(s)
. '.\Get-ModuleTestFileList.ps1'
. (Join-Path $PSScriptRoot 'Get-ModuleTestFileList.ps1')
. (Join-Path (Split-Path $PSScriptRoot -Parent) 'resourcePublish' 'helper' 'Get-BRMRepositoryName.ps1')

$brLink = Get-BRMRepositoryName -TemplateFilePath $TemplateFilePath
Expand Down Expand Up @@ -1682,7 +1682,7 @@ function Set-ModuleReadMe {

# Load external functions
. (Join-Path $PSScriptRoot 'helper' 'Merge-FileWithNewContent.ps1')
. '.\Get-NestedResourceList.ps1'
. (Join-Path $PSScriptRoot 'Get-NestedResourceList.ps1')

# Check template & make full path
$TemplateFilePath = Resolve-Path -Path $TemplateFilePath -ErrorAction Stop
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#region helper functions
<#
.SYNOPSIS
Find any nested dependency recursively
Expand Down Expand Up @@ -40,32 +41,49 @@ function Resolve-DependencyList {
return $resolvedDependencies
}

function Get-ReferenceObject {

[CmdletBinding()]
param (
[Parameter()]
[string] $ModuleTemplateFilePath
)

$moduleContent = Get-Content -Path $ModuleTemplateFilePath

$resourceReferences = $moduleContent | Where-Object { $_ -match "^resource .+ '(.+)' .+$" } | ForEach-Object { $matches[1] }
$remoteReferences = $moduleContent | Where-Object { $_ -match "^module .+ '(.+:.+)' .+$" } | ForEach-Object { $matches[1] }

return @{
resourceReferences = @() + ($resourceReferences | Select-Object -Unique)
remoteReferences = @() + ($remoteReferences | Select-Object -Unique)
}
}
#endregion

<#
.SYNOPSIS
Get a list of all resource/module references in a given module path
.DESCRIPTION
As an output you will receive a hashtable that (for each provider namespace) lists the
- Directly deployed resources (e.g. via "resource myDeployment 'Microsoft.(..)/(..)@(..)'")
- Linked local module templates (e.g. via "module myDeployment '../../main.bicep'")
- Linked remote module tempaltes (e.g. via "module rg 'br/modules:(..):(..)'")
.PARAMETER Path
Optional. The path to search in. Defaults to the 'res' folder.
Note, any local references will only be searched within this path too.
.EXAMPLE
Get-CrossReferencedModuleList
Invoke the function with the default path. Returns an object such as:
{
"Compute/availabilitySets": {
"localPathReferences": [
recovery-service/vault/protection-container/protected-item
network/public-ip-address
network/network-interface
"remoteReferences": [
"avm-res-recoveryservice-vault-protectioncontainer-protecteditem",
"avm-res-network-publicipaddress",
"avm-res-network-networkinterface"
],
"remoteReferences": null,
"resourceReferences": [
"Microsoft.Resources/deployments@2021-04-01",
"Microsoft.Compute/availabilitySets@2021-07-01",
Expand All @@ -87,93 +105,34 @@ function Get-CrossReferencedModuleList {
[CmdletBinding()]
param (
[Parameter()]
[string] $Path = (Join-Path (Split-Path (Split-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) -Parent) -Parent) 'res')
[string] $Path = (Get-Item $PSScriptRoot).Parent.Parent.Parent.Parent
)

$resultSet = [ordered]@{}

# Get all top-level module folders (i.e. one level below the Resource Provider folder)
$topLevelFolderPaths = (Get-ChildItem -Path $path -Depth 1 -Directory).FullName | Where-Object {
$_ -notlike '*.shared*' -and # Ignore shared templates
(($_ -split '[\\|\/]res[\\|\/]')[1] -split '[\\|/]').Count -eq 2 # From '/res/' only consider those which have 2 more path elements (i.e., are resource type folders)
}
$moduleTemplatePaths = (Get-ChildItem -Path $path -Recurse -File -Filter 'main.bicep').FullName
foreach ($moduleTemplatePath in $moduleTemplatePaths) {

foreach ($topLevelFolderPath in $topLevelFolderPaths) {
$referenceObject = Get-ReferenceObject -ModuleTemplateFilePath $moduleTemplatePath

$moduleTemplatePaths = (Get-ChildItem -Path $topLevelFolderPath -Recurse -Include '*.bicep' -File -Force).FullName | Where-Object {
$_ -notmatch '.+[\/|\\]tests[\/|\\].+'
}
# Add additional level of nesting for children of template path
$childModuleTemplatePaths = (Get-ChildItem -Path $moduleTemplatePath -Recurse -File -Filter 'main.bicep').FullName | Where-Object { $_ -ne $moduleTemplatePath }
foreach ($childModuleTemplatePath in $childModuleTemplatePaths) {
$childReferenceObject = Get-ReferenceObject -ModuleTemplateFilePath $childModuleTemplatePath

$resourceReferences = [System.Collections.ArrayList]@()
$localPathReferences = [System.Collections.ArrayList]@()
$remoteReferences = [System.Collections.ArrayList]@()

foreach ($templatePath in $moduleTemplatePaths) {
$content = Get-Content -Path $templatePath

$resourceReferences += $content | Where-Object { $_ -match "^resource .+ '(.+)' .+$" } | ForEach-Object { $matches[1] }
$localPathReferences += $content | Where-Object { $_ -match "^module .+ '(.+.bicep)' .+$" } | ForEach-Object { $matches[1] }
$remoteReferences += $content | Where-Object { $_ -match "^module .+ '(.+:.+)' .+$" } | ForEach-Object { $matches[1] }
}

$providerNamespace = Split-Path (Split-Path $topLevelFolderPath -Parent) -Leaf
$resourceType = Split-Path $topLevelFolderPath -Leaf

$resultSet["$providerNamespace/$resourceType"] = @{
resourceReferences = $resourceReferences | Select-Object -Unique
localPathReferences = $localPathReferences | Select-Object -Unique
remoteReferences = $remoteReferences | Select-Object -Unique
$referenceObject.resourceReferences = ($referenceObject.resourceReferences + $childReferenceObject.resourceReferences) | Select-Object -Unique
$referenceObject.remoteReferences += ($referenceObject.remoteReferences + $childReferenceObject.remoteReferences) | Select-Object -Unique
}
}

# Expand local references recursively
$localReferencesResultSet = [ordered]@{}
foreach ($resourceType in ($resultSet.Keys | Sort-Object)) {
$relevantLocalReferences = $resultSet[$resourceType].localPathReferences | Where-Object { $_ -match '^\.\..*$' } # e.g. '../
if ($relevantLocalReferences) {
$relevantLocalReferences = $relevantLocalReferences | ForEach-Object {
# remove main.bicep
Split-Path $_ -Parent
} | ForEach-Object {
# remove leading path elements
($_ -replace '\\', '/') -match '^[\.\/]*(.+)$'
} | ForEach-Object {
# We have to differentate the case that the referenced resources is inside or outside the same provider namespace (e.g. '../public-ip-address')
if ($matches[1] -like '*/*') {
# Reference outside of namespace
$matches[1]
}
else {
# Reference inside of namespace (-> we rebuild the namespace)
'{0}/{1}' -f (Split-Path $resourceType -Parent), $matches[1]
}
}
$localReferencesResultSet[$resourceType] = @() + $relevantLocalReferences
}
}
$moduleFolderPath = Split-Path $moduleTemplatePath -Parent
## avm/res/<provider>/<resourceType>
$resourceTypeIdentifier = ($moduleFolderPath -split '[\/|\\]{1}avm[\/|\\]{1}(res|ptn)[\/|\\]{1}')[2] -replace '\\', '/'

# Add nested dependencies
$resolvedlocalReferencesResultSet = @{}
foreach ($resourceType in $localReferencesResultSet.Keys) {
$resolvedDependencies = $localReferencesResultSet[$resourceType]
foreach ($dependency in $localReferencesResultSet[$resourceType]) {
$resolvedDependencies += Resolve-DependencyList -DependencyMap $localReferencesResultSet -ResourceType $resourceType
}
$resolvedlocalReferencesResultSet[$resourceType] = $resolvedDependencies | Select-Object -Unique
}
$localReferencesResultSet = $resolvedlocalReferencesResultSet
$providerNamespace = ($resourceTypeIdentifier -split '[\/|\\]')[0]
$resourceType = $resourceTypeIdentifier -replace "$providerNamespace[\/|\\]", ''

foreach ($resourceType in ($resultSet.Keys | Sort-Object)) {
$resultSet[$resourceType].localPathReferences = $resolvedlocalReferencesResultSet[$resourceType]
$resultSet["$providerNamespace/$resourceType"] = $referenceObject
}

Write-Verbose "The modules in path [$path] have the following local folder dependencies:"
foreach ($resourceType in $resolvedlocalReferencesResultSet.Keys) {
Write-Verbose ''
Write-Verbose "Resource: $resourceType"
$resolvedlocalReferencesResultSet[$resourceType] | ForEach-Object {
Write-Verbose "- $_"
}
}
return $resultSet
}
2 changes: 0 additions & 2 deletions avm/utilities/tools/Test-ModuleLocally.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ function Test-ModuleLocally {
$tokenConfiguration = @{
FilePathList = $moduleTestFiles
Tokens = @{}
TokenPrefix = '#_'
TokenSuffix = '_#'
}

# Add Other Parameter File Tokens (For Testing)
Expand Down

0 comments on commit 566064e

Please sign in to comment.