Skip to content

Commit 7af211f

Browse files
authored
Reimplemented overriding subscription ID (Azure#15743)
1 parent 44ffd02 commit 7af211f

File tree

6 files changed

+72
-29
lines changed

6 files changed

+72
-29
lines changed

src/Accounts/Accounts.Test/AzureRMProfileTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1717
using Microsoft.Azure.Commands.Common.Authentication.Factories;
1818
using Microsoft.Azure.Commands.Common.Authentication.Models;
19+
using Microsoft.Azure.Commands.Common.Authentication.ResourceManager.Common;
1920
using Microsoft.Azure.Commands.Profile;
2021
using Microsoft.Azure.Commands.Profile.Models;
2122
using Microsoft.Azure.Commands.Profile.Test;
@@ -1101,7 +1102,8 @@ public void ShouldShallowCopy()
11011102
var subscriptions = new List<string> { DefaultSubscription.ToString() };
11021103
var profile = SetupLogin(tenants, subscriptions, subscriptions);
11031104

1104-
var copy = profile.CopyForContextOverriding() as AzureRmProfile;
1105+
var utilities = new AzureRmSharedUtilities();
1106+
var copy = utilities.CopyForContextOverriding(profile) as AzureRmProfile;
11051107

11061108
// Should act as shallow copy
11071109
// except for that `Contexts` should be a new dictionary

src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core;
2929
using Microsoft.Azure.Commands.Common.Authentication.Factories;
3030
using Microsoft.Azure.Commands.Common.Authentication.Models;
31+
using Microsoft.Azure.Commands.Common.Authentication.ResourceManager.Common;
3132
using Microsoft.Azure.Commands.Profile.Common;
3233
using Microsoft.Azure.Commands.Profile.Models.Core;
3334
using Microsoft.Azure.Commands.Profile.Properties;
@@ -38,6 +39,7 @@
3839
using Microsoft.Identity.Client;
3940
using Microsoft.WindowsAzure.Commands.Common;
4041
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
42+
using Microsoft.WindowsAzure.Commands.Common.Utilities;
4143
using Microsoft.WindowsAzure.Commands.Utilities.Common;
4244

4345
namespace Microsoft.Azure.Commands.Profile
@@ -665,6 +667,9 @@ public void OnImport()
665667
}
666668
IAzureEventListenerFactory azureEventListenerFactory = new AzureEventListenerFactory();
667669
AzureSession.Instance.RegisterComponent(nameof(CommonUtilities), () => new CommonUtilities());
670+
// It's tricky to register a component as an Interface
671+
// Make sure componentInitializer return the Interface, not the derived type
672+
AzureSession.Instance.RegisterComponent(nameof(ISharedUtilities), () => new AzureRmSharedUtilities() as ISharedUtilities);
668673
AzureSession.Instance.RegisterComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, () => provider);
669674
AzureSession.Instance.RegisterComponent(nameof(IAzureEventListenerFactory), () => azureEventListenerFactory);
670675
AzureSession.Instance.RegisterComponent(nameof(AzureCredentialFactory), () => new AzureCredentialFactory());

src/Accounts/Authentication.ResourceManager/AzureRmProfile.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -830,12 +830,5 @@ public void RefreshContextsFromCache()
830830

831831
Save(ProfilePath, false);
832832
}
833-
834-
public IAzureContextContainer CopyForContextOverriding()
835-
{
836-
var copy = MemberwiseClone() as AzureRmProfile;
837-
copy.Contexts = new ConcurrentDictionary<string, IAzureContext>(copy.Contexts, StringComparer.CurrentCultureIgnoreCase);
838-
return copy;
839-
}
840833
}
841834
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core;
16+
using Microsoft.WindowsAzure.Commands.Common.Utilities;
17+
using System;
18+
using Microsoft.Azure.Commands.Common.Authentication.Models;
19+
using System.Collections.Concurrent;
20+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
21+
22+
namespace Microsoft.Azure.Commands.Common.Authentication.ResourceManager.Common
23+
{
24+
/// <summary>
25+
/// Contains helper methods shared between common lib and Az.Accounts.
26+
/// Named to be different from <see cref="SharedUtilities" />.
27+
/// </summary>
28+
public class AzureRmSharedUtilities : ISharedUtilities
29+
{
30+
public IAzureContextContainer CopyForContextOverriding(IAzureContextContainer contextContainer)
31+
{
32+
var profile = contextContainer as AzureRmProfile;
33+
return new AzureRmProfile()
34+
{
35+
DefaultContextKey = profile.DefaultContextKey,
36+
EnvironmentTable = profile.EnvironmentTable,
37+
// Contexts need a new instance else any change to DefaultContext will affect the original profile
38+
Contexts = new ConcurrentDictionary<string, IAzureContext>(profile.Contexts, StringComparer.CurrentCultureIgnoreCase),
39+
ShouldRefreshContextsFromCache = profile.ShouldRefreshContextsFromCache,
40+
// ProfilePath is not copied because
41+
// 1. it's protected setter
42+
// 2. copy won't be written back to disk, so it's not useful
43+
ExtendedProperties = profile.ExtendedProperties
44+
};
45+
}
46+
}
47+
}

