Skip to content

Commit

Permalink
Do not add api to IAzureContextContainer (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
isra-fel authored Aug 25, 2021
1 parent 887ae87 commit 6672e47
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,5 @@ public interface IAzureContextContainer: IExtensibleModel
/// Remove all contexts from the container
/// </summary>
void Clear();

/// <summary>
/// Copy the context container for overriding default context.
/// See <see cref="SupportsSubscriptionIdAttribute"/>
/// </summary>
/// <returns>The copy.</returns>
IAzureContextContainer CopyForContextOverriding();
}
}
33 changes: 33 additions & 0 deletions src/Common/Utilities/ISharedUtilities.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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.
/// </summary>
public interface ISharedUtilities
{
/// <summary>
/// Copy the context container for overriding default context.
/// See <see cref="Attributes.SupportsSubscriptionIdAttribute"/>.
/// </summary>
/// <param name="contextContainer">The original.</param>
/// <returns>The copy.</returns>
IAzureContextContainer CopyForContextOverriding(IAzureContextContainer contextContainer);
}
}
10 changes: 9 additions & 1 deletion src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ISharedUtilities>(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();
Expand Down

0 comments on commit 6672e47

Please sign in to comment.