Skip to content

Commit c076828

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

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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
17+
18+
.EXAMPLE
19+
.\Get-ConfigStorageKey.ps1 -Name stracc -useSecondary
20+
21+
#>
22+
23+
Param(
24+
[Parameter(Mandatory = $true)]
25+
[String]$Name,
26+
[Parameter(Mandatory = $false)]
27+
[switch]$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+
try {
34+
35+
Write-Log -LogLevel Information -Message "Checking for existing Storage Account"
36+
# --- Check if storage account exists in our subscription
37+
38+
$StorageAccount = Get-AzureRmResource -ResourceName $Name -ErrorAction SilentlyContinue
39+
40+
# --- If the Storage Account doesn't exist, erorr
41+
if (!$StorageAccount) {
42+
Write-Log -LogLevel Information -Message "StorageAccount $Name Does not exist"
43+
}
44+
45+
# --- If the storage account exists in this subscription get the key and set the env variable
46+
if ($StorageAccount -and !$UseSecondary ) {
47+
$Key = (Invoke-AzureRmResourceAction -Action listKeys -ResourceType "Microsoft.ClassicStorage/storageAccounts" -ApiVersion "2016-11-01" -ResourceGroupName $($StorageAccount.ResourceGroupName) -ResourceName $($StorageAccount.Name) -force).primaryKey
48+
$ConnectionString = "DefaultEndpointsProtocol=https;AccountName=$($Name);AccountKey=$($Key)"
49+
Write-Output ("##vso[task.setvariable variable=ConfigurationStorageConnectionString;issecret=true]$($ConnectionString)")
50+
}
51+
elseif ($StorageAccount) {
52+
$Key = (Invoke-AzureRmResourceAction -Action listKeys -ResourceType "Microsoft.ClassicStorage/storageAccounts" -ApiVersion "2016-11-01" -ResourceGroupName $($StorageAccount.ResourceGroupName) -ResourceName $($StorageAccount.Name) -force).secondaryKey
53+
$ConnectionString = "DefaultEndpointsProtocol=https;AccountName=$($Name);AccountKey=$($Key)"
54+
Write-Output ("##vso[task.setvariable variable=ConfigurationStorageConnectionString;issecret=true]$($ConnectionString)")
55+
}
56+
else {
57+
Write-log -LogLevel Information -Message "No Account Found"
58+
}
59+
}
60+
catch {
61+
throw "$_"
62+
}
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)