public void ClearAll()
        {
            Profile.Accounts.Clear();
            Profile.DefaultSubscription = null;
            Profile.Environments.Clear();
            Profile.Subscriptions.Clear();
            AzureSession.SetCurrentContext(null, null, null);
            Profile.Save();

            ProtectedFileTokenCache.Instance.Clear();
        }
        public AzureAccount RemoveAccount(string accountId)
        {
            if (string.IsNullOrEmpty(accountId))
            {
                throw new ArgumentNullException("User name needs to be specified.", "userName");
            }

            if (!Profile.Accounts.ContainsKey(accountId))
            {
                throw new ArgumentException("User name is not valid.", "userName");
            }

            AzureAccount account = Profile.Accounts[accountId];

            Profile.Accounts.Remove(account.Id);

            foreach (AzureSubscription subscription in account.GetSubscriptions(Profile).ToArray())
            {
                if (subscription.Account == accountId)
                {
                    AzureAccount remainingAccount = GetSubscriptionAccount(subscription.Id);
                    // 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))
                        {
                            WriteWarningMessage(Resources.RemoveDefaultSubscription);
                        }

                        // Warn the user if the removed subscription is the current one.
                        if (subscription.Equals(AzureSession.CurrentContext.Subscription))
                        {
                            WriteWarningMessage(Resources.RemoveCurrentSubscription);
                            AzureSession.SetCurrentContext(null, null, null);
                        }

                        Profile.Subscriptions.Remove(subscription.Id);
                    }
                    else
                    {
                        subscription.Account = remainingAccount.Id;
                        AddOrSetSubscription(subscription);
                    }
                }
            }

            return(account);
        }
        public AzureSubscription AddOrSetSubscription(AzureSubscription subscription)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException("Subscription needs to be specified.", "subscription");
            }
            if (subscription.Environment == null)
            {
                throw new ArgumentNullException("Environment needs to be specified.", "subscription.Environment");
            }
            // Validate environment
            GetEnvironmentOrDefault(subscription.Environment);

            if (Profile.Subscriptions.ContainsKey(subscription.Id))
            {
                Profile.Subscriptions[subscription.Id] = MergeSubscriptionProperties(subscription, Profile.Subscriptions[subscription.Id]);
            }
            else
            {
                Debug.Assert(!string.IsNullOrEmpty(subscription.Account));
                if (!Profile.Accounts.ContainsKey(subscription.Account))
                {
                    throw new KeyNotFoundException(string.Format("The specified account {0} does not exist in profile accounts", subscription.Account));
                }

                Profile.Subscriptions[subscription.Id] = subscription;
            }

            // Update in-memory subscription
            if (AzureSession.CurrentContext != null && AzureSession.CurrentContext.Subscription != null &&
                AzureSession.CurrentContext.Subscription.Id == subscription.Id)
            {
                var account     = GetAccountOrDefault(subscription.Account);
                var environment = GetEnvironmentOrDefault(subscription.Environment);
                AzureSession.SetCurrentContext(Profile.Subscriptions[subscription.Id], environment, account);
            }

            return(Profile.Subscriptions[subscription.Id]);
        }
        public AzureSubscription RemoveSubscription(Guid id)
        {
            if (!Profile.Subscriptions.ContainsKey(id))
            {
                throw new ArgumentException(string.Format(Resources.SubscriptionIdNotFoundMessage, id), "id");
            }

            var subscription = Profile.Subscriptions[id];

            if (subscription.IsPropertySet(AzureSubscription.Property.Default))
            {
                WriteWarningMessage(Resources.RemoveDefaultSubscription);
            }

            // Warn the user if the removed subscription is the current one.
            if (AzureSession.CurrentContext.Subscription != null && subscription.Id == AzureSession.CurrentContext.Subscription.Id)
            {
                WriteWarningMessage(Resources.RemoveCurrentSubscription);
                AzureSession.SetCurrentContext(null, null, null);
            }

            Profile.Subscriptions.Remove(id);

            // Remove this subscription from its associated AzureAccounts
            List <AzureAccount> accounts = ListSubscriptionAccounts(id);

            foreach (AzureAccount account in accounts)
            {
                account.RemoveSubscription(id);
                if (!account.IsPropertySet(AzureAccount.Property.Subscriptions))
                {
                    Profile.Accounts.Remove(account.Id);
                }
            }

            return(subscription);
        }