Skip to content

Commit f046ff2

Browse files
committed
Test passed
1 parent d3c8f93 commit f046ff2

File tree

7 files changed

+2274
-18
lines changed

7 files changed

+2274
-18
lines changed

src/ResourceManager/Resources/Commands.Resources.Test/Commands.Resources.Test.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
<Reference Include="Microsoft.Azure.KeyVault.Core">
7171
<HintPath>..\..\..\packages\Microsoft.Azure.KeyVault.Core.1.0.0\lib\net40\Microsoft.Azure.KeyVault.Core.dll</HintPath>
7272
</Reference>
73+
<Reference Include="Microsoft.Azure.Management.KeyVault, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
74+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.KeyVault.1.0.1\lib\net40\Microsoft.Azure.Management.KeyVault.dll</HintPath>
75+
<Private>True</Private>
76+
</Reference>
7377
<Reference Include="Microsoft.Azure.Management.Authorization">
7478
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.1.1.0\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
7579
</Reference>
@@ -245,6 +249,9 @@
245249
</ProjectReference>
246250
</ItemGroup>
247251
<ItemGroup>
252+
<None Include="KeyVaultSetupTemplate.json">
253+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
254+
</None>
248255
<None Include="keyVaultTemplateParams.json">
249256
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
250257
</None>
@@ -324,6 +331,9 @@
324331
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.DeploymentTests\TestNewDeploymentFromTemplateFile.json">
325332
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
326333
</None>
334+
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.DeploymentTests\TestNewDeploymentWithKeyVaultReference.json">
335+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
336+
</None>
327337
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.MoveResourceTest\TestMoveAzureResource.json">
328338
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
329339
</None>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"keyVaultName": {
6+
"type": "string",
7+
"metadata": {
8+
"description": "Name of the Vault"
9+
}
10+
},
11+
"secretName": {
12+
"type": "string",
13+
"metadata": {
14+
"description": "Name of the secret to store in the vault"
15+
}
16+
},
17+
"secretValue": {
18+
"type": "securestring",
19+
"metadata": {
20+
"description": "Value of the secret"
21+
}
22+
}
23+
},
24+
"resources": [
25+
{
26+
"type": "Microsoft.KeyVault/vaults/secrets",
27+
"name": "[concat(parameters('keyVaultName'), '/', parameters('secretName'))]",
28+
"apiVersion": "2015-06-01",
29+
"tags": { "displayName": "secret" },
30+
"properties": {
31+
"value": "[parameters('secretValue')]"
32+
}
33+
}
34+
]
35+
}

src/ResourceManager/Resources/Commands.Resources.Test/ResourceGroups/NewAzureResourceGroupCommandTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public class NewAzureResourceGroupCommandTests : RMTestBase
3939

4040
private string templateFile = @"Resources\sampleTemplateFile.json";
4141

42-
private string storageAccountName = "myStorageAccount";
43-
4442
private Hashtable[] tags;
4543

4644
public NewAzureResourceGroupCommandTests()

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/DeploymentTests.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ function Test-NewDeploymentFromTemplateFile
7070

