Skip to content

Commit

Permalink
feat: remove enable property
Browse files Browse the repository at this point in the history
  • Loading branch information
badmintoncryer committed Mar 5, 2024
1 parent f3089ac commit b545233
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 65 deletions.
21 changes: 7 additions & 14 deletions packages/aws-cdk-lib/aws-efs/lib/efs-file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,6 @@ export interface FileSystemAttributes {
* Replication configuration for the file system.
*/
export interface ReplicationConfiguration {
/**
* Whether to enable automatic replication.
*
* Other replication settings(`destinationFileSystem`, `kmsKey`, `region`, `az`) cannot be set if this is set to false.
*/
readonly enable: boolean;

/**
* The existing destination file system for the replication.
*
Expand Down Expand Up @@ -606,8 +599,8 @@ export class FileSystem extends FileSystemBase {
throw new Error('ThroughputMode ELASTIC is not supported for file systems with performanceMode MAX_IO');
}

const { destinationFileSystem, region, availabilityZone, kmsKey, enable } = props.replicationConfiguration ?? {};
if (enable) {
const { destinationFileSystem, region, availabilityZone, kmsKey } = props.replicationConfiguration ?? {};
if (props.replicationConfiguration) {
if (props.replicationOverwriteProtection === ReplicationOverwriteProtection.DISABLED) {
throw new Error('Cannot configure `replicationConfiguration` when `replicationOverwriteProtection` is set to `DISABLED`');
}
Expand All @@ -619,10 +612,10 @@ export class FileSystem extends FileSystemBase {
if (region && !Token.isUnresolved(region) && !/^[a-z]{2}-((iso[a-z]{0,1}-)|(gov-)){0,1}[a-z]+-{0,1}[0-9]{0,1}$/.test(region)) {
throw new Error('`replicationConfiguration.region` is invalid.');
}
}

if (enable === false && (destinationFileSystem || region || availabilityZone || kmsKey)) {
throw new Error('Cannot configure replication when `replicationConfiguration.enableReplication` is set to `false`');
if (availabilityZone && !Token.isUnresolved(availabilityZone) && !region) {
throw new Error('`replicationConfiguration.availabilityZone` cannot be specified without `replicationConfiguration.region`');
}
}

// we explictly use 'undefined' to represent 'false' to maintain backwards compatibility since
Expand Down Expand Up @@ -651,12 +644,12 @@ export class FileSystem extends FileSystemBase {
replicationOverwriteProtection: props.replicationOverwriteProtection,
} : undefined;

const replicationConfiguration = enable ? {
const replicationConfiguration = props.replicationConfiguration ? {
destinations: [
{
fileSystemId: destinationFileSystem?.fileSystemId,
kmsKeyId: kmsKey?.keyArn,
region: destinationFileSystem ? destinationFileSystem.env.region : (region ?? Stack.of(this).region)
region: destinationFileSystem ? destinationFileSystem.env.region : (region ?? Stack.of(this).region),
availabilityZoneName: availabilityZone,
},
],
Expand Down
60 changes: 9 additions & 51 deletions packages/aws-cdk-lib/aws-efs/test/efs-file-system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,9 +964,7 @@ describe('replication configuration', () => {
// WHEN
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
enable: true,
},
replicationConfiguration: {},
});

// THEN
Expand All @@ -993,7 +991,6 @@ describe('replication configuration', () => {
vpc,
replicationConfiguration: {
destinationFileSystem: destination,
enable: true,
},
});

Expand All @@ -1016,7 +1013,6 @@ describe('replication configuration', () => {
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
enable: true,
kmsKey: new kms.Key(stack, 'customKey'),
region: 'us-east-1',
availabilityZone: 'us-east-1a',
Expand Down Expand Up @@ -1048,7 +1044,7 @@ describe('replication configuration', () => {
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
enable: true,
region: 'us-east-1',
},
replicationOverwriteProtection: ReplicationOverwriteProtection.DISABLED,
});
Expand All @@ -1071,7 +1067,6 @@ describe('replication configuration', () => {
vpc,
replicationConfiguration: {
destinationFileSystem: destination,
enable: true,
...config,
},
});
Expand All @@ -1091,71 +1086,34 @@ describe('replication configuration', () => {
vpc,
replicationConfiguration: {
destinationFileSystem: destination,
enable: true,
kmsKey: new kms.Key(stack, 'customKey'),
},
});
}).toThrow('Cannot configure `replicationConfiguration.region`, `replicationConfiguration.az` or `replicationConfiguration.kmsKey` when `replicationConfiguration.destinationFileSystem` is set');
});

test.each([
{ region: 'us-east-1' },
{ availabilityZone: 'us-east-1a' },
])('throw error when configure replication settings for replication disabled file system', (config) => {
// THEN
expect(() => {
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
enable: false,
...config,
},
});
}).toThrow('Cannot configure replication when `replicationConfiguration.enableReplication` is set to `false`');
});

test('throw error when configure kmsKey for replication disabled file system', () => {
// THEN
expect(() => {
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
enable: false,
kmsKey: new kms.Key(stack, 'customKey'),
},
});
}).toThrow('Cannot configure replication when `replicationConfiguration.enableReplication` is set to `false`');
});

test('throw error when configure destinationFileSystem for replication disabled file system', () => {
// WHEN
const destination = new FileSystem(stack, 'DestinationFileSystem', {
vpc,
replicationOverwriteProtection: ReplicationOverwriteProtection.DISABLED,
});

test('throw error for invalid region', () => {
// THEN
expect(() => {
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
enable: false,
destinationFileSystem: destination,
enable: true,
region: 'invalid-region',
},
});
}).toThrow('Cannot configure replication when `replicationConfiguration.enableReplication` is set to `false`');
}).toThrow('`replicationConfiguration.region` is invalid.');
});

test('throw error for invalid region', () => {
test('throw error for specifying availabilityZone without region', () => {
// THEN
expect(() => {
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
enable: true,
region: 'invalid-region',
availabilityZone: 'us-east-1a',
},
});
}).toThrow('`replicationConfiguration.region` is invalid.');
}).toThrow('`replicationConfiguration.availabilityZone` cannot be specified without `replicationConfiguration.region`');
});
});

0 comments on commit b545233

Please sign in to comment.