/// <summary>
        /// Remove the given subscription from the given account
        /// </summary>
        /// <param name="account">The account to check</param>
        /// <param name="id">The id of the subscription to remove</param>
        public static void RemoveSubscription(this IAzureAccount account, Guid id)
        {
            if (account.HasSubscription(id))
            {
                var remainingSubscriptions = account.GetSubscriptions().Where(s => new Guid(s) != id).ToArray();

                if (remainingSubscriptions.Any())
                {
                    account.SetSubscriptions(remainingSubscriptions);
                }
                else
                {
                    account.ExtendedProperties.Remove(AzureAccount.Property.Subscriptions);
                }
            }
        }
Ejemplo n.º 2
0
        public IAzureAccount RemoveAccount(string accountId)
        {
            if (string.IsNullOrEmpty(accountId))
            {
                throw new ArgumentNullException("accountId", Resources.UserNameNeedsToBeSpecified);
            }

            if (!Profile.AccountTable.ContainsKey(accountId))
            {
                throw new ArgumentException(Resources.UserNameIsNotValid, "accountId");
            }

            IAzureAccount account = Profile.AccountTable[accountId];

            Profile.AccountTable.Remove(account.Id);

            foreach (AzureSubscription subscription in account.GetSubscriptions(Profile).ToArray())
            {
                if (string.Equals(subscription.GetAccount(), accountId, StringComparison.InvariantCultureIgnoreCase))
                {
                    IAzureAccount remainingAccount = GetSubscriptionAccount(subscription.GetId());
                    // There's no default account to use, remove the subscription.
                    if (remainingAccount == null)
                    {
                        // Warn the user if the removed subscription is the default one.
                        if (subscription.IsPropertySet(AzureSubscription.Property.Default))
                        {
                            Debug.Assert(subscription.Equals(Profile.DefaultSubscription));
                            WriteWarningMessage(Resources.RemoveDefaultSubscription);
                        }

                        Profile.SubscriptionTable.Remove(subscription.GetId());
                    }
                    else
                    {
                        subscription.SetAccount(remainingAccount.Id);
                        AddOrSetSubscription(subscription);
                    }
                }
            }

            return(account);
        }
        /// <summary>
        /// Get the list of subscriptions associated with the given account
        /// </summary>
        /// <param name="account">The account to look for</param>
        /// <param name="profile">The profile to search</param>
        /// <returns>A list of subscriptions available to the given account</returns>
        public static List <IAzureSubscription> GetSubscriptions(this IAzureAccount account, IAzureContextContainer profile)
        {
            string[] subscriptions = new string[0];
            List <IAzureSubscription> subscriptionsList = new List <IAzureSubscription>();

            if (account.IsPropertySet(AzureAccount.Property.Subscriptions))
            {
                subscriptions = account.GetSubscriptions();
            }

            foreach (var subscription in subscriptions)
            {
                var foundSubscription = profile.Subscriptions.FirstOrDefault((s) => string.Equals(s.Id, subscription, StringComparison.OrdinalIgnoreCase));
                if (foundSubscription != null)
                {
                    subscriptionsList.Add(foundSubscription);
                }
            }

            return(subscriptionsList);
        }