Skip to content

Commit 8aebcc7

Browse files
authored
fix: Updates atlas-serverless-basic example. (#210)
1 parent 5c57dc8 commit 8aebcc7

File tree

2 files changed

+19
-35
lines changed

2 files changed

+19
-35
lines changed

examples/l3-resources/atlas-serverless-basic.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,30 @@ interface AtlasStackProps {
88
readonly profile: string;
99
readonly region: string;
1010
readonly ip: string;
11-
readonly continuousBackupEnabled: string;
12-
readonly terminationProtectionEnabled: string;
11+
readonly instanceName: string;
12+
readonly projectName: string;
13+
readonly continuousBackupEnabled: boolean;
14+
readonly terminationProtectionEnabled: boolean;
1315
}
1416

1517
export class CdkTestingStack extends cdk.Stack {
1618
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
1719
super(scope, id, props);
1820

1921
const atlasProps = this.getContextProps();
20-
const atlasBasic = new AtlasServerlessBasic(this, 'AtlasServerlessBasic', {
21-
serverlessProps: {
22-
projectId: atlasProps.orgId,
22+
new AtlasServerlessBasic(this, 'AtlasServerlessBasic', {
23+
serverlessProps: {
24+
name: atlasProps.instanceName,
2325
profile: atlasProps.profile,
2426
continuousBackupEnabled: atlasProps.continuousBackupEnabled,
2527
providerSettings: {
26-
providerName: ServerlessInstanceProviderSettingsProviderName.SERVERLESS
28+
providerName: ServerlessInstanceProviderSettingsProviderName.SERVERLESS,
29+
regionName: atlasProps.region
2730
},
2831
terminationProtectionEnabled: atlasProps.terminationProtectionEnabled
2932
},
3033
projectProps: {
34+
name: atlasProps.projectName,
3135
orgId: atlasProps.orgId,
3236
},
3337

@@ -50,6 +54,8 @@ export class CdkTestingStack extends cdk.Stack {
5054
const terminationProtectionEnabled = this.node.tryGetContext('terminationProtectionEnabled');
5155
const continuousBackupEnabled = this.node.tryGetContext('continuousBackupEnabled');
5256
const region = this.node.tryGetContext('region') ?? "US_EAST_1";
57+
const instanceName = this.node.tryGetContext('instanceName');
58+
const projectName = this.node.tryGetContext('projectName');
5359
const ip = this.node.tryGetContext('ip');
5460
if (!ip){
5561
throw "No context value specified for ip. Please specify via the cdk context."
@@ -61,7 +67,9 @@ export class CdkTestingStack extends cdk.Stack {
6167
continuousBackupEnabled,
6268
profile,
6369
region,
64-
ip
70+
ip,
71+
instanceName,
72+
projectName
6573
}
6674
}
6775
}

src/l3-resources/atlas-serverless-basic/index.ts

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ import { Construct } from "constructs";
1616
import * as atlas from "../../index";
1717
import { AtlasServerlessBasicProps } from "../common/props";
1818

19-
/** @type {*} */
20-
const projectDefaults = {
21-
projectName: "atlas-project-",
22-
};
23-
/** @type {*} */
24-
const serverlessDefaults = {
25-
serverlessName: "atlas-serverless-",
26-
};
2719
/** @type {*} */
2820
const dbDefaults = {
2921
dbName: "admin",
@@ -81,9 +73,7 @@ export class AtlasServerlessBasic extends Construct {
8173
//Create a new MongoDB Atlas Project
8274
this.mProject = new atlas.CfnProject(this, "project-".concat(id), {
8375
profile: props.profile,
84-
name:
85-
props.projectProps.name ||
86-
projectDefaults.projectName.concat(String(randomNumber())),
76+
name: props.projectProps.name ?? `project-${id}`,
8777
...props.projectProps,
8878
});
8979
// Create a new serverless Instance and pass project ID
@@ -92,18 +82,10 @@ export class AtlasServerlessBasic extends Construct {
9282
"serverless-".concat(id),
9383
{
9484
projectId: this.mProject.attrId,
95-
name:
96-
props.serverlessProps.name ||
97-
serverlessDefaults.serverlessName.concat(String(randomNumber())),
98-
providerSettings: {
99-
providerName:
100-
atlas.ServerlessInstanceProviderSettingsProviderName.SERVERLESS,
101-
regionName: "us-east-1",
102-
},
85+
name: props.serverlessProps.name ?? `serverless-${id}`,
86+
providerSettings: props.serverlessProps.providerSettings,
10387
profile: props.profile,
104-
continuousBackupEnabled: this.node.tryGetContext(
105-
"continuousBackupEnabled"
106-
),
88+
continuousBackupEnabled: props.serverlessProps.continuousBackupEnabled,
10789
...props.serverlessProps,
10890
}
10991
);
@@ -133,9 +115,3 @@ export class AtlasServerlessBasic extends Construct {
133115
this.ipAccessList.addDependency(this.mProject);
134116
}
135117
}
136-
137-
function randomNumber() {
138-
const min = 10;
139-
const max = 9999999;
140-
return Math.floor(Math.random() * (max - min + 1) + min);
141-
}

0 commit comments

Comments
 (0)