private AzureAccount MergeAccountProperties(AzureAccount account1, AzureAccount account2) { if (account1 == null || account2 == null) { throw new ArgumentNullException("account1"); } if (account1.Id != account2.Id) { throw new ArgumentException("Account1 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> ListServiceManagementSubscriptions(ref AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior) { List<AzureSubscription> result = new List<AzureSubscription>(); if (!environment.IsEndpointSet(AzureEnvironment.Endpoint.ServiceManagement)) { return result; } foreach (var tenant in account.GetPropertyAsArray(AzureAccount.Property.Tenants)) { try { var tenantToken = AzureSession.AuthenticationFactory.Authenticate(ref account, environment, tenant, password, ShowDialog.Never); using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<WindowsAzure.Subscriptions.SubscriptionClient>( new TokenCloudCredentials(tenantToken.AccessToken), environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement))) { var subscriptionListResult = subscriptionClient.Subscriptions.List(); foreach (var subscription in subscriptionListResult.Subscriptions) { AzureSubscription psSubscription = new AzureSubscription { Id = new Guid(subscription.SubscriptionId), Name = subscription.SubscriptionName, Environment = environment.Name }; psSubscription.Properties[AzureSubscription.Property.SupportedModes] = AzureModule.AzureServiceManagement.ToString(); psSubscription.SetProperty(AzureSubscription.Property.Tenants, subscription.ActiveDirectoryTenantId); AzureSession.SubscriptionTokenCache[Tuple.Create(psSubscription.Id, account.Id)] = tenantToken; result.Add(psSubscription); } } } catch (CloudException cEx) { WriteOrThrowAadExceptionMessage(cEx); } catch (AadAuthenticationException aadEx) { WriteOrThrowAadExceptionMessage(aadEx); } } return result; }
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]; } }