Skip to content

Commit

Permalink
Merge pull request #332 from aws/kmalhar/integ-test-fix
Browse files Browse the repository at this point in the history
fix: Replace default values for option setting items with stack name as prefix
  • Loading branch information
96malhar authored Sep 20, 2021
2 parents 4783ec0 + 59d77ec commit c5b0ef6
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 28 deletions.
10 changes: 6 additions & 4 deletions src/AWS.Deploy.CLI/Commands/DeployCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace AWS.Deploy.CLI.Commands
{
public class DeployCommand
{
public const string REPLACE_TOKEN_STACK_NAME = "{StackName}";

private readonly IToolInteractiveService _toolInteractiveService;
private readonly IOrchestratorInteractiveService _orchestratorInteractiveService;
private readonly ICdkProjectHandler _cdkProjectHandler;
Expand Down Expand Up @@ -162,7 +164,7 @@ private void DisplayOutputResources(List<DisplayedResourceItem> displayedResourc

// Get Cloudformation stack name.
var cloudApplicationName = GetCloudApplicationName(stackName, userDeploymentSettings, compatibleApplications);

// Find existing application with the same CloudFormation stack name.
var deployedApplication = allDeployedApplications.FirstOrDefault(x => string.Equals(x.Name, cloudApplicationName));

Expand Down Expand Up @@ -191,6 +193,9 @@ private void DisplayOutputResources(List<DisplayedResourceItem> displayedResourc
}
}

// Apply the user entered stack name to the recommendation so that any default settings based on stack name are applied.
selectedRecommendation.AddReplacementToken(REPLACE_TOKEN_STACK_NAME, cloudApplicationName);

var cloudApplication = new CloudApplication(cloudApplicationName, selectedRecommendation.Recipe.Id);

return (orchestrator, selectedRecommendation, cloudApplication);
Expand Down Expand Up @@ -222,9 +227,6 @@ public async Task EvaluateSystemCapabilities(Recommendation selectedRecommendati
/// <param name="userDeploymentSettings"><see cref="UserDeploymentSettings"/></param>
public async Task ConfigureDeployment(CloudApplication cloudApplication, Orchestrator orchestrator, Recommendation selectedRecommendation, UserDeploymentSettings? userDeploymentSettings)
{
// Apply the user entered project name to the recommendation so that any default settings based on project name are applied.
selectedRecommendation.OverrideProjectName(cloudApplication.Name);

var configurableOptionSettings = selectedRecommendation.GetConfigurableOptionSettingItems();

if (userDeploymentSettings != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using AWS.Deploy.Orchestration.DisplayedResources;
using AWS.Deploy.Common.IO;
using AWS.Deploy.Orchestration.LocalUserSettings;
using AWS.Deploy.CLI.Commands;

namespace AWS.Deploy.CLI.ServerMode.Controllers
{
Expand Down Expand Up @@ -284,6 +285,7 @@ public async Task<IActionResult> SetDeploymentTarget(string sessionId, [FromBody

state.ApplicationDetails.Name = input.NewDeploymentName;
state.ApplicationDetails.RecipeId = input.NewDeploymentRecipeId;
state.SelectedRecommendation.AddReplacementToken(DeployCommand.REPLACE_TOKEN_STACK_NAME, input.NewDeploymentName);
}
else if(!string.IsNullOrEmpty(input.ExistingDeploymentName))
{
Expand All @@ -307,6 +309,7 @@ public async Task<IActionResult> SetDeploymentTarget(string sessionId, [FromBody

state.ApplicationDetails.Name = input.ExistingDeploymentName;
state.ApplicationDetails.RecipeId = existingDeployment.RecipeId;
state.SelectedRecommendation.AddReplacementToken(DeployCommand.REPLACE_TOKEN_STACK_NAME, input.ExistingDeploymentName);
}

return Ok();
Expand Down
18 changes: 5 additions & 13 deletions src/AWS.Deploy.Common/Recommendation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace AWS.Deploy.Common
{
public class Recommendation : IUserInputOption
{
private const string REPLACE_TOKEN_PROJECT_NAME = "{ProjectName}";

public string ProjectPath => ProjectDefinition.ProjectPath;

public ProjectDefinition ProjectDefinition { get; }
Expand Down Expand Up @@ -46,24 +44,13 @@ public Recommendation(RecipeDefinition recipe, ProjectDefinition projectDefiniti
DeploymentBundle = new DeploymentBundle();
DeploymentBundleSettings = deploymentBundleSettings;

_replacementTokens[REPLACE_TOKEN_PROJECT_NAME] = Path.GetFileNameWithoutExtension(projectDefinition.ProjectPath);

foreach (var replacement in additionalReplacements)
{
if (!_replacementTokens.ContainsKey(replacement.Key))
_replacementTokens[replacement.Key] = replacement.Value;
}
}

/// <summary>
/// Overrides the project name used as a replacement token in default setting values.
/// </summary>
/// <param name="name"></param>
public void OverrideProjectName(string name)
{
_replacementTokens[REPLACE_TOKEN_PROJECT_NAME] = name;
}

public Recommendation ApplyPreviousSettings(IDictionary<string, object> previousSettings)
{
var recommendation = this.DeepCopy();
Expand All @@ -73,6 +60,11 @@ public Recommendation ApplyPreviousSettings(IDictionary<string, object> previous
return recommendation;
}

public void AddReplacementToken(string key, string value)
{
_replacementTokens[key] = value;
}

private void ApplyPreviousSettings(Recommendation recommendation, IDictionary<string, object> previousSettings)
{
recommendation.IsExistingCloudApplication = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"TypeHint": "AppRunnerService",
"AdvancedSetting": false,
"Updatable": false,
"DefaultValue": "{ProjectName}-service"
"DefaultValue": "{StackName}-service"
},
{
"Id": "Port",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"Name": "New Cluster Name",
"Description": "The name of the new cluster to create.",
"Type": "String",
"DefaultValue": "{ProjectName}",
"DefaultValue": "{StackName}",
"AdvancedSetting": false,
"Updatable": false,
"Validators": [
Expand Down Expand Up @@ -145,7 +145,7 @@
"Description": "The name of the ECS service running in the cluster.",
"Type": "String",
"TypeHint": "ECSService",
"DefaultValue": "{ProjectName}-service",
"DefaultValue": "{StackName}-service",
"AdvancedSetting": false,
"Updatable": false,
"Validators": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"Name": "Application Name",
"Description": "The Elastic Beanstalk application name.",
"Type": "String",
"DefaultValue": "{ProjectName}",
"DefaultValue": "{StackName}",
"AdvancedSetting": false,
"Updatable": false,
"Validators": [
Expand All @@ -122,7 +122,7 @@
"Description": "The Elastic Beanstalk environment name.",
"Type": "String",
"TypeHint": "BeanstalkEnvironment",
"DefaultValue": "{ProjectName}-dev",
"DefaultValue": "{StackName}-dev",
"AdvancedSetting": false,
"Updatable": false,
"Validators": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
"Name": "New Cluster Name",
"Description": "The name of the new cluster to create.",
"Type": "String",
"DefaultValue": "{ProjectName}",
"DefaultValue": "{StackName}",
"AdvancedSetting": false,
"Updatable": false,
"Validators": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
"Name": "New Cluster Name",
"Description": "The name of the new cluster to create.",
"Type": "String",
"DefaultValue": "{ProjectName}",
"DefaultValue": "{StackName}",
"AdvancedSetting": false,
"Updatable": false,
"Validators": [
Expand Down Expand Up @@ -172,7 +172,7 @@
"Description": "The name of the ECS service running in the cluster.",
"Type": "String",
"TypeHint": "ECSService",
"DefaultValue": "{ProjectName}-service",
"DefaultValue": "{StackName}-service",
"AdvancedSetting": false,
"Updatable": false,
"Validators": [
Expand Down
11 changes: 8 additions & 3 deletions test/AWS.Deploy.CLI.UnitTests/RecommendationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public async Task ResetOptionSettingValue_String()
var recommendations = await engine.ComputeRecommendations();

var fargateRecommendation = recommendations.First(r => r.Recipe.Id == Constants.ASPNET_CORE_ASPNET_CORE_FARGATE_RECIPE_ID);
fargateRecommendation.AddReplacementToken("{StackName}", "MyAppStack");

var ecsServiceNameOptionSetting = fargateRecommendation.Recipe.OptionSettings.First(optionSetting => optionSetting.Id.Equals("ECSServiceName"));

var originalDefaultValue = fargateRecommendation.GetOptionSettingDefaultValue<string>(ecsServiceNameOptionSetting);
Expand Down Expand Up @@ -252,11 +254,14 @@ public async Task ApplyProjectNameToSettings()
var recommendations = await engine.ComputeRecommendations();

var beanstalkRecommendation = recommendations.FirstOrDefault(r => r.Recipe.Id == Constants.ASPNET_CORE_BEANSTALK_RECIPE_ID);

var beanstalEnvNameSetting = beanstalkRecommendation.Recipe.OptionSettings.FirstOrDefault(x => string.Equals("EnvironmentName", x.Id));
Assert.Equal("WebAppNoDockerFile-dev", beanstalkRecommendation.GetOptionSettingValue<string>(beanstalEnvNameSetting));

beanstalkRecommendation.OverrideProjectName("CustomName");
Assert.Equal("CustomName-dev", beanstalkRecommendation.GetOptionSettingValue<string>(beanstalEnvNameSetting));
beanstalkRecommendation.AddReplacementToken("{StackName}", "MyAppStack");
Assert.Equal("MyAppStack-dev", beanstalkRecommendation.GetOptionSettingValue<string>(beanstalEnvNameSetting));

beanstalkRecommendation.AddReplacementToken("{StackName}", "CustomAppStack");
Assert.Equal("CustomAppStack-dev", beanstalkRecommendation.GetOptionSettingValue<string>(beanstalEnvNameSetting));
}

[Theory]
Expand Down

0 comments on commit c5b0ef6

Please sign in to comment.