Skip to content

Commit 78d8dbc

Browse files
Refactor process_sample_data.sh for Azure output fetching
Updated the script to fetch parameters from Azure deployment outputs instead of requiring them as command-line arguments. Added authentication check for Azure CLI and improved error handling.
1 parent a957407 commit 78d8dbc

File tree

1 file changed

+91
-47
lines changed

1 file changed

+91
-47
lines changed

infra/scripts/process_sample_data.sh

Lines changed: 91 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22

33
# Variables
44
resourceGroupName="$1"
5-
cosmosDbAccountName="$2"
6-
storageAccount="$3"
7-
fileSystem="$4"
8-
keyvaultName="$5"
9-
sqlServerName="$6"
10-
SqlDatabaseName="$7"
11-
sqlManagedIdentityClientId="$8"
12-
sqlManagedIdentityDisplayName="$9"
13-
aiSearchName="${10}"
14-
aif_resource_id="${11}"
155

166
# Global variables to track original network access states
177
original_storage_public_access=""
@@ -20,6 +10,7 @@
2010
aif_resource_group=""
2111
aif_account_resource_id=""
2212
# Add global variable for SQL Server public access
13+
2314
original_sql_public_access=""
2415
created_sql_allow_all_firewall_rule="false"
2516
original_full_range_rule_present="false"
@@ -289,55 +280,108 @@
289280
# Set up trap to ensure cleanup happens on exit
290281
trap cleanup_on_exit EXIT INT TERM
291282

292-
# get parameters from azd env, if not provided
293-
if [ -z "$resourceGroupName" ]; then
294-
resourceGroupName=$(azd env get-value RESOURCE_GROUP_NAME)
283+
if az account show &> /dev/null; then
284+
echo "Already authenticated with Azure."
285+
else
286+
echo "Authenticating with Azure CLI..."
287+
az login
288+
echo "Authenticated with Azure CLI."
295289
fi
296290

291+
# fetch all variables from deployment outputs
297292

298-
if [ -z "$cosmosDbAccountName" ]; then
299-
cosmosDbAccountName=$(azd env get-value COSMOSDB_ACCOUNT_NAME)
300-
fi
293+
deploymentName=$(az group show --name "$resourceGroupName" --query "tags.DeploymentName" -o tsv)
294+
echo "Deployment Name (from tag): $deploymentName"
301295

302-
if [ -z "$storageAccount" ]; then
303-
storageAccount=$(azd env get-value STORAGE_ACCOUNT_NAME)
304-
fi
296+
if az deployment group show --resource-group "$resourceGroupName" --name "$deploymentName" &>/dev/null; then
297+
cosmosDbAccountName=$(az deployment group show \
298+
--name "$deploymentName" \
299+
--resource-group "$resourceGroupName" \
300+
--query "properties.outputs.cosmosdB_ACCOUNT_NAME.value" -o tsv)
301+
echo "Cosmos DB Account Name (from outputs): $cosmosDbAccountName"
305302

306-
if [ -z "$fileSystem" ]; then
307-
fileSystem=$(azd env get-value STORAGE_CONTAINER_NAME)
308-
fi
303+
storageAccount=$(az deployment group show \
304+
--name "$deploymentName" \
305+
--resource-group "$resourceGroupName" \
306+
--query "properties.outputs.storagE_ACCOUNT_NAME.value" -o tsv)
307+
echo "Storage Account Name (from outputs): $storageAccount"
309308

310-
if [ -z "$keyvaultName" ]; then
311-
keyvaultName=$(azd env get-value KEY_VAULT_NAME)
312-
fi
309+
fileSystem=$(az deployment group show \
310+
--name "$deploymentName" \
311+
--resource-group "$resourceGroupName" \
312+
--query "properties.outputs.storagE_CONTAINER_NAME.value" -o tsv)
313+
echo "Storage Container Name (from outputs): $fileSystem"
313314

314-
if [ -z "$sqlServerName" ]; then
315-
sqlServerName=$(azd env get-value SQLDB_SERVER_NAME)
316-
fi
315+
keyvaultName=$(az deployment group show \
316+
--name "$deploymentName" \
317+
--resource-group "$resourceGroupName" \
318+
--query "properties.outputs.keY_VAULT_NAME.value" -o tsv)
319+
echo "Key Vault Name (from outputs): $keyvaultName"
317320

318-
if [ -z "$SqlDatabaseName" ]; then
319-
SqlDatabaseName=$(azd env get-value SQLDB_DATABASE)
320-
fi
321+
sqlServerName=$(az deployment group show \
322+
--name "$deploymentName" \
323+
--resource-group "$resourceGroupName" \
324+
--query "properties.outputs.sqldB_SERVER_NAME.value" -o tsv)
325+
echo "SQL Server Name (from outputs): $sqlServerName"
321326

