Skip to content

chore: merge ssdk branch into main #2279

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

Merged
merged 6 commits into from
Apr 29, 2021
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.smithy.aws.typescript.codegen;

import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isSigV4Service;
import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_CONFIG;
import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_MIDDLEWARE;

Expand Down Expand Up @@ -46,6 +47,7 @@
/**
* Configure clients with AWS auth configurations and plugin.
*/
// TODO: Think about AWS Auth supported for only some operations and not all, when not AWS service, with say @auth([])
public final class AddAwsAuthPlugin implements TypeScriptIntegration {
static final String STS_CLIENT_PREFIX = "sts-client-";
static final String ROLE_ASSUMERS_FILE = "defaultRoleAssumers";
Expand All @@ -59,6 +61,9 @@ public void addConfigInterfaceFields(
TypeScriptWriter writer
) {
ServiceShape service = settings.getService(model);
if (!isSigV4Service(service)) {
return;
}
if (!areAllOptionalAuthOperations(model, service)) {
writer.addImport("Credentials", "__Credentials", TypeScriptDependency.AWS_SDK_TYPES.packageName);
writer.writeDocs("Default credentials provider; Not available in browser runtime.")
Expand All @@ -71,7 +76,9 @@ public List<RuntimeClientPlugin> getClientPlugins() {
return ListUtils.of(
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth", HAS_CONFIG)
.servicePredicate((m, s) -> !areAllOptionalAuthOperations(m, s) && !testServiceId(s, "STS"))
.servicePredicate((m, s) -> isSigV4Service(s)
&& !areAllOptionalAuthOperations(m, s)
&& !testServiceId(s, "STS"))
.build(),
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.STS_MIDDLEWARE.dependency,
Expand All @@ -83,7 +90,7 @@ public List<RuntimeClientPlugin> getClientPlugins() {
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth", HAS_MIDDLEWARE)
// See operationUsesAwsAuth() below for AwsAuth Middleware customizations.
.servicePredicate(
(m, s) -> !testServiceId(s, "STS") && !hasOptionalAuthOperation(m, s)
(m, s) -> !testServiceId(s, "STS") && isSigV4Service(s) && !hasOptionalAuthOperation(m, s)
).build(),
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth", HAS_MIDDLEWARE)
Expand All @@ -100,7 +107,7 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
LanguageTarget target
) {
ServiceShape service = settings.getService(model);
if (areAllOptionalAuthOperations(model, service)) {
if (!isSigV4Service(service) || areAllOptionalAuthOperations(model, service)) {
return Collections.emptyMap();
}
switch (target) {
Expand Down Expand Up @@ -187,8 +194,8 @@ private static boolean operationUsesAwsAuth(Model model, ServiceShape service, O
}

// optionalAuth trait doesn't require authentication.
if (hasOptionalAuthOperation(model, service)) {
return !operation.getTrait(OptionalAuthTrait.class).isPresent();
if (isSigV4Service(service) && hasOptionalAuthOperation(model, service)) {
return !operation.hasTrait(OptionalAuthTrait.class);
}
return false;
}
Expand All @@ -197,7 +204,7 @@ private static boolean hasOptionalAuthOperation(Model model, ServiceShape servic
TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<OperationShape> operations = topDownIndex.getContainedOperations(service);
for (OperationShape operation : operations) {
if (operation.getTrait(OptionalAuthTrait.class).isPresent()) {
if (operation.hasTrait(OptionalAuthTrait.class)) {
return true;
}
}
Expand All @@ -208,7 +215,7 @@ private static boolean areAllOptionalAuthOperations(Model model, ServiceShape se
TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<OperationShape> operations = topDownIndex.getContainedOperations(service);
for (OperationShape operation : operations) {
if (!operation.getTrait(OptionalAuthTrait.class).isPresent()) {
if (!operation.hasTrait(OptionalAuthTrait.class)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

package software.amazon.smithy.aws.typescript.codegen;

import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isAwsService;
import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isSigV4Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -31,6 +34,9 @@
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
import software.amazon.smithy.utils.MapUtils;

// TODO: This javadoc is specific to needs of AWS client. However it has elements that would be needed by non-AWS
// clients too, like logger, region for SigV4. We should refactor these into different Integration or rename this
// class to be generic.
/**
* AWS clients need to know the service name for collecting metrics, the
* region name used to resolve endpoints, the max attempt to retry a request
Expand Down Expand Up @@ -82,10 +88,14 @@ public void addConfigInterfaceFields(
writer.addImport("Provider", "__Provider", TypeScriptDependency.AWS_SDK_TYPES.packageName);
writer.addImport("Logger", "__Logger", TypeScriptDependency.AWS_SDK_TYPES.packageName);

writer.writeDocs("Unique service identifier.\n@internal")
.write("serviceId?: string;\n");
writer.writeDocs("The AWS region to which this client will send requests")
.write("region?: string | __Provider<string>;\n");
if (isAwsService(settings, model)) {
writer.writeDocs("Unique service identifier.\n@internal")
.write("serviceId?: string;\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a code suggestion but an idea: why not make serviceId as a required parameter just like custom endpoints? Since it's required to differentiate a request always.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's complicated because the service id is on an aws trait, which we don't want to require.

}
if (isSigV4Service(settings, model)) {
writer.writeDocs("The AWS region to which this client will send requests or use as signingRegion")
.write("region?: string | __Provider<string>;\n");
}
writer.writeDocs("Value for how many times a request will be made at most in case of retry.")
.write("maxAttempts?: number | __Provider<number>;\n");
writer.writeDocs("Optional logger for logging debug/info/warn/error.")
Expand Down Expand Up @@ -114,11 +124,17 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
+ "trait was found on " + service.getId());
}
}
runtimeConfigs.putAll(getDefaultConfig(target));
runtimeConfigs.putAll(getDefaultConfig(target, settings, model));
return runtimeConfigs;
}

private Map<String, Consumer<TypeScriptWriter>> getDefaultConfig(LanguageTarget target) {
private Map<String, Consumer<TypeScriptWriter>> getDefaultConfig(
LanguageTarget target,
TypeScriptSettings settings,
Model model
) {
Map<String, Consumer<TypeScriptWriter>> defaultConfigs = new HashMap();
boolean isSigV4Service = isSigV4Service(settings, model);
switch (target) {
case SHARED:
return MapUtils.of(
Expand All @@ -128,40 +144,46 @@ private Map<String, Consumer<TypeScriptWriter>> getDefaultConfig(LanguageTarget
}
);
case BROWSER:
return MapUtils.of(
"region", writer -> {
writer.addDependency(TypeScriptDependency.INVALID_DEPENDENCY);
writer.addImport("invalidProvider", "invalidProvider",
TypeScriptDependency.INVALID_DEPENDENCY.packageName);
writer.write("region: invalidProvider(\"Region is missing\"),");
},
"maxAttempts", writer -> {
writer.addDependency(TypeScriptDependency.MIDDLEWARE_RETRY);
writer.addImport("DEFAULT_MAX_ATTEMPTS", "DEFAULT_MAX_ATTEMPTS",
TypeScriptDependency.MIDDLEWARE_RETRY.packageName);
writer.write("maxAttempts: DEFAULT_MAX_ATTEMPTS,");
}
);
if (isSigV4Service) {
defaultConfigs.put("region", writer -> {
writer.addDependency(TypeScriptDependency.INVALID_DEPENDENCY);
writer.addImport("invalidProvider", "invalidProvider",
TypeScriptDependency.INVALID_DEPENDENCY.packageName);
writer.write("region: invalidProvider(\"Region is missing\"),");
});
}
defaultConfigs.put("maxAttempts", writer -> {
writer.addDependency(TypeScriptDependency.MIDDLEWARE_RETRY);
writer.addImport("DEFAULT_MAX_ATTEMPTS", "DEFAULT_MAX_ATTEMPTS",
TypeScriptDependency.MIDDLEWARE_RETRY.packageName);
writer.write("maxAttempts: DEFAULT_MAX_ATTEMPTS,");
});
return defaultConfigs;
case NODE:
return MapUtils.of(
"region", writer -> {
writer.addDependency(AwsDependency.NODE_CONFIG_PROVIDER);
writer.addImport("loadConfig", "loadNodeConfig",
AwsDependency.NODE_CONFIG_PROVIDER.packageName);
writer.addDependency(TypeScriptDependency.CONFIG_RESOLVER);
writer.addImport("NODE_REGION_CONFIG_OPTIONS", "NODE_REGION_CONFIG_OPTIONS",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
writer.addImport("NODE_REGION_CONFIG_FILE_OPTIONS", "NODE_REGION_CONFIG_FILE_OPTIONS",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
writer.write(
if (isSigV4Service) {
// TODO: For non-AWS service, figure out how the region should be configured.
defaultConfigs.put("region", writer -> {
writer.addDependency(AwsDependency.NODE_CONFIG_PROVIDER);
writer.addImport("loadConfig", "loadNodeConfig",
AwsDependency.NODE_CONFIG_PROVIDER.packageName);
writer.addDependency(TypeScriptDependency.CONFIG_RESOLVER);
writer.addImport("NODE_REGION_CONFIG_OPTIONS", "NODE_REGION_CONFIG_OPTIONS",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
writer.addImport("NODE_REGION_CONFIG_FILE_OPTIONS", "NODE_REGION_CONFIG_FILE_OPTIONS",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
writer.write(
"region: loadNodeConfig(NODE_REGION_CONFIG_OPTIONS, NODE_REGION_CONFIG_FILE_OPTIONS),");
},
"maxAttempts", writer -> {
writer.addImport("NODE_MAX_ATTEMPT_CONFIG_OPTIONS", "NODE_MAX_ATTEMPT_CONFIG_OPTIONS",
TypeScriptDependency.MIDDLEWARE_RETRY.packageName);
writer.write("maxAttempts: loadNodeConfig(NODE_MAX_ATTEMPT_CONFIG_OPTIONS),");
}
);
});
}
defaultConfigs.put("maxAttempts", writer -> {
writer.addDependency(AwsDependency.NODE_CONFIG_PROVIDER);
writer.addImport("loadConfig", "loadNodeConfig",
AwsDependency.NODE_CONFIG_PROVIDER.packageName);
writer.addImport("NODE_MAX_ATTEMPT_CONFIG_OPTIONS", "NODE_MAX_ATTEMPT_CONFIG_OPTIONS",
TypeScriptDependency.MIDDLEWARE_RETRY.packageName);
writer.write("maxAttempts: loadNodeConfig(NODE_MAX_ATTEMPT_CONFIG_OPTIONS),");
});
return defaultConfigs;
default:
return Collections.emptyMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.smithy.aws.typescript.codegen;

import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isAwsService;
import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_CONFIG;
import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_MIDDLEWARE;

Expand Down Expand Up @@ -44,9 +45,16 @@ public List<RuntimeClientPlugin> getClientPlugins() {
return ListUtils.of(
RuntimeClientPlugin.builder()
.withConventions(TypeScriptDependency.CONFIG_RESOLVER.dependency, "Region", HAS_CONFIG)
.servicePredicate((m, s) -> isAwsService(s))
.build(),
// Only one of Endpoints or CustomEndpoints should be used
RuntimeClientPlugin.builder()
.withConventions(TypeScriptDependency.CONFIG_RESOLVER.dependency, "Endpoints", HAS_CONFIG)
.servicePredicate((m, s) -> isAwsService(s))
.build(),
RuntimeClientPlugin.builder()
.withConventions(TypeScriptDependency.CONFIG_RESOLVER.dependency, "CustomEndpoints", HAS_CONFIG)
.servicePredicate((m, s) -> !isAwsService(s))
.build(),
RuntimeClientPlugin.builder()
.withConventions(TypeScriptDependency.MIDDLEWARE_RETRY.dependency, "Retry")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package software.amazon.smithy.aws.typescript.codegen;

import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isAwsService;

import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -33,12 +35,15 @@
/**
* Add client plubins and configs to support injecting user agent.
*/
// TODO: Looks to add this back for non-AWS service clients, by fixing the dependency on ClientSharedValues.serviceId
public class AddUserAgentDependency implements TypeScriptIntegration {
@Override
public List<RuntimeClientPlugin> getClientPlugins() {
return ListUtils.of(
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.MIDDLEWARE_USER_AGENT.dependency, "UserAgent").build());
.withConventions(AwsDependency.MIDDLEWARE_USER_AGENT.dependency, "UserAgent")
.servicePredicate((m, s) -> isAwsService(s))
.build());
}

@Override
Expand All @@ -48,6 +53,9 @@ public void addConfigInterfaceFields(
SymbolProvider symbolProvider,
TypeScriptWriter writer
) {
if (!isAwsService(settings, model)) {
return;
}
writer.addImport("Provider", "Provider", TypeScriptDependency.AWS_SDK_TYPES.packageName);
writer.addImport("UserAgent", "__UserAgent", TypeScriptDependency.AWS_SDK_TYPES.packageName);
writer.writeDocs("The provider populating default tracking information to be sent with `user-agent`, "
Expand All @@ -62,6 +70,9 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
SymbolProvider symbolProvider,
LanguageTarget target
) {
if (!isAwsService(settings, model)) {
return Collections.emptyMap();
}
switch (target) {
case NODE:
return MapUtils.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package software.amazon.smithy.aws.typescript.codegen;

import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isAwsService;

import java.util.Collections;
import java.util.Map;
import java.util.function.BiConsumer;
Expand All @@ -39,6 +41,10 @@ public void writeAdditionalFiles(
SymbolProvider symbolProvider,
BiConsumer<String, Consumer<TypeScriptWriter>> writerFactory
) {
if (!settings.generateClient() || !isAwsService(settings, model)) {
return;
}

writerFactory.accept("endpoints.ts", writer -> {
new EndpointGenerator(settings.getService(model), writer).run();
});
Expand All @@ -51,6 +57,10 @@ public void addConfigInterfaceFields(
SymbolProvider symbolProvider,
TypeScriptWriter writer
) {
if (!settings.generateClient() || !isAwsService(settings, model)) {
return;
}

writer.addImport("RegionInfoProvider", "RegionInfoProvider", TypeScriptDependency.AWS_SDK_TYPES.packageName);
writer.writeDocs("Fetch related hostname, signing name or signing region with given region.");
writer.write("regionInfoProvider?: RegionInfoProvider;\n");
Expand All @@ -63,6 +73,10 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
SymbolProvider symbolProvider,
LanguageTarget target
) {
if (!settings.generateClient() || !isAwsService(settings, model)) {
return Collections.emptyMap();
}

switch (target) {
case SHARED:
return MapUtils.of("regionInfoProvider", writer -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package software.amazon.smithy.aws.typescript.codegen;

import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isAwsService;

import java.util.Arrays;
import java.util.Calendar;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -55,6 +57,12 @@ public void writeAdditionalFiles(
resource = resource.replace("${year}", Integer.toString(Calendar.getInstance().get(Calendar.YEAR)));
writer.write(resource);
});

// TODO: May need to generate a different/modified README.md for these cases
if (!settings.generateClient() || !isAwsService(settings, model)) {
return;
}

writerFactory.accept("README.md", writer -> {
ServiceShape service = settings.getService(model);
String resource = IoUtils.readUtf8Resource(getClass(), "README.md.template");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ static boolean writeXmlNamespace(GenerationContext context, Shape shape, String
* @param context The generation context.
*/
static void addItempotencyAutofillImport(GenerationContext context) {
// servers do not autogenerate idempotency tokens during deserialization
if (!context.getSettings().generateClient()) {
return;
}
context.getModel().shapes(MemberShape.class)
.filter(memberShape -> memberShape.hasTrait(IdempotencyTokenTrait.class))
.findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public SymbolProvider decorateSymbolProvider(
return symbol;
}

// TODO: Should this WARNING be avoided somehow if client is not for an AWS service?
// If the SDK service ID trait is present, use that, otherwise fall back to
// the default naming strategy for the service.
return shape.getTrait(ServiceTrait.class)
Expand Down
Loading