Skip to content

Commit a79a082

Browse files
author
Seth
committed
Connections - added bash support and naming consistency
1 parent f7a3122 commit a79a082

5 files changed

Lines changed: 179 additions & 3 deletions

File tree

azure.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ hooks:
77
preup:
88
windows:
99
shell: pwsh
10-
run: ./scripts/SetConnectionsEnvironmentVariables.ps1
10+
run: ./scripts/set_conns_env_vars.ps1
11+
interactive: true
12+
continueOnError: false
13+
posix:
14+
shell: sh
15+
run: ./scripts/set_conns_env_vars.sh
1116
interactive: true
1217
continueOnError: false
1318
preprovision:

docs/Verify_Services_On_Network.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This guide will walk you through using a secure jump-box virtual machine to inst
66

77
### 1. Copy Testing Script to Virtual Machine
88

9-
Copy [TestConnections.ps1](./scripts/TestConnections.ps1) to the Virtual Machine.
9+
Copy [test_azure_resource_conns.ps1](./scripts/test_azure_resource_conns.ps1) to the Virtual Machine.
1010

1111
### 2. Install Azure CLI
1212

@@ -41,7 +41,7 @@ $containerRegistry = "your-container-registry-name"
4141
### 5. Execute Testing PowerShell Script
4242

4343
```powershell
44-
.\TestConnections.ps1 `
44+
.\test_azure_resource_conns.ps1 `
4545
-SubscriptionId $subscriptionId `
4646
-ResourceGroup $resourceGroup `
4747
-KeyVault $keyvault `
File renamed without changes.

