Skip to content

Commit ab5c86b

Browse files
Add SolutionSuffix tag to resource group for post deployment script fallback method
1 parent 1f5a12e commit ab5c86b

2 files changed

Lines changed: 137 additions & 79 deletions

File tree

Deployment/resourcedeployment.ps1

Lines changed: 136 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -362,91 +362,148 @@ class DeploymentResult {
362362
}
363363

364364
[void]MapResultAz([string]$resourceGroupName) {
365-
# Get deployment outputs
366-
$deploymentName=$(az group show --name "$resourceGroupName" --query "tags.DeploymentName" -o tsv)
367-
if (!$deploymentName) {
368-
Write-Error "Deployment name not found in the resource group tags."
369-
exit 1
370-
}
371-
372-
$deploymentOutputs=$(az deployment group show --resource-group "$resourceGroupName" --name "$deploymentName" --query "properties.outputs" -o json | ConvertFrom-Json)
373-
374-
# Helper function to get value from deployment outputs with fallback
375-
function Get-DeploymentOutputValue {
376-
param (
377-
[Parameter(Mandatory=$true)]
378-
$outputs,
379-
[Parameter(Mandatory=$true)]
380-
[string]$primaryKey,
381-
[Parameter(Mandatory=$true)]
382-
[string]$fallbackKey
383-
)
365+
try {
366+
Write-Host "Retrieving resource group tags..." -ForegroundColor Yellow
384367

385-
$value = $null
368+
# Get deployment name from tags
369+
$deploymentName = az group show --name $resourceGroupName --query "tags.DeploymentName" -o tsv 2>$null
386370

387-
# Try primary key first (old convention)
388-
if ($outputs.PSObject.Properties.Name -contains $primaryKey) {
389-
$value = $outputs.$primaryKey.value
371+
if ([string]::IsNullOrEmpty($deploymentName)) {
372+
Write-Host "DeploymentName tag not found. Using SolutionSuffix fallback..." -ForegroundColor Yellow
390373
}
391-
392-
# If not found or empty, try fallback key (new convention)
393-
if ([string]::IsNullOrEmpty($value) -and ($outputs.PSObject.Properties.Name -contains $fallbackKey)) {
394-
$value = $outputs.$fallbackKey.value
374+
else {
375+
Write-Host "Found deployment name from tag: $deploymentName" -ForegroundColor Green
376+
377+
# Check if deployment exists
378+
$deploymentState = az deployment group show `
379+
--resource-group $resourceGroupName `
380+
--name $deploymentName `
381+
--query "properties.provisioningState" `
382+
-o tsv 2>$null
383+
384+
if ($deploymentState -eq "Succeeded") {
385+
Write-Host "Deployment exists and succeeded. Retrieving outputs..." -ForegroundColor Green
386+
387+
# Get deployment outputs
388+
$deploymentOutputs = az deployment group show `
389+
--resource-group $resourceGroupName `
390+
--name $deploymentName `
391+
--query "properties.outputs" `
392+
-o json | ConvertFrom-Json
393+
394+
# Helper function to get value from deployment outputs with fallback
395+
function Get-DeploymentOutputValue {
396+
param (
397+
[Parameter(Mandatory=$true)]
398+
$outputs,
399+
[Parameter(Mandatory=$true)]
400+
[string]$primaryKey,
401+
[Parameter(Mandatory=$true)]
402+
[string]$fallbackKey
403+
)
404+
405+
$value = $null
406+
407+
# Try primary key first
408+
if ($outputs.PSObject.Properties.Name -contains $primaryKey) {
409+
$value = $outputs.$primaryKey.value
410+
}
411+
412+
# If not found or empty, try fallback key
413+
if ([string]::IsNullOrEmpty($value) -and ($outputs.PSObject.Properties.Name -contains $fallbackKey)) {
414+
$value = $outputs.$fallbackKey.value
415+
}
416+
417+
return $value
418+
}
419+
420+
# Map all outputs to properties
421+
$this.TenantId = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_TENANT_ID" -fallbackKey "azureTenantId"
422+
if ([string]::IsNullOrEmpty($this.TenantId)) {
423+
$this.TenantId = az account show --query tenantId -o tsv
424+
}
425+
426+
$this.SubscriptionId = az account show --query id -o tsv
427+
$this.ResourceGroupName = $resourceGroupName
428+
$this.ResourceGroupId = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_RESOURCE_GROUP_ID" -fallbackKey "azureResourceGroupId"
429+
430+
$this.StorageAccountName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "storagE_ACCOUNT_NAME" -fallbackKey "storageAccountName"
431+
$this.AzSearchServiceName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_SEARCH_SERVICE_NAME" -fallbackKey "azureSearchServiceName"
432+
$this.AzSearchServicEndpoint = "https://$($this.AzSearchServiceName).search.windows.net"
433+
434+
$this.AksName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_AKS_NAME" -fallbackKey "azureAksName"
435+
$this.AksMid = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_AKS_MI_ID" -fallbackKey "azureAksMiId"
436+
437+
$this.AzContainerRegistryName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_CONTAINER_REGISTRY_NAME" -fallbackKey "azureContainerRegistryName"
438+
$this.AzCognitiveServiceName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_COGNITIVE_SERVICE_NAME" -fallbackKey "azureCognitiveServiceName"
439+
$this.AzCognitiveServiceEndpoint = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_COGNITIVE_SERVICE_ENDPOINT" -fallbackKey "azureCognitiveServiceEndpoint"
440+
$this.AzOpenAiServiceName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_OPENAI_SERVICE_NAME" -fallbackKey "azureOpenAiServiceName"
441+
$this.AzOpenAiServiceEndpoint = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_OPENAI_SERVICE_ENDPOINT" -fallbackKey "azureOpenAiServiceEndpoint"
442+
443+
$this.AzCosmosDBName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_COSMOSDB_NAME" -fallbackKey "azureCosmosDbName"
444+
$this.AzGPT4oModelName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "aZ_GPT4O_MODEL_NAME" -fallbackKey "azGpt4oModelName"
445+
$this.AzGPT4oModelId = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "aZ_GPT4O_MODEL_ID" -fallbackKey "azGpt4oModelId"
446+
$this.AzGPTEmbeddingModelName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "aZ_GPT_EMBEDDING_MODEL_NAME" -fallbackKey "azGptEmbeddingModelName"
447+
$this.AzGPTEmbeddingModelId = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "aZ_GPT_EMBEDDING_MODEL_ID" -fallbackKey "azGptEmbeddingModelId"
448+
449+
$this.AzAppConfigEndpoint = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_APP_CONFIG_ENDPOINT" -fallbackKey "azureAppConfigEndpoint"
450+
$this.AzAppConfigName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_APP_CONFIG_NAME" -fallbackKey "azureAppConfigName"
451+
452+
Write-Host "Successfully retrieved deployment outputs" -ForegroundColor Green
453+
return
454+
}
455+
else {
456+
Write-Host "Deployment '$deploymentName' not found or did not succeed. Using SolutionSuffix fallback..." -ForegroundColor Yellow
457+
}
395458
}
396-
397-
return $value
398-
}
399-
400-
# Tenant ID
401-
$this.TenantId = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_TENANT_ID" -fallbackKey "azureTenantId"
402-
if (!$this.TenantId) {
403-
$this.TenantId = $(az account show --query tenantId -o tsv)
459+
460+
# FALLBACK: Use SolutionSuffix from tags
461+
Write-Host "Attempting to use SolutionSuffix fallback mechanism..." -ForegroundColor Yellow
462+
$solutionSuffix = az group show --name $resourceGroupName --query "tags.SolutionSuffix" -o tsv 2>$null
463+
464+
if ([string]::IsNullOrEmpty($solutionSuffix)) {
465+
throw "Cannot proceed: Neither deployment outputs nor SolutionSuffix tag found in resource group '$resourceGroupName'"
466+
}
467+
468+
Write-Host "Found SolutionSuffix from tags: $solutionSuffix" -ForegroundColor Green
469+
470+
# Basic properties
471+
$this.TenantId = az account show --query tenantId -o tsv
472+
$this.SubscriptionId = az account show --query id -o tsv
473+
$this.ResourceGroupName = $resourceGroupName
474+
$this.ResourceGroupId = az group show --name $resourceGroupName --query id -o tsv
475+
476+
# Reconstruct resource names using same pattern as main.bicep
477+
$this.StorageAccountName = "st$solutionSuffix"
478+
$this.AzSearchServiceName = "srch-$solutionSuffix"
479+
$this.AksName = "aks-$solutionSuffix"
480+
$this.AzContainerRegistryName = "cr$($solutionSuffix.Replace('-', ''))"
481+
$this.AzCognitiveServiceName = "di-$solutionSuffix"
482+
$this.AzOpenAiServiceName = "oai-$solutionSuffix"
483+
$this.AzCosmosDBName = "cosmos-$solutionSuffix"
484+
$this.AzAppConfigName = "appcs-$solutionSuffix"
485+
486+
# Model names from bicep defaults
487+
$this.AzGPT4oModelName = "gpt-4.1-mini"
488+
$this.AzGPT4oModelId = "gpt-4.1-mini"
489+
$this.AzGPTEmbeddingModelName = "text-embedding-3-large"
490+
$this.AzGPTEmbeddingModelId = "text-embedding-3-large"
491+
492+
# Construct endpoints
493+
$this.AzCognitiveServiceEndpoint = "https://$($this.AzCognitiveServiceName).cognitiveservices.azure.com/"
494+
$this.AzOpenAiServiceEndpoint = "https://$($this.AzOpenAiServiceName).openai.azure.com/"
495+
$this.AzSearchServicEndpoint = "https://$($this.AzSearchServiceName).search.windows.net"
496+
$this.AzAppConfigEndpoint = "https://$($this.AzAppConfigName).azconfig.io"
497+
498+
# Get AKS managed identity
499+
$this.AksMid = az aks show --name $this.AksName --resource-group $resourceGroupName --query "identity.principalId" -o tsv 2>$null
500+
501+
Write-Host "Successfully reconstructed resource names from SolutionSuffix '$solutionSuffix'" -ForegroundColor Green
404502
}
405-
406-
$this.SubscriptionId = $(az account show --query id -o tsv)
407-
408-
# Resource Group
409-
$this.ResourceGroupName = $resourceGroupName
410-
$this.ResourceGroupId = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_RESOURCE_GROUP_ID" -fallbackKey "azureResourceGroupId"
411-
if (!$this.ResourceGroupId) {
412-
Write-Error "Required value 'AZURE_RESOURCE_GROUP_ID' or 'azureResourceGroupId' not found in the deployment outputs."
413-
exit 1
503+
catch {
504+
Write-Host "Error in MapResultAz: $_" -ForegroundColor Red
505+
throw
414506
}
415-
416-
# Storage Account
417-
$this.StorageAccountName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "storagE_ACCOUNT_NAME" -fallbackKey "storageAccountName"
418-
419-
# Search Service
420-
$this.AzSearchServiceName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_SEARCH_SERVICE_NAME" -fallbackKey "azureSearchServiceName"
421-
$this.AzSearchServicEndpoint = "https://$($this.AzSearchServiceName).search.windows.net"
422-
423-
# AKS
424-
$this.AksName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_AKS_NAME" -fallbackKey "azureAksName"
425-
$this.AksMid = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_AKS_MI_ID" -fallbackKey "azureAksMiId"
426-
427-
# Container Registry
428-
$this.AzContainerRegistryName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_CONTAINER_REGISTRY_NAME" -fallbackKey "azureContainerRegistryName"
429-
430-
# Cognitive Service - Azure AI Document Intelligence Service
431-
$this.AzCognitiveServiceName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_COGNITIVE_SERVICE_NAME" -fallbackKey "azureCognitiveServiceName"
432-
$this.AzCognitiveServiceEndpoint = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_COGNITIVE_SERVICE_ENDPOINT" -fallbackKey "azureCognitiveServiceEndpoint"
433-
434-
# Open AI Service
435-
$this.AzOpenAiServiceName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_OPENAI_SERVICE_NAME" -fallbackKey "azureOpenAiServiceName"
436-
$this.AzOpenAiServiceEndpoint = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_OPENAI_SERVICE_ENDPOINT" -fallbackKey "azureOpenAiServiceEndpoint"
437-
438-
# Cosmos DB
439-
$this.AzCosmosDBName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_COSMOSDB_NAME" -fallbackKey "azureCosmosDbName"
440-
441-
# Open AI Service Models
442-
$this.AzGPT4oModelName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "aZ_GPT4O_MODEL_NAME" -fallbackKey "azGpt4oModelName"
443-
$this.AzGPT4oModelId = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "aZ_GPT4O_MODEL_ID" -fallbackKey "azGpt4oModelId"
444-
$this.AzGPTEmbeddingModelName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "aZ_GPT_EMBEDDING_MODEL_NAME" -fallbackKey "azGptEmbeddingModelName"
445-
$this.AzGPTEmbeddingModelId = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "aZ_GPT_EMBEDDING_MODEL_ID" -fallbackKey "azGptEmbeddingModelId"
446-
447-
# App Configuration
448-
$this.AzAppConfigEndpoint = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_APP_CONFIG_ENDPOINT" -fallbackKey "azureAppConfigEndpoint"
449-
$this.AzAppConfigName = Get-DeploymentOutputValue -outputs $deploymentOutputs -primaryKey "azurE_APP_CONFIG_NAME" -fallbackKey "azureAppConfigName"
450507
}
451508
}
452509

infra/main.bicep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ resource resourceGroupTags 'Microsoft.Resources/tags@2021-04-01' = {
117117
Type: enablePrivateNetworking ? 'WAF' : 'Non-WAF'
118118
CreatedBy: createdBy
119119
DeploymentName: deployment().name
120+
SolutionSuffix: solutionSuffix
120121
}
121122
}
122123
}

0 commit comments

Comments
 (0)