Skip to content

Commit ba4c668

Browse files
committed
Adding script cmdlets for managing storage accounts. This will replace use of production Storage management cmdlets in test setup code.
1 parent e2a51a8 commit ba4c668

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
function Get-AzureRmStorageAccount
3+
{
4+
5+
[CmdletBinding()]
6+
param(
7+
[string] [Parameter(Position=0, ValueFromPipelineByPropertyName=$true)] $ResourceGroupName,
8+
[string] [Parameter(Position=1, ValueFromPipelineByPropertyName=$true)] $Name)
9+
BEGIN {
10+
$context = Get-Context
11+
$client = Get-StorageClient $context
12+
}
13+
PROCESS {
14+
$getTask = $client.StorageAccounts.GetPropertiesAsync($ResourceGroupName, $name, [System.Threading.CancellationToken]::None)
15+
$sa = $getTask.Result
16+
$account = Get-StorageAccount $sa $ResourceGroupName
17+
Write-Output $account
18+
}
19+
END {}
20+
21+
}
22+
23+
function New-AzureRmStorageAccount
24+
{
25+
[CmdletBinding()]
26+
param(
27+
[string] [Parameter(Position=0, ValueFromPipelineByPropertyName=$true)] $ResourceGroupName,
28+
[string] [Parameter(Position=1, ValueFromPipelineByPropertyName=$true)] $Name,
29+
[string] [Parameter(Position=2, ValueFromPipelineByPropertyName=$true)] $Location,
30+
[string] [Parameter(Position=3, ValueFromPipelineByPropertyName=$true)] $Type)
31+
BEGIN {
32+
$context = Get-Context
33+
$client = Get-StorageClient $context
34+
}
35+
PROCESS {
36+
$createParms = New-Object -Type Microsoft.Azure.Management.Storage.Models.StorageAccountCreateParameters
37+
$createParms.AccountType = [Microsoft.Azure.Management.Storage.Models.AccountType]::StandardLRS
38+
$createParms.Location = $Location
39+
$getTask = $client.StorageAccounts.CreateAsync($ResourceGroupName, $name, $createParms, [System.Threading.CancellationToken]::None)
40+
$sa = $getTask.Result
41+
$account = Get-StorageAccount $ResourceGroupName $Name
42+
Write-Output $account
43+
}
44+
END {}
45+
46+
}
47+
48+
function Get-AzureRmStorageAccountKey
49+
{
50+
[CmdletBinding()]
51+
param(
52+
[string] [Parameter(Position=0, ValueFromPipelineByPropertyName=$true)] $ResourceGroupName,
53+
[string] [Parameter(Position=1, ValueFromPipelineByPropertyName=$true)] $Name)
54+
BEGIN {
55+
$context = Get-Context
56+
$client = Get-StorageClient $context
57+
}
58+
PROCESS {
59+
$getTask = $client.StorageAccounts.ListKeysAsync($ResourceGroupName, $name, [System.Threading.CancellationToken]::None)
60+
Write-Output $getTask.Result.StorageAccountKeys
61+
}
62+
END {}
63+
}
64+
65+
function Get-Context
66+
{
67+
[Microsoft.Azure.Commands.Common.Authentication.Models.AzureContext]$context = $null
68+
$profile = [Microsoft.WindowsAzure.Commands.Common.AzureRmProfileProvider]::Instance.Profile
69+
if ($profile -ne $null)
70+
{
71+
$context = $profile.Context
72+
}
73+
74+
return $context
75+
}
76+
77+
function Get-StorageClient
78+
{
79+
param([Microsoft.Azure.Commands.Common.Authentication.Models.AzureContext] $context)
80+
$factory = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::ClientFactory
81+
[System.Type[]]$types = [Microsoft.Azure.Commands.Common.Authentication.Models.AzureContext], [Microsoft.Azure.Commands.Common.Authentication.Models.AzureEnvironment+Endpoint]
82+
$method = [Microsoft.Azure.Commands.Common.Authentication.IClientFactory].GetMethod("CreateClient", $types)
83+
$closedMethod = $method.MakeGenericMethod([Microsoft.Azure.Management.Storage.StorageManagementClient])
84+
$arguments = $context, [Microsoft.Azure.Commands.Common.Authentication.Models.AzureEnvironment+Endpoint]::ResourceManager
85+
$client = $closedMethod.Invoke($factory, $arguments)
86+
return $client
87+
}
88+
89+
function Get-StorageAccount {
90+
param([string] $resourceGroupName, [string] $name)
91+
$sa = New-Object PSObject -Property @{"Name" = $name; "ResourceGroupName" = $resourceGroupName}
92+
return $sa
93+
}

src/ResourceManager/Common/Commands.ScenarioTests.ResourceManager.Common/Commands.ScenarioTests.ResourceManager.Common.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@
161161
<None Include="Assert.ps1">
162162
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
163163
</None>
164+
<None Include="AzureRM.Storage.ps1">
165+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
166+
</None>
164167
<None Include="Common.ps1">
165168
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
166169
</None>

src/ResourceManager/Compute/Commands.Compute.Test/Commands.Compute.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@
218218
<Link>ScenarioTests\Assert.ps1</Link>
219219
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
220220
</None>
221+
<None Include="..\..\Common\Commands.ScenarioTests.ResourceManager.Common\AzureRM.Storage.ps1">
222+
<Link>ScenarioTests\AzureRM.Storage.ps1</Link>
223+
</None>
221224
<None Include="..\..\Resources\Commands.Resources.Test\ScenarioTests\Common.ps1">
222225
<Link>ScenarioTests\Common.ps1</Link>
223226
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

src/ResourceManager/Compute/Commands.Compute.Test/Common/ComputeTestController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ public void RunPsTestWorkflow(
134134
helper.RMStorageDataPlaneModule,
135135
helper.RMStorageModule,
136136
helper.GetRMModulePath("AzureRM.Compute.psd1"),
137-
helper.GetRMModulePath("AzureRM.Network.psd1"));
137+
helper.GetRMModulePath("AzureRM.Network.psd1"),
138+
"AzureRM.Storage.ps1");
138139

139140
try
140141
{

0 commit comments

Comments
 (0)