Skip to content

Commit ca7ebf7

Browse files
authored
Merge pull request #81 from SkillsFundingAgency/DASD-3104_appid
DASD-3104 Annotations for App Insights: Added App Id to New-ApplicationInsights and new script Get-ApplicationInsightsApiKey
2 parents e19a38c + adecfe1 commit ca7ebf7

4 files changed

Lines changed: 101 additions & 13 deletions
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<#
2+
3+
.SYNOPSIS
4+
Gets an App Insight API key for annotations
5+
6+
.DESCRIPTION
7+
Gets an App Insight API key for annotations
8+
9+
.PARAMETER AppInsightName
10+
The name of the App Insight resource
11+
12+
.PARAMETER ResourceGroupName
13+
The name of the Resource Group of the App Insight resource
14+
15+
.PARAMETER KeyName
16+
Optional name for the API key (defaults to VSTSAnnotateKey)
17+
18+
.EXAMPLE
19+
20+
#>
21+
22+
param (
23+
[Parameter(Mandatory=$true)]
24+
[String[]]$AppInsightName,
25+
[Parameter(Mandatory = $false)]
26+
[ValidateNotNullOrEmpty()]
27+
[String]$ResourceGroupName = $ENV:ResourceGroup,
28+
[Parameter(Mandatory = $false)]
29+
[String]$KeyName = "VSTSAnnotateKey"
30+
)
31+
32+
# --- Import Helpers
33+
Import-Module (Resolve-Path -Path $PSScriptRoot\..\Modules\Helpers.psm1).Path
34+
35+
foreach ($Service in $AppInsightName) {
36+
37+
Get-AzureRmApplicationInsightsApiKey -ResourceGroupName $ResourceGroupName -Name $Service |
38+
Where-Object { $_.Description -eq $KeyName } | ForEach-Object {
39+
Write-Log -LogLevel Information -Message "Deleting $($_.Description) [$($_.Id)]"
40+
Remove-AzureRmApplicationInsightsApiKey -ResourceGroupName $ResourceGroupName -Name $Service -ApiKeyId $_.Id
41+
}
42+
43+
$apikey = New-AzureRmApplicationInsightsApiKey -ResourceGroupName $ResourceGroupName -Name $Service -Permissions WriteAnnotations -Description $KeyName
44+
Write-Output "##vso[task.setvariable variable=appInsightApiKey-$($Service);]$($apikey.ApiKey)"
45+
46+
}

Infrastructure/Resources/New-ApplicationInsights.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ foreach ($Service in $Name) {
7676
}
7777
if ((((Get-Module AzureRM -ListAvailable | Sort-Object { $_.Version.Major } -Descending).Version.Major))[0] -gt 5) {
7878
Write-Output ("##vso[task.setvariable variable=InstrumentationKey-$($Service);]$($ApplicationInsights.InstrumentationKey)")
79+
Write-Output ("##vso[task.setvariable variable=AppId-$($Service);]$($ApplicationInsights.AppId)")
7980
}
8081
else {
8182
Write-Output ("##vso[task.setvariable variable=InstrumentationKey-$($Service);]$($ApplicationInsights.Properties.InstrumentationKey)")
83+
Write-Output ("##vso[task.setvariable variable=AppId-$($Service);]$($ApplicationInsights.Properties.AppId)")
8284
}
8385
}
Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
11
$Config = Get-Content $PSScriptRoot\..\Tests\Acceptance.Config.json -Raw | ConvertFrom-Json
22
Push-Location -Path $PSScriptRoot\..\Infrastructure\Resources\
33

