Skip to content
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

BREAKFIX: Only generate AccountIDEndpointMode config for services that use it #2795

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
401 changes: 401 additions & 0 deletions .changelog/28f542b4288d47b982b4261b8f5f6530.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.function.BiPredicate;
import java.util.logging.Logger;

import software.amazon.smithy.aws.go.codegen.customization.AccountIDEndpointRouting;
import software.amazon.smithy.aws.go.codegen.customization.auth.AwsHttpBearerAuthScheme;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
Expand Down Expand Up @@ -242,6 +243,7 @@ public class AddAwsConfigFields implements GoIntegration {
.name(SDK_ACCOUNTID_ENDPOINT_MODE)
.type(SdkGoTypes.Aws.AccountIDEndpointMode)
.documentation("Indicates how aws account ID is applied in endpoint2.0 routing")
.servicePredicate(AccountIDEndpointRouting::hasAccountIdEndpoints)
.build()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import software.amazon.smithy.go.codegen.integration.GoIntegration;
import software.amazon.smithy.go.codegen.SmithyGoTypes;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.rulesengine.language.syntax.Identifier;
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
import software.amazon.smithy.utils.MapUtils;

Expand All @@ -17,6 +19,10 @@
public class AccountIDEndpointRouting implements GoIntegration {
@Override
public void renderPreEndpointResolutionHook(GoSettings settings, GoWriter writer, Model model) {
if (!hasAccountIdEndpoints(model, settings.getService(model))) {
return;
}

writer.write("""
if err := checkAccountID(getIdentity(ctx), m.options.AccountIDEndpointMode); err != nil {
return out, metadata, $T("invalid accountID set: %w", err)
Expand All @@ -32,9 +38,10 @@ public void writeAdditionalFiles(
SymbolProvider symbolProvider,
GoDelegator goDelegator
) {
if (!settings.getService(model).hasTrait(EndpointRuleSetTrait.class)) {
if (!hasAccountIdEndpoints(model, settings.getService(model))) {
return;
}

goDelegator.useShapeWriter(settings.getService(model), goTemplate("""
func checkAccountID(identity $auth:T, mode $accountIDEndpointMode:T) error {
switch mode {
Expand Down Expand Up @@ -67,4 +74,19 @@ func checkAccountID(identity $auth:T, mode $accountIDEndpointMode:T) error {
)
));
}

public static boolean hasAccountIdEndpoints(Model model, ServiceShape service) {
if (!service.hasTrait(EndpointRuleSetTrait.class)) {
return false;
}

var rules = service.expectTrait(EndpointRuleSetTrait.class).getEndpointRuleSet();
for (var param : rules.getParameters()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks perfect for a stream, but Parameters is not a collection so while doable it's very awkward to handle it as such, so I like this version better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no way to actually get a stream of Parameters that I could see. You can either iterate them, get() by string, or there's a forEach.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again it's super awkward to do via streams and I like your version better, but just for completion sake, you could wrap the Parameters spliterator using StreamSupport

return StreamSupport.stream(rules.getParameters().spliterator(), false)
                .map(x -> x.getBuiltIn().orElse(""))
                .anyMatch(x -> x.equals("AWS::Auth::AccountId"));

if (param.getBuiltIn().orElse("").equals("AWS::Auth::AccountId")) {
return true;
}
}

return false;
}
}
1 change: 0 additions & 1 deletion internal/protocoltest/awsrestjson/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/protocoltest/awsrestjson/options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion internal/protocoltest/ec2query/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/protocoltest/ec2query/options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion internal/protocoltest/jsonrpc/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/protocoltest/jsonrpc/options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion internal/protocoltest/jsonrpc10/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/protocoltest/jsonrpc10/options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion internal/protocoltest/query/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/protocoltest/query/options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion internal/protocoltest/restxml/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/protocoltest/restxml/options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions internal/protocoltest/restxmlwithnamespace/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/protocoltest/restxmlwithnamespace/options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions internal/protocoltest/smithyrpcv2cbor/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/protocoltest/smithyrpcv2cbor/options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 9 additions & 29 deletions service/accessanalyzer/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions service/accessanalyzer/endpoints.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions service/accessanalyzer/options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 9 additions & 29 deletions service/account/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions service/account/endpoints.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions service/account/options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading