From c076828acfd97e7daa32563c00858aa22b6b1ed5 Mon Sep 17 00:00:00 2001 From: Mike Wevill Date: Fri, 21 Dec 2018 09:34:41 +0000 Subject: [PATCH] Added Automation to Return Storage String --- .../Resources/Get-ConfigStorageKey.ps1 | 62 +++++++++++++++++++ Tests/AT015.Get-ConfigStorageKeyTests.ps1 | 21 +++++++ 2 files changed, 83 insertions(+) create mode 100644 Infrastructure/Resources/Get-ConfigStorageKey.ps1 create mode 100644 Tests/AT015.Get-ConfigStorageKeyTests.ps1 diff --git a/Infrastructure/Resources/Get-ConfigStorageKey.ps1 b/Infrastructure/Resources/Get-ConfigStorageKey.ps1 new file mode 100644 index 0000000..c17a246 --- /dev/null +++ b/Infrastructure/Resources/Get-ConfigStorageKey.ps1 @@ -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 "$_" +} diff --git a/Tests/AT015.Get-ConfigStorageKeyTests.ps1 b/Tests/AT015.Get-ConfigStorageKeyTests.ps1 new file mode 100644 index 0000000..12138e5 --- /dev/null +++ b/Tests/AT015.Get-ConfigStorageKeyTests.ps1 @@ -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