Skip to content

Commit ad7f978

Browse files
Chase Coalwelltrivikr
Chase Coalwell
authored andcommitted
feat: location constraint migration (#497)
* feat: rename location-constraint * feat: migrate locationConstraintMiddleware * feat: apply LocationConstraint plugin
1 parent 5e1e3a7 commit ad7f978

File tree

16 files changed

+116
-65
lines changed

16 files changed

+116
-65
lines changed

Diff for: codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddBuiltinPlugins.java

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ public List<RuntimeClientPlugin> getClientPlugins() {
8787
.withConventions(AwsDependency.SSEC_MIDDLEWARE.dependency, "Ssec", HAS_MIDDLEWARE)
8888
.servicePredicate((m, s) -> testServiceId(s, "S3"))
8989
.operationPredicate((m, s, o) -> testContainsMember(m, o, SSEC_OPERATIONS))
90+
.build(),
91+
RuntimeClientPlugin.builder()
92+
.withConventions(AwsDependency.LOCATION_CONSTRAINT.dependency, "LocationConstraint",
93+
HAS_MIDDLEWARE)
94+
.servicePredicate((m, s) -> testServiceId(s, "S3"))
95+
.operationPredicate((m, s, o) -> o.getId().getName().equals("CreateBucket"))
9096
.build()
9197
);
9298
}

