Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Infrastructure/Resources/Get-ConfigStorageKey.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<#

.SYNOPSIS
Return either the Primary or Seconday connection String to the Consig Storage account and Write to a VSTS variable.

.DESCRIPTION
Return either the Primary or Seconday connection String to the Consig Storage account and Write to a VSTS variable.

.PARAMETER Name
The name of the Storage Account

.PARAMETER useSecondary
Boolean Switch to Return Secondary String

.EXAMPLE
.\Get-ConfigStorageKey.ps1 -Name stracc

.EXAMPLE
.\Get-ConfigStorageKey.ps1 -Name stracc -useSecondary

#>

Param(
[Parameter(Mandatory = $true)]
[String]$Name,
[Parameter(Mandatory = $false)]
[switch]$UseSecondary = $false)


# --- Import Azure H
Import-Module (Resolve-Path -Path $PSScriptRoot\..\Modules\Azure.psm1).Path
Import-Module (Resolve-Path -Path $PSScriptRoot\..\Modules\Helpers.psm1).Path
try {

Write-Log -LogLevel Information -Message "Checking for existing Storage Account"
# --- Check if storage account exists in our subscription

$StorageAccount = Get-AzureRmResource -ResourceName $Name -ErrorAction SilentlyContinue

# --- If the Storage Account doesn't exist, erorr
if (!$StorageAccount) {
Write-Log -LogLevel Information -Message "StorageAccount $Name Does not exist"
}

# --- If the storage account exists in this subscription get the key and set the env variable
if ($StorageAccount -and !$UseSecondary ) {
$Key = (Invoke-AzureRmResourceAction -Action listKeys -ResourceType "Microsoft.ClassicStorage/storageAccounts" -ApiVersion "2016-11-01" -ResourceGroupName $($StorageAccount.ResourceGroupName) -ResourceName $($StorageAccount.Name) -force).primaryKey
$ConnectionString = "DefaultEndpointsProtocol=https;AccountName=$($Name);AccountKey=$($Key)"
Write-Output ("##vso[task.setvariable variable=ConfigurationStorageConnectionString;issecret=true]$($ConnectionString)")
}
elseif ($StorageAccount) {
$Key = (Invoke-AzureRmResourceAction -Action listKeys -ResourceType "Microsoft.ClassicStorage/storageAccounts" -ApiVersion "2016-11-01" -ResourceGroupName $($StorageAccount.ResourceGroupName) -ResourceName $($StorageAccount.Name) -force).secondaryKey
$ConnectionString = "DefaultEndpointsProtocol=https;AccountName=$($Name);AccountKey=$($Key)"
Write-Output ("##vso[task.setvariable variable=ConfigurationStorageConnectionString;issecret=true]$($ConnectionString)")
}
else {
Write-log -LogLevel Information -Message "No Account Found"
}
}
catch {
throw "$_"
}
21 changes: 21 additions & 0 deletions Tests/AT015.Get-ConfigStorageKeyTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
$Config = Get-Content $PSScriptRoot\..\Tests\Acceptance.Config.json -Raw | ConvertFrom-Json
Push-Location -Path $PSScriptRoot\..\Infrastructure\Resources\

# requires AT003.New-ClassicStorage.Acceptance.Tests.ps1 to have ran first!
Describe "GetConfigStorageKey Tests" -Tag "Acceptance-ARM" {

$StorageAccountName = "$($Config.classicStorageAccountName)$($Config.suffix)"

It "Should return one output with one parameter" {
$Result = .\Get-ConfigStorageKey.ps1 -name $StorageAccountName
$Result.Count | Should Be 1
}

It "Should return one output with two parameter" {

$Result = .\Get-ConfigStorageKey.ps1 -name $StorageAccountName -useSecondary $true
$Result.Count | Should Be 1
}
}

Pop-Location