7171
<#
7272
.SYNOPSIS
73-
Tests deployment via template file and parameter object.
73+
Tests deployment via template file and parameter file with KeyVault reference.
7474
#>
7575
function Test-NewDeploymentWithKeyVaultReference
7676
{
7777
# Setup
7878
$rgname = Get-ResourceGroupName
7979
$rname = Get-ResourceName
80-
$keyVaultResourceName = Get-ResourceName
80+
$keyVaultname = Get-ResourceName
8181
$secretName = Get-ResourceName
8282
$rglocation = Get-ProviderLocation ResourceManagement
8383
$location = Get-ProviderLocation "Microsoft.Web/sites"
@@ -88,8 +88,10 @@ function Test-NewDeploymentWithKeyVaultReference
8888
# Test
8989
New-AzureRmResourceGroup -Name $rgname -Location $rglocation
9090

91-
$keyVault = $keyVault = new-azurermkeyvault -VaultName $keyVaultResourceName -ResourceGroupName $rgname -Location $location -EnabledForTemplateDeployment
92-
Set-AzureKeyVaultSecret -VaultName $keyVaultResourceName -SecretName $secretName -SecretValue $hostplanName
91+
$keyVault = New-AzureRmKeyVault -VaultName $keyVaultname -ResourceGroupName $rgname -Location $location -EnabledForTemplateDeployment
92+
$parameters = @{ "keyVaultName" = $keyVaultname; "secretName" = $secretName; "secretValue" = $hostplanName }
93+
New-AzureRmResourceGroupDeployment -Name $rname -ResourceGroupName $rgname -TemplateFile keyVaultSetupTemplate.json -TemplateParameterObject $parameters
94+
9395
$content = (Get-Content keyVaultTemplateParams.json) -join '' | ConvertFrom-Json
9496
$content.hostingPlanName.reference.KeyVault.id = $keyVault.resourceid
9597
$content.hostingPlanName.reference.SecretName = $secretName

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/ResourcesController.cs

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
using System.Net.Http.Headers;
2020
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
2121
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
22-
using Microsoft.Azure.Commands.ResourceManager.Common;
2322
using Microsoft.Azure.Common.Authentication;
2423
using Microsoft.Azure.Gallery;
2524
using Microsoft.Azure.Graph.RBAC;
2625
using Microsoft.Azure.Insights;
2726
using Microsoft.Azure.Management.Authorization;
27+
using Microsoft.Azure.Management.KeyVault;
2828
using Microsoft.Azure.Management.Resources;
2929
using Microsoft.Azure.Subscriptions;
3030
using Microsoft.Azure.Test;
3131
using Microsoft.Azure.Test.HttpRecorder;
3232
using Microsoft.WindowsAzure.Commands.Common;
3333
using Microsoft.WindowsAzure.Commands.ScenarioTest;
34+
using Microsoft.Azure.Common.Authentication.Models;
3435

3536
namespace Microsoft.Azure.Commands.Resources.Test.ScenarioTests
3637
{
@@ -56,6 +57,8 @@ public sealed class ResourcesController
5657

5758
public AuthorizationManagementClient AuthorizationManagementClient { get; private set; }
5859

60+
public KeyVaultManagementClient KeyVaultManagementClient { get; private set; }
61+
5962
public string UserDomain { get; private set; }
6063

6164
public static ResourcesController NewInstance
@@ -104,18 +107,19 @@ public void RunPsTestWorkflow(
104107
initialize(this.csmTestFactory);
105108
}
106109

107-
SetupManagementClients();
108-
109110
helper.SetupEnvironment(AzureModule.AzureResourceManager);
110-
111+
SetupAzureContext();
112+
SetupManagementClients();
113+
111114
var callingClassName = callingClassType
112115
.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
113116
.Last();
114117
helper.SetupModules(AzureModule.AzureResourceManager,
115118
"ScenarioTests\\Common.ps1",
116119
"ScenarioTests\\" + callingClassName + ".ps1",
117120
helper.RMProfileModule,
118-
helper.RMResourceModule);
121+
helper.RMResourceModule,
122+
helper.GetRMModulePath("AzureRM.KeyVault.psd1"));
119123

120124
try
121125
{
@@ -148,6 +152,7 @@ private void SetupManagementClients()
148152
GraphClient = GetGraphClient();
149153
InsightsClient = GetInsightsClient();
150154
this.FeatureClient = this.GetFeatureClient();
155+
KeyVaultManagementClient = GetKeyVaultManagementClient();
151156
HttpClientHelperFactory.Instance = new TestHttpClientHelperFactory(this.csmTestFactory.GetTestEnvironment().Credentials as SubscriptionCloudCredentials);
152157

153158
helper.SetupManagementClients(ResourceManagementClient,
@@ -156,7 +161,8 @@ private void SetupManagementClients()
156161
AuthorizationManagementClient,
157162
GraphClient,
158163
InsightsClient,
159-
this.FeatureClient);
164+
this.FeatureClient,
165+
KeyVaultManagementClient);
160166
}
161167

162168
private GraphRbacManagementClient GetGraphClient()
@@ -221,6 +227,81 @@ private InsightsClient GetInsightsClient()
221227
return TestBase.GetServiceClient<InsightsClient>(this.csmTestFactory);
222228
}
223229

