Skip to content

Commit cdcd727

Browse files
fix to manage AI Foundry public network access during agent creation
1 parent 81c068b commit cdcd727

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

infra/scripts/agent_scripts/run_create_agents_scripts.sh

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,76 @@ aiSearchConnectionName="$6"
1212
aiSearchIndex="$7"
1313
resourceGroup="$8"
1414

15+
# Global variables to track original network access states for AI Foundry
16+
original_foundry_public_access=""
17+
aif_resource_group=""
18+
aif_account_resource_id=""
19+
20+
# Function to enable public network access temporarily for AI Foundry
21+
enable_foundry_public_access() {
22+
if [ -n "$aiFoundryResourceId" ] && [ "$aiFoundryResourceId" != "null" ]; then
23+
aif_account_resource_id="$aiFoundryResourceId"
24+
aif_resource_name=$(echo "$aiFoundryResourceId" | sed -n 's|.*/providers/Microsoft.CognitiveServices/accounts/\([^/]*\).*|\1|p')
25+
aif_resource_group=$(echo "$aiFoundryResourceId" | sed -n 's|.*/resourceGroups/\([^/]*\)/.*|\1|p')
26+
aif_subscription_id=$(echo "$aif_account_resource_id" | sed -n 's|.*/subscriptions/\([^/]*\)/.*|\1|p')
27+
28+
original_foundry_public_access=$(az cognitiveservices account show \
29+
--name "$aif_resource_name" \
30+
--resource-group "$aif_resource_group" \
31+
--subscription "$aif_subscription_id" \
32+
--query "properties.publicNetworkAccess" \
33+
--output tsv)
34+
35+
if [ -z "$original_foundry_public_access" ] || [ "$original_foundry_public_access" = "null" ]; then
36+
echo "⚠ Could not retrieve AI Foundry network access status"
37+
elif [ "$original_foundry_public_access" != "Enabled" ]; then
38+
echo "✓ Enabling AI Foundry public access"
39+
if ! MSYS_NO_PATHCONV=1 az resource update \
40+
--ids "$aif_account_resource_id" \
41+
--api-version 2024-10-01 \
42+
--set properties.publicNetworkAccess=Enabled properties.apiProperties="{}" \
43+
--output none; then
44+
echo "⚠ Failed to enable AI Foundry public access"
45+
fi
46+
# Wait a bit for changes to take effect
47+
sleep 10
48+
fi
49+
fi
50+
return 0
51+
}
52+
53+
# Function to restore original network access settings for AI Foundry
54+
restore_foundry_network_access() {
55+
if [ -n "$original_foundry_public_access" ] && [ "$original_foundry_public_access" != "Enabled" ]; then
56+
echo "✓ Restoring AI Foundry access"
57+
if ! MSYS_NO_PATHCONV=1 az resource update \
58+
--ids "$aif_account_resource_id" \
59+
--api-version 2024-10-01 \
60+
--set properties.publicNetworkAccess="$original_foundry_public_access" \
61+
--set properties.apiProperties.qnaAzureSearchEndpointKey="" \
62+
--set properties.networkAcls.bypass="AzureServices" \
63+
--output none 2>/dev/null; then
64+
echo "⚠ Failed to restore AI Foundry access - please check Azure portal"
65+
fi
66+
fi
67+
}
68+
69+
# Function to handle script cleanup on exit
70+
cleanup_on_exit() {
71+
exit_code=$?
72+
echo ""
73+
if [ $exit_code -ne 0 ]; then
74+
echo "❌ Script failed"
75+
else
76+
echo "✅ Script completed successfully"
77+
fi
78+
restore_foundry_network_access
79+
exit $exit_code
80+
}
81+
82+
# Register cleanup function to run on script exit
83+
trap cleanup_on_exit EXIT
84+
1585
# get parameters from azd env, if not provided
1686
if [ -z "$projectEndpoint" ]; then
1787
projectEndpoint=$(azd env get-value AZURE_AI_AGENT_ENDPOINT)
@@ -97,6 +167,13 @@ requirementFile="infra/scripts/agent_scripts/requirements.txt"
97167
python -m pip install --upgrade pip
98168
python -m pip install --quiet -r "$requirementFile"
99169

170+
# Enable public network access for AI Foundry before agent creation
171+
enable_foundry_public_access
172+
if [ $? -ne 0 ]; then
173+
echo "Error: Failed to enable public network access for AI Foundry."
174+
exit 1
175+
fi
176+
100177
# Execute the Python scripts
101178
echo "Running Python agents creation script..."
102179
eval $(python infra/scripts/agent_scripts/01_create_agents.py --ai_project_endpoint="$projectEndpoint" --solution_name="$solutionName" --gpt_model_name="$gptModelName" --azure_ai_search_connection_name="$aiSearchConnectionName" --azure_ai_search_index="$aiSearchIndex")

0 commit comments

Comments
 (0)