public AzureAccount RemoveAccount(string accountId)
{
if (string.IsNullOrEmpty(accountId))
{
throw new ArgumentNullException("accountId", Resources.UserNameNeedsToBeSpecified);
}
if (!Profile.Accounts.ContainsKey(accountId))
{
throw new ArgumentException(Resources.UserNameIsNotValid, "accountId");
}
AzureAccount account = Profile.Accounts[accountId];
Profile.Accounts.Remove(account.Id);
foreach (AzureSubscription subscription in account.GetSubscriptions(Profile).ToArray())
{
if (string.Equals(subscription.Account, accountId, StringComparison.InvariantCultureIgnoreCase))
{
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))
{
Debug.Assert(subscription.Equals(Profile.DefaultSubscription));
WriteWarningMessage(Resources.RemoveDefaultSubscription);
}
Profile.Subscriptions.Remove(subscription.Id);
}
else
{
subscription.Account = remainingAccount.Id;
AddOrSetSubscription(subscription);
}
}
}
return account;
}