Skip to content

Commit

Permalink
[CI environment] Retrieve main resource type leveraging api specs (#3508
Browse files Browse the repository at this point in the history
)

* update pester pipeline scripts

* cleanup

* Update utilities/tools/helper/Get-PipelineFileName.ps1

Co-authored-by: Alexander Sehr <ASehr@hotmail.de>

* readme utility update

* update get aligned specs

* synopsis

* synopsis update

* readme update 01

* readme update 02

* vpn regen

* manage additional exception

* Update utilities/tools/helper/Get-SpecsAlignedResourceName.ps1

Co-authored-by: Alexander Sehr <ASehr@hotmail.de>

* Apply suggestions from code review

* insight regen

* network regen

* auth regen

---------

Co-authored-by: Alexander Sehr <ASehr@hotmail.de>
  • Loading branch information
eriqua and AlexanderSehr authored Aug 8, 2023
1 parent 55784a6 commit aa19c3e
Show file tree
Hide file tree
Showing 34 changed files with 126 additions and 51 deletions.
2 changes: 1 addition & 1 deletion modules/authorization/locks/resource-group/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Authorization Locks (Resource Group scope) `[Microsoft.Authorization/locks/resourceGroup]`
# Authorization Locks (Resource Group scope) `[Microsoft.Authorization/locks]`

This module deploys an Authorization Lock at a Resource Group scope.

Expand Down
2 changes: 1 addition & 1 deletion modules/authorization/locks/subscription/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Authorization Locks (Subscription scope) `[Microsoft.Authorization/locks/subscription]`
# Authorization Locks (Subscription scope) `[Microsoft.Authorization/locks]`

This module deploys an Authorization Lock at a Subscription scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Policy Assignments (Management Group scope) `[Microsoft.Authorization/policyAssignments/managementGroup]`
# Policy Assignments (Management Group scope) `[Microsoft.Authorization/policyAssignments]`

This module deploys a Policy Assignment at a Management Group scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Policy Assignments (Resource Group scope) `[Microsoft.Authorization/policyAssignments/resourceGroup]`
# Policy Assignments (Resource Group scope) `[Microsoft.Authorization/policyAssignments]`

This module deploys a Policy Assignment at a Resource Group scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Policy Assignments (Subscription scope) `[Microsoft.Authorization/policyAssignments/subscription]`
# Policy Assignments (Subscription scope) `[Microsoft.Authorization/policyAssignments]`

This module deploys a Policy Assignment at a Subscription scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Policy Definitions (Management Group scope) `[Microsoft.Authorization/policyDefinitions/managementGroup]`
# Policy Definitions (Management Group scope) `[Microsoft.Authorization/policyDefinitions]`

This module deploys a Policy Definition at a Management Group scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Policy Definitions (Subscription scope) `[Microsoft.Authorization/policyDefinitions/subscription]`
# Policy Definitions (Subscription scope) `[Microsoft.Authorization/policyDefinitions]`

This module deploys a Policy Definition at a Subscription scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Policy Exemptions (Management Group scope) `[Microsoft.Authorization/policyExemptions/managementGroup]`
# Policy Exemptions (Management Group scope) `[Microsoft.Authorization/policyExemptions]`

This module deploys a Policy Exemption at a Management Group scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Policy Exemptions (Resource Group scope) `[Microsoft.Authorization/policyExemptions/resourceGroup]`
# Policy Exemptions (Resource Group scope) `[Microsoft.Authorization/policyExemptions]`

This module deploys a Policy Exemption at a Resource Group scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Policy Exemptions (Subscription scope) `[Microsoft.Authorization/policyExemptions/subscription]`
# Policy Exemptions (Subscription scope) `[Microsoft.Authorization/policyExemptions]`

This module deploys a Policy Exemption at a Subscription scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Policy Set Definitions (Initiatives) (Management Group scope) `[Microsoft.Authorization/policySetDefinitions/managementGroup]`
# Policy Set Definitions (Initiatives) (Management Group scope) `[Microsoft.Authorization/policySetDefinitions]`

This module deploys a Policy Set Definition (Initiative) at a Management Group scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Policy Set Definitions (Initiatives) (Subscription scope) `[Microsoft.Authorization/policySetDefinitions/subscription]`
# Policy Set Definitions (Initiatives) (Subscription scope) `[Microsoft.Authorization/policySetDefinitions]`

