Skip to content

Commit 0978493

Browse files
committed
Added Automation to Return Storage String
1 parent 080815f commit 0978493

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<#
2+
3+
.SYNOPSIS
4+
Return either the Primary or Seconday connection String to the Consig Storage account and Write to a VSTS variable.
5+
6+
.DESCRIPTION
7+
Return either the Primary or Seconday connection String to the Consig Storage account and Write to a VSTS variable.
8+
9+
.PARAMETER Name
10+
The name of the Storage Account
11+
12+
.PARAMETER useSecondary
13+
Boolean Switch to Return Secondary String
14+
15+
.EXAMPLE
16+
.\Get-ConfigStorageKey.ps1 -Name stracc - -useSecondary $false
17+
18+
.EXAMPLE
19+
.\Get-ConfigStorageKey.ps1 -Name stracc -useSecondary $true
20+
21+
#>
22+
23+
Param(
24+
[Parameter(Mandatory = $true)]
25+
[String]$Name,
26+
[Parameter(Mandatory = $false)]
27+
[bool]$useSecondary = $false)
28+
29+
30+
# --- Import Azure H
31+
Import-Module (Resolve-Path -Path $PSScriptRoot\..\Modules\Azure.psm1).Path
32+
Import-Module (Resolve-Path -Path $PSScriptRoot\..\Modules\Helpers.psm1).Path
33+
34+
Write-Log -LogLevel Information -Message "Checking for existing Storage Account"
35+
# --- Check if storage account exists in our subscription
36+
$StorageAccount = Get-AzureRmResource -ResourceName $Name -ErrorAction SilentlyContinue
37+
38+
# --- If the Storage Account doesn't exist, erorr
39+
if (!$StorageAccount) {
40+
Write-Log -LogLevel Information -Message "StorageAccount $Name Does not exist"
41+
}
42+
43+
# --- If the storage account exists in this subscription get the key and set the env variable
44+
if ($StorageAccount -and !$useSecondary ) {
45+
#$ResourceGroup = $($StorageAccount.ResourceGroupName)
46+
$Key = (Invoke-AzureRmResourceAction -Action listKeys -ResourceType "Microsoft.ClassicStorage/storageAccounts" -ApiVersion "2016-11-01" -ResourceGroupName $($StorageAccount.ResourceGroupName) -ResourceName $($StorageAccount.Name) -force).primaryKey
47+
$ConnectionString = "DefaultEndpointsProtocol=https;AccountName=$($Name);AccountKey=$($Key)"
48+
Write-Output ("##vso[task.setvariable variable=ConfigurationStorageConnectionString;]$($ConnectionString)")
49+
} else {
50+
#$ResourceGroup = $StorageAccount.ResourceGroupName
51+
$Key = (Invoke-AzureRmResourceAction -Action listKeys -ResourceType "Microsoft.ClassicStorage/storageAccounts" -ApiVersion "2016-11-01" -ResourceGroupName $($StorageAccount.ResourceGroupName) -ResourceName $($StorageAccount.Name) -force).secondaryKey
52+
$ConnectionString = "DefaultEndpointsProtocol=https;AccountName=$($Name);AccountKey=$($Key)"
53+
Write-Output ("##vso[task.setvariable variable=ConfigurationStorageConnectionString;]$($ConnectionString)")
54+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
$Config = Get-Content $PSScriptRoot\..\Tests\Acceptance.Config.json -Raw | ConvertFrom-Json
2+
Push-Location -Path $PSScriptRoot\..\Infrastructure\Resources\
3+
4+
# requires AT003.New-ClassicStorage.Acceptance.Tests.ps1 to have ran first!
5+
Describe "GetConfigStorageKey Tests" -Tag "Acceptance-ARM" {
6+
7+
$StorageAccountName = "$($Config.classicStorageAccountName)$($Config.suffix)"
8+
9+
It "Should return one output with one parameter" {
10+
$Result = .\Get-ConfigStorageKey.ps1 -name $StorageAccountName
11+
$Result.Count | Should Be 1
12+
}
13+
14+
It "Should return one output with two parameter" {
15+
16+
$Result = .\Get-ConfigStorageKey.ps1 -name $StorageAccountName -useSecondary $true
17+
$Result.Count | Should Be 1
18+
}
19+
}
20+
21+
Pop-Location

0 commit comments

Comments
 (0)