// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
using System;
using System.IO;
using AmazonGameLiftPlugin.Core.SettingsManagement.Models;
using AmazonGameLiftPlugin.Core.Shared;
namespace AmazonGameLift.Editor
{
internal class DeploymentRequestFactory
{
private CoreApi GameLiftCoreApi { get; }
internal DeploymentRequestFactory(CoreApi coreApi) =>
GameLiftCoreApi = coreApi ?? throw new ArgumentNullException(nameof(coreApi));
/// For all parameters.
internal virtual (DeploymentRequest request, bool success, Response failedResponse) CreateRequest(
string scenarioFolderPath, string gameName, bool isDevelopmentBuild)
{
if (scenarioFolderPath is null)
{
throw new ArgumentNullException(nameof(scenarioFolderPath));
}
if (gameName is null)
{
throw new ArgumentNullException(nameof(gameName));
}
string stackName = GameLiftCoreApi.GetStackName(gameName);
string cfnTemplatePath = Path.Combine(scenarioFolderPath, Paths.CfnTemplateFileName);
string parametersPath = Path.Combine(scenarioFolderPath, Paths.ParametersFileName);
string lambdaFolderPath = Path.Combine(scenarioFolderPath, Paths.LambdaFolderPathInScenario);
GetSettingResponse currentProfileResponse = GameLiftCoreApi.GetSetting(SettingsKeys.CurrentProfileName);
if (!currentProfileResponse.Success)
{
return (null, false, currentProfileResponse);
}
GetSettingResponse currentRegionResponse = GameLiftCoreApi.GetSetting(SettingsKeys.CurrentRegion);
if (!currentRegionResponse.Success)
{
return (null, false, currentRegionResponse);
}
GetSettingResponse bucketResponse = GameLiftCoreApi.GetSetting(SettingsKeys.CurrentBucketName);
if (!bucketResponse.Success)
{
return (null, false, bucketResponse);
}
var request = new DeploymentRequest()
{
Profile = currentProfileResponse.Value,
Region = currentRegionResponse.Value,
BucketName = bucketResponse.Value,
StackName = stackName,
CfnTemplatePath = cfnTemplatePath,
ParametersPath = parametersPath,
IsDevelopmentBuild = isDevelopmentBuild,
GameName = gameName,
LambdaFolderPath = lambdaFolderPath,
};
return (request, true, null);
}
///
internal virtual DeploymentRequest WithServerBuild(DeploymentRequest request, string buildFolderPath)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
if (buildFolderPath is null)
{
throw new ArgumentNullException(nameof(buildFolderPath));
}
request.BuildFolderPath = buildFolderPath;
request.BuildS3Key = GameLiftCoreApi.GetBuildS3Key();
return request;
}
}
}