Skip to content

Commit

Permalink
chore(opensearch): improve error message generation logic (#32651)
Browse files Browse the repository at this point in the history
### Issue # (if applicable)

N/A

### Reason for this change
Modified the code so that error messages don't need to be changed when adding new instance types in #32592.

Ref: #32592 (comment)

In #32592 , refactoring was only done for bug-related parts, so this PR will refactor the remaining areas.



### Description of changes
* Improve error message generation logic for encryption at rest and ultra warm.
* Refactor unit tests.



### Describe any new or updated permissions being added
Nothing

<!— What new or updated IAM permissions are needed to support the changes being introduced ? -->


### Description of how you validated changes
Re-run unit tests and integ tests



### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
mazyu36 authored Dec 24, 2024
1 parent dcf1d74 commit 744c2c1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
15 changes: 13 additions & 2 deletions packages/aws-cdk-lib/aws-opensearchservice/lib/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1588,22 +1588,33 @@ export class Domain extends DomainBase implements IDomain, ec2.IConnectable {
...unSupportEbsInstanceType,
];

const unSupportEncryptionAtRestInstanceType=[
ec2.InstanceClass.M3,
ec2.InstanceClass.R3,
ec2.InstanceClass.T2,
];

const unSupportUltraWarmInstanceType=[
ec2.InstanceClass.T2,
ec2.InstanceClass.T3,
];

// Validate against instance type restrictions, per
// https://docs.aws.amazon.com/opensearch-service/latest/developerguide/supported-instance-types.html
if (isSomeInstanceType(...unSupportEbsInstanceType) && ebsEnabled) {
throw new Error(`${formatInstanceTypesList(unSupportEbsInstanceType, 'and')} instance types do not support EBS storage volumes.`);
}

if (isSomeInstanceType('m3', 'r3', 't2') && encryptionAtRestEnabled) {
throw new Error('M3, R3, and T2 instance types do not support encryption of data at rest.');
throw new Error(`${formatInstanceTypesList(unSupportEncryptionAtRestInstanceType, 'and')} instance types do not support encryption of data at rest.`);
}

if (isInstanceType('t2.micro') && !(isElasticsearchVersion && versionNum <= 2.3)) {
throw new Error('The t2.micro.search instance type supports only Elasticsearch versions 1.5 and 2.3.');
}

if (isSomeInstanceType('t2', 't3') && warmEnabled) {
throw new Error('T2 and T3 instance types do not support UltraWarm storage.');
throw new Error(`${formatInstanceTypesList(unSupportUltraWarmInstanceType, 'and')} instance types do not support UltraWarm storage.`);
}

// Only R3, I3, R6GD, I4G, I4I, IM4GN and R7GD support instance storage, per
Expand Down
54 changes: 18 additions & 36 deletions packages/aws-cdk-lib/aws-opensearchservice/test/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1971,7 +1971,7 @@ each(testedOpenSearchVersions).describe('custom error responses', (engineVersion
'i4g.large.search',
'i4i.xlarge.search',
'r7gd.xlarge.search',
])('error when %4 instance types are specified with EBS enabled', (dataNodeInstanceType) => {
])('error when %s instance type is specified with EBS enabled', (dataNodeInstanceType) => {
expect(() => new Domain(stack, 'Domain2', {
version: engineVersion,
capacity: {
Expand All @@ -1984,35 +1984,21 @@ each(testedOpenSearchVersions).describe('custom error responses', (engineVersion
})).toThrow(/I3, R6GD, I4G, I4I, IM4GN and R7GD instance types do not support EBS storage volumes./);
});

test('error when m3, r3, or t2 instance types are specified with encryption at rest enabled', () => {
const error = /M3, R3, and T2 instance types do not support encryption of data at rest/;
test.each([
'm3.2xlarge.search',
'r3.2xlarge.search',
't2.2xlarge.search',
])
('error when %s instance type is specified with encryption at rest enabled', (masterNodeInstanceType) => {
expect(() => new Domain(stack, 'Domain1', {
version: engineVersion,
capacity: {
masterNodeInstanceType: 'm3.2xlarge.search',
},
encryptionAtRest: {
enabled: true,
},
})).toThrow(error);
expect(() => new Domain(stack, 'Domain2', {
version: engineVersion,
capacity: {
dataNodeInstanceType: 'r3.2xlarge.search',
},
encryptionAtRest: {
enabled: true,
},
})).toThrow(error);
expect(() => new Domain(stack, 'Domain3', {
version: engineVersion,
capacity: {
masterNodeInstanceType: 't2.2xlarge.search',
masterNodeInstanceType,
},
encryptionAtRest: {
enabled: true,
},
})).toThrow(error);
})).toThrow(/M3, R3 and T2 instance types do not support encryption of data at rest/);
});

test('error when t2.micro is specified with Elasticsearch version > 2.3', () => {
Expand All @@ -2027,14 +2013,14 @@ each(testedOpenSearchVersions).describe('custom error responses', (engineVersion
test.each([
'm5.large.search',
'r5.large.search',
])('error when any instance type other than R3, I3, R6GD, I4I, I4G, IM4GN or R7GD are specified without EBS enabled', () => {
])('error when any instance type other than R3, I3, R6GD, I4I, I4G, IM4GN or R7GD are specified without EBS enabled', (masterNodeInstanceType) => {
expect(() => new Domain(stack, 'Domain1', {
version: engineVersion,
ebs: {
enabled: false,
},
capacity: {
masterNodeInstanceType: 'm5.large.search',
masterNodeInstanceType,
},
})).toThrow(/EBS volumes are required when using instance types other than R3, I3, R6GD, I4G, I4I, IM4GN or R7GD./);
});
Expand Down Expand Up @@ -2160,22 +2146,18 @@ each(testedOpenSearchVersions).describe('custom error responses', (engineVersion
});
});

test('error when t2 or t3 instance types are specified with UltramWarm enabled', () => {
const error = /T2 and T3 instance types do not support UltraWarm storage/;
test.each([
't2.2xlarge.search',
't3.2xlarge.search',
])
('error when %s instance types is specified with UltramWarm enabled', (masterNodeInstanceType) => {
expect(() => new Domain(stack, 'Domain1', {
version: engineVersion,
capacity: {
masterNodeInstanceType: 't2.2xlarge.search',
masterNodeInstanceType,
warmNodes: 1,
},
})).toThrow(error);
expect(() => new Domain(stack, 'Domain2', {
version: engineVersion,
capacity: {
masterNodeInstanceType: 't3.2xlarge.search',
warmNodes: 1,
},
})).toThrow(error);
})).toThrow(/T2 and T3 instance types do not support UltraWarm storage/);
});

test('error when UltraWarm instance is used and no dedicated master instance specified', () => {
Expand Down

0 comments on commit 744c2c1

Please sign in to comment.