-
Notifications
You must be signed in to change notification settings - Fork 863
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
Adds accountId endpoint mode #4984
Merged
cenedhryn
merged 8 commits into
feature/master/accountid-endpoint-routing
from
salande/account-id-mode
Mar 8, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ed7e299
Add accountId endpoint mode enum, system properties, profile property
cenedhryn 18bf8cb
Supports resolving accountId and accountId endpoint mode parameters
cenedhryn c270352
Testing accountId and accountId endpoint mode resolution logic
cenedhryn 89f411c
Adds latest rules to accountId custom branch
cenedhryn 71112a7
Sets up SRA auth for DynamoDb in custom branch (not yet active in pub…
cenedhryn 8de3133
Adds a temporary functional test for accountId to DynamoDb module
cenedhryn 2be9cec
fixing merge conflicts
cenedhryn 86a74bf
Minor fixes
cenedhryn 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
46 changes: 46 additions & 0 deletions
46
codegen/src/main/java/software/amazon/awssdk/codegen/model/internal/LocalParameter.java
This file contains 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,46 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.codegen.model.internal; | ||
|
||
import com.squareup.javapoet.CodeBlock; | ||
|
||
/** | ||
* Represents a generic parameter that can be code generated, but isn't tied to a model shape | ||
*/ | ||
public class LocalParameter { | ||
|
||
private final String name; | ||
private final Class<?> type; | ||
private final CodeBlock documentation; | ||
|
||
public LocalParameter(String name, Class<?> type, CodeBlock documentation) { | ||
this.name = name; | ||
this.type = type; | ||
this.documentation = documentation; | ||
} | ||
|
||
public String name() { | ||
return name; | ||
} | ||
|
||
public Class<?> type() { | ||
return type; | ||
} | ||
|
||
public CodeBlock documentation() { | ||
return documentation; | ||
} | ||
} |
This file contains 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 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 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
190 changes: 190 additions & 0 deletions
190
...src/main/java/software/amazon/awssdk/codegen/poet/rules/EndpointParamsKnowledgeIndex.java
This file contains 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,190 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.codegen.poet.rules; | ||
|
||
import static javax.lang.model.element.Modifier.PRIVATE; | ||
import static javax.lang.model.element.Modifier.STATIC; | ||
|
||
import com.squareup.javapoet.ClassName; | ||
import com.squareup.javapoet.CodeBlock; | ||
import com.squareup.javapoet.MethodSpec; | ||
import com.squareup.javapoet.ParameterizedTypeName; | ||
import com.squareup.javapoet.TypeName; | ||
import com.squareup.javapoet.TypeVariableName; | ||
import java.util.EnumMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import javax.lang.model.element.Modifier; | ||
import software.amazon.awssdk.awscore.client.config.AwsClientOption; | ||
import software.amazon.awssdk.awscore.endpoints.AccountIdEndpointMode; | ||
import software.amazon.awssdk.awscore.endpoints.AccountIdEndpointModeResolver; | ||
import software.amazon.awssdk.codegen.internal.Utils; | ||
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel; | ||
import software.amazon.awssdk.codegen.model.internal.LocalParameter; | ||
import software.amazon.awssdk.codegen.model.rules.endpoints.BuiltInParameter; | ||
import software.amazon.awssdk.codegen.model.rules.endpoints.ParameterModel; | ||
import software.amazon.awssdk.codegen.poet.PoetUtils; | ||
import software.amazon.awssdk.core.SelectedAuthScheme; | ||
import software.amazon.awssdk.core.client.config.SdkClientConfiguration; | ||
import software.amazon.awssdk.core.client.config.SdkClientOption; | ||
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; | ||
import software.amazon.awssdk.identity.spi.Identity; | ||
import software.amazon.awssdk.utils.CompletableFutureUtils; | ||
import software.amazon.awssdk.utils.internal.CodegenNamingUtils; | ||
|
||
/** | ||
* Knowledge index to get access to endpoint parameters known to the client builder classes. | ||
*/ | ||
public final class EndpointParamsKnowledgeIndex { | ||
private static final Map<BuiltInParameter, LocalParameter> BUILT_IN_PARAMS_FOR_CLIENT_BUILDER = | ||
new EnumMap<>(BuiltInParameter.class); | ||
private final IntermediateModel intermediateModel; | ||
private Map<BuiltInParameter, LocalParameter> parametersToGenerate = new EnumMap<>(BuiltInParameter.class); | ||
|
||
static { | ||
BUILT_IN_PARAMS_FOR_CLIENT_BUILDER.put( | ||
BuiltInParameter.AWS_AUTH_ACCOUNT_ID_ENDPOINT_MODE, | ||
new LocalParameter("accountIdEndpointMode", | ||
AccountIdEndpointMode.class, | ||
CodeBlock.of("Sets the behavior when account ID based endpoints are created. " | ||
+ "See {@link $T} for values", AccountIdEndpointMode.class))); | ||
} | ||
|
||
private EndpointParamsKnowledgeIndex(IntermediateModel intermediateModel) { | ||
this.intermediateModel = intermediateModel; | ||
this.parametersToGenerate = builtInsForClientBuilder(intermediateModel.getEndpointRuleSetModel().getParameters()); | ||
} | ||
|
||
/** | ||
* Creates a new {@link EndpointParamsKnowledgeIndex} using the given {@code intermediateModel}.. | ||
*/ | ||
public static EndpointParamsKnowledgeIndex of(IntermediateModel intermediateModel) { | ||
return new EndpointParamsKnowledgeIndex(intermediateModel); | ||
} | ||
|
||
public boolean hasAccountIdEndpointModeBuiltIn() { | ||
return parametersToGenerate.containsKey(BuiltInParameter.AWS_AUTH_ACCOUNT_ID_ENDPOINT_MODE); | ||
} | ||
|
||
public Optional<MethodSpec> accountIdEndpointModeClassMethodSpec() { | ||
if (hasAccountIdEndpointModeBuiltIn()) { | ||
return Optional.of(clientClassBuilderParamSetter(accountIdEndpointModeBuiltInParam())); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public Optional<MethodSpec> accountIdEndpointModeInterfaceMethodSpec() { | ||
if (hasAccountIdEndpointModeBuiltIn()) { | ||
return Optional.of(clientInterfaceBuilderParamSetter(accountIdEndpointModeBuiltInParam())); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private LocalParameter accountIdEndpointModeBuiltInParam() { | ||
return parametersToGenerate.get(BuiltInParameter.AWS_AUTH_ACCOUNT_ID_ENDPOINT_MODE); | ||
} | ||
|
||
private MethodSpec clientClassBuilderParamSetter(LocalParameter param) { | ||
String setterName = Utils.unCapitalize(CodegenNamingUtils.pascalCase(param.name())); | ||
String keyName = intermediateModel.getNamingStrategy().getEnumValueName(param.name()); | ||
TypeName type = TypeName.get(param.type()); | ||
|
||
return MethodSpec.methodBuilder(setterName) | ||
.addModifiers(Modifier.PUBLIC) | ||
.returns(TypeVariableName.get("B")) | ||
.addParameter(type, setterName) | ||
.addStatement("clientConfiguration.option($T.$L, $L)", | ||
AwsClientOption.class, keyName, setterName) | ||
.addStatement("return thisBuilder()") | ||
.build(); | ||
} | ||
|
||
private MethodSpec clientInterfaceBuilderParamSetter(LocalParameter param) { | ||
String setterName = Utils.unCapitalize(CodegenNamingUtils.pascalCase(param.name())); | ||
TypeName type = TypeName.get(param.type()); | ||
|
||
MethodSpec.Builder b = MethodSpec.methodBuilder(setterName) | ||
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) | ||
.addParameter(type, setterName) | ||
.addJavadoc(param.documentation()) | ||
.returns(TypeVariableName.get("B")); | ||
|
||
return b.build(); | ||
} | ||
|
||
public Optional<MethodSpec> resolveAccountIdEndpointModeMethod() { | ||
if (!hasAccountIdEndpointModeBuiltIn()) { | ||
return Optional.empty(); | ||
} | ||
|
||
String name = "accountIdEndpointMode"; | ||
String keyName = intermediateModel.getNamingStrategy().getEnumValueName(name); | ||
TypeName typeName = TypeName.get(AccountIdEndpointMode.class); | ||
|
||
MethodSpec.Builder builder = MethodSpec.methodBuilder("resolveAccountIdEndpointMode") | ||
.addModifiers(PRIVATE) | ||
.addParameter(SdkClientConfiguration.class, "config") | ||
.returns(typeName); | ||
|
||
builder.addStatement("$T configuredMode = config.option($T.$L)", typeName, AwsClientOption.class, keyName); | ||
|
||
builder.beginControlFlow("if (configuredMode == null)"); | ||
builder.addCode("configuredMode = $T.create()", AccountIdEndpointModeResolver.class); | ||
builder.addCode(".profileFile(config.option($T.PROFILE_FILE_SUPPLIER))", SdkClientOption.class); | ||
builder.addCode(".profileName(config.option($T.PROFILE_NAME))", SdkClientOption.class); | ||
builder.addCode(".defaultMode($T.PREFERRED)", typeName); | ||
builder.addStatement(".resolve()"); | ||
builder.endControlFlow(); | ||
|
||
builder.addStatement("return configuredMode"); | ||
return Optional.of(builder.build()); | ||
} | ||
|
||
private Map<BuiltInParameter, LocalParameter> builtInsForClientBuilder(Map<String, ParameterModel> serviceEndpointParams) { | ||
Map<BuiltInParameter, LocalParameter> actualParams = new EnumMap<>(BuiltInParameter.class); | ||
serviceEndpointParams.forEach((k, v) -> { | ||
BuiltInParameter builtInEnum = v.getBuiltInEnum(); | ||
if (builtInEnum != null && BUILT_IN_PARAMS_FOR_CLIENT_BUILDER.containsKey(builtInEnum)) { | ||
actualParams.put(builtInEnum, BUILT_IN_PARAMS_FOR_CLIENT_BUILDER.get(builtInEnum)); | ||
} | ||
}); | ||
return actualParams; | ||
} | ||
|
||
public Optional<MethodSpec> accountIdFromIdentityMethod() { | ||
if (!hasAccountIdEndpointModeBuiltIn()) { | ||
return Optional.empty(); | ||
} | ||
|
||
ParameterizedTypeName paramType = ParameterizedTypeName.get(ClassName.get(SelectedAuthScheme.class), | ||
TypeVariableName.get("T")); | ||
|
||
MethodSpec.Builder builder = MethodSpec.methodBuilder("accountIdFromIdentity") | ||
.addModifiers(PRIVATE, STATIC) | ||
.addTypeVariable(TypeVariableName.get("T", Identity.class)) | ||
.addParameter(paramType, "selectedAuthScheme") | ||
.returns(String.class); | ||
|
||
builder.addStatement("$T identity = $T.joinLikeSync(selectedAuthScheme.identity())", TypeVariableName.get("T"), | ||
CompletableFutureUtils.class); | ||
builder.addStatement("$T accountId = null", String.class); | ||
builder.beginControlFlow("if (identity instanceof $T)", AwsCredentialsIdentity.class); | ||
builder.addStatement("accountId = (($T) identity).accountId().orElse(null)", AwsCredentialsIdentity.class); | ||
builder.endControlFlow(); | ||
builder.addStatement("return accountId"); | ||
return Optional.of(builder.build()); | ||
} | ||
} |
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.
Just curious, will
AwsCredentialsIdentity
be the only identity type that supports account ID?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.
We'd have to look in the crystal ball for that one. Nothing in the pipelines as for now, but it would be good to have another round of discussions here. It's a dilemma to build at the right level - neither adding support that's super generic and never diversified nor being too specific and then getting a broader use case is great.
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.
If we have to add accountId in the future, we can support it through an interface.