in src/AWS.Deploy.CLI/ServerMode/Controllers/DeploymentController.cs [393:469]
public async Task<IActionResult> SetDeploymentTarget(string sessionId, [FromBody] SetDeploymentTargetInput input)
{
var state = _stateServer.Get(sessionId);
if(state == null)
{
return NotFound($"Session ID {sessionId} not found.");
}
var serviceProvider = CreateSessionServiceProvider(state);
var orchestrator = CreateOrchestrator(state, serviceProvider);
var cloudApplicationNameGenerator = serviceProvider.GetRequiredService<ICloudApplicationNameGenerator>();
if (!string.IsNullOrEmpty(input.NewDeploymentRecipeId))
{
var newDeploymentName = input.NewDeploymentName ?? string.Empty;
state.SelectedRecommendation = state.NewRecommendations?.FirstOrDefault(x => string.Equals(input.NewDeploymentRecipeId, x.Recipe.Id));
if (state.SelectedRecommendation == null)
{
return NotFound($"Recommendation {input.NewDeploymentRecipeId} not found.");
}
// We only validate the name when the recipe deployment type is not ElasticContainerRegistryImage.
// This is because pushing images to ECR does not need a cloud application name.
if (state.SelectedRecommendation.Recipe.DeploymentType != Common.Recipes.DeploymentTypes.ElasticContainerRegistryImage)
{
var validationResult = cloudApplicationNameGenerator.IsValidName(newDeploymentName, state.ExistingDeployments ?? new List<CloudApplication>(), state.SelectedRecommendation.Recipe.DeploymentType);
if (!validationResult.IsValid)
return ValidationProblem(validationResult.ErrorMessage);
}
state.ApplicationDetails.Name = newDeploymentName;
state.ApplicationDetails.UniqueIdentifier = string.Empty;
state.ApplicationDetails.ResourceType = orchestrator.GetCloudApplicationResourceType(state.SelectedRecommendation.Recipe.DeploymentType);
state.ApplicationDetails.RecipeId = input.NewDeploymentRecipeId;
await orchestrator.ApplyAllReplacementTokens(state.SelectedRecommendation, newDeploymentName);
}
else if(!string.IsNullOrEmpty(input.ExistingDeploymentId))
{
var templateMetadataReader = serviceProvider.GetRequiredService<ICloudFormationTemplateReader>();
var deployedApplicationQueryer = serviceProvider.GetRequiredService<IDeployedApplicationQueryer>();
var optionSettingHandler = serviceProvider.GetRequiredService<IOptionSettingHandler>();
var existingDeployment = state.ExistingDeployments?.FirstOrDefault(x => string.Equals(input.ExistingDeploymentId, x.UniqueIdentifier));
if (existingDeployment == null)
{
return NotFound($"Existing deployment {input.ExistingDeploymentId} not found.");
}
state.SelectedRecommendation = state.NewRecommendations?.FirstOrDefault(x => string.Equals(existingDeployment.RecipeId, x.Recipe.Id));
if (state.SelectedRecommendation == null)
{
return NotFound($"Recommendation {input.NewDeploymentRecipeId} used in existing deployment {existingDeployment.RecipeId} not found.");
}
IDictionary<string, object> previousSettings;
if (existingDeployment.ResourceType == CloudApplicationResourceType.CloudFormationStack)
{
var metadata = await templateMetadataReader.LoadCloudApplicationMetadata(existingDeployment.Name);
previousSettings = metadata.Settings.Union(metadata.DeploymentBundleSettings).ToDictionary(x => x.Key, x => x.Value);
}
else
{
previousSettings = await deployedApplicationQueryer.GetPreviousSettings(existingDeployment, state.SelectedRecommendation);
}
state.SelectedRecommendation = await orchestrator.ApplyRecommendationPreviousSettings(state.SelectedRecommendation, previousSettings);
state.ApplicationDetails.Name = existingDeployment.Name;
state.ApplicationDetails.UniqueIdentifier = existingDeployment.UniqueIdentifier;
state.ApplicationDetails.RecipeId = existingDeployment.RecipeId;
state.ApplicationDetails.ResourceType = existingDeployment.ResourceType;
await orchestrator.ApplyAllReplacementTokens(state.SelectedRecommendation, existingDeployment.Name);
}
return Ok();
}