generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 24
Generate Regional endpoints #394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3e8fb33
Generic hook for EndpointsGenerator w/ default StaticEndpointsGenerator.
alextwoods dc3a7cc
Add implementation of AWS StandardRegionalEndpoints
alextwoods 49fc457
Merge branch 'develop' into regional_endpoints
alextwoods 2a4e6f0
Refactor endpoints generator to reduce duplicate code - generator now…
alextwoods 526740e
Add EndpointResolutionError
alextwoods a3118d7
Fix static endpoint parameter generation
alextwoods 7ad023f
Add tests for StandardRegionalEndpointsResolver
alextwoods d101b9a
Add test for sdk endpoint + region set
alextwoods 220d33d
Merge branch 'develop' into regional_endpoints
alextwoods 9d4dc15
Minor cleanups
alextwoods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...aws/core/src/main/java/software/amazon/smithy/python/aws/codegen/AwsPythonDependency.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package software.amazon.smithy.python.aws.codegen; | ||
|
|
||
| import software.amazon.smithy.python.codegen.PythonDependency; | ||
| import software.amazon.smithy.utils.SmithyUnstableApi; | ||
|
|
||
| /** | ||
| * AWS Dependencies used in the smithy python generator. | ||
| */ | ||
| @SmithyUnstableApi | ||
| public class AwsPythonDependency { | ||
| /** | ||
| * The core aws smithy runtime python package. | ||
| * | ||
| * <p>While in development this will use the develop branch. | ||
| */ | ||
| public static final PythonDependency SMITHY_AWS_CORE = new PythonDependency( | ||
| "smithy_aws_core", | ||
| // You'll need to locally install this before we publish | ||
| "==0.0.1", | ||
| PythonDependency.Type.DEPENDENCY, | ||
| false); | ||
| } |
82 changes: 82 additions & 0 deletions
82
...rc/main/java/software/amazon/smithy/python/aws/codegen/AwsRegionalEndpointsGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package software.amazon.smithy.python.aws.codegen; | ||
|
|
||
| import java.util.List; | ||
| import software.amazon.smithy.aws.traits.ServiceTrait; | ||
| import software.amazon.smithy.codegen.core.Symbol; | ||
| import software.amazon.smithy.python.codegen.CodegenUtils; | ||
| import software.amazon.smithy.python.codegen.ConfigProperty; | ||
| import software.amazon.smithy.python.codegen.GenerationContext; | ||
| import software.amazon.smithy.python.codegen.SmithyPythonDependency; | ||
| import software.amazon.smithy.python.codegen.integrations.EndpointsGenerator; | ||
| import software.amazon.smithy.python.codegen.writer.PythonWriter; | ||
|
|
||
| /** | ||
| * Generates endpoint config and resolution logic for standard regional endpoints. | ||
| */ | ||
| public class AwsRegionalEndpointsGenerator implements EndpointsGenerator { | ||
| @Override | ||
| public List<ConfigProperty> endpointsConfig(GenerationContext context) { | ||
| return List.of(ConfigProperty.builder() | ||
| .name("endpoint_resolver") | ||
| .type(Symbol.builder() | ||
| .name("EndpointResolver[RegionalEndpointParameters]") | ||
| .addReference(Symbol.builder() | ||
| .name("RegionalEndpointParameters") | ||
| .namespace(AwsPythonDependency.SMITHY_AWS_CORE.packageName() + ".endpoints.standard_regional", ".") | ||
| .addDependency(AwsPythonDependency.SMITHY_AWS_CORE) | ||
| .build()) | ||
| .addReference(Symbol.builder() | ||
| .name("EndpointResolver") | ||
| .namespace("smithy_http.aio.interfaces", ".") | ||
| .addDependency(SmithyPythonDependency.SMITHY_HTTP) | ||
| .build()) | ||
| .build()) | ||
| .documentation(""" | ||
| The endpoint resolver used to resolve the final endpoint per-operation based on the \ | ||
| configuration.""") | ||
| .nullable(false) | ||
| .initialize(writer -> { | ||
| writer.addImport("smithy_aws_core.endpoints.standard_regional", "StandardRegionalEndpointsResolver"); | ||
| String endpointPrefix = context.settings().service(context.model()) | ||
| .getTrait(ServiceTrait.class) | ||
| .map(ServiceTrait::getEndpointPrefix) | ||
| .orElse(context.settings().service().getName()); | ||
|
|
||
| writer.write( | ||
| "self.endpoint_resolver = endpoint_resolver or StandardRegionalEndpointsResolver(endpoint_prefix='$L')", | ||
| endpointPrefix); | ||
| }) | ||
| .build(), | ||
| ConfigProperty.builder() | ||
| .name("endpoint_uri") | ||
| .type(Symbol.builder() | ||
| .name("str | URI") | ||
| .addReference(Symbol.builder() | ||
| .name("URI") | ||
| .namespace("smithy_core.interfaces", ".") | ||
| .addDependency(SmithyPythonDependency.SMITHY_CORE) | ||
| .build()) | ||
| .build()) | ||
| .documentation("A static URI to route requests to.") | ||
| .build(), | ||
| ConfigProperty.builder() | ||
| .name("region") | ||
| .type(Symbol.builder().name("str").build()) | ||
| .documentation(" The AWS region to connect to. The configured region is used to " | ||
| + "determine the service endpoint.") | ||
| .build()); | ||
| } | ||
|
|
||
| @Override | ||
| public void renderEndpointParameterConstruction(GenerationContext context, PythonWriter writer) { | ||
|
|
||
| writer.addDependency(AwsPythonDependency.SMITHY_AWS_CORE); | ||
| writer.addImport("smithy_aws_core.endpoints.standard_regional", "RegionalEndpointParameters"); | ||
| writer.write(""" | ||
| endpoint_parameters = RegionalEndpointParameters( | ||
| sdk_endpoint=config.endpoint_uri, | ||
| region=config.region | ||
| ) | ||
| """); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
.../main/java/software/amazon/smithy/python/aws/codegen/AwsRegionalEndpointsIntegration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package software.amazon.smithy.python.aws.codegen; | ||
|
|
||
| import java.util.Optional; | ||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.shapes.ServiceShape; | ||
| import software.amazon.smithy.python.codegen.integrations.EndpointsGenerator; | ||
| import software.amazon.smithy.python.codegen.integrations.PythonIntegration; | ||
|
|
||
| public class AwsRegionalEndpointsIntegration implements PythonIntegration { | ||
| @Override | ||
| public Optional<EndpointsGenerator> getEndpointsGenerator(Model model, ServiceShape service) { | ||
| return Optional.of(new AwsRegionalEndpointsGenerator()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
.../main/java/software/amazon/smithy/python/codegen/generators/StaticEndpointsGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package software.amazon.smithy.python.codegen.generators; | ||
|
|
||
| import java.util.List; | ||
| import software.amazon.smithy.codegen.core.Symbol; | ||
| import software.amazon.smithy.python.codegen.ConfigProperty; | ||
| import software.amazon.smithy.python.codegen.GenerationContext; | ||
| import software.amazon.smithy.python.codegen.SmithyPythonDependency; | ||
| import software.amazon.smithy.python.codegen.SymbolProperties; | ||
| import software.amazon.smithy.python.codegen.integrations.EndpointsGenerator; | ||
| import software.amazon.smithy.python.codegen.writer.PythonWriter; | ||
|
|
||
| public class StaticEndpointsGenerator implements EndpointsGenerator { | ||
| @Override | ||
| public List<ConfigProperty> endpointsConfig(GenerationContext context) { | ||
| return List.of(ConfigProperty.builder() | ||
| .name("endpoint_resolver") | ||
| .type(Symbol.builder() | ||
| .name("EndpointResolver[Any]") | ||
| .addReference(Symbol.builder() | ||
| .name("Any") | ||
| .namespace("typing", ".") | ||
| .putProperty(SymbolProperties.STDLIB, true) | ||
| .build()) | ||
| .addReference(Symbol.builder() | ||
| .name("EndpointResolver") | ||
| .namespace("smithy_http.aio.interfaces", ".") | ||
| .addDependency(SmithyPythonDependency.SMITHY_HTTP) | ||
| .build()) | ||
| .build()) | ||
| .documentation(""" | ||
| The endpoint resolver used to resolve the final endpoint per-operation based on the \ | ||
| configuration.""") | ||
| .nullable(false) | ||
| .initialize(writer -> { | ||
| writer.addImport("smithy_http.aio.endpoints", "StaticEndpointResolver"); | ||
| writer.write("self.endpoint_resolver = endpoint_resolver or StaticEndpointResolver()"); | ||
| }) | ||
| .build(), | ||
| ConfigProperty.builder() | ||
| .name("endpoint_uri") | ||
| .type(Symbol.builder() | ||
| .name("str | URI") | ||
| .addReference(Symbol.builder() | ||
| .name("URI") | ||
| .namespace("smithy_core.interfaces", ".") | ||
| .addDependency(SmithyPythonDependency.SMITHY_CORE) | ||
| .build()) | ||
| .build()) | ||
| .documentation("A static URI to route requests to.") | ||
| .build()); | ||
| } | ||
|
|
||
| @Override | ||
| public void renderEndpointParameterConstruction(GenerationContext context, PythonWriter writer) { | ||
| writer.addDependency(SmithyPythonDependency.SMITHY_HTTP); | ||
| writer.addImport("smithy_http.endpoints", "StaticEndpointParams"); | ||
| writer.write(""" | ||
| endpoint_parameters = StaticEndpointParams( | ||
| uri=config.endpoint_uri | ||
| ) | ||
| """); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a prime use case for a
CodeSection. You would create a section like this calledEndpointParamSectionor something like that with the necessary context, and then aPythonIntegrationwould return aCodeInterceptorin itsinterceptorsmethod.Then here you'd have something like:
You can then get rid of the
EndpointsGeneratorentirely.smithy-python doesn't use sections or interceptors much currently, but it should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For an implementation of an approach using CodeSections and CodeInterceptor see: #407