This module deploys a Policy Set Definition (Initiative) at a Subscription scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Role Assignments (Management Group scope) `[Microsoft.Authorization/roleAssignments/managementGroup]`
# Role Assignments (Management Group scope) `[Microsoft.Authorization/roleAssignments]`

This module deploys a Role Assignment at a Management Group scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Role Assignments (Resource Group scope) `[Microsoft.Authorization/roleAssignments/resourceGroup]`
# Role Assignments (Resource Group scope) `[Microsoft.Authorization/roleAssignments]`

This module deploys a Role Assignment at a Resource Group scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Role Assignments (Subscription scope) `[Microsoft.Authorization/roleAssignments/subscription]`
# Role Assignments (Subscription scope) `[Microsoft.Authorization/roleAssignments]`

This module deploys a Role Assignment at a Subscription scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Role Definitions (Management Group scope) `[Microsoft.Authorization/roleDefinitions/managementGroup]`
# Role Definitions (Management Group scope) `[Microsoft.Authorization/roleDefinitions]`

This module deploys a Role Definition at a Management Group scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Role Definitions (Resource Group scope) `[Microsoft.Authorization/roleDefinitions/resourceGroup]`
# Role Definitions (Resource Group scope) `[Microsoft.Authorization/roleDefinitions]`

This module deploys a Role Definition at a Resource Group scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Role Definitions (Subscription scope) `[Microsoft.Authorization/roleDefinitions/subscription]`
# Role Definitions (Subscription scope) `[Microsoft.Authorization/roleDefinitions]`

This module deploys a Role Definition at a Subscription scope.

Expand Down
2 changes: 1 addition & 1 deletion modules/cache/redis/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Redis Cache `[Microsoft.Cache/redis]`
# Redis Cache `[Microsoft.Cache/Redis]`

This module deploys a Redis Cache.

Expand Down
2 changes: 1 addition & 1 deletion modules/db-for-postgre-sql/flexible-servers/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DBforPostgreSQL Flexible Servers `[Microsoft.DBforPostgreSQL/flexibleServers]`
# DBforPostgreSQL Flexible Servers `[Microsoft.DBForPostgreSql/flexibleServers]`

This module deploys a DBforPostgreSQL Flexible Server.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DBforPostgreSQL Flexible Server Configurations `[Microsoft.DBforPostgreSQL/flexibleServers/configurations]`
# DBforPostgreSQL Flexible Server Configurations `[Microsoft.DBForPostgreSql/flexibleServers/configurations]`

This module deploys a DBforPostgreSQL Flexible Server Configuration.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DBforPostgreSQL Flexible Server Databases `[Microsoft.DBforPostgreSQL/flexibleServers/databases]`
# DBforPostgreSQL Flexible Server Databases `[Microsoft.DBForPostgreSql/flexibleServers/databases]`

This module deploys a DBforPostgreSQL Flexible Server Database.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DBforPostgreSQL Flexible Server Firewall Rules `[Microsoft.DBforPostgreSQL/flexibleServers/firewallRules]`
# DBforPostgreSQL Flexible Server Firewall Rules `[Microsoft.DBForPostgreSql/flexibleServers/firewallRules]`

This module deploys a DBforPostgreSQL Flexible Server Firewall Rule.

Expand Down
2 changes: 1 addition & 1 deletion modules/insights/private-link-scopes/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Azure Monitor Private Link Scopes `[microsoft.insights/privateLinkScopes]`
# Azure Monitor Private Link Scopes `[Microsoft.Insights/privateLinkScopes]`

This module deploys an Azure Monitor Private Link Scope.

Expand Down
2 changes: 1 addition & 1 deletion modules/network/express-route-gateway/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Express Route Gateways `[Microsoft.Network/expressRouteGateway]`
# Express Route Gateways `[Microsoft.Network/expressRouteGateways]`

This module deploys an Express Route Gateway.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Policy Insights Remediations (Management Group scope) `[Microsoft.PolicyInsights/remediations/managementGroup]`
# Policy Insights Remediations (Management Group scope) `[Microsoft.PolicyInsights/remediations]`

This module deploys a Policy Insights Remediation on a Management Group scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Policy Insights Remediations (Resource Group scope) `[Microsoft.PolicyInsights/remediations/resourceGroup]`
# Policy Insights Remediations (Resource Group scope) `[Microsoft.PolicyInsights/remediations]`

This module deploys a Policy Insights Remediation on a Resource Group scope.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Policy Insights Remediations (Subscription scope) `[Microsoft.PolicyInsights/remediations/subscription]`
# Policy Insights Remediations (Subscription scope) `[Microsoft.PolicyInsights/remediations]`

