private void RunGetPowerAppsEnvironmentOperationAsync()
        {
            Task.Run(() =>
            {
                OperationRunner singleRunner = new OperationRunner(
                    null,
                    this,
                    WizardContext.LogFileStream);
                singleRunner.IndeterminateOps = true;
                singleRunner.OnLog           += WriteLog;

                singleRunner.Operations.Enqueue(new Operation()
                {
                    Name = "GetPowerAppsEnvironments",
                    OperationFunction = GetPowerAppsEnvironmentsAsync,
                    ValidateFunction  = (context) =>
                    {
                        return(context.LastOperationStatusCode == 0);
                    },
                    OperationCompletedHandler = (result) =>
                    {
                        GetPowerAppsEnvironmentsResponse response = (GetPowerAppsEnvironmentsResponse)result;

                        DataModel.InstallationConfiguration.PowerApps.Environments = response
                                                                                     .Value
                                                                                     .Select(x => new PowerAppsEnvironment()
                        {
                            DisplayName            = x.Properties.DisplayName,
                            EnvironmentName        = x.Name,
                            OrganizationName       = x.Properties.LinkedEnvironmentMetadata != null ? x.Properties.LinkedEnvironmentMetadata.UniqueName : null,
                            OrganizationDomainName = x.Properties.LinkedEnvironmentMetadata != null ? x.Properties.LinkedEnvironmentMetadata.DomainName : null,
                            WebApplicationUrl      = x.Properties.LinkedEnvironmentMetadata != null ? x.Properties.LinkedEnvironmentMetadata.InstanceUrl : null,
                        }).ToList();

                        PowerAppsEnvironment defaultEnvironment = DataModel
                                                                  .InstallationConfiguration
                                                                  .PowerApps
                                                                  .Environments
                                                                  .Where(x => DataModel.InstallationConfiguration.PowerApps.SelectedEnvironment != null &&
                                                                         string.Compare(x.EnvironmentName, DataModel.InstallationConfiguration.PowerApps.SelectedEnvironment.EnvironmentName, true) == 0)
                                                                  .FirstOrDefault();

                        DataModel.InstallationConfiguration.PowerApps.SelectedEnvironment =
                            defaultEnvironment ??
                            DataModel
                            .InstallationConfiguration
                            .PowerApps
                            .Environments
                            .FirstOrDefault();
                    },
                    ExceptionHandler = (ex) =>
                    {
                        DataModel.StatusMessage = "Failed to acquire PowerApps environments!";
                    },
                });

                singleRunner.RunOperations();
            });
        }
        private void RunGetAzureResourceGroupsOperationAsync()
        {
            Task.Run(() =>
            {
                OperationRunner singleRunner = new OperationRunner(
                    null,
                    this,
                    WizardContext.LogFileStream);
                singleRunner.IndeterminateOps = true;
                singleRunner.OnLog           += WriteLog;

                singleRunner.Operations.Enqueue(new Operation()
                {
                    Name = "GetAzureResourceGroups",
                    OperationFunction = GetAzureResourceGroupsAsync,
                    ValidateFunction  = (context) =>
                    {
                        return(context.LastOperationStatusCode == 0);
                    },
                    OperationCompletedHandler = (result) =>
                    {
                        Contract.ResourceGroup[] response = (Contract.ResourceGroup[])result;

                        DataModel.InstallationConfiguration.Azure.ResourceGroups = response
                                                                                   .Where(x => x.Tags != null &&
                                                                                          x.Tags.ContainsKey(InstallerModel.ResourceGroupTagKey))
                                                                                   .Select(x => new AzureResourceGroup()
                        {
                            Name     = x.Name,
                            Location = x.Location,
                            Tags     = x.Tags,
                        });

                        AzureResourceGroup defaultResourceGroup = DataModel
                                                                  .InstallationConfiguration
                                                                  .Azure
                                                                  .ResourceGroups
                                                                  .Where(x => string.Compare(x.Name, DataModel.InstallationConfiguration.Azure.ResourceGroupName, StringComparison.Ordinal) == 0)
                                                                  .FirstOrDefault();

                        DataModel.InstallationConfiguration.Azure.SelectedResourceGroup =
                            defaultResourceGroup ??
                            DataModel
                            .InstallationConfiguration
                            .Azure
                            .ResourceGroups
                            .FirstOrDefault();
                    },
                    ExceptionHandler = (ex) =>
                    {
                        DataModel.StatusMessage = "Failed to acquire Azure Resource Groups!";
                    },
                });

                singleRunner.RunOperations();
            });
        }
        private void RunGetAzureSubscriptionsOperationAsync()
        {
            Task.Run(() =>
            {
                OperationRunner singleRunner = new OperationRunner(
                    null,
                    this,
                    WizardContext.LogFileStream);
                singleRunner.IndeterminateOps = true;
                singleRunner.OnLog           += WriteLog;

                singleRunner.Operations.Enqueue(new Operation()
                {
                    Name = "GetAzureSubscriptions",
                    OperationFunction = GetAzureSubscriptionsAsync,
                    ValidateFunction  = (context) =>
                    {
                        return(context.LastOperationStatusCode == 0);
                    },
                    OperationCompletedHandler = (result) =>
                    {
                        Subscription[] response = (Subscription[])result;

                        DataModel.InstallationConfiguration.Azure.CheckedForSubscriptions = true;

                        DataModel.InstallationConfiguration.Azure.Subscriptions = response
                                                                                  .Select(x => new AzureSubscription()
                        {
                            Id   = x.SubscriptionId,
                            Name = x.DisplayName,
                        });

                        AzureSubscription defaultSubscription = DataModel
                                                                .InstallationConfiguration
                                                                .Azure
                                                                .Subscriptions
                                                                .Where(x => DataModel.InstallationConfiguration.Azure.SelectedSubscription != null &&
                                                                       string.Compare(x.Id, DataModel.InstallationConfiguration.Azure.SelectedSubscription.Id, true) == 0)
                                                                .FirstOrDefault();

                        DataModel.InstallationConfiguration.Azure.SelectedSubscription =
                            defaultSubscription ??
                            DataModel
                            .InstallationConfiguration
                            .Azure
                            .Subscriptions
                            .FirstOrDefault();
                    },
                    ExceptionHandler = (ex) =>
                    {
                        DataModel.StatusMessage = "Failed to acquire Azure subscriptions!";
                    },
                });
                singleRunner.RunOperations();
            });
        }
        private void RunCheckDynamics365CRMSolutionAsync()
        {
            Task.Run(() =>
            {
                OperationRunner singleRunner = new OperationRunner(
                    null,
                    this,
                    WizardContext.LogFileStream);
                singleRunner.IndeterminateOps = true;
                singleRunner.OnLog           += WriteLog;

                singleRunner.Operations.Enqueue(new Operation()
                {
                    Name = "CheckDynamics365CRMSolution",
                    OperationFunction         = CheckDynamics365CRMSolutionAsync,
                    OperationCompletedHandler = (result) =>
                    {
                        if (result == null)
                        {
                            DataModel.InstallationConfiguration.PowerApps.Environments.Remove(
                                DataModel.InstallationConfiguration.PowerApps.SelectedEnvironment);
                            DataModel.InstallationConfiguration.PowerApps.SelectedEnvironment = null;

                            throw new Exception($"Could not find {DataModel.InstallationConfiguration.DynamicsCrm.SolutionUniqueName} solution in selected PowerApps environment!");
                        }
                    },
                    ValidateFunction = (context) =>
                    {
                        return(context.LastOperationStatusCode == 0);
                    },
                    ExceptionHandler = (ex) =>
                    {
                        DataModel.StatusMessage = ex.Message;
                    },
                });
                singleRunner.RunOperations();
            });
        }