diff --git a/src/Common/Commands.Common/ProfileClient.cs b/src/Common/Commands.Common/ProfileClient.cs index feb9dafe26ef..1128d28ebb1d 100644 --- a/src/Common/Commands.Common/ProfileClient.cs +++ b/src/Common/Commands.Common/ProfileClient.cs @@ -841,6 +841,9 @@ private AzureSubscription MergeSubscriptionProperties(AzureSubscription subscrip Id = subscription1.Id, Name = subscription1.Name, Environment = subscription1.Environment, + State = (subscription1.State != null && + subscription1.State.Equals(subscription2.State, StringComparison.OrdinalIgnoreCase)) ? + subscription1.State: null, Account = subscription1.Account ?? subscription2.Account }; diff --git a/src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs b/src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs index 11c0bf9e7fcf..b3479e084e3d 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs @@ -20,6 +20,7 @@ using Microsoft.Azure.Commands.ResourceManager.Common; using Microsoft.WindowsAzure.Commands.Common; using Microsoft.Azure.Commands.Profile.Properties; +using System; namespace Microsoft.Azure.Commands.Profile { @@ -78,6 +79,18 @@ public override void ExecuteCmdlet() profileClient.SetCurrentContext(TenantId); } } + + if (AzureRmProfileProvider.Instance.Profile.Context != null && + AzureRmProfileProvider.Instance.Profile.Context.Subscription != null && + AzureRmProfileProvider.Instance.Profile.Context.Subscription.State != null && + !AzureRmProfileProvider.Instance.Profile.Context.Subscription.State.Equals( + "Enabled", + StringComparison.OrdinalIgnoreCase)) + { + WriteWarning(string.Format( + Microsoft.Azure.Commands.Profile.Properties.Resources.SelectedSubscriptionNotActive, + AzureRmProfileProvider.Instance.Profile.Context.Subscription.State)); + } WriteObject((PSAzureContext)AzureRmProfileProvider.Instance.Profile.Context); } } diff --git a/src/ResourceManager/Profile/Commands.Profile/Microsoft.Azure.Commands.Profile.format.ps1xml b/src/ResourceManager/Profile/Commands.Profile/Microsoft.Azure.Commands.Profile.format.ps1xml index 76cc3f806a77..0ca30c9f1b51 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Microsoft.Azure.Commands.Profile.format.ps1xml +++ b/src/ResourceManager/Profile/Commands.Profile/Microsoft.Azure.Commands.Profile.format.ps1xml @@ -85,6 +85,9 @@ TenantId + + State + diff --git a/src/ResourceManager/Profile/Commands.Profile/Models/ModelExtensions.cs b/src/ResourceManager/Profile/Commands.Profile/Models/ModelExtensions.cs index efe19076aac7..2d94ac29923e 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Models/ModelExtensions.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Models/ModelExtensions.cs @@ -31,6 +31,7 @@ internal static AzureSubscription ToAzureSubscription(this Subscription other, A subscription.Environment = context.Environment != null ? context.Environment.Name : EnvironmentName.AzureCloud; subscription.Id = new Guid(other.SubscriptionId); subscription.Name = other.DisplayName; + subscription.State = other.State; subscription.SetProperty(AzureSubscription.Property.Tenants, context.Tenant.Id.ToString()); return subscription; diff --git a/src/ResourceManager/Profile/Commands.Profile/Models/PSAzureSubscription.cs b/src/ResourceManager/Profile/Commands.Profile/Models/PSAzureSubscription.cs index 5345d159e30f..0092864f16fe 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Models/PSAzureSubscription.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Models/PSAzureSubscription.cs @@ -39,7 +39,8 @@ public static implicit operator PSAzureSubscription(AzureSubscription other) var subscription= new PSAzureSubscription { SubscriptionId = other.Id.ToString(), - SubscriptionName = other.Name , + SubscriptionName = other.Name, + State = other.State, TenantId = other.IsPropertySet(AzureSubscription.Property.Tenants)? other.GetProperty(AzureSubscription.Property.Tenants) : null }; @@ -89,6 +90,11 @@ public static implicit operator AzureSubscription(PSAzureSubscription other) result.Properties.SetProperty(AzureSubscription.Property.StorageAccount, other.CurrentStorageAccount); } + if (other.State != null) + { + result.State = other.State; + } + return result; } @@ -102,6 +108,11 @@ public static implicit operator AzureSubscription(PSAzureSubscription other) /// public string SubscriptionName { get; set; } + /// + /// Gets or sets subscription State + /// + public string State { get; set; } + /// /// The tenant home for the subscription. /// diff --git a/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs b/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs index e7ba73bd1f46..7c3f121d88ad 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs @@ -143,6 +143,12 @@ public AzureRMProfile Login( else { _profile.Context = new AzureContext(newSubscription, account, environment, newTenant); + if (!newSubscription.State.Equals("Enabled", StringComparison.OrdinalIgnoreCase)) + { + WriteWarningMessage(string.Format( + Microsoft.Azure.Commands.Profile.Properties.Resources.SelectedSubscriptionNotActive, + newSubscription.State)); + } } _profile.Context.TokenCache = TokenCache.DefaultShared.Serialize(); @@ -222,7 +228,11 @@ private void SwitchSubscription(AzureSubscription subscription) _profile.Context.Subscription.Properties[AzureSubscription.Property.Tenants] = tenantId; } - var newSubscription = new AzureSubscription { Id = subscription.Id }; + var newSubscription = new AzureSubscription + { + Id = subscription.Id, + State = subscription.State + }; if (_profile.Context.Subscription != null) { newSubscription.Account = _profile.Context.Subscription.Account; @@ -523,6 +533,7 @@ private bool TryGetTenantSubscription(IAccessToken accessToken, Account = accessToken.UserId, Environment = environment.Name, Name = subscriptionFromServer.DisplayName, + State = subscriptionFromServer.State, Properties = new Dictionary { { AzureSubscription.Property.Tenants, accessToken.TenantId } } }; diff --git a/src/ResourceManager/Profile/Commands.Profile/Profile/SelectAzureRMProfile.cs b/src/ResourceManager/Profile/Commands.Profile/Profile/SelectAzureRMProfile.cs index 6891e6a7c6ea..0b08ec0474c7 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Profile/SelectAzureRMProfile.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Profile/SelectAzureRMProfile.cs @@ -58,6 +58,18 @@ public override void ExecuteCmdlet() throw new ArgumentException(Resources.AzureProfileMustNotBeNull); } + if (AzureRmProfileProvider.Instance.Profile.Context != null && + AzureRmProfileProvider.Instance.Profile.Context.Subscription != null && + AzureRmProfileProvider.Instance.Profile.Context.Subscription.State != null && + !AzureRmProfileProvider.Instance.Profile.Context.Subscription.State.Equals( + "Enabled", + StringComparison.OrdinalIgnoreCase)) + { + WriteWarning(string.Format( + Microsoft.Azure.Commands.Profile.Properties.Resources.SelectedSubscriptionNotActive, + AzureRmProfileProvider.Instance.Profile.Context.Subscription.State)); + } + WriteObject((PSAzureProfile)AzureRmProfileProvider.Instance.Profile); } } diff --git a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs index 19595829b2cd..feddf6cd0a08 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs +++ b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs @@ -150,6 +150,15 @@ internal static string NoValidTenant { } } + /// + /// Looks up a localized string similar to Selected subscription is in '{0}' state. . + /// + internal static string SelectedSubscriptionNotActive { + get { + return ResourceManager.GetString("SelectedSubscriptionNotActive", resourceCulture); + } + } + /// /// Looks up a localized string similar to Please provide either a subscription ID, subscription name, tenant Id or domain.. /// diff --git a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx index f78e48e6668c..d35a459a31eb 100644 --- a/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx +++ b/src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx @@ -147,6 +147,9 @@ Please provide a valid tenant Id on the command line or execute Login-AzureRmAccount. + + Selected subscription is in '{0}' state. + Please provide either a subscription ID, subscription name, tenant Id or domain.