230+
private KeyVaultManagementClient GetKeyVaultManagementClient()
231+
{
232+
return TestBase.GetServiceClient<KeyVaultManagementClient>(this.csmTestFactory);
233+
}
234+
235+
private void SetupAzureContext()
236+
{
237+
TestEnvironment csmEnvironment = new CSMTestEnvironmentFactory().GetTestEnvironment();
238+
239+
if (csmEnvironment.SubscriptionId != null)
240+
{
241+
//Overwrite the default subscription and default account
242+
//with ones using user ID and tenant ID from auth context
243+
var user = GetUser(csmEnvironment);
244+
var tenantId = GetTenantId(csmEnvironment);
245+
246+
var testSubscription = new AzureSubscription()
247+
{
248+
Id = new Guid(csmEnvironment.SubscriptionId),
249+
Name = AzureRmProfileProvider.Instance.Profile.Context.Subscription.Name,
250+
Environment = AzureRmProfileProvider.Instance.Profile.Context.Environment.Name,
251+
Account = user,
252+
Properties = new Dictionary<AzureSubscription.Property, string>
253+
{
254+
{AzureSubscription.Property.Default, "True"},
255+
{
256+
AzureSubscription.Property.StorageAccount,
257+
Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT")
258+
},
259+
{AzureSubscription.Property.Tenants, tenantId},
260+
}
261+
};
262+
263+
var testAccount = new AzureAccount()
264+
{
265+
Id = user,
266+
Type = AzureAccount.AccountType.User,
267+
Properties = new Dictionary<AzureAccount.Property, string>
268+
{
269+
{AzureAccount.Property.Subscriptions, csmEnvironment.SubscriptionId},
270+
{AzureAccount.Property.Tenants, tenantId},
271+
}
272+
};
273+
274+
AzureRmProfileProvider.Instance.Profile.Context = new AzureContext(testSubscription, testAccount, AzureRmProfileProvider.Instance.Profile.Context.Environment, new AzureTenant { Id = new Guid(tenantId) });
275+
}
276+
277+
}
278+
279+
private string GetTenantId(TestEnvironment environment)
280+
{
281+
if (HttpMockServer.Mode == HttpRecorderMode.Record)
282+
{
283+
HttpMockServer.Variables["TenantId"] = environment.AuthorizationContext.TenantId;
284+
return environment.AuthorizationContext.TenantId;
285+
}
286+
else
287+
{
288+
return HttpMockServer.Variables["TenantId"];
289+
}
290+
}
291+
292+
private string GetUser(TestEnvironment environment)
293+
{
294+
if (HttpMockServer.Mode == HttpRecorderMode.Record)
295+
{
296+
HttpMockServer.Variables["User"] = environment.AuthorizationContext.UserId;
297+
return environment.AuthorizationContext.UserId;
298+
}
299+
else
300+
{
301+
return HttpMockServer.Variables["User"];
302+
}
303+
}
304+
224305
/// <summary>
225306
/// The test http client helper factory.
226307
/// </summary>

src/ResourceManager/Resources/Commands.Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.DeploymentTests/TestNewDeploymentWithKeyVaultReference.json

Lines changed: 2126 additions & 0 deletions
Large diffs are not rendered by default.

src/ResourceManager/Resources/Commands.Resources/Models.ResourceGroups/ResourceWithParameterBaseCmdlet.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,28 +143,32 @@ public object GetDynamicParameters()
143143

144144
protected Hashtable GetTemplateParameterObject(Hashtable templateParameterObject)
145145
{
146-
templateParameterObject = templateParameterObject ?? new Hashtable();
147-
foreach (var parameterKey in templateParameterObject.Keys)
146+
// NOTE(jogao): create a new Hashtable so that user can re-use the templateParameterObject.
147+
var prameterObject = new Hashtable();
148+
if (templateParameterObject != null)
148149
{
149-
templateParameterObject[parameterKey] = new Hashtable { { "value", templateParameterObject[parameterKey] } };
150+
foreach (var parameterKey in templateParameterObject.Keys)
151+
{
152+
prameterObject[parameterKey] = new Hashtable { { "value", templateParameterObject[parameterKey] } };
153+
}
150154
}
151155

152156
// Load parameters from the file
153157
string templateParameterFilePath = this.TryResolvePath(TemplateParameterFile);
154158
if (templateParameterFilePath != null && FileUtilities.DataStore.FileExists(templateParameterFilePath))
155159
{
156160
var parametersFromFile = GalleryTemplatesClient.ParseTemplateParameterFileContents(templateParameterFilePath);
157-
parametersFromFile.ForEach(dp => templateParameterObject[dp.Key] = new Hashtable { { "value", dp.Value.Value }, { "reference", dp.Value.Reference } });
161+
parametersFromFile.ForEach(dp => prameterObject[dp.Key] = new Hashtable { { "value", dp.Value.Value }, { "reference", dp.Value.Reference } });
158162
}
159163

160164
// Load dynamic parameters
161165
IEnumerable<RuntimeDefinedParameter> parameters = PowerShellUtilities.GetUsedDynamicParameters(dynamicParameters, MyInvocation);
162166
if (parameters.Any())
163167
{
164-
parameters.ForEach(dp => templateParameterObject[((ParameterAttribute)dp.Attributes[0]).HelpMessage] = new Hashtable { { "value", dp.Value } });
168+
parameters.ForEach(dp => prameterObject[((ParameterAttribute)dp.Attributes[0]).HelpMessage] = new Hashtable { { "value", dp.Value } });
165169
}
166170

167-
return templateParameterObject;
171+
return prameterObject;
168172
}
169173
}
170174
}

0 commit comments

Comments
 (0)