Skip to content

Commit

Permalink
feat: migrate route-53 middleware and apply plugin (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chase Coalwell authored and trivikr committed Jan 3, 2020
1 parent f8a9c54 commit 272ae8d
Show file tree
Hide file tree
Showing 20 changed files with 219 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class AddBuiltinPlugins implements TypeScriptIntegration {

private static final Set<String> SSEC_OPERATIONS = SetUtils.of("SSECustomerKey", "CopySourceSSECustomerKey");

private static final Set<String> ROUTE_53_ID_MEMBERS = SetUtils.of("DelegationSetId", "HostedZoneId", "Id");

private static final Set<String> S3_MD5_OPERATIONS = SetUtils.of(
"DeleteObjects",
"PutBucketCors",
Expand Down Expand Up @@ -115,6 +117,18 @@ public List<RuntimeClientPlugin> getClientPlugins() {
HAS_MIDDLEWARE)
.servicePredicate((m, s) -> testServiceId(s, "S3"))
.operationPredicate((m, s, o) -> S3_MD5_OPERATIONS.contains(o.getId().getName()))
.build(),
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.ROUTE53_MIDDLEWARE.dependency,
"ChangeResourceRecordSets", HAS_MIDDLEWARE)
.servicePredicate((m, s) -> testServiceId(s, "Route 53"))
.operationPredicate((m, s, o) -> o.getId().getName().equals("ChangeResourceRecordSets"))
.build(),
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.ROUTE53_MIDDLEWARE.dependency, "IdNormalizer",
HAS_MIDDLEWARE)
.servicePredicate((m, s) -> testServiceId(s, "Route 53"))
.operationPredicate((m, s, o) -> testInputContainsMember(m, o, ROUTE_53_ID_MEMBERS))
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public enum AwsDependency implements SymbolDependencyContainer {
MD5_BROWSER(NORMAL_DEPENDENCY, "@aws-sdk/md5-js", "^0.1.0-preview.6"),
STREAM_HASHER_NODE(NORMAL_DEPENDENCY, "@aws-sdk/hash-stream-node", "^0.1.0-preview.4"),
STREAM_HASHER_BROWSER(NORMAL_DEPENDENCY, "@aws-sdk/hash-blob-browser", "^0.1.0-preview.4"),
ROUTE53_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-route53", "^0.1.0-preview.1"),
BODY_CHECKSUM(NORMAL_DEPENDENCY, "@aws-sdk/middleware-apply-body-checksum", "^0.1.0-preview.5");

public final String packageName;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions packages/middleware-sdk-route53/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# @aws-sdk/middleware-sdk-route53

[![NPM version](https://img.shields.io/npm/v/@aws-sdk/middleware-sdk-route53/preview.svg)](https://www.npmjs.com/package/@aws-sdk/middleware-sdk-route53)
[![NPM downloads](https://img.shields.io/npm/dm/@aws-sdk/middleware-sdk-route53.svg)](https://www.npmjs.com/package/@aws-sdk/middleware-sdk-route53)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aws-sdk/route53-id-normalizer-middleware",
"version": "0.1.0-preview.7",
"name": "@aws-sdk/middleware-sdk-route53",
"version": "0.1.0-preview.1",
"scripts": {
"prepublishOnly": "tsc",
"pretest": "tsc -p tsconfig.test.json",
Expand All @@ -22,4 +22,4 @@
"jest": "^24.7.1",
"typescript": "~3.4.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
import {
idNormalizerMiddleware,
changeBatchAliasTargetIdNormalizerMiddleware
} from "./";
import { changeResourceRecordSetsMiddleware } from "./change-resource-record-sets";

const prefixedProps = ["/hostedzone/ID", "/change/ID", "/delegationset/ID"];
const idParams = ["DelegationSetId", "HostedZoneId", "Id"];

describe("locationConstrainMiddleware", () => {
for (const paramName of idParams) {
for (const prefixed of prefixedProps) {
it(`should strip the prefix from the ${paramName} parameter`, async () => {
const next = jest.fn();
const input = { [paramName]: prefixed };

await idNormalizerMiddleware(next)({ input });

expect(next.mock.calls.length).toBe(1);
expect(next.mock.calls[0][0]).toEqual({
input: { [paramName]: "ID" }
});
});
}
}
});

describe("changeBatchAliasTargetIdNormalizerMiddleware", () => {
describe("changeResourceRecordSetsMiddleware", () => {
for (const prefixed of prefixedProps) {
it(`should strip the prefix from the ChangeBatch.Changes[*].ResourceRecordSet.AliasTarget.HostedZoneId parameter`, async () => {
const next = jest.fn();
Expand All @@ -45,9 +23,9 @@ describe("changeBatchAliasTargetIdNormalizerMiddleware", () => {
}
};

await changeBatchAliasTargetIdNormalizerMiddleware(next)({
input
});
const handler = changeResourceRecordSetsMiddleware()(next, {} as any);

await handler({ input });

expect(next.mock.calls.length).toBe(1);
expect(next.mock.calls[0][0]).toEqual({
Expand Down
86 changes: 86 additions & 0 deletions packages/middleware-sdk-route53/src/change-resource-record-sets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {
InitializeHandler,
InitializeMiddleware,
InitializeHandlerArguments,
InitializeHandlerOptions,
InitializeHandlerOutput,
MetadataBearer,
Pluggable
} from "@aws-sdk/types";
import { IDENTIFIER_PREFIX_PATTERN } from "./constants";

export interface Change {
ResourceRecordSet: {
AliasTarget?: {
HostedZoneId: string;
};
};
}

export interface ChangeBatchBearer {
ChangeBatch: {
Changes: Iterable<Change>;
};
}

export function changeResourceRecordSetsMiddleware(): InitializeMiddleware<
any,
any
> {
return <Output extends MetadataBearer>(
next: InitializeHandler<any, Output>
): InitializeHandler<any, Output> => async (
args: InitializeHandlerArguments<any>
): Promise<InitializeHandlerOutput<Output>> => {
const { ChangeBatch } = args.input;
const Changes: Array<Change> = [];
for (const change of ChangeBatch.Changes) {
const { AliasTarget } = change.ResourceRecordSet;
if (AliasTarget) {
Changes.push({
...change,
ResourceRecordSet: {
...change.ResourceRecordSet,
AliasTarget: {
...AliasTarget,
HostedZoneId: AliasTarget.HostedZoneId.replace(
IDENTIFIER_PREFIX_PATTERN,
""
)
}
}
});
} else {
Changes.push(change);
}
}

return next({
...args,
input: {
...(args.input as any),
ChangeBatch: {
...ChangeBatch,
Changes
}
}
});
};
}

export const changeResourceRecordSetsMiddlewareOptions: InitializeHandlerOptions = {
step: "initialize",
tags: ["ROUTE53_IDS", "CHANGE_RESOURCE_RECORD_SETS"],
name: "changeResourceRecordSetsMiddleware"
};

export const getChangeResourceRecordSetsPlugin = (
unused: any
): Pluggable<any, any> => ({
applyToStack: clientStack => {
clientStack.add(
changeResourceRecordSetsMiddleware(),
changeResourceRecordSetsMiddlewareOptions
);
}
});
1 change: 1 addition & 0 deletions packages/middleware-sdk-route53/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const IDENTIFIER_PREFIX_PATTERN = /^\/(hostedzone|change|delegationset)\//;
24 changes: 24 additions & 0 deletions packages/middleware-sdk-route53/src/id-normalizer-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { idNormalizerMiddleware } from "./id-normalizer";

const prefixedProps = ["/hostedzone/ID", "/change/ID", "/delegationset/ID"];
const idParams = ["DelegationSetId", "HostedZoneId", "Id"];

describe("idNormalizerMiddleware", () => {
for (const paramName of idParams) {
for (const prefixed of prefixedProps) {
it(`should strip the prefix from the ${paramName} parameter`, async () => {
const next = jest.fn();
const input = { [paramName]: prefixed };

const handler = idNormalizerMiddleware()(next, {} as any);

await handler({ input });

expect(next.mock.calls.length).toBe(1);
expect(next.mock.calls[0][0]).toEqual({
input: { [paramName]: "ID" }
});
});
}
}
});
55 changes: 55 additions & 0 deletions packages/middleware-sdk-route53/src/id-normalizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
InitializeHandler,
InitializeMiddleware,
InitializeHandlerArguments,
InitializeHandlerOptions,
InitializeHandlerOutput,
MetadataBearer,
Pluggable
} from "@aws-sdk/types";
import { IDENTIFIER_PREFIX_PATTERN } from "./constants";

export interface IdentifierBearer {
DelegationSetId?: string;
HostedZoneId?: string;
Id?: string;
}

const IDENTIFIER_PARAMETERS: Array<keyof IdentifierBearer> = [
"DelegationSetId",
"HostedZoneId",
"Id"
];

export function idNormalizerMiddleware(): InitializeMiddleware<any, any> {
return <Output extends MetadataBearer>(
next: InitializeHandler<any, Output>
): InitializeHandler<any, Output> => async (
args: InitializeHandlerArguments<any>
): Promise<InitializeHandlerOutput<Output>> => {
const input = { ...(args.input as any) };
for (const paramName of IDENTIFIER_PARAMETERS) {
const param = input[paramName];
if (param) {
input[paramName] = param.replace(IDENTIFIER_PREFIX_PATTERN, "");
}
}

return next({
...args,
input
});
};
}

export const idNormalizerMiddlewareOptions: InitializeHandlerOptions = {
step: "initialize",
tags: ["ROUTE53_IDS"],
name: "idNormalizerMiddleware"
};

export const getIdNormalizerPlugin = (unused: any): Pluggable<any, any> => ({
applyToStack: clientStack => {
clientStack.add(idNormalizerMiddleware(), idNormalizerMiddlewareOptions);
}
});
24 changes: 24 additions & 0 deletions packages/middleware-sdk-route53/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
changeResourceRecordSetsMiddleware,
getChangeResourceRecordSetsPlugin,
idNormalizerMiddleware,
getIdNormalizerPlugin
} from "./index";

describe("middleware-sdk-route53 package exports", () => {
it("changeResourceRecordSetsMiddleware", () => {
expect(typeof changeResourceRecordSetsMiddleware).toBe("function");
});

it("getChangeResourceRecordSetsPlugin", () => {
expect(typeof getChangeResourceRecordSetsPlugin).toBe("function");
});

it("idNormalizerMiddleware", () => {
expect(typeof idNormalizerMiddleware).toBe("function");
});

it("getIdNormalizerPlugin", () => {
expect(typeof getIdNormalizerPlugin).toBe("function");
});
});
2 changes: 2 additions & 0 deletions packages/middleware-sdk-route53/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./change-resource-record-sets";
export * from "./id-normalizer";
Loading

0 comments on commit 272ae8d

Please sign in to comment.