4+
$ValidGuid = [System.Guid]::NewGuid()
5+
46
Describe "New-ApplicationInsights Tests" -Tag "Acceptance-ARM" {
57

68
$ResourceGroupName = "$($Config.ResourceGroupName)$($Config.suffix)"
79
$AppInsightsName = "$($Config.appInsightsName)$($Config.suffix)"
810

9-
It "Should create an ApplicationInsights and return a valid instrumentation key" {
11+
It "Should create an ApplicationInsights and return a valid Instrumentation Key and App Id" {
1012
$Result = .\New-ApplicationInsights.ps1 -Location $Config.location -Name $AppInsightsName -ResourceGroupName $ResourceGroupName
11-
$Result.Count | Should Be 1
12-
$Result.Contains("InstrumentationKey") | Should Be $true
13-
$Result = $Result.Split("]")[1]
14-
$ValidGuid = [System.Guid]::NewGuid()
15-
$Success = [System.Guid]::TryParse($Result, [ref]$ValidGuid);
13+
$Result.Count | Should Be 2
14+
$ik = $Result | Where-Object { $_.Contains("InstrumentationKey") }
15+
$ik.Contains("InstrumentationKey") | Should Be $true
16+
$ikguid = $ik.Split("]")[1]
17+
$Success = [System.Guid]::TryParse($ikguid, [ref]$ValidGuid);
18+
$Success | Should Be $true
19+
$aid = $Result | Where-Object { $_.Contains("AppId") }
20+
$aid.Contains("AppId") | Should Be $true
21+
$aidguid = $aid.Split("]")[1]
22+
$Success = [System.Guid]::TryParse($aidguid, [ref]$ValidGuid);
1623
$Success | Should Be $true
1724
}
1825

19-
It "Should return a valid instrumentation key on subsequent runs" {
26+
It "Should return a valid Instrumentation Key and App Id on subsequent runs" {
2027
$Result = .\New-ApplicationInsights.ps1 -Location $Config.location -Name $AppInsightsName -ResourceGroupName $ResourceGroupName
21-
$Result.Count | Should Be 1
22-
$Result.Contains("InstrumentationKey") | Should Be $true
23-
$Result = $Result.Split("]")[1]
24-
$ValidGuid = [System.Guid]::NewGuid()
25-
$Success = [System.Guid]::TryParse($Result, [ref]$ValidGuid);
28+
$Result.Count | Should Be 2
29+
$ik = $Result | Where-Object { $_.Contains("InstrumentationKey") }
30+
$ik.Contains("InstrumentationKey") | Should Be $true
31+
$ikguid = $ik.Split("]")[1]
32+
$Success = [System.Guid]::TryParse($ikguid, [ref]$ValidGuid);
33+
$Success | Should Be $true
34+
$aid = $Result | Where-Object { $_.Contains("AppId") }
35+
$aid.Contains("AppId") | Should Be $true
36+
$aidguid = $aid.Split("]")[1]
37+
$Success = [System.Guid]::TryParse($aidguid, [ref]$ValidGuid);
2638
$Success | Should Be $true
2739
}
2840
}
2941

30-
Pop-Location
42+
Pop-Location
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
$Config = Get-Content $PSScriptRoot\..\Tests\Acceptance.Config.json -Raw | ConvertFrom-Json
2+
Push-Location -Path $PSScriptRoot\..\Infrastructure\Resources\
3+
4+
# requires AT006.New-ApplicationInsights.Acceptance.Tests.ps1 to have ran first!
5+
$global:firstkey = $null
6+
7+
Describe "Get-ApplicationInsightsApiKey Tests" -Tag "Acceptance-ARM" {
8+
9+
$ResourceGroupName = "$($Config.ResourceGroupName)$($Config.suffix)"
10+
$AppInsightsName = "$($Config.appInsightsName)$($Config.suffix)"
11+
12+
It "Should return an API Key" {
13+
$Result = .\Get-ApplicationInsightsApiKey.ps1 -AppInsightName $AppInsightsName -ResourceGroupName $ResourceGroupName
14+
$Result.Count | Should Be 1
15+
$Result.Contains("appInsightApiKey") | Should Be $true
16+
$global:firstkey = $Result.Split("]")[1]
17+
}
18+
19+
It "Should generate new API Key on subsequent runs" {
20+
$Result = .\Get-ApplicationInsightsApiKey.ps1 -AppInsightName $AppInsightsName -ResourceGroupName $ResourceGroupName
21+
$Result.Count | Should Be 1
22+
$Result.Contains("appInsightApiKey") | Should Be $true
23+
$Result.Split("]")[1] | Should Not Be $global:firstkey
24+
}
25+
26+
}
27+
28+
Pop-Location

0 commit comments

Comments
 (0)