Exemple #1
0
        public async Task <ServiceResponse <ServerInstanceInfo> > GetServerInstancesAsync()
        {
            ServiceResponse <ServerInstanceInfo> result          = new ServiceResponse <ServerInstanceInfo>();
            List <ServerInstanceInfo>            serverInstances = new List <ServerInstanceInfo>();

            if (AccountManager != null && AzureAccountManager != null && AzureResourceManager != null)
            {
                try
                {
                    IEnumerable <IAzureUserAccountSubscriptionContext> subscriptions =
                        await AzureAccountManager.GetSelectedSubscriptionsAsync();

                    if (subscriptions != null)
                    {
                        foreach (IAzureUserAccountSubscriptionContext subscription in subscriptions)
                        {
                            using (IAzureResourceManagementSession session = await AzureResourceManager.CreateSessionAsync(subscription))
                            {
                                IEnumerable <IAzureSqlServerResource> azureResources =
                                    await AzureResourceManager.GetSqlServerAzureResourcesAsync(session);

                                serverInstances.AddRange(
                                    azureResources.Select(x =>
                                                          new ServerInstanceInfo(ServerDefinition)
                                {
                                    Name = x.Name,
                                    FullyQualifiedDomainName = x.FullyQualifiedDomainName,
                                    AdministratorLogin       = x.AdministratorLogin
                                }));
                            }
                        }
                    }
                    result = new ServiceResponse <ServerInstanceInfo>(serverInstances);
                }
                catch (Exception ex)
                {
                    result = new ServiceResponse <ServerInstanceInfo>(serverInstances, new List <Exception>()
                    {
                        ex
                    });
                }
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Returns a  list of Azure sql databases for given subscription
        /// </summary>
        private async Task <ServiceResponse <DatabaseInstanceInfo> > GetDatabaseForSubscriptionFromServiceAsync(
            IAzureUserAccountSubscriptionContext input, string serverName,
            CancellationToken cancellationToken, CancellationToken internalCancellationToken)
        {
            ServiceResponse <DatabaseInstanceInfo> result = null;

            try
            {
                if (!cancellationToken.IsCancellationRequested && !internalCancellationToken.IsCancellationRequested)
                {
                    using (IAzureResourceManagementSession session = await AzureResourceManager.CreateSessionAsync(input))
                    {
                        //find the server matches with the given servername which should be only one
                        bool shouldFilter = !string.IsNullOrEmpty(serverName);
                        IEnumerable <IAzureSqlServerResource> sqlAzureServers = await AzureResourceManager.GetSqlServerAzureResourcesAsync(session);

                        IEnumerable <IAzureSqlServerResource> filteredServers = !shouldFilter ? sqlAzureServers : sqlAzureServers.Where(x =>
                                                                                                                                        x.FullyQualifiedDomainName != null &&
                                                                                                                                        x.FullyQualifiedDomainName.Equals(serverName,
                                                                                                                                                                          StringComparison.OrdinalIgnoreCase));

                        IList <IAzureSqlServerResource> filteredServersList = filteredServers.ToList();
                        result = await GetDatabasesForSubscriptionServersAsync(session, filteredServersList.ToList(), cancellationToken);

                        //Set response Found to true to notify the other tasks to cancel
                        if (shouldFilter && filteredServersList.Any())
                        {
                            result.Found = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result = new ServiceResponse <DatabaseInstanceInfo>(ex);
            }

            return(result ?? new ServiceResponse <DatabaseInstanceInfo>());
        }