forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.ps1
158 lines (144 loc) · 3.99 KB
/
Common.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# ----------------------------------------------------------------------------------
#
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------
<#
.SYNOPSIS
Gets valid resource name for compute test
#>
function GetResourceGroupName
{
$stack = Get-PSCallStack
$testName = $null;
foreach ($frame in $stack)
{
if ($frame.Command.StartsWith("Test-", "CurrentCultureIgnoreCase"))
{
$testName = $frame.Command;
}
}
$oldErrorActionPreferenceValue = $ErrorActionPreference;
$ErrorActionPreference = "SilentlyContinue";
try
{
$assetName = [Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::GetAssetName($testName, "pstestrg");
}
catch
{
if (($Error.Count -gt 0) -and ($Error[0].Exception.Message -like '*Unable to find type*'))
{
$assetName = Get-RandomItemName;
}
else
{
throw;
}
}
finally
{
$ErrorActionPreference = $oldErrorActionPreferenceValue;
}
return $assetName
}
<#
.SYNOPSIS
Create a resource group
#>
function CreateResourceGroup([string]$rgname, [string]$location)
{
$resourceGroup = New-AzResourceGroup -Name $rgname -Location $location -Force
return $resourceGroup
}
<#
.SYNOPSIS
Remove a resource group
#>
function RemoveResourceGroup([string]$rgname)
{
Remove-AzResourceGroup -Name $rgname -Force
}
<#
.SYNOPSIS
Create a storage account
#>
function CreateStorageAccount([string]$rgname, [string]$name, [string]$location)
{
New-AzStorageAccount -ResourceGroupName $rgname -Name $name -Location $location -Type "Standard_GRS"
$storageAccount = Get-AzStorageAccount -ResourceGroupName $rgname -Name $name
return $storageAccount
}
<#
.SYNOPSIS
Remove a storage account
#>
function RemoveStorageAccount([string]$rgname, [string]$name)
{
Remove-AzStorageAccount -ResourceGroupName $rgname -Name $name
}
<#
.SYNOPSIS
Get a storage account
#>
function GetStorageAccount
{
[CmdletBinding()]
param(
[string] [Parameter(Position=0, ValueFromPipelineByPropertyName=$true)] $ResourceGroupName,
[string] [Parameter(Position=1, ValueFromPipelineByPropertyName=$true)] [alias("StorageAccountName")] $Name)
BEGIN {
$context = Get-Context
$client = Get-StorageClient $context
}
PROCESS {
$getTask = $client.StorageAccounts.GetPropertiesAsync($ResourceGroupName, $Name, [System.Threading.CancellationToken]::None)
<#$account = New-Object PSObject -Property @{"StorageAccountName" = $Name; "ResourceGroupName" = $ResourceGroupName; }#>
Write-Output $getTask.Result.StorageAccount
}
END {}
}
<#
.SYNOPSIS
Asserts if two tags are equal
#>
function Assert-Tags($tags1, $tags2)
{
if($tags1.count -ne $tags2.count)
{
throw "Tag size not equal. Tag1: $tags1.count Tag2: $tags2.count"
}
foreach($key in $tags1.Keys)
{
if($tags1[$key] -ne $tags2[$key])
{
throw "Tag content not equal. Key:$key Tags1:" + $tags1[$key] + "Tags2:" + $tags2[$key]
}
}
}
<#
.SYNOPSIS
Get a location for test.
#>
function Get-AvailableLocation($preferedLocation)
{
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback)
{
$namespace = "Microsoft.Media"
$provider = Get-AzResourceProvider -ProviderNamespace Microsoft.Media | where {$_.Locations.length -ne 0}
$locations = $provider.Locations
if($locations -contains $preferedLocation)
{
return $preferedLocation
}
return $locations[0]
}
return $preferedLocation
}