FReply SCreateContainerGroupSection::DeployCloudFormation()

in GameLiftPlugin/Source/GameLiftPlugin/Private/SMenu/Containers/SCreateContainerGroupSection.cpp [476:580]


FReply SCreateContainerGroupSection::DeployCloudFormation()
{
	ProgressBar->ChangeProgressBarUIState(SProgressBar::EProgressBarUIState::InProgress);
	UGameLiftContainersStatus* ContainersStatus = GetMutableDefault<UGameLiftContainersStatus>();
	ContainersStatus->ErrorCreatingContainerGroupSection = false;
	ContainersStatus->IsCgdFailed = false;
	ContainersStatus->SaveConfig();
	if (IsContainerDeploymentStatusActive())
	{
		auto choice = FMessageDialog::Open(
			EAppMsgType::YesNo, Menu::DeployManagedEC2::kConfirmReploymentText);
		if (choice != EAppReturnType::Yes) {
			return FReply::Handled();
		}
	}
	PopulateDeploymentStatus();

	FText NotifyMessage = FText::GetEmpty();
	NotifyMessage = FText::Format(LOCTEXT("DeploymentNotificationAws", "{0}\n{1}"),
		FText::FromString(Menu::DeployContainers::kDeploymentStartedAwsScenarioNotification), ContainersStatus->Scenario);
	NotificationItem = Notifier::CreateNotification(NotifyMessage);
	ContainersStatus->Status = FText::FromString(EDeploymentMessageStateToString(EDeploymentMessageState::InProgressMessage));
	ContainersStatus->SaveConfig();

	// Start polling before deploying
	PollDescribeContainerGroupDefinition();

	Async(EAsyncExecution::Thread, 
		[this, NotificationItem = NotificationItem] () mutable
		{
			UGameLiftContainersStatus* ContainersStatus = GetMutableDefault<UGameLiftContainersStatus>();
			auto& deployer = IGameLiftCoreModule::Get().GetScenarioDeployer();

			// Pass in StackIdentifier for the GameName field in the deployer as a quick workaround
			// to decouple containers and ec2 stack deployments and fix describe container fleet calls
			FString HostingSolution = "-Containers";
			ContainersStatus->StackIdentifier = ContainersStatus->GameName.ToString() + HostingSolution;

			bool IsDeployed = deployer.DeployContainerScenario(
				ContainersStatus->Scenario,
				IGameLiftCoreModule::Get().GetProfileBootstrap().GetAccountInstance(),
				ContainersStatus->CgdName,
				ContainersStatus->ContainerImageName.ToString(),
				ContainersStatus->ContainerImageURI.ToString(),
				ContainersStatus->IntraContainerLaunchPath,
				ContainersStatus->StackIdentifier,
				ContainersStatus->OutConfigFilePath.ToString(),
				ContainersStatus->ConnectionPortRange,
				ContainersStatus->TotalVCPULimit.ToString(),
				ContainersStatus->TotalMemoryLimit.ToString()
			);
			if (IsDeployed)
			{
				UE_LOG(GameLiftPluginLog, Display, TEXT("%s"), Menu::DeployContainers::kDeploymentSucceed);
				ContainersStatus->IsLastStepComplete = true;
				ContainersStatus->ApiGatewayEndpoint = FText::FromString(deployer.GetLastApiGatewayEndpoint());
				ContainersStatus->CognitoClientId = FText::FromString(deployer.GetLastCognitoClientId());
				ContainersStatus->Status = FText::FromString(EDeploymentMessageStateToString(EDeploymentMessageState::ActiveMessage));
			}
			else
			{
				UE_LOG(GameLiftPluginLog, Display, TEXT("%s"), Menu::DeployContainers::kDeploymentFailed);
				if (!ContainersStatus->IsCgdConfigurationFinished)
				{
					ContainersStatus->ErrorCreatingContainerGroupSection = true;
					if (ContainersStatus->IsCgdFailed)
					{
						ContainersStatus->ErrorMessageCreatingContainerImage = Menu::DeployContainers::kCgdStatusFailedErrorMessage;
						ContainersStatus->LatestError = Menu::DeployContainers::kCgdStatusFailedErrorMessage;
					}
					ContainersStatus->LatestError = Menu::DeployContainers::kDeploymentFailedMessage;
				}
				else 
				{
					ContainersStatus->ErrorCreatingContainerFleet = true;
					ContainersStatus->ErrorMessageCreatingContainerFleet = Menu::DeployContainers::kDeploymentFailedMessage;
					ContainersStatus->LatestError = ContainersStatus->ErrorMessageCreatingContainerFleet;
				}
				ContainersStatus->Status = FText::FromString(EDeploymentMessageStateToString(EDeploymentMessageState::FailureMessage));
			}
			ContainersStatus->SaveConfig();

			Async(EAsyncExecution::TaskGraphMainThread,
				[this, NotificationItem = NotificationItem]
				{
					UGameLiftContainersStatus* ContainersStatus = GetMutableDefault<UGameLiftContainersStatus>();
					if (ContainersStatus->ErrorCreatingContainerGroupSection)
					{
						ProgressBar->ChangeProgressBarUIState(SProgressBar::EProgressBarUIState::ProgressError);
						ErrorTextBlock->SetText(ContainersStatus->LatestError);
					}
					if (ContainersStatus->IsLastStepComplete)
					{
						Notifier::CompleteWithSuccess(NotificationItem, Menu::DeployContainers::kDeploymentCompletedNotification);
					}
					else
					{
						Notifier::CompleteWithFailure(NotificationItem, Menu::DeployContainers::kDeploymentFailedNotification);
					}
					OnDeploymentCompleteMultiDelegate.Broadcast();
				});
		});

		return FReply::Handled();
}