322-
if [ -z "$sqlManagedIdentityClientId" ]; then
323-
# Use the SQL-specific managed identity for database operations with limited permissions
324-
sqlManagedIdentityClientId=$(azd env get-value MANAGEDIDENTITY_SQL_CLIENTID)
325-
fi
327+
webAppManagedIdentityDisplayName=$(az deployment group show \
328+
--name "$deploymentName" \
329+
--resource-group "$resourceGroupName" \
330+
--query "properties.outputs.managedidentitY_WEBAPP_NAME.value" -o tsv)
331+
echo "Web App Managed Identity Display Name (from outputs): $webAppManagedIdentityDisplayName"
326332

327-
if [ -z "$sqlManagedIdentityDisplayName" ]; then
328-
# Use the SQL-specific managed identity for database operations with limited permissions
329-
sqlManagedIdentityDisplayName=$(azd env get-value MANAGEDIDENTITY_SQL_NAME)
330-
fi
333+
SqlDatabaseName=$(az deployment group show \
334+
--name "$deploymentName" \
335+
--resource-group "$resourceGroupName" \
336+
--query "properties.outputs.sqldB_DATABASE.value" -o tsv)
337+
echo "SQL Database Name (from outputs): $SqlDatabaseName"
331338

332-
if [ -z "$aiSearchName" ]; then
333-
aiSearchName=$(azd env get-value AI_SEARCH_SERVICE_NAME)
334-
fi
339+
sqlManagedIdentityClientId=$(az deployment group show \
340+
--name "$deploymentName" \
341+
--resource-group "$resourceGroupName" \
342+
--query "properties.outputs.managedidentitY_SQL_CLIENTID.value" -o tsv)
343+
echo "SQL Managed Identity Client ID (from outputs): $sqlManagedIdentityClientId"
335344

336-
if [ -z "$aif_resource_id" ]; then
337-
aif_resource_id=$(azd env get-value AI_FOUNDRY_RESOURCE_ID)
338-
fi
345+
sqlManagedIdentityDisplayName=$(az deployment group show \
346+
--name "$deploymentName" \
347+
--resource-group "$resourceGroupName" \
348+
--query "properties.outputs.managedidentitY_SQL_NAME.value" -o tsv)
349+
echo "SQL Managed Identity Display Name (from outputs): $sqlManagedIdentityDisplayName"
350+
351+
aiSearchName=$(az deployment group show \
352+
--name "$deploymentName" \
353+
--resource-group "$resourceGroupName" \
354+
--query "properties.outputs.aI_SEARCH_SERVICE_NAME.value" -o tsv)
355+
echo "AI Search Service Name (from outputs): $aiSearchName"
339356

340-
azSubscriptionId=$(azd env get-value AZURE_SUBSCRIPTION_ID)
357+
aif_resource_id=$(az deployment group show \
358+
--name "$deploymentName" \
359+
--resource-group "$resourceGroupName" \
360+
--query "properties.outputs.aI_FOUNDRY_RESOURCE_ID.value" -o tsv)
361+
echo "AI Foundry Resource ID (from outputs): $aif_resource_id"
362+
363+
azSubscriptionId=$(az deployment group show \
364+
--name "$deploymentName" \
365+
--resource-group "$resourceGroupName" \
366+
--query "properties.outputs.azurE_SUBSCRIPTION_ID.value" -o tsv)
367+
368+
echo "Azure Subscription ID (from outputs): $azSubscriptionId"
369+
else
370+
echo "Deployment does NOT exist in resource group $resourceGroupName."
371+
echo "Please enter required values manually."
372+
373+
read -rp "Enter Cosmos DB Account Name: " cosmosDbAccountName
374+
read -rp "Enter Storage Account Name: " storageAccount
375+
read -rp "Enter Storage Container/File System Name: " fileSystem
376+
read -rp "Enter SQL Server Name: " sqlServerName
377+
read -rp "Enter SQL Database Name: " SqlDatabaseName
378+
read -rp "Enter Key Vault Name: " keyvaultName
379+
read -rp "Enter Web App Managed Identity Display Name: " webAppManagedIdentityDisplayName
380+
read -rp "Enter Web App Managed Identity Client ID: " webAppManagedIdentityClientId
381+
read -rp "Enter AI Search Service Name: " aiSearchName
382+
read -rp "Enter AI Foundry Resource ID: " aif_resource_id
383+
read -rp "Enter Azure Subscription ID: " azSubscriptionId
384+
fi
341385

342386
# Check if all required arguments are provided
343387
if [ -z "$resourceGroupName" ] || [ -z "$cosmosDbAccountName" ] || [ -z "$storageAccount" ] || [ -z "$fileSystem" ] || [ -z "$keyvaultName" ] || [ -z "$sqlServerName" ] || [ -z "$SqlDatabaseName" ] || [ -z "$sqlManagedIdentityClientId" ] || [ -z "$sqlManagedIdentityDisplayName" ] || [ -z "$aiSearchName" ] || [ -z "$aif_resource_id" ]; then
@@ -453,4 +497,4 @@
453497

454498
echo "All scripts executed successfully."
455499
echo "Network access will be restored to original settings..."
456-
# Note: cleanup_on_exit will be called automatically via the trap
500+
# Note: cleanup_on_exit will be called automatically via the trap

0 commit comments

Comments
 (0)