src/Storage/Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ public void Clear()
9191
{
9292

9393
}
94-
public IAzureContextContainer CopyForContextOverriding()
95-
{
96-
return this;
97-
}
9894
}
9995

10096
public AzureSMProfileProvider smProvider = AzureSMProfileProvider.Instance;

tools/Common.Netcore.Dependencies.targets

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
<ItemGroup>
44
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.23"/>
55
<PackageReference Include="Microsoft.Rest.ClientRuntime.Azure" Version="3.3.19"/>
6-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Aks" Version="1.3.44-preview"/>
7-
<PackageReference Include="Microsoft.Azure.PowerShell.Authentication.Abstractions" Version="1.3.44-preview"/>
8-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Authorization" Version="1.3.44-preview"/>
9-
<PackageReference Include="Microsoft.Azure.PowerShell.Common" Version="1.3.44-preview"/>
10-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Compute" Version="1.3.44-preview"/>
11-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Graph.Rbac" Version="1.3.44-preview"/>
12-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.KeyVault" Version="1.3.44-preview"/>
13-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Monitor" Version="1.3.44-preview"/>
14-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Network" Version="1.3.44-preview"/>
15-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.PolicyInsights" Version="1.3.44-preview"/>
16-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.ResourceManager" Version="1.3.44-preview"/>
17-
<PackageReference Include="Microsoft.Azure.PowerShell.Storage" Version="1.3.44-preview"/>
18-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Storage.Management" Version="1.3.44-preview"/>
19-
<PackageReference Include="Microsoft.Azure.PowerShell.Strategies" Version="1.3.44-preview"/>
20-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Websites" Version="1.3.44-preview"/>
21-
<PackageReference Include="Microsoft.Azure.PowerShell.Common.Share" Version="1.3.44-preview"/>
6+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Aks" Version="1.3.45-preview"/>
7+
<PackageReference Include="Microsoft.Azure.PowerShell.Authentication.Abstractions" Version="1.3.45-preview"/>
8+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Authorization" Version="1.3.45-preview"/>
9+
<PackageReference Include="Microsoft.Azure.PowerShell.Common" Version="1.3.45-preview"/>
10+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Compute" Version="1.3.45-preview"/>
11+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Graph.Rbac" Version="1.3.45-preview"/>
12+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.KeyVault" Version="1.3.45-preview"/>
13+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Monitor" Version="1.3.45-preview"/>
14+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Network" Version="1.3.45-preview"/>
15+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.PolicyInsights" Version="1.3.45-preview"/>
16+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.ResourceManager" Version="1.3.45-preview"/>
17+
<PackageReference Include="Microsoft.Azure.PowerShell.Storage" Version="1.3.45-preview"/>
18+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Storage.Management" Version="1.3.45-preview"/>
19+
<PackageReference Include="Microsoft.Azure.PowerShell.Strategies" Version="1.3.45-preview"/>
20+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Websites" Version="1.3.45-preview"/>
21+
<PackageReference Include="Microsoft.Azure.PowerShell.Common.Share" Version="1.3.45-preview"/>
2222
</ItemGroup>
2323
<ItemGroup>
2424
<PackageReference Include="Azure.Core" Version="1.16.0"/>
@@ -36,7 +36,7 @@
3636
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" PrivateAssets="All" />
3737
</ItemGroup>
3838
<PropertyGroup>
39-
<StorageToolsPath>$(NugetPackageRoot)\microsoft.azure.powershell.storage\1.3.44-preview\tools\</StorageToolsPath>
39+
<StorageToolsPath>$(NugetPackageRoot)\microsoft.azure.powershell.storage\1.3.45-preview\tools\</StorageToolsPath>
4040
</PropertyGroup>
4141
<ItemGroup Condition="'$(OmitJsonPackage)' != 'true'">
4242
<PackageReference Include="Newtonsoft.Json" Version="10.0.3"/>

0 commit comments

Comments
 (0)