Skip to content

Commit b539538

Browse files
authored
feat: Adds support for stream-connection 'Sample' type (#246)
1 parent d10807b commit b539538

File tree

9 files changed

+174
-24
lines changed

9 files changed

+174
-24
lines changed

API.md

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
import * as cdk from 'aws-cdk-lib';
22
import { Construct } from 'constructs';
3-
import { CfnStreamConnection, CfnStreamConnectionPropsType } from 'awscdk-resources-mongodbatlas'
4-
import { env } from 'node:process';
3+
import { CfnStreamConnection, CfnStreamConnectionPropsType, DbRoleToExecute, DbRoleToExecuteType } from 'awscdk-resources-mongodbatlas'
54

65
interface AtlasStackProps {
76
readonly projectId: string;
87
readonly profile: string;
98
readonly instanceName: string;
109
readonly connectionName: string;
11-
readonly type: CfnStreamConnectionPropsType;
1210
readonly clusterName: string;
1311
}
1412

1513
const app = new cdk.App();
1614

1715

18-
export class CdkTestStack extends cdk.Stack {
16+
export class CdkTestStackCluster extends cdk.Stack {
1917
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
2018
super(scope, id, props);
2119

2220
const atlasProps = this.getContextProps();
23-
const streamConnection = new CfnStreamConnection(this, "stream-connection-testing-stack", {
21+
const streamConnection = new CfnStreamConnection(this, "stream-connection-cluster-stack", {
2422
profile: atlasProps.profile,
2523
instanceName: atlasProps.instanceName,
2624
projectId: atlasProps.projectId,
2725
connectionName: atlasProps.connectionName,
2826
type: CfnStreamConnectionPropsType.CLUSTER,
29-
clusterName: atlasProps.clusterName
27+
clusterName: atlasProps.clusterName,
28+
dbRoleToExecute: {role: "atlasAdmin", type: DbRoleToExecuteType.BUILT_IN}
3029
});
3130
}
3231

@@ -36,7 +35,7 @@ export class CdkTestStack extends cdk.Stack {
3635
const projectId = this.node.tryGetContext('projectId');
3736
const instanceName = this.node.tryGetContext('instanceName');
3837
const connectionName = this.node.tryGetContext('connectionName');
39-
const type = this.node.tryGetContext('type');
38+
const clusterName = this.node.tryGetContext('clusterName');
4039
if (!projectId) {
4140
throw "No context value specified for projectId. Please specify via the cdk context."
4241
}
@@ -46,17 +45,13 @@ export class CdkTestStack extends cdk.Stack {
4645
if (!connectionName) {
4746
throw "No context value specified for connectionName. Please specify via the cdk context."
4847
}
49-
if (!type) {
50-
throw "No context value specified for type. Please specify via the cdk context."
51-
}
5248

5349
return {
5450
projectId,
5551
profile,
5652
instanceName,
5753
connectionName,
58-
type
54+
clusterName
5955
}
6056
}
61-
6257
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as cdk from 'aws-cdk-lib';
2+
import { Construct } from 'constructs';
3+
import { CfnStreamConnection, CfnStreamConnectionPropsType } from 'awscdk-resources-mongodbatlas'
4+
5+
interface AtlasStackProps {
6+
readonly projectId: string;
7+
readonly profile: string;
8+
readonly instanceName: string;
9+
readonly sampleDataset: string;
10+
}
11+
12+
const app = new cdk.App();
13+
14+
15+
export class CdkTestStack extends cdk.Stack {
16+
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
17+
super(scope, id, props);
18+
19+
const atlasProps = this.getContextProps();
20+
const streamConnection = new CfnStreamConnection(this, "stream-connection-sample-stack", {
21+
profile: atlasProps.profile,
22+
instanceName: atlasProps.instanceName,
23+
projectId: atlasProps.projectId,
24+
connectionName: atlasProps.sampleDataset,
25+
type: CfnStreamConnectionPropsType.SAMPLE,
26+
});
27+
}
28+
29+
30+
getContextProps(): AtlasStackProps {
31+
const profile = this.node.tryGetContext('profile') ?? 'default';
32+
const projectId = this.node.tryGetContext('projectId');
33+
const instanceName = this.node.tryGetContext('instanceName');
34+
const sampleDataset = this.node.tryGetContext('sampleDataset');
35+
if (!projectId) {
36+
throw "No context value specified for projectId. Please specify via the cdk context."
37+
}
38+
if (!instanceName) {
39+
throw "No context value specified for instanceName. Please specify via the cdk context."
40+
}
41+
if (!sampleDataset) {
42+
throw "No context value specified for sampleDataset. Please specify via the cdk context."
43+
}
44+
45+
return {
46+
projectId,
47+
profile,
48+
instanceName,
49+
sampleDataset,
50+
}
51+
}
52+
}

scripts/cdk.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ for file in "${dir}"/mongodb-atlas-*.json; do
6161
# When the Resource is not merged to main branch of submodule, you see the above error.
6262
cdk-import cfn -l typescript -s "${file}" -o "src/l1-resources/${path}" "${src}"
6363
# need rename resource file to index.ts file
64-
mv "src/l1-resources/${path}/mongodb-atlas-${path//-/}.ts" "src/l1-resources/${path}/index.ts"
64+
dest="src/l1-resources/${path}/index.ts"
65+
mv "src/l1-resources/${path}/mongodb-atlas-${path//-/}.ts" "${dest}"
66+
python "${root_dir}/scripts/rename_in_file.py" "${dest}"
6567
fi
6668
done
6769

scripts/rename_in_file.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import re
2+
import sys
3+
import pathlib
4+
5+
_underscore_pattern = re.compile(r"\w+_UNDERSCORE_\w+")
6+
7+
8+
def rename_underscore(text: str) -> str:
9+
def remove_underscore(match: re.Match):
10+
return match.group(0).replace("UNDERSCORE_", "")
11+
12+
return _underscore_pattern.sub(remove_underscore, text)
13+
14+
15+
def perform_renaming(path_str: str):
16+
path = pathlib.Path(path_str)
17+
assert path.exists(), f"no path @ {path}"
18+
assert path.name == "index.ts"
19+
print(f"renaming: {path_str}")
20+
old_text = path.read_text()
21+
new_text = rename_underscore(old_text)
22+
if old_text != new_text:
23+
path.write_text(new_text)
24+
else:
25+
print(f"renamed content in {path}")
26+
27+
28+
if __name__ == "__main__":
29+
*_, index_ts = sys.argv
30+
perform_renaming(index_ts)

scripts/rename_in_file_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
import rename_in_file
3+
4+
examples = {
5+
"remove_underscore": (
6+
"""\
7+
export enum DbRoleToExecuteType {
8+
/** BUILT_IN */
9+
BUILT_UNDERSCORE_IN = "BUILT_IN",
10+
/** CUSTOM */
11+
CUSTOM = "CUSTOM",
12+
}""",
13+
"""\
14+
export enum DbRoleToExecuteType {
15+
/** BUILT_IN */
16+
BUILT_IN = "BUILT_IN",
17+
/** CUSTOM */
18+
CUSTOM = "CUSTOM",
19+
}""",
20+
),
21+
"unchanged_not_found": (
22+
"some text", "some text"
23+
)
24+
}
25+
26+
27+
@pytest.mark.parametrize(
28+
"input_text,output_text", examples.values(), ids=examples.keys()
29+
)
30+
def test_examples(input_text, output_text, tmp_path):
31+
path = tmp_path / "index.ts"
32+
path.write_text(input_text)
33+
rename_in_file.perform_renaming(str(path))
34+
text_after = path.read_text()
35+
assert text_after == output_text

src/l1-resources/stream-connection/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface CfnStreamConnectionProps {
2525
readonly profile?: string;
2626

2727
/**
28-
* Human-readable label that identifies the stream connection.
28+
* Human-readable label that identifies the stream connection. In the case of the Sample type, this is the name of the sample source.
2929
*
3030
* @schema CfnStreamConnectionProps#ConnectionName
3131
*/
@@ -39,7 +39,7 @@ export interface CfnStreamConnectionProps {
3939
readonly instanceName: string;
4040

4141
/**
42-
* Type of the connection. Can be either Cluster or Kafka.
42+
* Type of the connection. Can be either Cluster, Kafka, or Sample.
4343
*
4444
* @schema CfnStreamConnectionProps#Type
4545
*/
@@ -112,7 +112,7 @@ export function toJson_CfnStreamConnectionProps(
112112
/* eslint-enable max-len, quote-props */
113113

114114
/**
115-
* Type of the connection. Can be either Cluster or Kafka.
115+
* Type of the connection. Can be either Cluster, Kafka, or Sample.
116116
*
117117
* @schema CfnStreamConnectionPropsType
118118
*/
@@ -121,6 +121,8 @@ export enum CfnStreamConnectionPropsType {
121121
KAFKA = "Kafka",
122122
/** Cluster */
123123
CLUSTER = "Cluster",
124+
/** Sample */
125+
SAMPLE = "Sample",
124126
}
125127

126128
/**
@@ -267,7 +269,7 @@ export function toJson_StreamsKafkaSecurity(
267269
*/
268270
export enum DbRoleToExecuteType {
269271
/** BUILT_IN */
270-
BUILT_UNDERSCORE_IN = "BUILT_IN",
272+
BUILT_IN = "BUILT_IN",
271273
/** CUSTOM */
272274
CUSTOM = "CUSTOM",
273275
}

test/l1-resources/stream-connection/index.test.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const PROFILE = "default";
2525
const PROJECT_ID = "testProjectId";
2626
const INSTANCE_NAME = "testInstanceName";
2727
const CONNECTION_NAME = "testConnectionName";
28+
const CONNECTION_NAME_SAMPLE_STREAM_SOLAR = "sample_stream_solar";
2829
const CLUSTER_NAME = "testClusterName";
2930
const ROLE = "testRole";
3031
const PROTOCOL = "PLAINTEXT";
@@ -47,7 +48,7 @@ test("AtlasStreamConnection of type Cluster construct should contain default pro
4748
clusterName: CLUSTER_NAME,
4849
dbRoleToExecute: {
4950
role: ROLE,
50-
type: DbRoleToExecuteType.BUILT_UNDERSCORE_IN,
51+
type: DbRoleToExecuteType.BUILT_IN,
5152
},
5253
});
5354

@@ -62,7 +63,7 @@ test("AtlasStreamConnection of type Cluster construct should contain default pro
6263
ClusterName: CLUSTER_NAME,
6364
DbRoleToExecute: {
6465
Role: ROLE,
65-
Type: DbRoleToExecuteType.BUILT_UNDERSCORE_IN,
66+
Type: DbRoleToExecuteType.BUILT_IN,
6667
},
6768
});
6869
});
@@ -109,3 +110,26 @@ test("AtlasStreamConnection of type Kafka construct should contain default prope
109110
},
110111
});
111112
});
113+
114+
test("AtlasStreamConnection of type Sample construct should contain default properties", () => {
115+
const mockApp = new App();
116+
const stack = new Stack(mockApp);
117+
118+
new CfnStreamConnection(stack, "stream-connection-testing-stack", {
119+
profile: PROFILE,
120+
instanceName: INSTANCE_NAME,
121+
projectId: PROJECT_ID,
122+
connectionName: CONNECTION_NAME_SAMPLE_STREAM_SOLAR,
123+
type: CfnStreamConnectionPropsType.SAMPLE,
124+
});
125+
126+
const template = Template.fromStack(stack);
127+
128+
template.hasResourceProperties(RESOURCE_NAME, {
129+
Profile: PROFILE,
130+
InstanceName: INSTANCE_NAME,
131+
ProjectId: PROJECT_ID,
132+
ConnectionName: CONNECTION_NAME_SAMPLE_STREAM_SOLAR,
133+
Type: CfnStreamConnectionPropsType.SAMPLE,
134+
});
135+
});

0 commit comments

Comments
 (0)