scripts/set_conns_env_vars.sh

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#!/bin/bash
2+
3+
# Usage: ./set_conns_env_vars.sh [--tenant TENANT] [--subscription SUBSCRIPTION] [--resource-group RESOURCE_GROUP] [--workspace WORKSPACE] [--include-verbose]
4+
5+
while [[ $# -gt 0 ]]; do
6+
case "$1" in
7+
--tenant)
8+
TENANT="$2"
9+
shift 2
10+
;;
11+
--subscription)
12+
SUBSCRIPTION="$2"
13+
shift 2
14+
;;
15+
--resource-group)
16+
RESOURCE_GROUP="$2"
17+
shift 2
18+
;;
19+
--workspace)
20+
WORKSPACE="$2"
21+
shift 2
22+
;;
23+
--include-verbose)
24+
INCLUDE_VERBOSE=true
25+
shift
26+
;;
27+
*)
28+
echo "Unknown option: $1"
29+
exit 1
30+
;;
31+
esac
32+
done
33+
34+
TENANT="${TENANT:-$AZURE_ORIGINAL_TENANT_ID}"
35+
SUBSCRIPTION="${SUBSCRIPTION:-$AZURE_ORIGINAL_SUBSCRIPTION_ID}"
36+
RESOURCE_GROUP="${RESOURCE_GROUP:-$AZURE_ORIGINAL_RESOURCE_GROUP}"
37+
WORKSPACE="${WORKSPACE:-$AZURE_ORIGINAL_WORKSPACE_NAME}"
38+
39+
if [[ -z "$TENANT" || -z "$SUBSCRIPTION" || -z "$RESOURCE_GROUP" || -z "$WORKSPACE" ]]; then
40+
read -p "Start with existing Project connections? [NOTE: This action cannot be undone after executing. To revert, create a new AZD environment and run the process again.] (yes/no) " response
41+
if [[ "$response" == "yes" ]]; then
42+
[[ -z "$TENANT" ]] && read -p "Enter Tenant ID: " TENANT
43+
[[ -z "$SUBSCRIPTION" ]] && read -p "Enter Subscription ID: " SUBSCRIPTION
44+
[[ -z "$RESOURCE_GROUP" ]] && read -p "Enter Resource Group: " RESOURCE_GROUP
45+
[[ -z "$WORKSPACE" ]] && read -p "Enter Workspace / Project Name: " WORKSPACE
46+
else
47+
echo "Not starting with existing Project. Exiting script."
48+
exit 0
49+
fi
50+
else
51+
echo "All parameters provided. Starting with existing Project ${WORKSPACE}."
52+
fi
53+
54+
if [[ -z "$TENANT" || -z "$SUBSCRIPTION" || -z "$RESOURCE_GROUP" || -z "$WORKSPACE" ]]; then
55+
echo "Unable to start with existing Project: One or more required parameters are missing."
56+
exit 1
57+
fi
58+
59+
az account set --subscription "$SUBSCRIPTION"
60+
61+
TOKEN=$(az account get-access-token --resource https://management.azure.com --query accessToken -o tsv)
62+
if [[ -z "$TOKEN" ]]; then
63+
echo "Failed to get Azure access token."
64+
exit 1
65+
fi
66+
67+
CONNECTIONS_URL="https://management.azure.com/subscriptions/$SUBSCRIPTION/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.MachineLearningServices/workspaces/$WORKSPACE/connections?api-version=2024-10-01"
68+
CONNECTIONS_RESPONSE=$(curl -s -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" "$CONNECTIONS_URL")
69+
CONNECTIONS=$(echo "$CONNECTIONS_RESPONSE" | jq '.value')
70+
71+
echo "Connections in workspace ${WORKSPACE}"
72+
echo "----------------------------------"
73+
CONNECTION_COUNT=$(echo "$CONNECTIONS" | jq 'length')
74+
echo "Connection count: $CONNECTION_COUNT"
75+
if [[ "$CONNECTION_COUNT" -eq 0 ]]; then
76+
echo "No connections found in the workspace."
77+
exit 0
78+
fi
79+
80+
if [[ "$INCLUDE_VERBOSE" == true ]]; then
81+
echo "Connections response:"
82+
echo "$CONNECTIONS"
83+
fi
84+
echo "----------------------------------"
85+
86+
COGSVC_URL="https://management.azure.com/subscriptions/$SUBSCRIPTION/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.CognitiveServices/accounts/?api-version=2023-05-01"
87+
COGSVC_RESPONSE=$(curl -s -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" "$COGSVC_URL")
88+
COGSVC_ACCOUNTS=$(echo "$COGSVC_RESPONSE" | jq '.value')
89+
90+
echo "Cognitive Service Accounts in resource group ${RESOURCE_GROUP}"
91+
echo "----------------------------------"
92+
COGSVC_COUNT=$(echo "$COGSVC_ACCOUNTS" | jq 'length')
93+
echo "Cognitive Service Account count: $COGSVC_COUNT"
94+
if [[ "$COGSVC_COUNT" -eq 0 ]]; then
95+
echo "No Cognitive Service Accounts found in the resource group."
96+
exit 0
97+
fi
98+
99+
if [[ "$INCLUDE_VERBOSE" == true ]]; then
100+
echo "Cognitive Service Accounts response:"
101+
echo "$COGSVC_ACCOUNTS"
102+
fi
103+
104+
for i in $(seq 0 $(($COGSVC_COUNT - 1))); do
105+
ACCOUNT_NAME=$(echo "$COGSVC_ACCOUNTS" | jq -r ".[$i].name")
106+
NORMALIZED_ACCOUNT_NAME=$(echo "$ACCOUNT_NAME" | tr -d '-_')
107+
echo "Normalized Cognitive Service Account Name: $NORMALIZED_ACCOUNT_NAME"
108+
done
109+
echo "----------------------------------"
110+
111+
echo "Connections details:"
112+
echo "----------------------------------"
113+
for i in $(seq 0 $(($CONNECTION_COUNT - 1))); do
114+
NAME=$(echo "$CONNECTIONS" | jq -r ".[$i].name")
115+
AUTHTYPE=$(echo "$CONNECTIONS" | jq -r ".[$i].properties.authType")
116+
CATEGORY=$(echo "$CONNECTIONS" | jq -r ".[$i].properties.category")
117+
TARGET=$(echo "$CONNECTIONS" | jq -r ".[$i].properties.target")
118+
119+
echo "Name: $NAME"
120+
echo "AuthType: $AUTHTYPE"
121+
echo "Category: $CATEGORY"
122+
echo "Target: $TARGET"
123+
124+
if [[ "$CATEGORY" == "CognitiveSearch" ]]; then
125+
azd env set 'AZURE_AI_SEARCH_ENABLED' 'true'
126+
echo "Environment variable AZURE_AI_SEARCH_ENABLED set to true"
127+
fi
128+
129+
if [[ "$CATEGORY" == "CognitiveService" ]]; then
130+
for j in $(seq 0 $(($COGSVC_COUNT - 1))); do
131+
ACCOUNT_NAME=$(echo "$COGSVC_ACCOUNTS" | jq -r ".[$j].name")
132+
NORMALIZED_ACCOUNT_NAME=$(echo "$ACCOUNT_NAME" | tr -d '-_')
133+
if [[ "$NORMALIZED_ACCOUNT_NAME" == "$NAME" ]]; then
134+
RESOURCE_NAME="$ACCOUNT_NAME"
135+
KIND=$(echo "$COGSVC_ACCOUNTS" | jq -r ".[$j].kind")
136+
echo "Matched Cognitive Service Account - Connection: '$NAME' Resource: $RESOURCE_NAME"
137+
case "$KIND" in
138+
ContentSafety)
139+
azd env set 'AZURE_AI_CONTENT_SAFETY_ENABLED' 'true'
140+
echo "Environment variable AZURE_AI_CONTENT_SAFETY_ENABLED set to true"
141+
;;
142+
SpeechServices)
143+
azd env set 'AZURE_AI_SPEECH_ENABLED' 'true'
144+
echo "Environment variable AZURE_AI_SPEECH_ENABLED set to true"
145+
;;
146+
FormRecognizer)
147+
azd env set 'AZURE_AI_DOC_INTELLIGENCE_ENABLED' 'true'
148+
echo "Environment variable AZURE_AI_DOC_INTELLIGENCE_ENABLED set to true"
149+
;;
150+
ComputerVision)
151+
azd env set 'AZURE_AI_VISION_ENABLED' 'true'
152+
echo "Environment variable AZURE_AI_VISION_ENABLED set to true"
153+
;;
154+
TextAnalytics)
155+
azd env set 'AZURE_AI_LANGUAGE_ENABLED' 'true'
156+
echo "Environment variable AZURE_AI_LANGUAGE_ENABLED set to true"
157+
;;
158+
TextTranslation)
159+
azd env set 'AZURE_AI_TRANSLATOR_ENABLED' 'true'
160+
echo "Environment variable AZURE_AI_TRANSLATOR_ENABLED set to true"
161+
;;
162+
*)
163+
echo "Unknown resource kind: $KIND"
164+
;;
165+
esac
166+
fi
167+
done
168+
fi
169+
echo "-------------------------"
170+
done
171+
echo "----------------------------------"

0 commit comments

Comments
 (0)