Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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