15
15
16
16
package software .amazon .smithy .aws .typescript .codegen ;
17
17
18
+ import java .util .ArrayList ;
18
19
import java .util .List ;
19
20
import java .util .Map ;
20
21
import java .util .Optional ;
24
25
import software .amazon .smithy .aws .traits .ServiceTrait ;
25
26
import software .amazon .smithy .aws .traits .auth .SigV4Trait ;
26
27
import software .amazon .smithy .codegen .core .CodegenException ;
28
+ import software .amazon .smithy .model .node .ArrayNode ;
27
29
import software .amazon .smithy .model .node .Node ;
28
30
import software .amazon .smithy .model .node .ObjectNode ;
29
31
import software .amazon .smithy .model .node .StringNode ;
@@ -90,15 +92,16 @@ private void loadServiceEndpoints() {
90
92
91
93
for (Map .Entry <String , Node > entry : endpointMap .getStringMap ().entrySet ()) {
92
94
ObjectNode config = entry .getValue ().expectObjectNode ();
95
+ // TODO: Do not populate config if "deprecated" is present.
93
96
if (config .containsMember ("hostname" )) {
94
97
// Resolve the hostname.
95
98
String hostName = config .expectStringMember ("hostname" ).getValue ();
96
99
hostName = hostName .replace ("{dnsSuffix}" , dnsSuffix );
97
100
hostName = hostName .replace ("{service}" , endpointPrefix );
98
101
hostName = hostName .replace ("{region}" , entry .getKey ());
99
102
config = config .withMember ("hostname" , hostName );
100
- endpoints .put (entry .getKey (), config );
101
103
}
104
+ endpoints .put (entry .getKey (), config );
102
105
}
103
106
}
104
107
}
@@ -134,6 +137,14 @@ private void writePartitionHash() {
134
137
OptionalUtils .ifPresentOrElse (partition .getPartitionEndpoint (),
135
138
endpoint -> writer .write ("endpoint: $S," , endpoint ),
136
139
() -> writer .write ("hostname: $S," , partition .hostnameTemplate ));
140
+ List <Node > variants = partition .getVariants ();
141
+ if (!variants .isEmpty ()) {
142
+ writer .openBlock ("variants: [" , "]," , () -> {
143
+ variants .forEach (variant -> {
144
+ writer .write ("$L, " , Node .prettyPrintJson (variant ));
145
+ });
146
+ });
147
+ }
137
148
});
138
149
});
139
150
});
@@ -159,10 +170,18 @@ private void writeEndpointProviderFunction() {
159
170
}
160
171
161
172
private void writeEndpointSpecificResolver (String region , ObjectNode resolved ) {
162
- if (resolved .containsMember ("hostname" ) || resolved .containsMember ("credentialScope" )) {
173
+ if (resolved .containsMember ("variants" )
174
+ || resolved .containsMember ("hostname" )
175
+ || resolved .containsMember ("credentialScope" )) {
163
176
writer .openBlock ("$S: {" , "}," , region , () -> {
164
- String hostname = resolved .expectStringMember ("hostname" ).getValue ();
165
- writer .write ("hostname: $S," , hostname );
177
+ if (resolved .containsMember ("hostname" )) {
178
+ String hostname = resolved .expectStringMember ("hostname" ).getValue ();
179
+ writer .write ("hostname: $S," , hostname );
180
+ }
181
+ if (resolved .containsMember ("variants" )) {
182
+ ArrayNode variants = resolved .expectArrayMember ("variants" );
183
+ writer .write ("variants: $L," , ArrayNode .prettyPrintJson (variants ));
184
+ }
166
185
resolved .getObjectMember ("credentialScope" ).ifPresent (scope -> {
167
186
scope .getStringMember ("region" ).ifPresent (signingRegion -> {
168
187
writer .write ("signingRegion: $S," , signingRegion );
@@ -177,10 +196,10 @@ private void writeEndpointSpecificResolver(String region, ObjectNode resolved) {
177
196
178
197
private final class Partition {
179
198
final ObjectNode defaults ;
180
- final String hostnameTemplate ;
181
199
final String dnsSuffix ;
182
200
final String identifier ;
183
201
final String regionRegex ;
202
+ final String hostnameTemplate ;
184
203
private final ObjectNode config ;
185
204
186
205
private Partition (ObjectNode config , String partition ) {
@@ -189,15 +208,15 @@ private Partition(ObjectNode config, String partition) {
189
208
ObjectNode partitionDefaults = config .expectObjectMember ("defaults" );
190
209
defaults = partitionDefaults .merge (getService ().getObjectMember ("defaults" ).orElse (Node .objectNode ()));
191
210
211
+ dnsSuffix = config .expectStringMember ("dnsSuffix" ).getValue ();
212
+ identifier = partition ;
213
+ regionRegex = config .expectStringMember ("regionRegex" ).getValue ();
214
+
192
215
// Resolve the template to use for this service in this partition.
193
216
String template = defaults .expectStringMember ("hostname" ).getValue ();
194
217
template = template .replace ("{service}" , endpointPrefix );
195
- template = template .replace ("{dnsSuffix}" , config . expectStringMember ( " dnsSuffix" ). getValue () );
218
+ template = template .replace ("{dnsSuffix}" , dnsSuffix );
196
219
hostnameTemplate = template ;
197
-
198
- dnsSuffix = config .expectStringMember ("dnsSuffix" ).getValue ();
199
- identifier = partition ;
200
- regionRegex = config .expectStringMember ("regionRegex" ).getValue ();
201
220
}
202
221
203
222
ObjectNode getDefaults () {
@@ -222,6 +241,27 @@ Set<String> getAllRegions() {
222
241
return regions ;
223
242
}
224
243
244
+ List <Node > getVariants () {
245
+ List <Node > allVariants = new ArrayList <Node >();
246
+
247
+ if (defaults .containsMember ("variants" )) {
248
+ ArrayNode variants = defaults .expectArrayMember ("variants" );
249
+ variants .forEach (variant -> {
250
+ ObjectNode variantNode = variant .expectObjectNode ();
251
+ String hostname = variantNode .expectStringMember ("hostname" ).getValue ();
252
+ if (variantNode .containsMember ("dnsSuffix" )) {
253
+ String dnsSuffix = variantNode .expectStringMember ("dnsSuffix" ).getValue ();
254
+ hostname = hostname .replace ("{dnsSuffix}" , dnsSuffix );
255
+ }
256
+ hostname = hostname .replace ("{service}" , endpointPrefix );
257
+ hostname = hostname .replace ("{dnsSuffix}" , dnsSuffix );
258
+ allVariants .add (variantNode .withMember ("hostname" , hostname ).withoutMember ("dnsSuffix" ));
259
+ });
260
+ }
261
+
262
+ return allVariants ;
263
+ }
264
+
225
265
Optional <String > getPartitionEndpoint () {
226
266
ObjectNode service = getService ();
227
267
// Note: regionalized services always use regionalized endpoints.
0 commit comments