Skip to content

Commit

Permalink
fix(codegen): populate dnsSuffix just before consuming hostname (#3170)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Jan 10, 2022
1 parent ac246b5 commit 0f0e77e
Showing 1 changed file with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ private void loadServiceEndpoints() {
&& (config.containsMember("hostname") || config.containsMember("variants"))) {
String region = entry.getKey();
String hostname = config.getStringMemberOrDefault("hostname", partition.hostnameTemplate);
String resolvedHostname = getResolvedHostname(hostname, dnsSuffix, endpointPrefix, region);
String resolvedHostname = getResolvedHostnameWithDnsSuffix(
getResolvedHostname(hostname, endpointPrefix, region),
dnsSuffix
);

ArrayNode variants = config.getArrayMember("variants").orElse(ArrayNode.fromNodes());
ArrayNode defaultVariant = ArrayNode.fromNodes(getDefaultVariant(resolvedHostname));
Expand Down Expand Up @@ -187,15 +190,18 @@ private ObjectNode getDefaultVariant(String hostname) {
.withMember("tags", ArrayNode.fromStrings(Collections.emptyList()));
}

private String getResolvedHostname(String hostnameTemplate, String dnsSuffix, String service) {
return getResolvedHostname(hostnameTemplate, dnsSuffix, service, "{region}");
private String getResolvedHostname(String hostnameTemplate, String service) {
return getResolvedHostname(hostnameTemplate, service, "{region}");
}

private String getResolvedHostname(String hostnameTemplate, String dnsSuffix, String service, String region) {
private String getResolvedHostname(String hostnameTemplate, String service, String region) {
return hostnameTemplate
.replace("{service}", service)
.replace("{region}", region)
.replace("{dnsSuffix}", dnsSuffix);
.replace("{region}", region);
}

private String getResolvedHostnameWithDnsSuffix(String hostnameTemplate, String dnsSuffix) {
return hostnameTemplate.replace("{dnsSuffix}", dnsSuffix);
}

private final class Partition {
Expand All @@ -219,7 +225,7 @@ private Partition(ObjectNode config, String partition) {

// Resolve the template to use for this service in this partition.
String hostname = defaults.expectStringMember("hostname").getValue();
hostnameTemplate = getResolvedHostname(hostname, dnsSuffix, endpointPrefix);
hostnameTemplate = getResolvedHostname(hostname, endpointPrefix);

ArrayNode mergedVariants = getMergedVariants(
partitionDefaults.getArrayMember("variants").orElse(Node.arrayNode()),
Expand Down Expand Up @@ -249,12 +255,15 @@ Set<String> getAllRegions() {
private ArrayNode getVariants(ArrayNode mergedVariants) {
List<Node> allVariants = new ArrayList<Node>();

allVariants.add(getDefaultVariant(hostnameTemplate));
allVariants.add(getDefaultVariant(getResolvedHostnameWithDnsSuffix(hostnameTemplate, this.dnsSuffix)));
mergedVariants.forEach(mergedVariant -> {
ObjectNode variantNode = mergedVariant.expectObjectNode();
String hostname = variantNode.expectStringMember("hostname").getValue();
String dnsSuffix = variantNode.getStringMemberOrDefault("dnsSuffix", this.dnsSuffix);
String resolvedHostname = getResolvedHostname(hostname, dnsSuffix, endpointPrefix);
String resolvedHostname = getResolvedHostnameWithDnsSuffix(
getResolvedHostname(hostname, endpointPrefix),
dnsSuffix
);
allVariants.add(variantNode.withMember("hostname", resolvedHostname).withoutMember("dnsSuffix"));
});

Expand Down

0 comments on commit 0f0e77e

Please sign in to comment.