Skip to content

Commit f68cf32

Browse files
committed
fix(codegen): add default aws regional endpoints for generated clients
1 parent c449a9b commit f68cf32

File tree

3 files changed

+429
-0
lines changed

3 files changed

+429
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,395 @@
1+
package software.amazon.smithy.aws.typescript.codegen;
2+
3+
import java.util.List;
4+
import software.amazon.smithy.aws.traits.ServiceTrait;
5+
import software.amazon.smithy.model.Model;
6+
import software.amazon.smithy.model.node.Node;
7+
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
8+
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
9+
import software.amazon.smithy.typescript.codegen.endpointsV2.AddDefaultEndpointRuleSet;
10+
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
11+
12+
13+
/**
14+
* This replaces behavior from {@link EndpointGenerator}.
15+
*/
16+
public class AddDefaultAwsEndpointRuleSet implements TypeScriptIntegration {
17+
/**
18+
* Running before the smithy-typescript default endpoint integration
19+
* will prevent it from applying its non-AWS default ruleset and
20+
* the endpointRequired plugin, both of which are not applicable
21+
* for default regional AWS endpoints.
22+
*/
23+
@Override
24+
public List<String> runBefore() {
25+
return List.of(AddDefaultEndpointRuleSet.class.getCanonicalName());
26+
}
27+
28+
@Override
29+
public Model preprocessModel(Model model, TypeScriptSettings settings) {
30+
Model.Builder modelBuilder = model.toBuilder();
31+
32+
model.getServiceShapes().forEach(serviceShape -> {
33+
if (!serviceShape.hasTrait(EndpointRuleSetTrait.class)
34+
&& AwsTraitsUtils.isAwsService(settings, model)) {
35+
// this branch is for models that identify as AWS services
36+
// but do not include an endpoint ruleset.
37+
38+
modelBuilder.removeShape(serviceShape.toShapeId());
39+
modelBuilder.addShape(serviceShape.toBuilder()
40+
.addTrait(getDefaultRegionalEndpointRuleSet(
41+
serviceShape.expectTrait(ServiceTrait.class).getEndpointPrefix())
42+
)
43+
.build());
44+
}
45+
});
46+
47+
return modelBuilder.build();
48+
}
49+
50+
private EndpointRuleSetTrait getDefaultRegionalEndpointRuleSet(String endpointPrefix) {
51+
return EndpointRuleSetTrait.builder()
52+
.ruleSet(Node.parse("""
53+
{
54+
"version": "1.0",
55+
"parameters": {
56+
"Region": {
57+
"builtIn": "AWS::Region",
58+
"required": false,
59+
"documentation": "The AWS Region. This is a default regional AWS endpointRuleSet.",
60+
"type": "String"
61+
},
62+
"UseDualStack": {
63+
"builtIn": "AWS::UseDualStack",
64+
"required": true,
65+
"default": false,
66+
"documentation": "When true, use the dual-stack endpoint. If the configured endpoint does not support dual-stack, dispatching the request MAY return an error.",
67+
"type": "Boolean"
68+
},
69+
"UseFIPS": {
70+
"builtIn": "AWS::UseFIPS",
71+
"required": true,
72+
"default": false,
73+
"documentation": "When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.",
74+
"type": "Boolean"
75+
},
76+
"Endpoint": {
77+
"builtIn": "SDK::Endpoint",
78+
"required": false,
79+
"documentation": "Override the endpoint used to send this request",
80+
"type": "String"
81+
}
82+
},
83+
"rules": [
84+
{
85+
"conditions": [
86+
{
87+
"fn": "isSet",
88+
"argv": [
89+
{
90+
"ref": "Endpoint"
91+
}
92+
]
93+
}
94+
],
95+
"rules": [
96+
{
97+
"conditions": [
98+
{
99+
"fn": "booleanEquals",
100+
"argv": [
101+
{
102+
"ref": "UseFIPS"
103+
},
104+
true
105+
]
106+
}
107+
],
108+
"error": "Invalid Configuration: FIPS and custom endpoint are not supported",
109+
"type": "error"
110+
},
111+
{
112+
"conditions": [
113+
{
114+
"fn": "booleanEquals",
115+
"argv": [
116+
{
117+
"ref": "UseDualStack"
118+
},
119+
true
120+
]
121+
}
122+
],
123+
"error": "Invalid Configuration: Dualstack and custom endpoint are not supported",
124+
"type": "error"
125+
},
126+
{
127+
"conditions": [],
128+
"endpoint": {
129+
"url": {
130+
"ref": "Endpoint"
131+
},
132+
"properties": {},
133+
"headers": {}
134+
},
135+
"type": "endpoint"
136+
}
137+
],
138+
"type": "tree"
139+
},
140+
{
141+
"conditions": [
142+
{
143+
"fn": "isSet",
144+
"argv": [
145+
{
146+
"ref": "Region"
147+
}
148+
]
149+
}
150+
],
151+
"rules": [
152+
{
153+
"conditions": [
154+
{
155+
"fn": "aws.partition",
156+
"argv": [
157+
{
158+
"ref": "Region"
159+
}
160+
],
161+
"assign": "PartitionResult"
162+
}
163+
],
164+
"rules": [
165+
{
166+
"conditions": [
167+
{
168+
"fn": "booleanEquals",
169+
"argv": [
170+
{
171+
"ref": "UseFIPS"
172+
},
173+
true
174+
]
175+
},
176+
{
177+
"fn": "booleanEquals",
178+
"argv": [
179+
{
180+
"ref": "UseDualStack"
181+
},
182+
true
183+
]
184+
}
185+
],
186+
"rules": [
187+
{
188+
"conditions": [
189+
{
190+
"fn": "booleanEquals",
191+
"argv": [
192+
true,
193+
{
194+
"fn": "getAttr",
195+
"argv": [
196+
{
197+
"ref": "PartitionResult"
198+
},
199+
"supportsFIPS"
200+
]
201+
}
202+
]
203+
},
204+
{
205+
"fn": "booleanEquals",
206+
"argv": [
207+
true,
208+
{
209+
"fn": "getAttr",
210+
"argv": [
211+
{
212+
"ref": "PartitionResult"
213+
},
214+
"supportsDualStack"
215+
]
216+
}
217+
]
218+
}
219+
],
220+
"rules": [
221+
{
222+
"conditions": [],
223+
"endpoint": {
224+
"url": "https://%1$s-fips.{Region}.{PartitionResult#dualStackDnsSuffix}",
225+
"properties": {},
226+
"headers": {}
227+
},
228+
"type": "endpoint"
229+
}
230+
],
231+
"type": "tree"
232+
},
233+
{
234+
"conditions": [],
235+
"error": "FIPS and DualStack are enabled, but this partition does not support one or both",
236+
"type": "error"
237+
}
238+
],
239+
"type": "tree"
240+
},
241+
{
242+
"conditions": [
243+
{
244+
"fn": "booleanEquals",
245+
"argv": [
246+
{
247+
"ref": "UseFIPS"
248+
},
249+
true
250+
]
251+
}
252+
],
253+
"rules": [
254+
{
255+
"conditions": [
256+
{
257+
"fn": "booleanEquals",
258+
"argv": [
259+
{
260+
"fn": "getAttr",
261+
"argv": [
262+
{
263+
"ref": "PartitionResult"
264+
},
265+
"supportsFIPS"
266+
]
267+
},
268+
true
269+
]
270+
}
271+
],
272+
"rules": [
273+
{
274+
"conditions": [
275+
{
276+
"fn": "stringEquals",
277+
"argv": [
278+
{
279+
"fn": "getAttr",
280+
"argv": [
281+
{
282+
"ref": "PartitionResult"
283+
},
284+
"name"
285+
]
286+
},
287+
"aws-us-gov"
288+
]
289+
}
290+
],
291+
"endpoint": {
292+
"url": "https://%1$s.{Region}.amazonaws.com",
293+
"properties": {},
294+
"headers": {}
295+
},
296+
"type": "endpoint"
297+
},
298+
{
299+
"conditions": [],
300+
"endpoint": {
301+
"url": "https://%1$s-fips.{Region}.{PartitionResult#dnsSuffix}",
302+
"properties": {},
303+
"headers": {}
304+
},
305+
"type": "endpoint"
306+
}
307+
],
308+
"type": "tree"
309+
},
310+
{
311+
"conditions": [],
312+
"error": "FIPS is enabled but this partition does not support FIPS",
313+
"type": "error"
314+
}
315+
],
316+
"type": "tree"
317+
},
318+
{
319+
"conditions": [
320+
{
321+
"fn": "booleanEquals",
322+
"argv": [
323+
{
324+
"ref": "UseDualStack"
325+
},
326+
true
327+
]
328+
}
329+
],
330+
"rules": [
331+
{
332+
"conditions": [
333+
{
334+
"fn": "booleanEquals",
335+
"argv": [
336+
true,
337+
{
338+
"fn": "getAttr",
339+
"argv": [
340+
{
341+
"ref": "PartitionResult"
342+
},
343+
"supportsDualStack"
344+
]
345+
}
346+
]
347+
}
348+
],
349+
"rules": [
350+
{
351+
"conditions": [],
352+
"endpoint": {
353+
"url": "https://%1$s.{Region}.{PartitionResult#dualStackDnsSuffix}",
354+
"properties": {},
355+
"headers": {}
356+
},
357+
"type": "endpoint"
358+
}
359+
],
360+
"type": "tree"
361+
},
362+
{
363+
"conditions": [],
364+
"error": "DualStack is enabled but this partition does not support DualStack",
365+
"type": "error"
366+
}
367+
],
368+
"type": "tree"
369+
},
370+
{
371+
"conditions": [],
372+
"endpoint": {
373+
"url": "https://%1$s.{Region}.{PartitionResult#dnsSuffix}",
374+
"properties": {},
375+
"headers": {}
376+
},
377+
"type": "endpoint"
378+
}
379+
],
380+
"type": "tree"
381+
}
382+
],
383+
"type": "tree"
384+
},
385+
{
386+
"conditions": [],
387+
"error": "Invalid Configuration: Missing Region",
388+
"type": "error"
389+
}
390+
]
391+
}
392+
""".formatted(endpointPrefix)))
393+
.build();
394+
}
395+
}

0 commit comments

Comments
 (0)