This module deploys a Policy Insights Remediation on a Subscription scope.

Expand Down
2 changes: 1 addition & 1 deletion modules/resources/tags/resource-groups/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Resources Tags Resource Group `[Microsoft.Resources/tags/resourceGroups]`
# Resources Tags Resource Group `[Microsoft.Resources/tags]`

This module deploys a Resource Tag on a Resource Group scope.

Expand Down
2 changes: 1 addition & 1 deletion modules/resources/tags/subscriptions/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Resources Tags Subscription Scope `[Microsoft.Resources/tags/subscriptions]`
# Resources Tags Subscription Scope `[Microsoft.Resources/tags]`

This module deploys a Resource Tag on a Subscription scope.

Expand Down
2 changes: 1 addition & 1 deletion modules/security/azure-security-center/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Azure Security Center (Defender for Cloud) `[Microsoft.Security/azureSecurityCenter]`
# Azure Security Center (Defender for Cloud) `[Microsoft.Security/azuresecuritycenter]`

This module deploys an Azure Security Center (Defender for Cloud) Configuration.

Expand Down
17 changes: 3 additions & 14 deletions utilities/tools/Set-ModuleReadMe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1417,22 +1417,11 @@ function Initialize-ReadMe {
[hashtable] $TemplateFileContent
)

. (Join-Path $PSScriptRoot 'helper' 'ConvertTo-ModuleResourceType.ps1')

$moduleName = $TemplateFileContent.metadata.name
$moduleDescription = $TemplateFileContent.metadata.description

$splitHyphens = $FullModuleIdentifier.split('-')
$splitHyphens = $splitHyphens | ForEach-Object { $_.substring(0, 1).toupper() + $_.substring(1) }
$splitHyphens = $splitHyphens -join ''
$fullResourceType = 'Microsoft.{0}' -f $splitHyphens.Replace('-', '')

# Resolve resource type as per used API name to use matching casing
$relevantResourceTypeObjects = (Get-NestedResourceList $TemplateFileContent).type | Select-Object -Unique
$formattedResourceType = $relevantResourceTypeObjects | Where-Object { $_ -eq $fullResourceType }

if (-not $formattedResourceType) {
Write-Warning "Did not find module [$FullModuleIdentifier] formatted as [$fullResourceType] in the module template's resource types."
$formattedResourceType = $fullResourceType
}
$formattedResourceType = ConvertTo-ModuleResourceType -ResourceIdentifier $FullModuleIdentifier

