Skip to content

Commit

Permalink
elasticsearchVersion as a number
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdottv authored and stephanh committed Aug 2, 2020
1 parent 95f0b37 commit d11f364
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 63 deletions.
71 changes: 22 additions & 49 deletions packages/@aws-cdk/aws-elasticsearch/lib/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,15 @@ export interface DomainProps {
readonly clusterConfig: ClusterConfig;

/**
* The Elasticsearch Version
* The Elasticsearch version that your domain will leverage.
*
* @default ElasticsearchVersion.ES_VERSION_7_4
* Per https://aws.amazon.com/elasticsearch-service/faqs/, Amazon Elasticsearch Service
* currently supports Elasticsearch versions 7.4, 7.1, 6.8, 6.7, 6.5, 6.4, 6.3, 6.2, 6.0,
* 5.6, 5.5, 5.3, 5.1, 2.3, and 1.5.
*
* @default 7.4
*/
readonly elasticsearchVersion?: ElasticsearchVersion;
readonly elasticsearchVersion?: number;

/**
* Encryption at rest options for the cluster.
Expand Down Expand Up @@ -975,8 +979,17 @@ export class Domain extends DomainBase implements IDomain {
throw new Error('Master and data node instance types must end with ".elasticsearch".');
}

const elasticsearchVersion = props.elasticsearchVersion ?? ElasticsearchVersion.ES_VERSION_7_4;
const versionNumber = parseFloat(elasticsearchVersion.toString());
const elasticsearchVersion = props.elasticsearchVersion ?? 7.4;
if (
elasticsearchVersion <= 7.4 &&
![
1.5, 2.3, 5.1, 5.3, 5.5, 5.6, 6.0,
6.2, 6.3, 6.4, 6.5, 6.7, 6.8, 7.1, 7.4,
].includes(elasticsearchVersion)
) {
throw new Error(`Unknown Elasticsearch version: ${elasticsearchVersion}`);
}

const encryptionAtRestEnabled = props.encryptionAtRestOptions?.enabled ?? (props.encryptionAtRestOptions?.kmsKey != null);
const ebsEnabled = props.ebsOptions != null;

Expand All @@ -995,7 +1008,7 @@ export class Domain extends DomainBase implements IDomain {

// Validate feature support for the given Elasticsearch version, per
// https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/aes-features-by-version.html
if (versionNumber < 5.1) {
if (elasticsearchVersion < 5.1) {
if (
props.logPublishingOptions?.slowIndexLogEnabled
|| props.logPublishingOptions?.appLogEnabled
Expand All @@ -1014,7 +1027,7 @@ export class Domain extends DomainBase implements IDomain {
}
}

if (versionNumber < 6.0) {
if (elasticsearchVersion < 6.0) {
if (props.nodeToNodeEncryptionEnabled) {
throw new Error('Node-to-node encryption requires Elasticsearch version 6.0 or later.');
}
Expand All @@ -1030,7 +1043,7 @@ export class Domain extends DomainBase implements IDomain {
throw new Error('M3, R3, and T2 instance types do not support encryption of data at rest.');
}

if (isInstanceType('t2.micro') && versionNumber > 2.3) {
if (isInstanceType('t2.micro') && elasticsearchVersion > 2.3) {
throw new Error('The t2.micro.elasticsearch instance type supports only Elasticsearch 1.5 and 2.3.');
}

Expand Down Expand Up @@ -1098,7 +1111,7 @@ export class Domain extends DomainBase implements IDomain {
// Create the domain
this.domain = new CfnDomain(this, 'Resource', {
domainName: this.physicalName,
elasticsearchVersion: elasticsearchVersion,
elasticsearchVersion: elasticsearchVersion.toString(),
elasticsearchClusterConfig: {
dedicatedMasterEnabled: props.clusterConfig.masterNodes != null,
dedicatedMasterCount: props.clusterConfig.masterNodes,
Expand Down Expand Up @@ -1163,46 +1176,6 @@ export class Domain extends DomainBase implements IDomain {
}
}

/**
* The Elasticsearch version that your domain will leverage.
*
* Per https://aws.amazon.com/elasticsearch-service/faqs/, Amazon Elasticsearch Service
* currently supports Elasticsearch versions 7.4, 7.1, 6.8, 6.7, 6.5, 6.4, 6.3, 6.2, 6.0,
* 5.6, 5.5, 5.3, 5.1, 2.3, and 1.5.
*/
export enum ElasticsearchVersion {
/** Elasticsearch Version 7.4 */
ES_VERSION_7_4 = '7.4',
/** Elasticsearch Version 7.1 */
ES_VERSION_7_1 = '7.1',
/** Elasticsearch Version 6.8 */
ES_VERSION_6_8 = '6.8',
/** Elasticsearch Version 6.7 */
ES_VERSION_6_7 = '6.7',
/** Elasticsearch Version 6.5 */
ES_VERSION_6_5 = '6.5',
/** Elasticsearch Version 6.4 */
ES_VERSION_6_4 = '6.4',
/** Elasticsearch Version 6.3 */
ES_VERSION_6_3 = '6.3',
/** Elasticsearch Version 6.2 */
ES_VERSION_6_2 = '6.2',
/** Elasticsearch Version 6.0 */
ES_VERSION_6_0 = '6.0',
/** Elasticsearch Version 5.6 */
ES_VERSION_5_6 = '5.6',
/** Elasticsearch Version 5.5 */
ES_VERSION_5_5 = '5.5',
/** Elasticsearch Version 5.3 */
ES_VERSION_5_3 = '5.3',
/** Elasticsearch Version 5.1 */
ES_VERSION_5_1 = '5.1',
/** Elasticsearch Version 2.3 */
ES_VERSION_2_3 = '2.3',
/** Elasticsearch Version 1.5 */
ES_VERSION_1_5 = '1.5',
}

/**
* Given an Elasticsearch domain endpoint, returns a CloudFormation expression that
* extracts the domain name.
Expand Down
33 changes: 20 additions & 13 deletions packages/@aws-cdk/aws-elasticsearch/test/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Metric, Statistic } from '@aws-cdk/aws-cloudwatch';
import { Subnet, Vpc, EbsDeviceVolumeType } from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import { App, Stack, Duration } from '@aws-cdk/core';
import { Domain, ElasticsearchVersion } from '../lib';
import { Domain } from '../lib';

let app: App;
let stack: Stack;
Expand Down Expand Up @@ -33,7 +33,7 @@ const readWriteActions = [

test('minimal example renders correctly', () => {
new Domain(stack, 'Domain', {
elasticsearchVersion: ElasticsearchVersion.ES_VERSION_7_1,
elasticsearchVersion: 7.1,
clusterConfig: defaultClusterConfig,
});

Expand Down Expand Up @@ -473,24 +473,31 @@ describe('custom error responses', () => {
})).toThrow(error);
});

test('error when elasticsearchVersion is unsupported/unknown', () => {
expect(() => new Domain(stack, 'Domain1', {
clusterConfig: defaultClusterConfig,
elasticsearchVersion: 5.4,
})).toThrow(/Unknown Elasticsearch version: 5\.4/);
});

test('error when log publishing is enabled for elasticsearch version < 5.1', () => {
const error = /logs publishing requires Elasticsearch version 5.1 or later/;
expect(() => new Domain(stack, 'Domain1', {
elasticsearchVersion: ElasticsearchVersion.ES_VERSION_2_3,
elasticsearchVersion: 2.3,
clusterConfig: defaultClusterConfig,
logPublishingOptions: {
appLogEnabled: true,
},
})).toThrow(error);
expect(() => new Domain(stack, 'Domain2', {
elasticsearchVersion: ElasticsearchVersion.ES_VERSION_1_5,
elasticsearchVersion: 1.5,
clusterConfig: defaultClusterConfig,
logPublishingOptions: {
slowSearchLogEnabled: true,
},
})).toThrow(error);
expect(() => new Domain(stack, 'Domain3', {
elasticsearchVersion: ElasticsearchVersion.ES_VERSION_1_5,
elasticsearchVersion: 1.5,
clusterConfig: defaultClusterConfig,
logPublishingOptions: {
slowIndexLogEnabled: true,
Expand All @@ -500,7 +507,7 @@ describe('custom error responses', () => {

test('error when encryption at rest is enabled for elasticsearch version < 5.1', () => {
expect(() => new Domain(stack, 'Domain1', {
elasticsearchVersion: ElasticsearchVersion.ES_VERSION_2_3,
elasticsearchVersion: 2.3,
clusterConfig: defaultClusterConfig,
encryptionAtRestOptions: {
enabled: true,
Expand All @@ -511,7 +518,7 @@ describe('custom error responses', () => {
test('error when cognito for kibana is enabled for elasticsearch version < 5.1', () => {
const user = new iam.User(stack, 'user');
expect(() => new Domain(stack, 'Domain1', {
elasticsearchVersion: ElasticsearchVersion.ES_VERSION_2_3,
elasticsearchVersion: 2.3,
clusterConfig: defaultClusterConfig,
cognitoOptions: {
identityPoolId: 'test-identity-pool-id',
Expand All @@ -524,28 +531,28 @@ describe('custom error responses', () => {
test('error when C5, I3, M5, or R5 instance types are specified for elasticsearch version < 5.1', () => {
const error = /C5, I3, M5, and R5 instance types require Elasticsearch version 5.1 or later/;
expect(() => new Domain(stack, 'Domain1', {
elasticsearchVersion: ElasticsearchVersion.ES_VERSION_2_3,
elasticsearchVersion: 2.3,
clusterConfig: {
...defaultClusterConfig,
masterNodeInstanceType: 'c5.medium.elasticsearch',
},
})).toThrow(error);
expect(() => new Domain(stack, 'Domain2', {
elasticsearchVersion: ElasticsearchVersion.ES_VERSION_1_5,
elasticsearchVersion: 1.5,
clusterConfig: {
...defaultClusterConfig,
dataNodeInstanceType: 'i3.2xlarge.elasticsearch',
},
})).toThrow(error);
expect(() => new Domain(stack, 'Domain3', {
elasticsearchVersion: ElasticsearchVersion.ES_VERSION_1_5,
elasticsearchVersion: 1.5,
clusterConfig: {
...defaultClusterConfig,
dataNodeInstanceType: 'm5.2xlarge.elasticsearch',
},
})).toThrow(error);
expect(() => new Domain(stack, 'Domain4', {
elasticsearchVersion: ElasticsearchVersion.ES_VERSION_1_5,
elasticsearchVersion: 1.5,
clusterConfig: {
...defaultClusterConfig,
masterNodeInstanceType: 'r5.2xlarge.elasticsearch',
Expand All @@ -555,7 +562,7 @@ describe('custom error responses', () => {

test('error when node to node encryption is enabled for elasticsearch version < 6.0', () => {
expect(() => new Domain(stack, 'Domain1', {
elasticsearchVersion: ElasticsearchVersion.ES_VERSION_5_6,
elasticsearchVersion: 5.6,
clusterConfig: defaultClusterConfig,
nodeToNodeEncryptionEnabled: true,
})).toThrow(/Node-to-node encryption requires Elasticsearch version 6.0 or later/);
Expand Down Expand Up @@ -607,7 +614,7 @@ describe('custom error responses', () => {

test('error when t2.micro is specified with elasticsearch version > 2.3', () => {
expect(() => new Domain(stack, 'Domain1', {
elasticsearchVersion: ElasticsearchVersion.ES_VERSION_6_7,
elasticsearchVersion: 6.7,
clusterConfig: {
...defaultClusterConfig,
masterNodeInstanceType: 't2.micro.elasticsearch',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TestStack extends Stack {
super(scope, id, props);

new es.Domain(this, 'Domain', {
elasticsearchVersion: es.ElasticsearchVersion.ES_VERSION_7_1,
elasticsearchVersion: 7.1,
clusterConfig: {
masterNodes: 3,
masterNodeInstanceType: 'm5.large.elasticsearch',
Expand Down

0 comments on commit d11f364

Please sign in to comment.