Skip to content

Commit

Permalink
feat: start endpoint resolver generation (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling authored and trivikr committed Jan 3, 2020
1 parent 3aca84f commit d0f9250
Show file tree
Hide file tree
Showing 24 changed files with 6,296 additions and 88 deletions.
4 changes: 3 additions & 1 deletion buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ phases:
- echo Building...
- yarn
- echo Executing unit tests
- yarn test
- yarn build:smithy-client #TODO: change to `yarn test:all` after clients are ready
- echo Executing functional test
- yarn test:functional
post_build:
commands:
- ./node_modules/.bin/codecov -f coverage/*.json
9 changes: 7 additions & 2 deletions clients/client-rds-data/RDSDataClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ import {
HttpHandlerOptions as __HttpHandlerOptions,
Provider as __Provider,
StreamCollector as __StreamCollector,
UrlParser as __UrlParser
UrlParser as __UrlParser,
RegionInfoProvider
} from "@aws-sdk/types";

export type ServiceInputTypes =
Expand Down Expand Up @@ -140,6 +141,11 @@ export interface ClientDefaults
* Provider function that return promise of a region string
*/
regionDefaultProvider?: (input: any) => __Provider<string>;

/**
* Fetch hostname, signing name or signing region of given region
*/
regionInfoProvider?: RegionInfoProvider;
}

export type RDSDataClientConfig = Partial<
Expand Down Expand Up @@ -184,7 +190,6 @@ export class RDSDataClient extends __Client<

constructor(configuration: RDSDataClientConfig) {
let _config_0 = {
service: "rds-data", //TODO: remove this
...__ClientDefaultValues,
...configuration
};
Expand Down
78 changes: 78 additions & 0 deletions clients/client-rds-data/endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { RegionInfo, RegionInfoProvider } from "@aws-sdk/types";

// Partition default templates
const AWS_TEMPLATE = "rds-data.{region}.amazonaws.com";
const AWS_CN_TEMPLATE = "rds-data.{region}.amazonaws.com.cn";
const AWS_ISO_TEMPLATE = "rds-data.{region}.c2s.ic.gov";
const AWS_ISO_B_TEMPLATE = "rds-data.{region}.sc2s.sgov.gov";
const AWS_US_GOV_TEMPLATE = "rds-data.{region}.amazonaws.com";

// Partition regions
const AWS_REGIONS = new Set([
"ap-south-1",
"eu-north-1",
"eu-west-3",
"eu-west-2",
"eu-west-1",
"ap-northeast-2",
"ap-northeast-1",
"me-south-1",
"ca-central-1",
"sa-east-1",
"ap-east-1",
"ap-southeast-1",
"ap-southeast-2",
"eu-central-1",
"us-east-1",
"us-east-2",
"us-west-1",
"us-west-2"
]);
const AWS_CN_REGIONS = new Set(["cn-north-1", "cn-northwest-1"]);
const AWS_ISO_REGIONS = new Set(["us-iso-east-1"]);
const AWS_ISO_B_REGIONS = new Set(["us-isob-east-1"]);
const AWS_US_GOV_REGIONS = new Set(["us-gov-west-1", "us-gov-east-1"]);

export const defaultRegionInfoProvider: RegionInfoProvider = (
region: string,
options?: any
) => {
let regionInfo: RegionInfo | undefined = undefined;
switch (region) {
// First, try to match exact region names.
// Next, try to match partition endpoints.
default:
if (AWS_REGIONS.has(region)) {
regionInfo = {
hostname: AWS_TEMPLATE.replace("{region}", region)
};
}
if (AWS_CN_REGIONS.has(region)) {
regionInfo = {
hostname: AWS_CN_TEMPLATE.replace("{region}", region)
};
}
if (AWS_ISO_REGIONS.has(region)) {
regionInfo = {
hostname: AWS_ISO_TEMPLATE.replace("{region}", region)
};
}
if (AWS_ISO_B_REGIONS.has(region)) {
regionInfo = {
hostname: AWS_ISO_B_TEMPLATE.replace("{region}", region)
};
}
if (AWS_US_GOV_REGIONS.has(region)) {
regionInfo = {
hostname: AWS_US_GOV_TEMPLATE.replace("{region}", region)
};
}
// Finally, assume it's an AWS partition endpoint.
if (regionInfo === undefined) {
regionInfo = {
hostname: AWS_TEMPLATE.replace("{region}", region)
};
}
}
return Promise.resolve(regionInfo);
};
5 changes: 3 additions & 2 deletions clients/client-rds-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
"clean": "npm run remove-definitions && npm run remove-dist && npm run remove-js && npm run remove-maps",
"build-documentation": "npm run clean && typedoc ./",
"prepublishOnly": "yarn build",
"pretest": "tsc",
"pretest": "yarn build",
"remove-definitions": "rimraf ./types",
"remove-dist": "rimraf ./dist",
"remove-documentation": "rimraf ./docs",
"remove-js": "rimraf *.js && rimraf ./commands/*.js && rimraf ./lib/*.js && rimraf ./models/*.js && rimraf ./protocols/*.js",
"remove-maps": "rimraf *.js.map && rimraf ./commands/*.js.map && rimraf ./lib/*.js.map && rimraf ./models/*.js.map && rimraf ./protocols/*.js.map",
"test": "exit 0",
"smoke-test": "npm run pretest && node ./test/smoke/index.spec.js",
"build:cjs": "tsc -p tsconfig.json",
"build:es": "tsc -p tsconfig.es.json",
"build": "yarn pretest && yarn build:es"
"build": "yarn build:cjs && yarn build:es"
},
"main": "./dist/cjs/index.js",
"types": "./types/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions clients/client-rds-data/runtimeConfig.shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { defaultRegionInfoProvider } from "./endpoints";
export const ClientSharedValues = {
apiVersion: "2018-08-01",
signingName: "rds-data",
regionInfoProvider: defaultRegionInfoProvider
};
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public List<RuntimeClientPlugin> getClientPlugins() {
.withConventions(TypeScriptDependency.CONFIG_RESOLVER.dependency, "Region", HAS_CONFIG)
.build(),
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth")
.withConventions(TypeScriptDependency.CONFIG_RESOLVER.dependency, "Endpoints", HAS_CONFIG)
.build(),
RuntimeClientPlugin.builder()
.withConventions(TypeScriptDependency.CONFIG_RESOLVER.dependency, "Endpoints", HAS_CONFIG)
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth")
.build(),
RuntimeClientPlugin.builder()
.withConventions(TypeScriptDependency.MIDDLEWARE_RETRY.dependency, "Retry")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2019 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.smithy.aws.typescript.codegen;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.typescript.codegen.LanguageTarget;
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;

/**
* Generates an endpoint resolver from endpoints.json.
*/
public final class AwsEndpointGeneratorIntegration implements TypeScriptIntegration {
@Override
public void writeAdditionalFiles(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
BiConsumer<String, Consumer<TypeScriptWriter>> writerFactory
) {
writerFactory.accept("endpoints.ts", writer -> {
new EndpointGenerator(settings.getService(model), writer).run();
});
}

@Override
public void addConfigInterfaceFields(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer
) {
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;");
}

@Override
public void addRuntimeConfigValues(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer,
LanguageTarget target
) {
switch (target) {
case SHARED:
writer.addImport("defaultRegionInfoProvider", "defaultRegionInfoProvider", "./endpoints");
writer.write("regionInfoProvider: defaultRegionInfoProvider");
break;
default:
//do nothing
}
}
}
Loading

0 comments on commit d0f9250

Please sign in to comment.