|
| 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.codegen.poet.rules; |
| 17 | + |
| 18 | +import static javax.lang.model.element.Modifier.PRIVATE; |
| 19 | +import static javax.lang.model.element.Modifier.STATIC; |
| 20 | + |
| 21 | +import com.squareup.javapoet.ClassName; |
| 22 | +import com.squareup.javapoet.CodeBlock; |
| 23 | +import com.squareup.javapoet.MethodSpec; |
| 24 | +import com.squareup.javapoet.ParameterizedTypeName; |
| 25 | +import com.squareup.javapoet.TypeName; |
| 26 | +import com.squareup.javapoet.TypeVariableName; |
| 27 | +import java.util.EnumMap; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Optional; |
| 30 | +import javax.lang.model.element.Modifier; |
| 31 | +import software.amazon.awssdk.awscore.client.config.AwsClientOption; |
| 32 | +import software.amazon.awssdk.awscore.endpoints.AccountIdEndpointMode; |
| 33 | +import software.amazon.awssdk.awscore.endpoints.AccountIdEndpointModeResolver; |
| 34 | +import software.amazon.awssdk.codegen.internal.Utils; |
| 35 | +import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel; |
| 36 | +import software.amazon.awssdk.codegen.model.internal.LocalParameter; |
| 37 | +import software.amazon.awssdk.codegen.model.rules.endpoints.BuiltInParameter; |
| 38 | +import software.amazon.awssdk.codegen.model.rules.endpoints.ParameterModel; |
| 39 | +import software.amazon.awssdk.core.SelectedAuthScheme; |
| 40 | +import software.amazon.awssdk.core.client.config.SdkClientConfiguration; |
| 41 | +import software.amazon.awssdk.core.client.config.SdkClientOption; |
| 42 | +import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; |
| 43 | +import software.amazon.awssdk.identity.spi.Identity; |
| 44 | +import software.amazon.awssdk.utils.CompletableFutureUtils; |
| 45 | +import software.amazon.awssdk.utils.internal.CodegenNamingUtils; |
| 46 | + |
| 47 | +/** |
| 48 | + * Knowledge index to get access to endpoint parameters known to the client builder classes. |
| 49 | + */ |
| 50 | +public final class EndpointParamsKnowledgeIndex { |
| 51 | + private static final Map<BuiltInParameter, LocalParameter> BUILT_IN_PARAMS_FOR_CLIENT_BUILDER = |
| 52 | + new EnumMap<>(BuiltInParameter.class); |
| 53 | + private final IntermediateModel intermediateModel; |
| 54 | + private Map<BuiltInParameter, LocalParameter> parametersToGenerate = new EnumMap<>(BuiltInParameter.class); |
| 55 | + |
| 56 | + static { |
| 57 | + BUILT_IN_PARAMS_FOR_CLIENT_BUILDER.put( |
| 58 | + BuiltInParameter.AWS_AUTH_ACCOUNT_ID_ENDPOINT_MODE, |
| 59 | + new LocalParameter("accountIdEndpointMode", |
| 60 | + AccountIdEndpointMode.class, |
| 61 | + CodeBlock.of("Sets the behavior when account ID based endpoints are created. " |
| 62 | + + "See {@link $T} for values", AccountIdEndpointMode.class))); |
| 63 | + } |
| 64 | + |
| 65 | + private EndpointParamsKnowledgeIndex(IntermediateModel intermediateModel) { |
| 66 | + this.intermediateModel = intermediateModel; |
| 67 | + this.parametersToGenerate = builtInsForClientBuilder(intermediateModel.getEndpointRuleSetModel().getParameters()); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Creates a new {@link EndpointParamsKnowledgeIndex} using the given {@code intermediateModel}.. |
| 72 | + */ |
| 73 | + public static EndpointParamsKnowledgeIndex of(IntermediateModel intermediateModel) { |
| 74 | + return new EndpointParamsKnowledgeIndex(intermediateModel); |
| 75 | + } |
| 76 | + |
| 77 | + public boolean hasAccountIdEndpointModeBuiltIn() { |
| 78 | + return parametersToGenerate.containsKey(BuiltInParameter.AWS_AUTH_ACCOUNT_ID_ENDPOINT_MODE); |
| 79 | + } |
| 80 | + |
| 81 | + public Optional<MethodSpec> accountIdEndpointModeClassMethodSpec() { |
| 82 | + if (hasAccountIdEndpointModeBuiltIn()) { |
| 83 | + return Optional.of(clientClassBuilderParamSetter(accountIdEndpointModeBuiltInParam())); |
| 84 | + } |
| 85 | + return Optional.empty(); |
| 86 | + } |
| 87 | + |
| 88 | + public Optional<MethodSpec> accountIdEndpointModeInterfaceMethodSpec() { |
| 89 | + if (hasAccountIdEndpointModeBuiltIn()) { |
| 90 | + return Optional.of(clientInterfaceBuilderParamSetter(accountIdEndpointModeBuiltInParam())); |
| 91 | + } |
| 92 | + return Optional.empty(); |
| 93 | + } |
| 94 | + |
| 95 | + private LocalParameter accountIdEndpointModeBuiltInParam() { |
| 96 | + return parametersToGenerate.get(BuiltInParameter.AWS_AUTH_ACCOUNT_ID_ENDPOINT_MODE); |
| 97 | + } |
| 98 | + |
| 99 | + private MethodSpec clientClassBuilderParamSetter(LocalParameter param) { |
| 100 | + String setterName = Utils.unCapitalize(CodegenNamingUtils.pascalCase(param.name())); |
| 101 | + String keyName = intermediateModel.getNamingStrategy().getEnumValueName(param.name()); |
| 102 | + TypeName type = TypeName.get(param.type()); |
| 103 | + |
| 104 | + return MethodSpec.methodBuilder(setterName) |
| 105 | + .addModifiers(Modifier.PUBLIC) |
| 106 | + .returns(TypeVariableName.get("B")) |
| 107 | + .addParameter(type, setterName) |
| 108 | + .addStatement("clientConfiguration.option($T.$L, $L)", |
| 109 | + AwsClientOption.class, keyName, setterName) |
| 110 | + .addStatement("return thisBuilder()") |
| 111 | + .build(); |
| 112 | + } |
| 113 | + |
| 114 | + private MethodSpec clientInterfaceBuilderParamSetter(LocalParameter param) { |
| 115 | + String setterName = Utils.unCapitalize(CodegenNamingUtils.pascalCase(param.name())); |
| 116 | + TypeName type = TypeName.get(param.type()); |
| 117 | + |
| 118 | + MethodSpec.Builder b = MethodSpec.methodBuilder(setterName) |
| 119 | + .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) |
| 120 | + .addParameter(type, setterName) |
| 121 | + .addJavadoc(param.documentation()) |
| 122 | + .returns(TypeVariableName.get("B")); |
| 123 | + |
| 124 | + return b.build(); |
| 125 | + } |
| 126 | + |
| 127 | + public Optional<MethodSpec> resolveAccountIdEndpointModeMethod() { |
| 128 | + if (!hasAccountIdEndpointModeBuiltIn()) { |
| 129 | + return Optional.empty(); |
| 130 | + } |
| 131 | + |
| 132 | + String name = "accountIdEndpointMode"; |
| 133 | + String keyName = intermediateModel.getNamingStrategy().getEnumValueName(name); |
| 134 | + TypeName typeName = TypeName.get(AccountIdEndpointMode.class); |
| 135 | + |
| 136 | + MethodSpec.Builder builder = MethodSpec.methodBuilder("resolveAccountIdEndpointMode") |
| 137 | + .addModifiers(PRIVATE) |
| 138 | + .addParameter(SdkClientConfiguration.class, "config") |
| 139 | + .returns(typeName); |
| 140 | + |
| 141 | + builder.addStatement("$T configuredMode = config.option($T.$L)", typeName, AwsClientOption.class, keyName); |
| 142 | + |
| 143 | + builder.beginControlFlow("if (configuredMode == null)"); |
| 144 | + builder.addCode("configuredMode = $T.create()", AccountIdEndpointModeResolver.class); |
| 145 | + builder.addCode(".profileFile(config.option($T.PROFILE_FILE_SUPPLIER))", SdkClientOption.class); |
| 146 | + builder.addCode(".profileName(config.option($T.PROFILE_NAME))", SdkClientOption.class); |
| 147 | + builder.addCode(".defaultMode($T.PREFERRED)", typeName); |
| 148 | + builder.addStatement(".resolve()"); |
| 149 | + builder.endControlFlow(); |
| 150 | + |
| 151 | + builder.addStatement("return configuredMode"); |
| 152 | + return Optional.of(builder.build()); |
| 153 | + } |
| 154 | + |
| 155 | + private Map<BuiltInParameter, LocalParameter> builtInsForClientBuilder(Map<String, ParameterModel> serviceEndpointParams) { |
| 156 | + Map<BuiltInParameter, LocalParameter> actualParams = new EnumMap<>(BuiltInParameter.class); |
| 157 | + serviceEndpointParams.forEach((k, v) -> { |
| 158 | + BuiltInParameter builtInEnum = v.getBuiltInEnum(); |
| 159 | + if (builtInEnum != null && BUILT_IN_PARAMS_FOR_CLIENT_BUILDER.containsKey(builtInEnum)) { |
| 160 | + actualParams.put(builtInEnum, BUILT_IN_PARAMS_FOR_CLIENT_BUILDER.get(builtInEnum)); |
| 161 | + } |
| 162 | + }); |
| 163 | + return actualParams; |
| 164 | + } |
| 165 | + |
| 166 | + public Optional<MethodSpec> accountIdFromIdentityMethod() { |
| 167 | + if (!hasAccountIdEndpointModeBuiltIn()) { |
| 168 | + return Optional.empty(); |
| 169 | + } |
| 170 | + |
| 171 | + ParameterizedTypeName paramType = ParameterizedTypeName.get(ClassName.get(SelectedAuthScheme.class), |
| 172 | + TypeVariableName.get("T")); |
| 173 | + |
| 174 | + MethodSpec.Builder builder = MethodSpec.methodBuilder("accountIdFromIdentity") |
| 175 | + .addModifiers(PRIVATE, STATIC) |
| 176 | + .addTypeVariable(TypeVariableName.get("T", Identity.class)) |
| 177 | + .addParameter(paramType, "selectedAuthScheme") |
| 178 | + .returns(String.class); |
| 179 | + |
| 180 | + builder.addStatement("$T identity = $T.joinLikeSync(selectedAuthScheme.identity())", TypeVariableName.get("T"), |
| 181 | + CompletableFutureUtils.class); |
| 182 | + builder.addStatement("$T accountId = null", String.class); |
| 183 | + builder.beginControlFlow("if (identity instanceof $T)", AwsCredentialsIdentity.class); |
| 184 | + builder.addStatement("accountId = (($T) identity).accountId().orElse(null)", AwsCredentialsIdentity.class); |
| 185 | + builder.endControlFlow(); |
| 186 | + builder.addStatement("return accountId"); |
| 187 | + return Optional.of(builder.build()); |
| 188 | + } |
| 189 | +} |
0 commit comments