diff --git a/avm/res/key-vault/vault/secret/main.bicep b/avm/res/key-vault/vault/secret/main.bicep index f5e3cc6e44..3c87ff9b24 100644 --- a/avm/res/key-vault/vault/secret/main.bicep +++ b/avm/res/key-vault/vault/secret/main.bicep @@ -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 diff --git a/avm/utilities/pipelines/resourcePublish/Publish-ModuleFromPathToPBR.ps1 b/avm/utilities/pipelines/resourcePublish/Publish-ModuleFromPathToPBR.ps1 index 32de4894cb..955176e3e7 100644 --- a/avm/utilities/pipelines/resourcePublish/Publish-ModuleFromPathToPBR.ps1 +++ b/avm/utilities/pipelines/resourcePublish/Publish-ModuleFromPathToPBR.ps1 @@ -48,8 +48,6 @@ function Publish-ModuleFromPathToPBR { Tokens = @{ 'moduleVersion' = $targetVersion } - TokenPrefix = '#_' - TokenSuffix = '_#' } $null = Convert-TokensInFileList @tokenConfiguration diff --git a/avm/utilities/pipelines/resourcePublish/Publish-ModuleFromTagToPBR.ps1 b/avm/utilities/pipelines/resourcePublish/Publish-ModuleFromTagToPBR.ps1 index ea638cbde7..5f883096ab 100644 --- a/avm/utilities/pipelines/resourcePublish/Publish-ModuleFromTagToPBR.ps1 +++ b/avm/utilities/pipelines/resourcePublish/Publish-ModuleFromTagToPBR.ps1 @@ -36,8 +36,6 @@ function Publish-ModuleFromTagToPBR { Tokens = @{ 'moduleVersion' = $targetVersion } - TokenPrefix = '#_' - TokenSuffix = '_#' } $null = Convert-TokensInFileList @tokenConfiguration diff --git a/avm/utilities/pipelines/sharedScripts/Set-ModuleReadMe.ps1 b/avm/utilities/pipelines/sharedScripts/Set-ModuleReadMe.ps1 index 923ea25d89..d01840b2d1 100644 --- a/avm/utilities/pipelines/sharedScripts/Set-ModuleReadMe.ps1 +++ b/avm/utilities/pipelines/sharedScripts/Set-ModuleReadMe.ps1 @@ -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 @@ -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 diff --git a/avm/utilities/pipelines/sharedScripts/helper/Get-CrossReferencedModuleList.ps1 b/avm/utilities/pipelines/sharedScripts/helper/Get-CrossReferencedModuleList.ps1 index 760a4d18d3..d4c926a295 100644 --- a/avm/utilities/pipelines/sharedScripts/helper/Get-CrossReferencedModuleList.ps1 +++ b/avm/utilities/pipelines/sharedScripts/helper/Get-CrossReferencedModuleList.ps1 @@ -1,3 +1,4 @@ +#region helper functions <# .SYNOPSIS Find any nested dependency recursively @@ -40,6 +41,26 @@ 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 @@ -47,12 +68,10 @@ 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 @@ -60,12 +79,11 @@ 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", @@ -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// + $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 } diff --git a/avm/utilities/tools/Test-ModuleLocally.ps1 b/avm/utilities/tools/Test-ModuleLocally.ps1 index fc112c3e4d..9ddb39e760 100644 --- a/avm/utilities/tools/Test-ModuleLocally.ps1 +++ b/avm/utilities/tools/Test-ModuleLocally.ps1 @@ -184,8 +184,6 @@ function Test-ModuleLocally { $tokenConfiguration = @{ FilePathList = $moduleTestFiles Tokens = @{} - TokenPrefix = '#_' - TokenSuffix = '_#' } # Add Other Parameter File Tokens (For Testing)