Skip to content

Commit

Permalink
use default value, if present, when encountering a missing loigcal id
Browse files Browse the repository at this point in the history
  • Loading branch information
cartwrightian committed Nov 22, 2023
1 parent 119938a commit bb10580
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
62 changes: 38 additions & 24 deletions src/tw/com/parameters/AutoDiscoverParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import java.util.Map;
import java.util.stream.Collectors;

import static java.lang.String.format;

public class AutoDiscoverParams extends PopulatesParameters {
private static final Logger logger = LoggerFactory.getLogger(AutoDiscoverParams.class);

private File templateFile;
private VpcRepository vpcRepository;
private CloudFormRepository cfnRepository;
private final File templateFile;
private final VpcRepository vpcRepository;
private final CloudFormRepository cfnRepository;

public AutoDiscoverParams(File file,VpcRepository vpcRepository,CloudFormRepository cfnRepository) {
this.vpcRepository = vpcRepository;
Expand All @@ -39,11 +41,12 @@ public void addParameters(Collection<Parameter> result,
throws CannotFindVpcException, IOException, InvalidStackParameterException {
Map<String, AvailabilityZone> zones = providesZones.getZones();
List<Parameter> autoPopulatedParametes = fetchAutopopulateParametersFor(projAndEnv, declaredParameters, zones);
result.addAll(autoPopulatedParametes.stream().collect(Collectors.toList()));
result.addAll(autoPopulatedParametes.stream().toList());
}

private List<Parameter> fetchAutopopulateParametersFor(ProjectAndEnv projectAndEnv, List<TemplateParameter> declaredParameters, Map<String, AvailabilityZone> zones) throws IOException, InvalidStackParameterException, CannotFindVpcException {
logger.info(String.format("Discover and populate parameters for %s and %s", templateFile.getAbsolutePath(), projectAndEnv));
private List<Parameter> fetchAutopopulateParametersFor(ProjectAndEnv projectAndEnv, List<TemplateParameter> declaredParameters,
Map<String, AvailabilityZone> zones) throws IOException, InvalidStackParameterException, CannotFindVpcException {
logger.info(format("Discover and populate parameters for %s and %s", templateFile.getAbsolutePath(), projectAndEnv));
List<Parameter> matches = new LinkedList<>();
for(TemplateParameter templateParam : declaredParameters) {
String name = templateParam.parameterKey();
Expand All @@ -54,7 +57,7 @@ private List<Parameter> fetchAutopopulateParametersFor(ProjectAndEnv projectAndE
logger.info("Checking if parameter should be auto-populated from an existing resource, param name is " + name);
String description = templateParam.description();
if (shouldPopulateFor(description)) {
populateParameter(projectAndEnv, matches, name, description, declaredParameters, zones);
populateParameter(projectAndEnv, matches, templateParam, declaredParameters, zones);
}
}
return matches;
Expand All @@ -75,66 +78,77 @@ private boolean shouldPopulateFor(String description) {
return description.startsWith(PopulatesParameters.PARAM_PREFIX) && (!description.equals(PopulatesParameters.ENV_TAG));
}

private void populateParameter(ProjectAndEnv projectAndEnv, List<Parameter> results, String parameterName,
String parameterDescription, List<TemplateParameter> declaredParameters, Map<String, AvailabilityZone> zones)
private void populateParameter(ProjectAndEnv projectAndEnv, List<Parameter> results, TemplateParameter templateParameter,
List<TemplateParameter> declaredParameters, Map<String, AvailabilityZone> zones)
throws InvalidStackParameterException, CannotFindVpcException {
String parameterDescription = templateParameter.description();

if (parameterDescription.equals(PopulatesParameters.CFN_TAG_ON_OUTPUT)) {
populateParameterFromVPCTag(projectAndEnv, results, parameterName, declaredParameters);
populateParameterFromVPCTag(projectAndEnv, results, templateParameter, declaredParameters);
} else if (parameterDescription.startsWith(PopulatesParameters.CFN_TAG_ZONE)) {
populateParamForZone(results, declaredParameters, zones, parameterName, parameterDescription);
populateParamForZone(results, declaredParameters, zones, templateParameter, parameterDescription);
}
else {
populateParameterFromPhysicalID(projectAndEnv.getEnvTag(), results, parameterName,
parameterDescription, declaredParameters);
populateParameterFromPhysicalID(projectAndEnv.getEnvTag(), results, templateParameter, declaredParameters);
}
}

private void populateParamForZone(Collection<Parameter> results,
List<TemplateParameter> declaredParameters,
Map<String, AvailabilityZone> zones, String parameterName,
Map<String, AvailabilityZone> zones, TemplateParameter templateParameter,
String parameterDescription) {
logger.info(String.format("Check parameter for zone %s and target %s", parameterName, parameterDescription));
String parameterName = templateParameter.parameterKey();

logger.info(format("Check parameter for zone %s and target %s", parameterName, parameterDescription));
String target = parameterDescription.replaceFirst(PopulatesParameters.CFN_TAG_ZONE, "").toLowerCase();
logger.debug("Check for zone " + target);
if (zones.containsKey(target)) {
String zoneName = zones.get(target).zoneName();
declaredParameters.stream().filter(declaredParameter -> declaredParameter.parameterKey().equals(parameterName)).
forEach(declaredParameter -> {
addParameterTo(results, declaredParameters, parameterName, zoneName);
logger.info(String.format("Adding zone parameter %s with value %s", parameterName, zoneName));
logger.info(format("Adding zone parameter %s with value %s", parameterName, zoneName));
});
} else {
logger.error("Could not find matching zone for target " + target);
}
}

private void populateParameterFromVPCTag(ProjectAndEnv projectAndEnv,
List<Parameter> results, String parameterName,
List<Parameter> results, TemplateParameter templateParameter,
List<TemplateParameter> declaredParameters) throws CannotFindVpcException, InvalidStackParameterException {
String parameterName = templateParameter.parameterKey();

logger.info("Attempt to find VPC matching name: " + parameterName);
String value = vpcRepository.getVpcTag(parameterName, projectAndEnv);
if (value==null) {
String msg = String.format("Failed to find VPC TAG matching: %s", parameterName);
String msg = format("Failed to find VPC TAG matching: %s", parameterName);
logger.error(msg);
throw new InvalidStackParameterException(msg);
}
addParameterTo(results, declaredParameters, parameterName, value);
}

private void populateParameterFromPhysicalID(EnvironmentTag envTag,
List<Parameter> matches, String parameterName,
String parameterDescription,
List<Parameter> matches, TemplateParameter templateParameter,
List<TemplateParameter> declaredParameters)
throws InvalidStackParameterException {
String parameterName = templateParameter.parameterKey();
String parameterDescription = templateParameter.description();
String logicalId = parameterDescription.substring(PopulatesParameters.PARAM_PREFIX.length());
logger.info("Attempt to find physical ID for LogicalID: " + logicalId);
String value = cfnRepository.findPhysicalIdByLogicalId(envTag, logicalId);
if (value==null) {
String msg = String.format("Failed to find physicalID to match logicalID: %s required for parameter: %s" , logicalId, parameterName);
logger.error(msg);
throw new InvalidStackParameterException(msg);
if (templateParameter.defaultValue()==null) {
String msg = format("No default given, and Failed to find physicalID to match logicalID: %s required for parameter: %s", logicalId, parameterName);
logger.error(msg);
throw new InvalidStackParameterException(msg);
}
value = templateParameter.defaultValue();
logger.info(format("Using default value %s for %s as did not find %s", value, parameterName, logicalId));
} else {
logger.info(format("Found physicalID: %s matching logicalID: %s Populating this into parameter %s", value, logicalId, parameterName));
}
logger.info(String.format("Found physicalID: %s matching logicalID: %s Populating this into parameter %s", value, logicalId, parameterName));
addParameterTo(matches, declaredParameters, parameterName, value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/tw/com/parameters/PopulatesParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected void addParameterTo(Collection<Parameter> parameters, List<TemplatePar
boolean isDeclared = false;
for(TemplateParameter declaration : declared) {
isDeclared = (declaration.parameterKey().equals(parameterName));
if (isDeclared==true) break;
if (isDeclared) break;
}
if (!isDeclared) {
logger.info(String.format("Not populating parameter %s as it is not declared in the json file", parameterName));
Expand Down

0 comments on commit bb10580

Please sign in to comment.