-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(rds): does not print all failed validations for DatabaseCluster props #32841
Merged
+199
−37
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/aws-cdk-lib/aws-rds/lib/validate-database-cluster-props.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Construct } from 'constructs'; | ||
import { ClusterScailabilityType, DatabaseCluster, DatabaseClusterProps, DBClusterStorageType } from './cluster'; | ||
import { PerformanceInsightRetention } from './props'; | ||
import { validateAllProps, ValidationRule } from '../../core/lib/helpers-internal'; | ||
|
||
const standardDatabaseRules: ValidationRule<DatabaseClusterProps>[] = [ | ||
{ | ||
condition: (props) => props.enablePerformanceInsights === false && | ||
(props.performanceInsightRetention !== undefined || props.performanceInsightEncryptionKey !== undefined), | ||
message: () => '`enablePerformanceInsights` disabled, but `performanceInsightRetention` or `performanceInsightEncryptionKey` was set', | ||
|
||
}, | ||
]; | ||
|
||
const limitlessDatabaseRules: ValidationRule<DatabaseClusterProps>[] = [ | ||
{ | ||
condition: (props) => !props.enablePerformanceInsights, | ||
message: () => 'Performance Insights must be enabled for Aurora Limitless Database', | ||
}, | ||
{ | ||
condition: (props) => !props.performanceInsightRetention | ||
|| props.performanceInsightRetention < PerformanceInsightRetention.MONTHS_1, | ||
message: () => 'Performance Insights retention period must be set to at least 31 days for Aurora Limitless Database', | ||
}, | ||
{ | ||
condition: (props) => !props.monitoringInterval || !props.enableClusterLevelEnhancedMonitoring, | ||
message: () => 'Cluster level enhanced monitoring must be set for Aurora Limitless Database. Please set \'monitoringInterval\' and enable \'enableClusterLevelEnhancedMonitoring\'', | ||
}, | ||
{ | ||
condition: (props) => !!(props.writer || props.readers), | ||
message: () => 'Aurora Limitless Database does not support reader or writer instances', | ||
}, | ||
{ | ||
condition: (props) => !props.engine.engineVersion?.fullVersion?.endsWith('limitless'), | ||
message: (props) => `Aurora Limitless Database requires an engine version that supports it, got: ${props.engine.engineVersion?.fullVersion}`, | ||
}, | ||
{ | ||
condition: (props) => props.storageType !== DBClusterStorageType.AURORA_IOPT1, | ||
message: (props) => `Aurora Limitless Database requires I/O optimized storage type, got: ${props.storageType}`, | ||
}, | ||
{ | ||
condition: (props) => props.cloudwatchLogsExports === undefined || props.cloudwatchLogsExports.length === 0, | ||
message: () => 'Aurora Limitless Database requires CloudWatch Logs exports to be set', | ||
}, | ||
]; | ||
|
||
export function validateDatabaseClusterProps(scope: Construct, props: DatabaseClusterProps): void { | ||
const isLimitlessCluster = props.clusterScailabilityType === ClusterScailabilityType.LIMITLESS; | ||
const applicableRules = isLimitlessCluster | ||
? [...standardDatabaseRules, ...limitlessDatabaseRules] | ||
: standardDatabaseRules; | ||
|
||
validateAllProps(scope, DatabaseCluster.name, props, applicableRules); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/aws-cdk-lib/core/lib/helpers-internal/validate-all-props.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Construct } from 'constructs'; | ||
import { ValidationError } from '../errors'; | ||
|
||
/** | ||
* Represents a validation rule for props of type T. | ||
* @template T The type of the props being validated. | ||
*/ | ||
export type ValidationRule<T> = { | ||
/** | ||
* A function that checks if the validation rule condition is met. | ||
* @param {T} props - The props object to validate. | ||
* @returns {boolean} True if the condition is met (i.e., validation fails), false otherwise. | ||
*/ | ||
condition: (props: T) => boolean; | ||
|
||
/** | ||
* A function that returns an error message if the validation fails. | ||
* @param {T} props - The props that failed validation. | ||
* @returns {string} The error message. | ||
*/ | ||
message: (props: T) => string; | ||
}; | ||
|
||
/** | ||
* Validates props against a set of rules and throws an error if any validations fail. | ||
* | ||
* @template T The type of the props being validated. | ||
* @param {string} className - The name of the class being validated, used in the error message. Ex. for SQS, might be Queue.name | ||
* @param {T} props - The props object to validate. | ||
* @param {ValidationRule<T>[]} rules - An array of validation rules to apply. | ||
* @throws {Error} If any validation rules fail, with a message detailing all failures. | ||
*/ | ||
export function validateAllProps<T>(scope: Construct, className: string, props: T, rules: ValidationRule<T>[]): void { | ||
const validationErrors = rules | ||
.filter(rule => rule.condition(props)) | ||
.map(rule => rule.message(props)); | ||
|
||
if (validationErrors.length > 0) { | ||
const errorMessage = `${className} initialization failed due to the following validation error(s):\n${validationErrors.map(error => `- ${error}`).join('\n')}`; | ||
throw new ValidationError(errorMessage, scope); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
packages/aws-cdk-lib/core/test/helpers-internal/validate-all-props.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { Construct } from 'constructs'; | ||
import { ValidationError } from '../../lib/errors'; | ||
import { validateAllProps, ValidationRule } from '../../lib/helpers-internal/validate-all-props'; | ||
|
||
class TestConstruct extends Construct { | ||
constructor() { | ||
super(undefined as any, 'TestConstruct'); | ||
} | ||
} | ||
|
||
describe('validateAllProps', () => { | ||
let testScope: Construct; | ||
|
||
beforeEach(() => { | ||
testScope = new TestConstruct(); | ||
}); | ||
|
||
it('should not throw an error when all validations pass', () => { | ||
const props = { value: 5 }; | ||
const rules: ValidationRule<typeof props>[] = [ | ||
{ | ||
condition: (p) => p.value < 0, | ||
message: (p) => `Value ${p.value} should be non-negative`, | ||
}, | ||
]; | ||
|
||
expect(() => validateAllProps(testScope, 'TestClass', props, rules)).not.toThrow(); | ||
}); | ||
|
||
it('should throw a ValidationError when a validation fails', () => { | ||
const props = { value: -5 }; | ||
const rules: ValidationRule<typeof props>[] = [ | ||
{ | ||
condition: (p) => p.value < 0, | ||
message: (p) => `Value ${p.value} should be non-negative`, | ||
}, | ||
]; | ||
|
||
expect(() => validateAllProps(testScope, 'TestClass', props, rules)).toThrow(ValidationError); | ||
}); | ||
|
||
it('should include all failed validation messages in the error', () => { | ||
const props = { value1: -5, value2: 15 }; | ||
const rules: ValidationRule<typeof props>[] = [ | ||
{ | ||
condition: (p) => p.value1 < 0, | ||
message: (p) => `Value1 ${p.value1} should be non-negative`, | ||
}, | ||
{ | ||
condition: (p) => p.value2 > 10, | ||
message: (p) => `Value2 ${p.value2} should be 10 or less`, | ||
}, | ||
]; | ||
|
||
expect(() => validateAllProps(testScope, 'TestClass', props, rules)).toThrow(ValidationError); | ||
try { | ||
validateAllProps(testScope, 'TestClass', props, rules); | ||
} catch (error) { | ||
if (error instanceof ValidationError) { | ||
expect(error.message).toBe( | ||
'TestClass initialization failed due to the following validation error(s):\n' + | ||
'- Value1 -5 should be non-negative\n' + | ||
'- Value2 15 should be 10 or less', | ||
); | ||
} | ||
} | ||
}); | ||
|
||
it('should work with complex object structures', () => { | ||
const props = { nested: { value: 'invalid' } }; | ||
const rules: ValidationRule<typeof props>[] = [ | ||
{ | ||
condition: (p) => p.nested.value !== 'valid', | ||
message: (p) => `Nested value "${p.nested.value}" is not valid`, | ||
}, | ||
]; | ||
|
||
expect(() => validateAllProps(testScope, 'TestClass', props, rules)).toThrow(ValidationError); | ||
try { | ||
validateAllProps(testScope, 'TestClass', props, rules); | ||
} catch (error) { | ||
if (error instanceof ValidationError) { | ||
expect(error.message).toBe( | ||
'TestClass initialization failed due to the following validation error(s):\n' + | ||
'- Nested value "invalid" is not valid', | ||
); | ||
} | ||
} | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💅🏻 nit: Might be nicer to limit the test exception to what the test is trying to test. Like for this specific test, do we really care of the other error and the prelude is included?
We could keep the existing ones and then have a single test case that checks all possible parallel rules
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way that the validations are implemented now, the prelude will always be included. Since we were only testing for
Performance Insights must be enabled for Aurora Limitless Database
, I could change the test to include a valid retention period. However, this seems kind of contrived... since a user is unlikely to include a retention period if they did not enable performance insights.