TerminateEnvironmentAsync() public method

Initiates the asynchronous execution of the TerminateEnvironment operation.
public TerminateEnvironmentAsync ( TerminateEnvironmentRequest request, System cancellationToken = default(CancellationToken) ) : Task
request Amazon.ElasticBeanstalk.Model.TerminateEnvironmentRequest Container for the necessary parameters to execute the TerminateEnvironment operation.
cancellationToken System /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. ///
return Task
        public async Task SingleSiteDockerApplicationEnsureSetupAsync(List<ConfigurationOptionSetting> optionSettings)
        {
            using (var ebClient = new AmazonElasticBeanstalkClient(creds, configurationProvider.RegionEndpoint))
            {
                var describeEnvironmentsResponse = await ebClient.DescribeEnvironmentsAsync();
                var current = describeEnvironmentsResponse.Environments
                    .SingleOrDefault(env =>
                        env?.CNAME != null && env.CNAME.StartsWith(currentCNamePrefix, StringComparison.CurrentCultureIgnoreCase) &&
                        env.Status == EnvironmentStatus.Ready);
                loggerProvider.GetLogger().Debug("Current: {@current}", current);
                
                List<Task> terminatingTasks = new List<Task>();
                if (current != null && current.Health != EnvironmentHealth.Green)
                {
                    loggerProvider.GetLogger().Debug("Current Environment is not in green state.  Deleting environment.");
                    var terminateCurrentEnvTask = ebClient.TerminateEnvironmentAsync(new TerminateEnvironmentRequest { EnvironmentId = current.EnvironmentId, TerminateResources = true });
                    terminatingTasks.Add(terminateCurrentEnvTask);
                    current = null;
                }
                
                if (terminatingTasks.Count > 0)
                {
                    await Task.WhenAll(terminatingTasks);
                    await EnsureTerminationsCompleteAsync();
                }

                if (current != null)
                {
                    loggerProvider.GetLogger().Debug("Environment is set up properly");
                    return;
                }
                
                loggerProvider.GetLogger().Debug("No environment is up - create");
                Task.WaitAll(new List<Task>
                {
                    CreateDockerCurrentEnvironmentAsync("A", optionSettings),
                }.ToArray());
                
                
            }
        }
        public async Task IisEnsureInitialEnvironmentSetupAsync(List<ConfigurationOptionSetting> optionSettings)
        {
            using (var ebClient = new AmazonElasticBeanstalkClient(creds, configurationProvider.RegionEndpoint))
            {
                var describeEnvironmentsResponse = await ebClient.DescribeEnvironmentsAsync(new DescribeEnvironmentsRequest
                {
                    IncludeDeleted = false
                });
                loggerProvider.GetLogger().Debug("Environment Repponse: {@response}", describeEnvironmentsResponse);
                var current = describeEnvironmentsResponse.Environments
                    .SingleOrDefault(env =>
                        env?.CNAME != null && env.CNAME.StartsWith(currentCNamePrefix, StringComparison.CurrentCultureIgnoreCase) && env.Status == EnvironmentStatus.Ready);

                loggerProvider.GetLogger().Debug("Current: {@current}", current);
                var next = describeEnvironmentsResponse.Environments
                    .SingleOrDefault(env =>
                        env?.CNAME != null && env.CNAME.StartsWith(nextCNamePrefix, StringComparison.CurrentCultureIgnoreCase) && env.Status == EnvironmentStatus.Ready);
                loggerProvider.GetLogger().Debug("Next: {@next}", next);

                List<Task> terminatingTasks = new List<Task>();
                if (current != null && current.Health != EnvironmentHealth.Green)
                {
                    loggerProvider.GetLogger().Debug("Current Environment is not in green state.  Deleting environment.");
                    var terminateCurrentEnvTask = ebClient.TerminateEnvironmentAsync(new TerminateEnvironmentRequest { EnvironmentId = current.EnvironmentId, TerminateResources = true });
                    terminatingTasks.Add(terminateCurrentEnvTask);
                    current = null;
                }

                if (next != null && next.Health != EnvironmentHealth.Green)
                {
                    loggerProvider.GetLogger().Debug("Next Environment is not in green state.  Deleting environment.");
                    var terminateNextEnvTask = ebClient.TerminateEnvironmentAsync(new TerminateEnvironmentRequest { EnvironmentId = next.EnvironmentId, TerminateResources = true });
                    terminatingTasks.Add(terminateNextEnvTask);
                    next = null;
                }

                if (terminatingTasks.Count > 0)
                {
                    await Task.WhenAll(terminatingTasks);
                    await EnsureTerminationsCompleteAsync();
                }

                if (current != null && next != null)
                {
                    loggerProvider.GetLogger().Debug("Environments are set up properly");
                    return;
                }

                if (current == null && next != null)
                {
                    var msg = "Error state: no current running, but next is up";
                    loggerProvider.GetLogger().Error(msg);
                    throw new ElasticBeanstalkDeployerException(msg);
                }

                if (current == null)
                {
                    loggerProvider.GetLogger().Debug("Neither environment is up - create both");
                    Task.WaitAll(new List<Task>
                    {
                        CreateIisCurrentEnvironmentAsync("A", optionSettings),
                        CreateIisNextEnvironmentAsync("B", configurationProvider.Version, optionSettings)
                    }.ToArray());
                }
                else
                {
                    var useA = current.EnvironmentName.EndsWith("-B", StringComparison.CurrentCultureIgnoreCase);
                    if (useA)
                    {
                        await CreateIisNextEnvironmentAsync("A", optionSettings);
                    }
                    else
                    {
                        var currentEnvironment = await GetCurrentEnvironmentDescriptionAsync();
                        await CreateIisNextEnvironmentAsync("B", currentEnvironment.VersionLabel, optionSettings);
                    }
                }
            }
        }