Skip to content

Commit ad6dded

Browse files
authored
Allow code generating services without an endpoints rules file. (#3590)
1 parent 789d549 commit ad6dded

File tree

9 files changed

+190
-7
lines changed

9 files changed

+190
-7
lines changed

codegen-maven-plugin/src/main/java/software/amazon/awssdk/codegen/maven/plugin/GenerationMojo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ private Paginators loadPaginatorModel(Path root) {
145145
}
146146

147147
private EndpointRuleSetModel loadEndpointRuleSetModel(Path root) {
148-
return loadRequiredModel(EndpointRuleSetModel.class, root.resolve(ENDPOINT_RULE_SET_FILE));
148+
return loadOptionalModel(EndpointRuleSetModel.class, root.resolve(ENDPOINT_RULE_SET_FILE)).orElse(null);
149149
}
150150

151151
private EndpointTestSuiteModel loadEndpointTestSuiteModel(Path root) {
152-
return loadRequiredModel(EndpointTestSuiteModel.class, root.resolve(ENDPOINT_TESTS_FILE));
152+
return loadOptionalModel(EndpointTestSuiteModel.class, root.resolve(ENDPOINT_TESTS_FILE)).orElse(null);
153153
}
154154

155155
/**

codegen/src/main/java/software/amazon/awssdk/codegen/emitters/tasks/EndpointProviderTasks.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected List<GeneratorTask> createTasks() throws Exception {
5050
tasks.add(generateParams());
5151
tasks.add(generateDefaultProvider());
5252
tasks.addAll(generateInterceptors());
53-
if (shouldGenerateTests()) {
53+
if (shouldGenerateEndpointTests()) {
5454
tasks.add(generateClientTests());
5555
tasks.add(generateProviderTests());
5656
}
@@ -105,10 +105,10 @@ private String endpointTestsDir() {
105105
return generatorTaskParams.getPathProvider().getEndpointRulesTestDirectory();
106106
}
107107

108-
private boolean shouldGenerateTests() {
108+
private boolean shouldGenerateEndpointTests() {
109109
CustomizationConfig customizationConfig = generatorTaskParams.getModel().getCustomizationConfig();
110-
111-
return !Boolean.TRUE.equals(customizationConfig.isSkipEndpointTestGeneration());
110+
return !Boolean.TRUE.equals(customizationConfig.isSkipEndpointTestGeneration()) &&
111+
!generatorTaskParams.getModel().getEndpointTestSuiteModel().getTestCases().isEmpty();
112112
}
113113

114114
private boolean hasClientContextParams() {

codegen/src/main/java/software/amazon/awssdk/codegen/internal/Jackson.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public static <T> T load(Class<T> clazz, File file) throws IOException {
5858
return MAPPER.beanFrom(clazz, file);
5959
}
6060

61+
public static <T> T load(Class<T> clazz, String content) throws IOException {
62+
return MAPPER.beanFrom(clazz, content);
63+
}
64+
6165
public static <T> T load(Class<T> clazz, File file, boolean failOnUnknownProperties) throws IOException {
6266
if (failOnUnknownProperties) {
6367
return FAIL_ON_UNKNOWN_PROPERTIES_MAPPER.beanFrom(clazz, file);

codegen/src/main/java/software/amazon/awssdk/codegen/model/intermediate/IntermediateModel.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,16 @@ public Map<String, WaiterDefinition> getWaiters() {
172172
}
173173

174174
public EndpointRuleSetModel getEndpointRuleSetModel() {
175+
if (endpointRuleSetModel == null) {
176+
endpointRuleSetModel = EndpointRuleSetModel.defaultRules(metadata.getEndpointPrefix());
177+
}
175178
return endpointRuleSetModel;
176179
}
177180

178181
public EndpointTestSuiteModel getEndpointTestSuiteModel() {
182+
if (endpointTestSuiteModel == null) {
183+
endpointTestSuiteModel = new EndpointTestSuiteModel();
184+
}
179185
return endpointTestSuiteModel;
180186
}
181187

codegen/src/main/java/software/amazon/awssdk/codegen/model/rules/endpoints/EndpointTestSuiteModel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515

1616
package software.amazon.awssdk.codegen.model.rules.endpoints;
1717

18+
import java.util.ArrayList;
1819
import java.util.List;
1920

2021
public class EndpointTestSuiteModel {
2122

22-
private List<EndpointTestModel> testCases;
23+
private List<EndpointTestModel> testCases = new ArrayList<>();
2324

2425
public List<EndpointTestModel> getTestCases() {
2526
return testCases;

codegen/src/main/java/software/amazon/awssdk/codegen/model/service/EndpointRuleSetModel.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,33 @@
1515

1616
package software.amazon.awssdk.codegen.model.service;
1717

18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.io.UncheckedIOException;
1821
import java.util.List;
1922
import java.util.Map;
23+
import software.amazon.awssdk.codegen.internal.Jackson;
2024
import software.amazon.awssdk.codegen.model.rules.endpoints.ParameterModel;
2125
import software.amazon.awssdk.codegen.model.rules.endpoints.RuleModel;
26+
import software.amazon.awssdk.utils.IoUtils;
2227

2328
public class EndpointRuleSetModel {
2429
private String serviceId;
2530
private String version;
2631
private Map<String, ParameterModel> parameters;
2732
private List<RuleModel> rules;
2833

34+
public static EndpointRuleSetModel defaultRules(String endpointPrefix) {
35+
try (InputStream defaultRulesSet = EndpointRuleSetModel.class
36+
.getResourceAsStream("/software/amazon/awssdk/codegen/default-endpoint-rule-set.json")) {
37+
String rules = IoUtils.toUtf8String(defaultRulesSet);
38+
rules = String.format(rules, endpointPrefix);
39+
return Jackson.load(EndpointRuleSetModel.class, rules);
40+
} catch (IOException e) {
41+
throw new UncheckedIOException(e);
42+
}
43+
}
44+
2945
public String getServiceId() {
3046
return serviceId;
3147
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"version": "1.0",
3+
"parameters": {
4+
"Region": {
5+
"builtIn": "AWS::Region",
6+
"required": true,
7+
"documentation": "The AWS region used to dispatch the request, if the endpoint is not specified.",
8+
"type": "String"
9+
},
10+
"Endpoint": {
11+
"builtIn": "SDK::Endpoint",
12+
"required": false,
13+
"documentation": "Override the endpoint used to send this request",
14+
"type": "String"
15+
}
16+
},
17+
"rules": [
18+
{
19+
"conditions": [
20+
{
21+
"fn": "isSet",
22+
"argv": [
23+
{
24+
"ref": "Endpoint"
25+
}
26+
]
27+
}
28+
],
29+
"type": "endpoint",
30+
"endpoint": {
31+
"url": {
32+
"ref": "Endpoint"
33+
},
34+
"properties": {},
35+
"headers": {}
36+
}
37+
},
38+
{
39+
"conditions": [
40+
{
41+
"fn": "aws.partition",
42+
"argv": [
43+
{
44+
"ref": "Region"
45+
}
46+
],
47+
"assign": "PartitionResult"
48+
}
49+
],
50+
"type": "endpoint",
51+
"endpoint": {
52+
"url": "https://%s.{Region}.{PartitionResult#dnsSuffix}",
53+
"properties": {},
54+
"headers": {}
55+
}
56+
}
57+
]
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"version":"2.0",
3+
"metadata":{
4+
"apiVersion":"2016-03-11",
5+
"endpointPrefix":"default-endpoint-provider",
6+
"jsonVersion":"1.1",
7+
"protocol":"rest-json",
8+
"serviceAbbreviation":"AmazonDefaultEndpointProvider",
9+
"serviceFullName":"Amazon Default Endpoint Provider",
10+
"serviceId":"Default Endpoint Provider",
11+
"signatureVersion":"v4",
12+
"targetPrefix":"default-endpoint-provider-prefix",
13+
"timestampFormat":"unixTimestamp",
14+
"uid":"restjson-2016-03-11"
15+
},
16+
"operations":{
17+
"OneOperation":{
18+
"name":"OneOperation",
19+
"http":{
20+
"method":"POST",
21+
"requestUri":"/2016-03-11/oneoperation"
22+
},
23+
"input":{"shape":"OneShape"}
24+
}
25+
},
26+
"shapes": {
27+
"OneShape": {
28+
"type": "structure",
29+
"members": {
30+
"StringMember": {
31+
"shape": "String"
32+
}
33+
}
34+
},
35+
"String":{"type":"string"}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.services.defaultendpointprovider;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.junit.jupiter.api.Test;
21+
import software.amazon.awssdk.regions.Region;
22+
import software.amazon.awssdk.services.defaultendpointprovider.endpoints.DefaultEndpointProviderEndpointProvider;
23+
24+
public class DefaultEndpointProviderTest {
25+
private static final DefaultEndpointProviderEndpointProvider DEFAULT_ENDPOINT_PROVIDER =
26+
DefaultEndpointProviderEndpointProvider.defaultProvider();
27+
28+
@Test
29+
public void unknownRegion_resolvesToAwsPartition() {
30+
assertThat(resolveEndpoint("unknown-region", null))
31+
.isEqualTo("https://default-endpoint-provider.unknown-region.amazonaws.com");
32+
}
33+
34+
@Test
35+
public void awsRegion_resolvesToAwsPartition() {
36+
assertThat(resolveEndpoint("us-west-2", null))
37+
.isEqualTo("https://default-endpoint-provider.us-west-2.amazonaws.com");
38+
}
39+
40+
@Test
41+
public void cnRegion_resolvesToCnPartition() {
42+
assertThat(resolveEndpoint("cn-north-1", null))
43+
.isEqualTo("https://default-endpoint-provider.cn-north-1.amazonaws.com.cn");
44+
}
45+
46+
@Test
47+
public void endpointOverride_resolvesToEndpointOverride() {
48+
assertThat(resolveEndpoint("unknown-region", "http://localhost:1234"))
49+
.isEqualTo("http://localhost:1234");
50+
assertThat(resolveEndpoint("us-west-2", "http://localhost:1234"))
51+
.isEqualTo("http://localhost:1234");
52+
}
53+
54+
private String resolveEndpoint(String region, String endpointOverride) {
55+
return DEFAULT_ENDPOINT_PROVIDER.resolveEndpoint(e -> e.region(Region.of(region))
56+
.endpoint(endpointOverride))
57+
.join()
58+
.url()
59+
.toString();
60+
}
61+
}

0 commit comments

Comments
 (0)