From 4bea04f749fffc648868415e3ce7fc909b1a2de9 Mon Sep 17 00:00:00 2001 From: Yeming Liu <11371776+isra-fel@users.noreply.github.com> Date: Tue, 24 Aug 2021 19:16:41 +0800 Subject: [PATCH] Do not add api to IAzureContextContainer --- .../Interfaces/IAzureContextContainer.cs | 7 ---- src/Common/Utilities/ISharedUtilities.cs | 33 +++++++++++++++++++ .../Version2016_09_01/AzureRMCmdlet.cs | 10 +++++- 3 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 src/Common/Utilities/ISharedUtilities.cs diff --git a/src/Authentication.Abstractions/Interfaces/IAzureContextContainer.cs b/src/Authentication.Abstractions/Interfaces/IAzureContextContainer.cs index dba1e7d11c..2aaa2a5dfd 100644 --- a/src/Authentication.Abstractions/Interfaces/IAzureContextContainer.cs +++ b/src/Authentication.Abstractions/Interfaces/IAzureContextContainer.cs @@ -49,12 +49,5 @@ public interface IAzureContextContainer: IExtensibleModel /// Remove all contexts from the container /// void Clear(); - - /// - /// Copy the context container for overriding default context. - /// See - /// - /// The copy. - IAzureContextContainer CopyForContextOverriding(); } } diff --git a/src/Common/Utilities/ISharedUtilities.cs b/src/Common/Utilities/ISharedUtilities.cs new file mode 100644 index 0000000000..1516e4fb33 --- /dev/null +++ b/src/Common/Utilities/ISharedUtilities.cs @@ -0,0 +1,33 @@ +// ---------------------------------------------------------------------------------- +// +// 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. +// ---------------------------------------------------------------------------------- + +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core; + +namespace Microsoft.WindowsAzure.Commands.Common.Utilities +{ + /// + /// Contains helper methods shared between common lib and Az.Accounts. + /// An instance of a class that implements this interface should be registered to the session. + /// + public interface ISharedUtilities + { + /// + /// Copy the context container for overriding default context. + /// See . + /// + /// The original. + /// The copy. + IAzureContextContainer CopyForContextOverriding(IAzureContextContainer contextContainer); + } +} diff --git a/src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs b/src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs index b770794087..6c425b8b2f 100644 --- a/src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs +++ b/src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs @@ -25,6 +25,7 @@ using Microsoft.Rest; using Microsoft.WindowsAzure.Commands.Common; using Microsoft.WindowsAzure.Commands.Common.Attributes; +using Microsoft.WindowsAzure.Commands.Common.Utilities; using Microsoft.WindowsAzure.Commands.Utilities.Common; using System; using System.Collections.Generic; @@ -111,7 +112,14 @@ private IAzureContextContainer CloneProfileAndModifyContext() if (matchingSub != null) { // going to modify default context, so only shallow copying other stuff - profile = GetDefaultProfile().CopyForContextOverriding(); + if (AzureSession.Instance.TryGetComponent(nameof(ISharedUtilities), out var sharedUtilities)) + { + profile = sharedUtilities.CopyForContextOverriding(GetDefaultProfile()); + } + else + { + throw new AzPSException(Resources.ProfileNotInitialized, Commands.Common.ErrorKind.InternalError); + } profile.DefaultContext = profile.DefaultContext.DeepCopy(); profile.DefaultContext.Subscription.CopyFrom(matchingSub); profile.DefaultContext.Tenant.Id = matchingSub.GetTenant();