private async Task RefreshDatafactories()

in SamplesV1/ADFSecurePublish/SecurePublishForm/MainWindow.xaml.cs [167:250]


        private async Task RefreshDatafactories()
        {
            SettingsContext settingsContext;
            environment = environmentList.SelectedItem as string;

            try
            {
                settingsContext = settingsContextManager.GetSettingsContext(environment);

                // Get KeyVault resolver which is used to retreive keyvault secrets based on environment context
                IKeyVaultResolver keyVault;
                if (!string.IsNullOrEmpty(settingsContext.KeyVaultCertificateThumbprint))
                {
                    var cert = KeyVaultResolver.FindCertificateByThumbprint(settingsContext.KeyVaultCertificateThumbprint);

                    if (cert == null)
                    {
                        Write($"No cert was found using thumbprint {settingsContext.KeyVaultCertificateThumbprint}", "Red");
                        return;
                    }

                    keyVault = new KeyVaultResolver(settingsContext.KeyVaultName, settingsContext.KeyVaultDnsSuffix, settingsContext.KeyVaultCertClientId, cert);
                }
                else
                {
                    keyVault = new KeyVaultResolver(settingsContext.KeyVaultName, settingsContext.KeyVaultDnsSuffix, settingsContext.KeyVaultCertClientId, settingsContext.KeyVaultCertClientSecret);
                }

                settingsContext.SubscriptionId = settings.Subscriptions[subscriptionList.SelectedIndex].Id;
                try
                {
                    settingsContext.AdfClientSecret = (await keyVault.GetSecret("SecurePublishAdfClientSecret")).Value;
                }
                catch (Exception ex)
                {
                    Write($"The secret called SecurePublishAdfClientSecret was not found in the KeyVault '{settingsContext.KeyVaultName}'. The ADF Client Secret is a password which was associated with the AAD Client ID '{settingsContext.KeyVaultCertClientId}' when it was originally set up. If you are setting up a new KeyVault and a previous KeyVault has already been used, you can get this value from the previous KeyVault. Otherwise refer to the user documentation on creating a new client ID and associating it with your Azure subscription.", "Red");
                    WriteError(ex);
                    return;
                }

                publishManager = new PublishManager(keyVault, settingsContext, this);

                dataFactoryList = await AzureAccessUtilities.GetDataFactories(settingsContext);
            }
            catch (Exception e)
            {
                Write(e.Message, "Red");

                Dispatcher.Invoke(() =>
                {
                    dataFactoryListBox.IsEnabled = false;
                });

                return;
            }

            if (!dataFactoryList.Any())
            {
                Write("No data factories found in subscription: " + settingsContext.SubscriptionId, "Orange");
                Write($"They either do not exist or else you may need to associate the Client ID '{settingsContext.AdfClientId}' with the subscription. To do that, perform the following steps:", "Orange");
                Write("1. Open up PowerShell", "Orange");
                Write("2. Log in to Azure by typing in the cmd: Login-AzureRmAccount", "Orange");
                Write($"3. Change to the subscription you wish to use by typing the cmd: Select-AzureRmSubscription -SubscriptionId '{settingsContext.SubscriptionId}'", "Orange");
                Write("4. Associate the Client ID with the Data Factory Contributer role in the current subscription by typing:", "Orange");
                Write($"New-AzureRmRoleAssignment -RoleDefinitionName 'Data Factory Contributor' -ServicePrincipalName '{settingsContext.AdfClientId}'", "Orange");

                Dispatcher.Invoke(() =>
                {
                    dataFactoryListBox.IsEnabled = false;
                });
            }
            else
            {
                Dispatcher.Invoke(() =>
                {
                    dataFactoryListBox.IsEnabled = true;
                });
            }

            Dispatcher.Invoke(() =>
            {
                dataFactoryListBox.ItemsSource = dataFactoryList.Select(x => x.Name);
            });
        }