Skip to content

Commit f773cbd

Browse files
authored
Merge branch 'master' into master
2 parents e99c0e9 + 2627939 commit f773cbd

4 files changed

Lines changed: 124 additions & 28 deletions

File tree

packages/@aws-cdk/aws-elasticsearch/lib/domain.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,22 +1273,16 @@ export class Domain extends DomainBase implements IDomain, ec2.IConnectable {
12731273
const defaultInstanceType = 'r5.large.elasticsearch';
12741274
const warmDefaultInstanceType = 'ultrawarm1.medium.elasticsearch';
12751275

1276-
const dedicatedMasterType =
1277-
props.capacity?.masterNodeInstanceType?.toLowerCase() ??
1278-
defaultInstanceType;
1276+
const dedicatedMasterType = initializeInstanceType(defaultInstanceType, props.capacity?.masterNodeInstanceType);
12791277
const dedicatedMasterCount = props.capacity?.masterNodes ?? 0;
1280-
const dedicatedMasterEnabled = dedicatedMasterCount > 0;
1278+
const dedicatedMasterEnabled = cdk.Token.isUnresolved(dedicatedMasterCount) ? true : dedicatedMasterCount > 0;
12811279

1282-
const instanceType =
1283-
props.capacity?.dataNodeInstanceType?.toLowerCase() ??
1284-
defaultInstanceType;
1280+
const instanceType = initializeInstanceType(defaultInstanceType, props.capacity?.dataNodeInstanceType);
12851281
const instanceCount = props.capacity?.dataNodes ?? 1;
12861282

1287-
const warmType =
1288-
props.capacity?.warmInstanceType?.toLowerCase() ??
1289-
warmDefaultInstanceType;
1283+
const warmType = initializeInstanceType(warmDefaultInstanceType, props.capacity?.warmInstanceType);
12901284
const warmCount = props.capacity?.warmNodes ?? 0;
1291-
const warmEnabled = warmCount > 0;
1285+
const warmEnabled = cdk.Token.isUnresolved(warmCount) ? true : warmCount > 0;
12921286

12931287
const availabilityZoneCount =
12941288
props.zoneAwareness?.availabilityZoneCount ?? 2;
@@ -1319,11 +1313,11 @@ export class Domain extends DomainBase implements IDomain, ec2.IConnectable {
13191313
throw new Error('When providing vpc options you need to provide a subnet for each AZ you are using');
13201314
}
13211315

1322-
if ([dedicatedMasterType, instanceType, warmType].some(t => !t.endsWith('.elasticsearch'))) {
1316+
if ([dedicatedMasterType, instanceType, warmType].some(t => (!cdk.Token.isUnresolved(t) && !t.endsWith('.elasticsearch')))) {
13231317
throw new Error('Master, data and UltraWarm node instance types must end with ".elasticsearch".');
13241318
}
13251319

1326-
if (!warmType.startsWith('ultrawarm')) {
1320+
if (!cdk.Token.isUnresolved(warmType) && !warmType.startsWith('ultrawarm')) {
13271321
throw new Error('UltraWarm node instance type must start with "ultrawarm".');
13281322
}
13291323

@@ -1822,3 +1816,18 @@ function selectSubnets(vpc: ec2.IVpc, vpcSubnets: ec2.SubnetSelection[]): ec2.IS
18221816
}
18231817
return selected;
18241818
}
1819+
1820+
/**
1821+
* Initializes an instance type.
1822+
*
1823+
* @param defaultInstanceType Default instance type which is used if no instance type is provided
1824+
* @param instanceType Instance type
1825+
* @returns Instance type in lowercase (if provided) or default instance type
1826+
*/
1827+
function initializeInstanceType(defaultInstanceType: string, instanceType?: string): string {
1828+
if (instanceType) {
1829+
return cdk.Token.isUnresolved(instanceType) ? instanceType : instanceType.toLowerCase();
1830+
} else {
1831+
return defaultInstanceType;
1832+
}
1833+
}

packages/@aws-cdk/aws-elasticsearch/test/domain.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as iam from '@aws-cdk/aws-iam';
88
import * as kms from '@aws-cdk/aws-kms';
99
import * as logs from '@aws-cdk/aws-logs';
1010
import * as route53 from '@aws-cdk/aws-route53';
11-
import { App, Stack, Duration, SecretValue, CfnParameter } from '@aws-cdk/core';
11+
import { App, Stack, Duration, SecretValue, CfnParameter, Token } from '@aws-cdk/core';
1212
import { Domain, ElasticsearchVersion } from '../lib';
1313

1414
let app: App;
@@ -246,6 +246,45 @@ describe('UltraWarm instances', () => {
246246

247247
});
248248

249+
test('can use tokens in capacity configuration', () => {
250+
new Domain(stack, 'Domain', {
251+
version: ElasticsearchVersion.V7_10,
252+
capacity: {
253+
dataNodeInstanceType: Token.asString({ Ref: 'dataNodeInstanceType' }),
254+
dataNodes: Token.asNumber({ Ref: 'dataNodes' }),
255+
masterNodeInstanceType: Token.asString({ Ref: 'masterNodeInstanceType' }),
256+
masterNodes: Token.asNumber({ Ref: 'masterNodes' }),
257+
warmInstanceType: Token.asString({ Ref: 'warmInstanceType' }),
258+
warmNodes: Token.asNumber({ Ref: 'warmNodes' }),
259+
},
260+
});
261+
262+
expect(stack).toHaveResourceLike('AWS::Elasticsearch::Domain', {
263+
ElasticsearchClusterConfig: {
264+
InstanceCount: {
265+
Ref: 'dataNodes',
266+
},
267+
InstanceType: {
268+
Ref: 'dataNodeInstanceType',
269+
},
270+
DedicatedMasterEnabled: true,
271+
DedicatedMasterCount: {
272+
Ref: 'masterNodes',
273+
},
274+
DedicatedMasterType: {
275+
Ref: 'masterNodeInstanceType',
276+
},
277+
WarmEnabled: true,
278+
WarmCount: {
279+
Ref: 'warmNodes',
280+
},
281+
WarmType: {
282+
Ref: 'warmInstanceType',
283+
},
284+
},
285+
});
286+
});
287+
249288
describe('log groups', () => {
250289

251290
test('slowSearchLogEnabled should create a custom log group', () => {

packages/@aws-cdk/aws-opensearchservice/lib/domain.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,22 +1204,16 @@ export class Domain extends DomainBase implements IDomain, ec2.IConnectable {
12041204
const defaultInstanceType = 'r5.large.search';
12051205
const warmDefaultInstanceType = 'ultrawarm1.medium.search';
12061206

1207-
const dedicatedMasterType =
1208-
props.capacity?.masterNodeInstanceType?.toLowerCase() ??
1209-
defaultInstanceType;
1207+
const dedicatedMasterType = initializeInstanceType(defaultInstanceType, props.capacity?.masterNodeInstanceType);
12101208
const dedicatedMasterCount = props.capacity?.masterNodes ?? 0;
1211-
const dedicatedMasterEnabled = dedicatedMasterCount > 0;
1209+
const dedicatedMasterEnabled = cdk.Token.isUnresolved(dedicatedMasterCount) ? true : dedicatedMasterCount > 0;
12121210

1213-
const instanceType =
1214-
props.capacity?.dataNodeInstanceType?.toLowerCase() ??
1215-
defaultInstanceType;
1211+
const instanceType = initializeInstanceType(defaultInstanceType, props.capacity?.dataNodeInstanceType);
12161212
const instanceCount = props.capacity?.dataNodes ?? 1;
12171213

1218-
const warmType =
1219-
props.capacity?.warmInstanceType?.toLowerCase() ??
1220-
warmDefaultInstanceType;
1214+
const warmType = initializeInstanceType(warmDefaultInstanceType, props.capacity?.warmInstanceType);
12211215
const warmCount = props.capacity?.warmNodes ?? 0;
1222-
const warmEnabled = warmCount > 0;
1216+
const warmEnabled = cdk.Token.isUnresolved(warmCount) ? true : warmCount > 0;
12231217

12241218
const availabilityZoneCount =
12251219
props.zoneAwareness?.availabilityZoneCount ?? 2;
@@ -1250,11 +1244,11 @@ export class Domain extends DomainBase implements IDomain, ec2.IConnectable {
12501244
throw new Error('When providing vpc options you need to provide a subnet for each AZ you are using');
12511245
}
12521246

1253-
if ([dedicatedMasterType, instanceType, warmType].some(t => !t.endsWith('.search'))) {
1247+
if ([dedicatedMasterType, instanceType, warmType].some(t => (!cdk.Token.isUnresolved(t) && !t.endsWith('.search')))) {
12541248
throw new Error('Master, data and UltraWarm node instance types must end with ".search".');
12551249
}
12561250

1257-
if (!warmType.startsWith('ultrawarm')) {
1251+
if (!cdk.Token.isUnresolved(warmType) && !warmType.startsWith('ultrawarm')) {
12581252
throw new Error('UltraWarm node instance type must start with "ultrawarm".');
12591253
}
12601254

@@ -1761,3 +1755,18 @@ function selectSubnets(vpc: ec2.IVpc, vpcSubnets: ec2.SubnetSelection[]): ec2.IS
17611755
}
17621756
return selected;
17631757
}
1758+
1759+
/**
1760+
* Initializes an instance type.
1761+
*
1762+
* @param defaultInstanceType Default instance type which is used if no instance type is provided
1763+
* @param instanceType Instance type
1764+
* @returns Instance type in lowercase (if provided) or default instance type
1765+
*/
1766+
function initializeInstanceType(defaultInstanceType: string, instanceType?: string): string {
1767+
if (instanceType) {
1768+
return cdk.Token.isUnresolved(instanceType) ? instanceType : instanceType.toLowerCase();
1769+
} else {
1770+
return defaultInstanceType;
1771+
}
1772+
}

packages/@aws-cdk/aws-opensearchservice/test/domain.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as iam from '@aws-cdk/aws-iam';
88
import * as kms from '@aws-cdk/aws-kms';
99
import * as logs from '@aws-cdk/aws-logs';
1010
import * as route53 from '@aws-cdk/aws-route53';
11-
import { App, Stack, Duration, SecretValue, CfnParameter } from '@aws-cdk/core';
11+
import { App, Stack, Duration, SecretValue, CfnParameter, Token } from '@aws-cdk/core';
1212
import { Domain, EngineVersion } from '../lib';
1313

1414
let app: App;
@@ -248,6 +248,45 @@ describe('UltraWarm instances', () => {
248248

249249
});
250250

251+
test('can use tokens in capacity configuration', () => {
252+
new Domain(stack, 'Domain', {
253+
version: defaultVersion,
254+
capacity: {
255+
dataNodeInstanceType: Token.asString({ Ref: 'dataNodeInstanceType' }),
256+
dataNodes: Token.asNumber({ Ref: 'dataNodes' }),
257+
masterNodeInstanceType: Token.asString({ Ref: 'masterNodeInstanceType' }),
258+
masterNodes: Token.asNumber({ Ref: 'masterNodes' }),
259+
warmInstanceType: Token.asString({ Ref: 'warmInstanceType' }),
260+
warmNodes: Token.asNumber({ Ref: 'warmNodes' }),
261+
},
262+
});
263+
264+
expect(stack).toHaveResourceLike('AWS::OpenSearchService::Domain', {
265+
ClusterConfig: {
266+
InstanceCount: {
267+
Ref: 'dataNodes',
268+
},
269+
InstanceType: {
270+
Ref: 'dataNodeInstanceType',
271+
},
272+
DedicatedMasterEnabled: true,
273+
DedicatedMasterCount: {
274+
Ref: 'masterNodes',
275+
},
276+
DedicatedMasterType: {
277+
Ref: 'masterNodeInstanceType',
278+
},
279+
WarmEnabled: true,
280+
WarmCount: {
281+
Ref: 'warmNodes',
282+
},
283+
WarmType: {
284+
Ref: 'warmInstanceType',
285+
},
286+
},
287+
});
288+
});
289+
251290
describe('log groups', () => {
252291

253292
test('slowSearchLogEnabled should create a custom log group', () => {

0 commit comments

Comments
 (0)