Skip to content

Commit 0b56661

Browse files
sw-joelmutgabog
andauthored
[microsoft#476] Feedback on ConfigureAppSettings.ps1 util (microsoft#484)
* Add support for Subscription, updates ResourceSuffix and general tool improvement * Fix ConfigureAppSettings typos Co-authored-by: Gabo Gilabert <[email protected]>
1 parent 9c98b3a commit 0b56661

File tree

2 files changed

+168
-37
lines changed

2 files changed

+168
-37
lines changed

Tests/SkillFunctionalTests/Utils/ConfigureAppSettings.ps1

Lines changed: 117 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,48 @@
66
Gets DirectLine Secrets from Azure.
77
88
.DESCRIPTION
9-
Configure AppSettings file with DirectLine Secrets gathered from Azure Bot Resources based on the Resource Group and Resource Suffix provided by the user.
9+
Configure AppSettings file with DirectLine Secrets gathered from Azure Bot Resources based on the Resource Group, Resource Suffix and Subscription provided by the user.
1010
1111
.PARAMETER ResourceGroup
12-
Specifies the name for the specific Resource Group where the resources are deployed at.
12+
Specifies the Name for the specific Resource Group where the resources are deployed. For each specific language will concatenate (DotNet, JS and Python). Default (BFFN).
1313
1414
.PARAMETER ResourceSuffix
15-
Specifies the suffix the resources name are built with.
15+
Specifies the Suffix used to concatenate at the end of the Resource Name followed up by the ResourceSuffixSeparator.
16+
17+
.PARAMETER Subscription
18+
Specifies the Name or Id of the Subscription where the resources are located. Default (current Subscription).
19+
20+
.PARAMETER ResourceSuffixSeparator
21+
Specifies the separator used for the Suffix to split the Resource Name from the ResourceSuffix. Default (-).
22+
23+
.EXAMPLE
24+
PS> .\ConfigureAppSettings.ps1 -ResourceGroup "bffnbots" -ResourceSuffix "{buildId}"
25+
26+
.EXAMPLE
27+
PS> .\ConfigureAppSettings.ps1 -ResourceGroup "bffnbots"
1628
1729
.EXAMPLE
18-
PS> .\ConfigureAppSettings.ps1 -ResourceGroup "BFFN-bots" -ResourceSuffix "{suffix}-{buildId}"
30+
PS> .\ConfigureAppSettings.ps1 -Subscription 00000000-0000-0000-0000-000000000000
1931
2032
.EXAMPLE
21-
PS> .\ConfigureAppSettings.ps1 -ResourceGroup "BFFN-bots"
33+
PS> .\ConfigureAppSettings.ps1 -Subscription bffnbots-subscription
34+
35+
.EXAMPLE
36+
PS> .\ConfigureAppSettings.ps1 -ResourceSuffixSeparator "" -ResourceSuffix "microsoft-396"
2237
2338
.EXAMPLE
2439
PS> .\ConfigureAppSettings.ps1
2540
#>
2641

2742
param (
28-
[Parameter(Mandatory=$false)]
29-
[string]$ResourceGroup,
30-
[Parameter(Mandatory=$false)]
31-
[string]$ResourceSuffix
43+
[Parameter(Mandatory = $false)]
44+
[string]$Subscription,
45+
[Parameter(Mandatory = $false)]
46+
[string]$ResourceGroup,
47+
[Parameter(Mandatory = $false)]
48+
[string]$ResourceSuffixSeparator = "-",
49+
[Parameter(Mandatory = $false)]
50+
[string]$ResourceSuffix
3251
)
3352

3453
$PSVersion = $PSVersionTable.PSVersion;
@@ -70,6 +89,69 @@ if ($LoginOutput) {
7089
exit 1;
7190
}
7291

92+
# Subscription Input
93+
if ([string]::IsNullOrEmpty($Subscription)) {
94+
$Subscriptions = @(az account list | ConvertFrom-Json);
95+
96+
if (-not $Subscriptions) {
97+
Write-Host "There are no Subscriptions available." -ForegroundColor Red;
98+
exit 1;
99+
}
100+
101+
$SubscriptionDefault = ($Subscriptions | Where-Object { $_.isDefault }) ?? $Subscriptions.GetValue(0);
102+
103+
if ($Subscriptions.Length -gt 1) {
104+
do {
105+
Write-Host "! " -ForegroundColor Yellow -NoNewline;
106+
Write-Host "Multiple Subscriptions were found." -ForegroundColor White;
107+
108+
$Subscriptions | Format-Table @(
109+
@{Label = ' # '; Expression = { [array]::IndexOf($Subscriptions, $_) + 1 } },
110+
@{Label = 'Name'; Expression = { $_.name } },
111+
@{Label = 'Id'; Expression = { $_.id } },
112+
@{Label = 'Default'; Expression = { $_.isDefault } }
113+
) -AutoSize
114+
115+
Write-Host "? " -ForegroundColor Green -NoNewline;
116+
Write-Host "Enter a Subscription, by providing '#', 'Name' or 'Id': " -ForegroundColor White -NoNewline;
117+
Write-Host "default (" -ForegroundColor DarkGray -NoNewline;
118+
Write-Host $SubscriptionDefault.name -ForegroundColor Magenta -NoNewline;
119+
Write-Host ") " -ForegroundColor DarkGray -NoNewline;
120+
121+
$UserInput = (Read-Host).Trim();
122+
123+
if ([string]::IsNullOrEmpty($UserInput)) {
124+
$SubscriptionInput = $SubscriptionDefault;
125+
break;
126+
}
127+
else {
128+
$SubscriptionInput = $Subscriptions | Where-Object {
129+
$Sub = $_;
130+
$Number = [array]::IndexOf($Subscriptions, $Sub) + 1;
131+
if (@($Number, $Sub.id, $Sub.name) -contains $UserInput) {
132+
return $true;
133+
}
134+
}
135+
136+
if ($SubscriptionInput) {
137+
break;
138+
}
139+
else {
140+
Write-Host " - A Subscription must be provided, could be either ('#', 'Name' or 'Id').`n" -ForegroundColor Red;
141+
}
142+
}
143+
} while ($true)
144+
145+
$Inputs.Subscription = $SubscriptionInput.name;
146+
}
147+
else {
148+
$Inputs.Subscription = $SubscriptionDefault.name;
149+
}
150+
}
151+
else {
152+
$Inputs.Subscription = $Subscription;
153+
}
154+
73155
# Resource Group Input
74156
if ([string]::IsNullOrEmpty($ResourceGroup)) {
75157
Write-Host "? " -ForegroundColor Green -NoNewline;
@@ -83,6 +165,9 @@ else {
83165
$Inputs.ResourceGroup = $ResourceGroup;
84166
}
85167

168+
# Resource Suffix Separator Input
169+
$Inputs.ResourceSuffixSeparator = $ResourceSuffixSeparator;
170+
86171
# Resource Suffix Input
87172
if ([string]::IsNullOrEmpty($ResourceSuffix)) {
88173
do {
@@ -107,10 +192,14 @@ if ([string]::IsNullOrEmpty($Inputs.ResourceGroup)) {
107192
}
108193

109194
Write-Host "`nSummary" -ForegroundColor Cyan;
110-
Write-Host " - Resource Group : " -ForegroundColor Gray -NoNewline;
195+
Write-Host " - Subscription : " -ForegroundColor Gray -NoNewline;
196+
Write-Host $Inputs.Subscription -ForegroundColor Magenta;
197+
Write-Host " - Resource Group : " -ForegroundColor Gray -NoNewline;
111198
Write-Host $Inputs.ResourceGroup -ForegroundColor Magenta;
112-
Write-Host " - Resource Suffix : " -ForegroundColor Gray -NoNewline;
199+
Write-Host " - Resource Suffix : " -ForegroundColor Gray -NoNewline;
113200
Write-Host $Inputs.ResourceSuffix -ForegroundColor Magenta;
201+
Write-Host " - Resource Suffix Separator : " -ForegroundColor Gray -NoNewline;
202+
Write-Host $Inputs.ResourceSuffixSeparator -ForegroundColor Magenta;
114203

115204
# Read AppSettings
116205
Start-Sleep -Milliseconds 300;
@@ -138,27 +227,28 @@ Write-Host " - Looking for Bot Resources existence..." -ForegroundColor Gray;
138227
$NonExistingResources = $AppSettings.HostBotClientOptions.PSObject.Properties | ForEach-Object -Parallel {
139228
$GroupsSuffix = $using:Settings.GroupsSuffix;
140229
$ResourceGroup = $using:Inputs.ResourceGroup;
230+
$ResourceSuffixSeparator = $using:Inputs.ResourceSuffixSeparator;
141231
$ResourceSuffix = $using:Inputs.ResourceSuffix;
232+
$Subscription = $using:Inputs.Subscription;
142233

143234
$Bot = $_;
144235
$BotId = "bffn$($Bot.Name)".ToLower();
145-
$Resource = "$BotId$ResourceSuffix";
236+
$Resource = "$BotId$ResourceSuffixSeparator$ResourceSuffix";
146237
$ResourceGroupSuffix = $GroupsSuffix | Where-Object { $Bot.Name -like "*$($_)*" }
147238
$ResourceGroup = "$ResourceGroup-$ResourceGroupSuffix";
148239

149-
$exists = (az webapp show --name $Resource --resource-group $ResourceGroup 2>$null | ConvertFrom-Json).enabled;
240+
$exists = (az webapp show --name $Resource --resource-group $ResourceGroup --subscription $Subscription 2>$null | ConvertFrom-Json).enabled;
150241

151242
return [PSCustomObject]@{
152-
Bot = $Bot.Name
243+
BotId = $Resource
153244
'Resource Group' = $ResourceGroup
154-
Resource = $Resource
155245
Exists = if ($exists) { $true } else { $false }
156246
}
157-
} | Where-Object { $_.Exists -eq $false }
247+
} | Where-Object { $_.Exists -eq $false } | Sort-Object -Property BotId
158248

159249
if ($NonExistingResources) {
160250
Write-Host "`nThe following Bot Resources were not found. Check if they're still available in Azure." -ForegroundColor Red;
161-
$NonExistingResources | Select-Object 'Resource Group', 'Resource' | Format-Table -AutoSize;
251+
$NonExistingResources | Select-Object 'BotId', 'Resource Group' | Format-Table -AutoSize;
162252
exit 1;
163253
}
164254
else {
@@ -172,17 +262,19 @@ Write-Host " - Getting DirectLine Secrets from Bot Resources..." -ForegroundCol
172262
$AppSettings.HostBotClientOptions.PSObject.Properties | ForEach-Object -Parallel {
173263
$GroupsSuffix = $using:Settings.GroupsSuffix;
174264
$ResourceGroup = $using:Inputs.ResourceGroup;
265+
$ResourceSuffixSeparator = $using:Inputs.ResourceSuffixSeparator;
175266
$ResourceSuffix = $using:Inputs.ResourceSuffix;
267+
$Subscription = $using:Inputs.Subscription;
176268
$BotSettings = $using:Settings.BotSettings;
177269
$BotResources = $using:Settings.BotResources;
178270

179271
$Bot = $_;
180272
$BotId = "bffn$($Bot.Name)".ToLower();
181-
$Resource = "$BotId$ResourceSuffix";
273+
$Resource = "$BotId$ResourceSuffixSeparator$ResourceSuffix";
182274
$ResourceGroupSuffix = $GroupsSuffix | Where-Object { $Bot.Name -like "*$($_)*" };
183275
$ResourceGroup = "$ResourceGroup-$ResourceGroupSuffix";
184276

185-
$DirectLine = (az bot directline show --name $Resource --resource-group $ResourceGroup --with-secrets true 2>$null | ConvertFrom-Json).properties.properties.sites.key;
277+
$DirectLine = (az bot directline show --name $Resource --resource-group $ResourceGroup --subscription $Subscription --with-secrets true 2>$null | ConvertFrom-Json).properties.properties.sites.key;
186278

187279
$BotResource = @{
188280
Resource = $Resource
@@ -197,7 +289,9 @@ $AppSettings.HostBotClientOptions.PSObject.Properties | ForEach-Object -Parallel
197289
$BotSettings.TryAdd($Bot.Name, $Settings) 1>$null;
198290
}
199291

200-
$AppSettings.HostBotClientOptions = $Settings.BotSettings;
292+
$SortedBotSettings = [ordered]@{};
293+
$Settings.BotSettings.GetEnumerator() | Sort-Object -Property Key | ForEach-Object { $SortedBotSettings[$_.Key] = $_.Value };
294+
$AppSettings.HostBotClientOptions = $SortedBotSettings;
201295

202296
$AppSettings | ConvertTo-Json | Set-Content $Settings.AppSettingsDevPath;
203297
Write-Host " - AppSettings successfully configured" -ForegroundColor Gray;
@@ -207,11 +301,10 @@ Start-Sleep -Milliseconds 300;
207301
Write-Host "`nConfiguration saved" -ForegroundColor Cyan;
208302
$Settings.BotResources.GetEnumerator() | ForEach-Object {
209303
return [PSCustomObject]@{
210-
Bot = $_.Key
211-
'Resource Group' = $_.Value.ResourceGroup
212-
Resource = $_.Value.Resource
304+
BotId = $_.Value.Resource
213305
'DirectLine Secret' = $Settings.BotSettings[$_.Key].DirectLineSecret
306+
'Resource Group' = $_.Value.ResourceGroup
214307
}
215-
} | Format-Table -AutoSize;
308+
} | Sort-Object -Property BotId | Format-Table -AutoSize;
216309

217310
Write-Host "Process Finished!" -ForegroundColor Green;

Tests/SkillFunctionalTests/Utils/README.md

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,59 @@
22

33
## ConfigureAppSettings
44

5-
This script configures the _appsettings.Development.json_ file from the SkillsFunctionalTest project to be able to run the tests locally connecting them with bots deployed in Azure. Its behavior consists on gathering the DirectLine Secrets from the deployed Azure Bot Resources and configure this file with them. For its purpose it uses the '**ResourceGroup**' and '**ResourceSuffix**' provided by the user when running it.
5+
### Description
66

7-
>**ResourceGroup**: the specific Resource Group where the Azure Resource Bots are deployed in. The default value is 'BFFN'. _Eg: bffnbots_
8-
>
9-
>**ResourceSuffix**: the suffix the resources name is built with. It will be a combination of the suffix itself and the build id where the resource was created. _Eg: gitali-218_
7+
This script configures the `appsettings.Development.json` file from the SkillsFunctionalTest project to be able to run the tests locally, connecting them with bots deployed in Azure. Its behavior consists on gathering the DirectLine Secrets from the deployed Azure Bot Resources and configure this file with them.
108

11-
The user can execute the script providing one or both parameters and the process will start automatically, or execute it without parameters and provide them using the prompt.
9+
For the process to be able to find the resources, the following inputs must be provided.
1210

13-
<p align="center">
14-
<img src="https://user-images.githubusercontent.com/54330145/124808628-899d9a00-df35-11eb-9b1c-bc88c352636a.png" />
15-
</p>
11+
### Requirements
1612

17-
The script will communicate with Azure through the **azure-cli** tool and gather the DirectLine secrets from each Azure Resource Bot deployed in the Resource Group resultant from the combination of the provided one with the language (DotNet, JS or Python) extracted from the _HostBotClientOptions_ keys, and '**ResourceSuffix**' parameter.
13+
- [Azure CLI][azure-cli]
14+
- [PowerShell 7+][powershell]
1815

19-
Here you can see the entire process being executed
20-
<p align="center">
21-
<img src="https://user-images.githubusercontent.com/54330145/124788742-0756ab00-df20-11eb-8eea-8f54c07708f1.png" />
22-
</p>
16+
### Inputs
17+
18+
| Input | Description | Condition | Default | Example |
19+
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | -------------------- | ----------------------------------------------------------------- |
20+
| ResourceGroup | The Name for the specific Resource Group where the resources are deployed. For each specific language will concatenate (DotNet, JS and Python). | Required | BFFN | "bffnbots" |
21+
| ResourceSuffix | The Suffix used to concatenate at the end of the Resource Name followed up by the ResourceSuffixSeparator. | Required | | "microsoft-396" |
22+
| Subscription | The Name or Id of the Subscription where the resources are located. | Optional | Current Subscription | "00000000-0000-0000-0000-000000000000" or "bffnbots-subscription" |
23+
| ResourceSuffixSeparator | The separator used for the Suffix to split the Resource Name from the ResourceSuffix. <br> **Note:** _Only available when providing it through `parameters`_. | Optional | - | "" or "-microsoft-" |
24+
25+
### The inputs can be provided as `prompts` or `parameters`
26+
27+
1. When using `prompts`, it will ask for the `required` inputs to be provided. When no input is entered, it will use the `default (...)` value instead. For more information about, see [Inputs][inputs].
28+
29+
![prompts][prompts]
30+
31+
2. When using `parameters`, all listed inputs can be provided before executing the script. When no input is entered, it will use the `default` value instead. For more information, see [Inputs][inputs].
32+
33+
![parameters][parameters]
34+
35+
### Result
36+
37+
After providing the desired [Inputs][inputs], the script will start looking for the Bot Resources, followed up by gathering each DirectLine Secret from the Azure Bot Resource and set it in the `appsettings.Development.json`. Moreover, all steps will be shown in the terminal as well as the result with each DirectLine Secret set for each Bot.
38+
39+
![sample][sample]
40+
41+
> **Note:** When no `appsettings.Development.json` file is found, it will proceed by generating a copy from the `appsettings.json` baseline file.
42+
43+
> **Note:** Applies to `Subscription` input. When multiple Subscriptions are detected, a prompt to choose the desired one will appear. `Can be entered by Number (#), Name or Id`, otherwise the `default` one will be used instead.
44+
45+
> **Note:** Applies to all `Inputs`. A mix between the two ways (`prompts` and `parameters`) to provide the inputs can be used. _E.g. prompts: ResourceSuffix and ResourceGroup, parameters: Subscription and ResourceSuffixSeparator_.
46+
47+
<!-- Requirements -->
48+
49+
[azure-cli]: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
50+
[powershell]: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.1
51+
52+
<!-- Inputs -->
53+
54+
[inputs]: #inputs
55+
56+
<!-- Images -->
57+
58+
[prompts]: https://user-images.githubusercontent.com/62260472/134236938-b85fd5a1-6e32-4b78-a67f-cf9d8d98c3b4.png
59+
[parameters]: https://user-images.githubusercontent.com/62260472/134376619-aa7c27b7-52e6-4d72-837a-ec92e59afff6.png
60+
[sample]: https://user-images.githubusercontent.com/62260472/134376693-f8c109f8-a735-4be1-a601-5fdfb087078a.png

0 commit comments

Comments
 (0)