Diff for: codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsDependency.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public enum AwsDependency implements SymbolDependencyContainer {
3535
VALIDATE_BUCKET_NAME(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-s3", "^0.1.0-preview.2"),
3636
ADD_EXPECT_CONTINUE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-expect-continue", "^0.1.0-preview.5"),
3737
ADD_GLACIER_API_VERSION(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-glacier", "^0.1.0-preview.7"),
38-
SSEC_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-ssec", "^0.1.0-preview.5");
38+
SSEC_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-ssec", "^0.1.0-preview.5"),
39+
LOCATION_CONSTRAINT(NORMAL_DEPENDENCY, "@aws-sdk/middleware-location-constraint", "^0.1.0-preview.5");
3940

4041
public final String packageName;
4142
public final String version;

Diff for: packages/location-constraint-middleware/README.md

-4
This file was deleted.

Diff for: packages/location-constraint-middleware/src/index.ts

-47
This file was deleted.

Diff for: packages/middleware-location-constraint/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# @aws-sdk/middleware-location-constraint
2+
3+
[![NPM version](https://img.shields.io/npm/v/@aws-sdk/middleware-location-constraint/preview.svg)](https://www.npmjs.com/package/@aws-sdk/middleware-location-constraint)
4+
[![NPM downloads](https://img.shields.io/npm/dm/@aws-sdk/middleware-location-constraint.svg)](https://www.npmjs.com/package/@aws-sdk/middleware-location-constraint)

Diff for: packages/location-constraint-middleware/package.json renamed to packages/middleware-location-constraint/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@aws-sdk/location-constraint-middleware",
3-
"version": "0.1.0-preview.7",
2+
"name": "@aws-sdk/middleware-location-constraint",
3+
"version": "0.1.0-preview.5",
44
"scripts": {
55
"prepublishOnly": "tsc",
66
"pretest": "tsc -p tsconfig.test.json",
@@ -22,4 +22,4 @@
2222
"jest": "^24.7.1",
2323
"typescript": "~3.4.0"
2424
}
25-
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Provider } from "@aws-sdk/types";
2+
3+
export interface LocationConstraintInputConfig {}
4+
5+
interface PreviouslyResolved {
6+
region: Provider<string>;
7+
}
8+
9+
export interface LocationConstraintResolvedConfig {
10+
region: Provider<string>;
11+
}
12+
export function resolveLocationConstraintConfig<T>(
13+
input: T & LocationConstraintInputConfig & PreviouslyResolved
14+
): T & LocationConstraintResolvedConfig {
15+
return { ...input };
16+
}

Diff for: packages/location-constraint-middleware/src/index.spec.ts renamed to packages/middleware-location-constraint/src/index.spec.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import { locationConstraintMiddleware } from "./";
22

33
describe("locationConstrainMiddleware", () => {
4+
const next = jest.fn();
5+
6+
beforeEach(() => {
7+
jest.clearAllMocks();
8+
});
9+
410
it("should remove any CreateBucketConfiguration from requests directed at us-east-1", async () => {
5-
const mw = locationConstraintMiddleware(async () => "us-east-1");
6-
const next = jest.fn();
11+
const handler = locationConstraintMiddleware({
12+
region: "us-east-1"
13+
})(next, {} as any);
714
const input = {
815
CreateBucketConfiguration: { LocationConstraint: "us-east-1" },
916
foo: "bar"
1017
};
11-
12-
await mw(next, {} as any)({ input });
18+
await handler({ input });
1319

1420
expect(next.mock.calls.length).toBe(1);
1521
expect(next.mock.calls[0][0]).toEqual({
@@ -21,13 +27,14 @@ describe("locationConstrainMiddleware", () => {
2127
});
2228

2329
it("should apply a CreateBucketConfiguration with a LocationConstraint of the target region for requests directed outside of us-east-1", async () => {
24-
const mw = locationConstraintMiddleware(async () => "us-east-2");
25-
const next = jest.fn();
30+
const handler = locationConstraintMiddleware({
31+
region: "us-east-2"
32+
})(next, {} as any);
2633
const input = {
2734
foo: "bar"
2835
};
2936

30-
await mw(next, {} as any)({ input });
37+
await handler({ input });
3138

3239
expect(next.mock.calls.length).toBe(1);
3340
expect(next.mock.calls[0][0]).toEqual({
@@ -39,14 +46,15 @@ describe("locationConstrainMiddleware", () => {
3946
});
4047

4148
it("should do nothing if a LocationConstraint had already been set on a request directed outside of us-east-1", async () => {
42-
const mw = locationConstraintMiddleware(async () => "us-east-2");
43-
const next = jest.fn();
49+
const handler = locationConstraintMiddleware({
50+
region: "us-east-2"
51+
})(next, {} as any);
4452
const input = {
4553
CreateBucketConfiguration: { LocationConstraint: "us-east-1" },
4654
foo: "bar"
4755
};
4856

49-
await mw(next, {} as any)({ input });
57+
await handler({ input });
5058

5159
expect(next.mock.calls.length).toBe(1);
5260
expect(next.mock.calls[0][0]).toEqual({ input });

Diff for: packages/middleware-location-constraint/src/index.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import {
2+
InitializeHandler,
3+
InitializeMiddleware,
4+
InitializeHandlerArguments,
5+
InitializeHandlerOptions,
6+
InitializeHandlerOutput,
7+
MetadataBearer,
8+
Pluggable
9+
} from "@aws-sdk/types";
10+
import { LocationConstraintResolvedConfig } from "./configuration";
11+
12+
/**
13+
* This middleware modifies the input on S3 CreateBucket requests. If the LocationConstraint has not been set, this
14+
* middleware will set a LocationConstraint to match the configured region. The CreateBucketConfiguration will be
15+
* removed entirely on requests to the us-east-1 region.
16+
*/
17+
18+
export function locationConstraintMiddleware(
19+
options: LocationConstraintResolvedConfig
20+
): InitializeMiddleware<any, any> {
21+
return <Output extends MetadataBearer>(
22+
next: InitializeHandler<any, Output>
23+
): InitializeHandler<any, Output> => async (
24+
args: InitializeHandlerArguments<any>
25+
): Promise<InitializeHandlerOutput<Output>> => {
26+
const { CreateBucketConfiguration } = args.input;
27+
if (
28+
!CreateBucketConfiguration ||
29+
!CreateBucketConfiguration.LocationConstraint
30+
) {
31+
args = {
32+
...args,
33+
input: {
34+
...args.input,
35+
CreateBucketConfiguration: { LocationConstraint: options.region }
36+
}
37+
};
38+
} else if (options.region === "us-east-1") {
39+
args = {
40+
...args,
41+
input: {
42+
...args.input,
43+
CreateBucketConfiguration: undefined
44+
}
45+
};
46+
}
47+
48+
return next(args);
49+
};
50+
}
51+
52+
export const locationConstraintMiddlewareOptions: InitializeHandlerOptions = {
53+
step: "initialize",
54+
tags: ["LOCATION_CONSTRAINT", "CREATE_BUCKET_CONFIGURATION"],
55+
name: "locationConstraintMiddleware"
56+
};
57+
58+
export const getLocationConstraintPlugin = (
59+
config: LocationConstraintResolvedConfig
60+
): Pluggable<any, any> => ({
61+
applyToStack: clientStack => {
62+
clientStack.add(
63+
locationConstraintMiddleware(config),
64+
locationConstraintMiddlewareOptions
65+
);
66+
}
67+
});

0 commit comments

Comments
 (0)