@@ -36,24 +36,92 @@ sqlServerLocation=$(az sql server list --resource-group "$resourceGroupName" --q
3636# === Retrieve the principal ID of the first user-assigned identity with name starting with 'id-' ===
3737managedIdentityClientId=$( az identity list --resource-group " $resourceGroupName " --query " [?starts_with(name, 'id-') && !starts_with(name, 'id-sql-')].clientId | [0]" -o tsv)
3838
39+ # === Check for VNet deployment ===
40+ echo " Checking for VNet deployment in resource group: $resourceGroupName "
41+ vnetResourceId=$( az network vnet list --resource-group " $resourceGroupName " --query " [0].id" -o tsv)
42+
43+ # === Get resource group location ===
44+ rgLocation=$( az group show --name " $resourceGroupName " --query " location" -o tsv)
45+
46+ # === Find storage account (always needed) ===
47+ echo " Looking for storage account in resource group..."
48+ storageAccountResourceId=$( az storage account list --resource-group " $resourceGroupName " --query " [0].id" -o tsv)
49+
50+ if [ -z " $storageAccountResourceId " ]; then
51+ echo " ERROR: No storage account found in resource group $resourceGroupName "
52+ exit 1
53+ else
54+ echo " Using storage account: $storageAccountResourceId "
55+ fi
56+
57+ if [ -z " $vnetResourceId " ]; then
58+ echo " No VNet found in resource group. Private networking is disabled."
59+ enablePrivateNetworking=" false"
60+ subnetId=" "
61+ solutionLocation=" $sqlServerLocation "
62+ echo " Using SQL Server location for solution: $solutionLocation "
63+ else
64+ echo " VNet found: $vnetResourceId "
65+ echo " VNet detected - enabling private networking."
66+ enablePrivateNetworking=" true"
67+ solutionLocation=" $rgLocation "
68+ echo " Using Resource Group location for solution: $solutionLocation "
69+
70+ # === Find the deployment script subnet ===
71+ echo " Looking for deployment-scripts subnet..."
72+ subnetId=$( az network vnet subnet list --resource-group " $resourceGroupName " --vnet-name $( basename " $vnetResourceId " ) --query " [?name=='deployment-scripts'].id | [0]" -o tsv)
73+
74+ if [ -z " $subnetId " ]; then
75+ echo " Warning: deployment-scripts subnet not found. Checking for alternative subnet names..."
76+ # Try alternative names
77+ subnetId=$( az network vnet subnet list --resource-group " $resourceGroupName " --vnet-name $( basename " $vnetResourceId " ) --query " [?contains(name, 'deployment') || contains(name, 'script')].id | [0]" -o tsv)
78+ fi
79+
80+ if [ -z " $subnetId " ]; then
81+ echo " Warning: No deployment script subnet found. Private networking will be disabled for deployment script."
82+ enablePrivateNetworking=" false"
83+ subnetId=" "
84+ else
85+ echo " Using deployment script subnet: $subnetId "
86+ fi
87+ fi
88+
3989# === Validate that all required resources were found ===
40- if [[ -z " $keyVaultName " || -z " $sqlServerLocation " || -z " $managedIdentityResourceId " || ! " $managedIdentityResourceId " =~ ^/subscriptions/ ]]; then
90+ if [[ -z " $keyVaultName " || -z " $solutionLocation " || -z " $managedIdentityResourceId " || ! " $managedIdentityResourceId " =~ ^/subscriptions/ ]]; then
4191 echo " ERROR: Could not find required resources in resource group $resourceGroupName or managedIdentityResourceId is invalid"
4292 exit 1
4393fi
4494
45- echo " Using SQL Server Location: $sqlServerLocation "
95+ echo " Using Solution Location: $solutionLocation "
4696echo " Using Key Vault: $keyVaultName "
4797echo " Using Managed Identity Resource Id: $managedIdentityResourceId "
4898echo " Using Managed Identity ClientId Id: $managedIdentityClientId "
99+ echo " Enable Private Networking: $enablePrivateNetworking "
100+ echo " Subnet ID: $subnetId "
101+ echo " Storage Account Resource ID: $storageAccountResourceId "
49102
50103# === Deploy resources using the specified Bicep template ===
51104echo " Deploying Bicep template..."
52105
106+ # Build base parameters
107+ deploymentParams=" solutionLocation=$solutionLocation keyVaultName=$keyVaultName managedIdentityResourceId=$managedIdentityResourceId managedIdentityClientId=$managedIdentityClientId storageAccount=$storageAccountResourceId "
108+
109+ # Add networking parameters if VNet is deployed
110+ if [ " $enablePrivateNetworking " = " true" ]; then
111+ deploymentParams=" $deploymentParams enablePrivateNetworking=true"
112+ if [ -n " $subnetId " ]; then
113+ deploymentParams=" $deploymentParams subnetId=$subnetId "
114+ fi
115+ else
116+ deploymentParams=" $deploymentParams enablePrivateNetworking=false"
117+ fi
118+
119+ echo " Deployment parameters: $deploymentParams "
120+
53121# MSYS_NO_PATHCONV disables path conversion in Git Bash for Windows
54122MSYS_NO_PATHCONV=1 az deployment group create \
55123 --resource-group " $resourceGroupName " \
56124 --template-file " $bicepFile " \
57- --parameters solutionLocation= " $sqlServerLocation " keyVaultName= " $keyVaultName " managedIdentityResourceId= " $managedIdentityResourceId " managedIdentityClientId= " $managedIdentityClientId "
125+ --parameters $deploymentParams
58126
59127echo " Deployment completed."
0 commit comments