if (-not (Test-Path $ReadMeFilePath) -or ([String]::IsNullOrEmpty((Get-Content $ReadMeFilePath -Raw)))) {

Expand Down
57 changes: 57 additions & 0 deletions utilities/tools/helper/ConvertTo-ModuleResourceType.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<#
.SYNOPSIS
Converts a parent or child module folder path to the corresponding resource type.
.DESCRIPTION
Converts a parent or child module folder path to the corresponding resource type.
.PARAMETER ResourceIdentifier
Mandatory. The resource identifier to search for, i.e. the relative module file path starting from the resource provider folder.
.EXAMPLE
ConvertTo-ModuleResourceType -ResourceIdentifier 'storage/storage-account'.
Returns 'Microsoft.Storage/storageAccounts'.
.EXAMPLE
ConvertTo-ModuleResourceType -ResourceIdentifier 'storage/storage-account/blob-service/container/immutability-policy'.
Returns 'Microsoft.Storage/storageAccounts/blobServices/containers/immutabilityPolicies'.
#>
function ConvertTo-ModuleResourceType {

[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $ResourceIdentifier
)

. (Join-Path $PSScriptRoot 'Get-SpecsAlignedResourceName.ps1')

$provider, $parentType, $childTypeString = $ResourceIdentifier -Split '[\/|\\]', 3
$parentResourceIdentifier = $provider, $parentType -join '/'

$fullParentResourceType = Get-SpecsAlignedResourceName -ResourceIdentifier $parentResourceIdentifier

if (-not $childTypeString) {
$fullResourceType = $fullParentResourceType
} else {
$childTypeArray = $childTypeString -split '\/'

$innerResourceType = $fullParentResourceType
foreach ($childType in $childTypeArray) {
# Additional check for child types non existing on their own (e.g. sites/hybridConnectionNamespaces does not exist, sites/hybridConnectionNamespaces/relays does)
$innerResourceTypeLeafReduced = Get-ReducedWordString -StringToReduce ($innerResourceType -Split '[\/|\\]')[-1]
$childTypeReduced = Get-ReducedWordString -StringToReduce $childType
if ($innerResourceTypeLeafReduced -eq $childTypeReduced) {
break
}

$innerResourceType = $innerResourceType.Replace('Microsoft.', '', 'OrdinalIgnoreCase'), $childType -join '/'
$fullResourceType = Get-SpecsAlignedResourceName -ResourceIdentifier $innerResourceType
$innerResourceType = $fullResourceType
}
}

return $fullResourceType
}
41 changes: 35 additions & 6 deletions utilities/tools/helper/Get-SpecsAlignedResourceName.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,54 @@ function Get-SpecsAlignedResourceName {

$reducedResourceIdentifier = $ResourceIdentifier -replace '-'

$rawProviderNamespace = $reducedResourceIdentifier.Split('/')[0]
$rawProviderNamespace, $rawResourceType = $reducedResourceIdentifier -Split '[\/|\\]', 2 # e.g. 'keyvault' & 'vaults/keys'

$foundProviderNamespaceMatches = ($specs.Keys | Sort-Object) | Where-Object { $_ -like "Microsoft.$rawProviderNamespace*" }

if (-not $foundProviderNamespaceMatches) {
$providerNamespace = "Microsoft.$rawProviderNamespace"
Write-Warning "Failed to identifier provider namespace [$rawProviderNamespace]. Falling back to [$providerNamespace]."
Write-Warning "Failed to identify provider namespace [$rawProviderNamespace]. Falling back to [$providerNamespace]."
} else {
$providerNamespace = ($foundProviderNamespaceMatches.Count -eq 1) ? $foundProviderNamespaceMatches : $foundProviderNamespaceMatches[0]
}

$innerResourceTypes = $specs[$providerNamespace].Keys | Sort-Object
$rawResourceType = Get-ReducedWordString -StringToReduce ($reducedResourceIdentifier -replace ('{0}/' -f ($reducedResourceIdentifier.Split('/')[0])), '')
$foundResourceTypeMatches = $innerResourceTypes | Where-Object { $_ -like "$rawResourceType*" }
$rawResourceTypeReduced = Get-ReducedWordString -StringToReduce $rawResourceType
$foundResourceTypeMatches = $innerResourceTypes | Where-Object { $_ -like "$rawResourceTypeReduced*" }

if (-not $foundResourceTypeMatches) {
$resourceType = $reducedResourceIdentifier.Split('/')[0]
$resourceType = $reducedResourceIdentifier.Split('/')[1]
Write-Warning "Failed to identify resource type [$rawResourceType] in provider namespace [$providerNamespace]. Fallback to [$resourceType]."
} elseif ($foundResourceTypeMatches.Count -eq 1) {
$resourceType = $foundResourceTypeMatches
} else {
$resourceType = ($foundResourceTypeMatches.Count -eq 1) ? $foundResourceTypeMatches : $foundResourceTypeMatches[0]
# If more than one specs resource type matches the input resource type core string, get all specs core strings and check exact match
# This is to avoid that e.g. web/connection falls to Microsoft.Web/connectionGateways instead of Microsoft.Web/connections
foreach ($foundResourceTypeMatch in $foundResourceTypeMatches) {
$foundResourceTypeMatchReduced = Get-ReducedWordString -StringToReduce $foundResourceTypeMatch
if ($rawResourceTypeReduced -eq $foundResourceTypeMatchReduced) {
$resourceType = $foundResourceTypeMatch
break
}
}

if (-not $resourceType) {
# Try removing last split of each match, then reduce to core and compare
# This is needed to deal cases such as Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers where backupFabrics does not exist on its own
foreach ($foundResourceTypeMatch in $foundResourceTypeMatches) {
$foundResourceTypeMatch = $foundResourceTypeMatch.SubString(0, $foundResourceTypeMatch.LastIndexOf('/'))
$foundResourceTypeMatchReduced = Get-ReducedWordString -StringToReduce $foundResourceTypeMatch
if ($rawResourceTypeReduced -eq $foundResourceTypeMatchReduced) {
$resourceType = $foundResourceTypeMatch
break
}
}
# Finally fallback to first match in the list
if (-not $resourceType) {
$resourceType = $foundResourceTypeMatches[0]
Write-Warning "Failed to find exact match between core matched resource types and [$rawResourceTypeReduced]. Fallback to first ResourceType in the match list [$resourceType]."
}
}
}

return "$providerNamespace/$resourceType"
Expand Down

0 comments on commit aa19c3e

Please sign in to comment.