private AzureAccount MergeAccountProperties(AzureAccount account1, AzureAccount account2) { if (account1 == null || account2 == null) { throw new ArgumentNullException("account1"); } if (!string.Equals(account1.Id, account2.Id, StringComparison.InvariantCultureIgnoreCase)) { throw new ArgumentException("Account Ids do not match."); } if (account1.Type != account2.Type) { throw new ArgumentException("Account1 types do not match."); } AzureAccount mergeAccount = new AzureAccount { Id = account1.Id, Type = account1.Type }; // Merge all properties foreach (AzureAccount.Property property in Enum.GetValues(typeof(AzureAccount.Property))) { string propertyValue = account1.GetProperty(property) ?? account2.GetProperty(property); if (propertyValue != null) { mergeAccount.Properties[property] = propertyValue; } } // Merge Tenants var tenants = account1.GetPropertyAsArray(AzureAccount.Property.Tenants) .Union(account2.GetPropertyAsArray(AzureAccount.Property.Tenants), StringComparer.CurrentCultureIgnoreCase); mergeAccount.SetProperty(AzureAccount.Property.Tenants, tenants.ToArray()); // Merge Subscriptions var subscriptions = account1.GetPropertyAsArray(AzureAccount.Property.Subscriptions) .Union(account2.GetPropertyAsArray(AzureAccount.Property.Subscriptions), StringComparer.CurrentCultureIgnoreCase); mergeAccount.SetProperty(AzureAccount.Property.Subscriptions, subscriptions.ToArray()); return mergeAccount; }
private IEnumerable<AzureSubscription> ListSubscriptionsFromServer(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior) { string[] tenants = null; try { if (!account.IsPropertySet(AzureAccount.Property.Tenants)) { tenants = LoadAccountTenants(account, environment, password, promptBehavior); } } catch (AadAuthenticationException aadEx) { WriteOrThrowAadExceptionMessage(aadEx); return new AzureSubscription[0]; } try { tenants = tenants ?? account.GetPropertyAsArray(AzureAccount.Property.Tenants); List<AzureSubscription> mergedSubscriptions = MergeSubscriptions( ListServiceManagementSubscriptions(account, environment, password, ShowDialog.Never, tenants).ToList(), ListResourceManagerSubscriptions(account, environment, password, ShowDialog.Never, tenants).ToList()); // Set user ID foreach (var subscription in mergedSubscriptions) { account.SetOrAppendProperty(AzureAccount.Property.Subscriptions, subscription.Id.ToString()); } if (mergedSubscriptions.Any()) { return mergedSubscriptions; } else { return new AzureSubscription[0]; } } catch (AadAuthenticationException aadEx) { WriteOrThrowAadExceptionMessage(aadEx); return new AzureSubscription[0]; } }