@@ -95,16 +95,14 @@ private void loadServiceEndpoints() {
95
95
// TODO: Do not populate config if "deprecated" is present.
96
96
if (config .containsMember ("hostname" ) || config .containsMember ("variants" )) {
97
97
String hostname = config .getStringMemberOrDefault ("hostname" , partition .hostnameTemplate );
98
+ String resolvedHostname = getResolvedHostname (hostname , dnsSuffix , endpointPrefix , entry .getKey ());
98
99
99
- hostname = hostname .replace ("{dnsSuffix}" , dnsSuffix );
100
- hostname = hostname .replace ("{service}" , endpointPrefix );
101
- hostname = hostname .replace ("{region}" , entry .getKey ());
102
100
// TODO: Remove hostname after fully switching to variants.
103
- config = config .withMember ("hostname" , hostname );
101
+ config = config .withMember ("hostname" , resolvedHostname );
104
102
105
103
ArrayNode variants = config .getArrayMember ("variants" ).orElse (
106
104
ArrayNode .fromNodes ());
107
- ArrayNode defaultVariant = ArrayNode .fromNodes (getDefaultVariant (hostname ));
105
+ ArrayNode defaultVariant = ArrayNode .fromNodes (getDefaultVariant (resolvedHostname ));
108
106
109
107
// Populate hostname as the default variant.
110
108
config = config .withMember ("variants" , variants .merge (defaultVariant ));
@@ -200,6 +198,24 @@ private ObjectNode getDefaultVariant(String hostname) {
200
198
return ObjectNode .fromStringMap (defaultVariant ).withMember ("tags" , defaultVariantTags );
201
199
}
202
200
201
+ private String getResolvedHostname (
202
+ String hostnameTemplate ,
203
+ String dnsSuffix ,
204
+ String service ) {
205
+ return getResolvedHostname (hostnameTemplate , dnsSuffix , service , "{region}" );
206
+ }
207
+
208
+ private String getResolvedHostname (
209
+ String hostnameTemplate ,
210
+ String dnsSuffix ,
211
+ String service ,
212
+ String region ) {
213
+ return hostnameTemplate
214
+ .replace ("{service}" , service )
215
+ .replace ("{region}" , region )
216
+ .replace ("{dnsSuffix}" , dnsSuffix );
217
+ }
218
+
203
219
private final class Partition {
204
220
final ObjectNode defaults ;
205
221
final String dnsSuffix ;
@@ -219,10 +235,8 @@ private Partition(ObjectNode config, String partition) {
219
235
regionRegex = config .expectStringMember ("regionRegex" ).getValue ();
220
236
221
237
// Resolve the template to use for this service in this partition.
222
- String template = defaults .expectStringMember ("hostname" ).getValue ();
223
- template = template .replace ("{service}" , endpointPrefix );
224
- template = template .replace ("{dnsSuffix}" , dnsSuffix );
225
- hostnameTemplate = template ;
238
+ String hostname = defaults .expectStringMember ("hostname" ).getValue ();
239
+ hostnameTemplate = getResolvedHostname (hostname , dnsSuffix , endpointPrefix );
226
240
}
227
241
228
242
ObjectNode getDefaults () {
@@ -255,13 +269,9 @@ ArrayNode getVariants() {
255
269
variants .forEach (variant -> {
256
270
ObjectNode variantNode = variant .expectObjectNode ();
257
271
String hostname = variantNode .expectStringMember ("hostname" ).getValue ();
258
- if (variantNode .containsMember ("dnsSuffix" )) {
259
- String dnsSuffix = variantNode .expectStringMember ("dnsSuffix" ).getValue ();
260
- hostname = hostname .replace ("{dnsSuffix}" , dnsSuffix );
261
- }
262
- hostname = hostname .replace ("{service}" , endpointPrefix );
263
- hostname = hostname .replace ("{dnsSuffix}" , dnsSuffix );
264
- allVariants .add (variantNode .withMember ("hostname" , hostname ).withoutMember ("dnsSuffix" ));
272
+ String dnsSuffix = variantNode .getStringMemberOrDefault ("dnsSuffix" , this .dnsSuffix );
273
+ String resolvedHostname = getResolvedHostname (hostname , dnsSuffix , endpointPrefix );
274
+ allVariants .add (variantNode .withMember ("hostname" , resolvedHostname ).withoutMember ("dnsSuffix" ));
265
275
});
266
276
}
267
277
allVariants .add (getDefaultVariant (hostnameTemplate ));
0 commit comments