|
| 1 | +package software.amazon.smithy.aws.go.codegen.customization; |
| 2 | + |
| 3 | +import software.amazon.smithy.aws.go.codegen.SdkGoTypes; |
| 4 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 5 | +import software.amazon.smithy.go.codegen.GoDelegator; |
| 6 | +import software.amazon.smithy.go.codegen.GoSettings; |
| 7 | +import software.amazon.smithy.go.codegen.GoStdlibTypes; |
| 8 | +import software.amazon.smithy.go.codegen.GoWriter; |
| 9 | +import software.amazon.smithy.go.codegen.integration.GoIntegration; |
| 10 | +import software.amazon.smithy.go.codegen.SmithyGoTypes; |
| 11 | +import software.amazon.smithy.model.Model; |
| 12 | +import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; |
| 13 | +import software.amazon.smithy.utils.MapUtils; |
| 14 | + |
| 15 | +import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; |
| 16 | + |
| 17 | +public class AccountIDEndpointRouting implements GoIntegration { |
| 18 | + @Override |
| 19 | + public void renderPreEndpointResolutionHook(GoSettings settings, GoWriter writer, Model model) { |
| 20 | + writer.write(""" |
| 21 | + if err := checkAccountID(getIdentity(ctx), m.options.AccountIDEndpointMode); err != nil { |
| 22 | + return out, metadata, $T("invalid accountID set: %w", err) |
| 23 | + } |
| 24 | + """, |
| 25 | + GoStdlibTypes.Fmt.Errorf); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public void writeAdditionalFiles( |
| 30 | + GoSettings settings, |
| 31 | + Model model, |
| 32 | + SymbolProvider symbolProvider, |
| 33 | + GoDelegator goDelegator |
| 34 | + ) { |
| 35 | + if (!settings.getService(model).hasTrait(EndpointRuleSetTrait.class)) { |
| 36 | + return; |
| 37 | + } |
| 38 | + goDelegator.useShapeWriter(settings.getService(model), goTemplate(""" |
| 39 | + func checkAccountID(identity $auth:T, mode $accountIDEndpointMode:T) error { |
| 40 | + switch mode { |
| 41 | + case $aidModeUnset:T: |
| 42 | + case $aidModePreferred:T: |
| 43 | + case $aidModeDisabled:T: |
| 44 | + case $aidModeRequired:T: |
| 45 | + if ca, ok := identity.(*$credentialsAdapter:T); !ok { |
| 46 | + return $errorf:T("accountID is required but not set") |
| 47 | + } else if ca.Credentials.AccountID == "" { |
| 48 | + return $errorf:T("accountID is required but not set") |
| 49 | + } |
| 50 | + // default check in case invalid mode is configured through request config |
| 51 | + default: |
| 52 | + return $errorf:T("invalid accountID endpoint mode %s, must be preferred/required/disabled", mode) |
| 53 | + } |
| 54 | + |
| 55 | + return nil |
| 56 | + } |
| 57 | + """, |
| 58 | + MapUtils.of( |
| 59 | + "auth", SmithyGoTypes.Auth.Identity, |
| 60 | + "accountIDEndpointMode", SdkGoTypes.Aws.AccountIDEndpointMode, |
| 61 | + "credentialsAdapter", SdkGoTypes.Internal.Auth.Smithy.CredentialsAdapter, |
| 62 | + "aidModePreferred", SdkGoTypes.Aws.AccountIDEndpointModePreferred, |
| 63 | + "aidModeRequired", SdkGoTypes.Aws.AccountIDEndpointModeRequired, |
| 64 | + "aidModeUnset", SdkGoTypes.Aws.AccountIDEndpointModeUnset, |
| 65 | + "aidModeDisabled", SdkGoTypes.Aws.AccountIDEndpointModeDisabled, |
| 66 | + "errorf", GoStdlibTypes.Fmt.Errorf |
| 67 | + ) |
| 68 | + )); |
| 69 | + } |
| 70 | +} |
0 commit comments