-
Notifications
You must be signed in to change notification settings - Fork 917
Adds accountId endpoint mode #4984
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
Changes from 7 commits
ed7e299
18bf8cb
c270352
89f411c
71112a7
8de3133
2be9cec
86a74bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* Represents a generic parameter that can be code generated, but isn't tied to a specific model type | ||
*/ | ||
public class LocalParameter { | ||
|
||
private final String name; | ||
private final Class<?> type; | ||
private final String documentation; | ||
|
||
public LocalParameter(String name, Class<?> type, String documentation) { | ||
this.name = name; | ||
this.type = type; | ||
this.documentation = documentation; | ||
} | ||
|
||
public String name() { | ||
return name; | ||
} | ||
|
||
public Class<?> type() { | ||
return type; | ||
} | ||
|
||
public String documentation() { | ||
return documentation; | ||
} | ||
} |
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.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, | ||
"Sets the behavior when account ID based endpoints are created. " | ||
+ "See {@link AccountIdEndpointMode} for values")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: when referencing classes in a javadoc, we should still use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the reminder, will fix! |
||
} | ||
|
||
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) | ||
.returns(TypeVariableName.get("B")); | ||
|
||
PoetUtils.addJavadoc(b::addJavadoc, param.documentation()); | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, will There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. |
||
builder.addStatement("accountId = (($T) identity).accountId().orElse(null)", AwsCredentialsIdentity.class); | ||
builder.endControlFlow(); | ||
builder.addStatement("return accountId"); | ||
return Optional.of(builder.build()); | ||
} | ||
} |
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.
What does model type refer to here?
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.
A model shape that can be a parameter, such as a context parameter. Our existing codegen types are tightly coupled to the model, which didn't work for account id routing mode. Any suggestions for updating the description?
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.
I think "model shape" as you describe sounds better