Skip to content

Commit

Permalink
chore(aws-ec2): run aws-sdk-js-codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Aug 3, 2023
1 parent 03c6f6f commit 2ce84cf
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 48 deletions.
40 changes: 20 additions & 20 deletions packages/aws-cdk-lib/aws-ec2/lib/cfn-init-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ export interface InitCommandOptions {
export abstract class InitCommandWaitDuration {
/** Wait for a specified duration after a command. */
public static of(duration: Duration): InitCommandWaitDuration {
return new class extends InitCommandWaitDuration {
return new (class extends InitCommandWaitDuration {
/** @internal */
public _render() { return duration.toSeconds(); }
}();
})();
}

/** Do not wait for this command. */
Expand Down Expand Up @@ -330,7 +330,7 @@ export abstract class InitFile extends InitElement {
if (!content) {
throw new Error(`InitFile ${fileName}: cannot create empty file. Please supply at least one character of content.`);
}
return new class extends InitFile {
return new (class extends InitFile {
protected _doBind(bindOptions: InitBindOptions) {
return {
config: this._standardConfig(options, bindOptions.platform, {
Expand All @@ -339,7 +339,7 @@ export abstract class InitFile extends InitElement {
}),
};
}
}(fileName, options);
})(fileName, options);
}

/**
Expand All @@ -359,15 +359,15 @@ export abstract class InitFile extends InitElement {
* May contain tokens.
*/
public static fromObject(fileName: string, obj: Record<string, any>, options: InitFileOptions = {}): InitFile {
return new class extends InitFile {
return new (class extends InitFile {
protected _doBind(bindOptions: InitBindOptions) {
return {
config: this._standardConfig(options, bindOptions.platform, {
content: obj,
}),
};
}
}(fileName, options);
})(fileName, options);
}

/**
Expand All @@ -388,22 +388,22 @@ export abstract class InitFile extends InitElement {
* Download from a URL at instance startup time
*/
public static fromUrl(fileName: string, url: string, options: InitFileOptions = {}): InitFile {
return new class extends InitFile {
return new (class extends InitFile {
protected _doBind(bindOptions: InitBindOptions) {
return {
config: this._standardConfig(options, bindOptions.platform, {
source: url,
}),
};
}
}(fileName, options);
})(fileName, options);
}

/**
* Download a file from an S3 bucket at instance startup time
*/
public static fromS3Object(fileName: string, bucket: s3.IBucket, key: string, options: InitFileOptions = {}): InitFile {
return new class extends InitFile {
return new (class extends InitFile {
protected _doBind(bindOptions: InitBindOptions) {
bucket.grantRead(bindOptions.instanceRole, key);
return {
Expand All @@ -413,7 +413,7 @@ export abstract class InitFile extends InitElement {
authentication: standardS3Auth(bindOptions.instanceRole, bucket.bucketName),
};
}
}(fileName, options);
})(fileName, options);
}

/**
Expand All @@ -422,7 +422,7 @@ export abstract class InitFile extends InitElement {
* This is appropriate for files that are too large to embed into the template.
*/
public static fromAsset(targetFileName: string, path: string, options: InitFileAssetOptions = {}): InitFile {
return new class extends InitFile {
return new (class extends InitFile {
protected _doBind(bindOptions: InitBindOptions) {
const asset = new s3_assets.Asset(bindOptions.scope, `${targetFileName}Asset`, {
path,
Expand All @@ -438,14 +438,14 @@ export abstract class InitFile extends InitElement {
assetHash: asset.assetHash,
};
}
}(targetFileName, options);
})(targetFileName, options);
}

/**
* Use a file from an asset at instance startup time
*/
public static fromExistingAsset(targetFileName: string, asset: s3_assets.Asset, options: InitFileOptions = {}): InitFile {
return new class extends InitFile {
return new (class extends InitFile {
protected _doBind(bindOptions: InitBindOptions) {
asset.grantRead(bindOptions.instanceRole);
return {
Expand All @@ -456,7 +456,7 @@ export abstract class InitFile extends InitElement {
assetHash: asset.assetHash,
};
}
}(targetFileName, options);
})(targetFileName, options);
}

public readonly elementType = InitElementType.FILE.toString();
Expand Down Expand Up @@ -923,7 +923,7 @@ export abstract class InitSource extends InitElement {
* Extract an archive stored in an S3 bucket into the given directory
*/
public static fromS3Object(targetDirectory: string, bucket: s3.IBucket, key: string, options: InitSourceOptions = {}): InitSource {
return new class extends InitSource {
return new (class extends InitSource {
protected _doBind(bindOptions: InitBindOptions) {
bucket.grantRead(bindOptions.instanceRole, key);

Expand All @@ -932,14 +932,14 @@ export abstract class InitSource extends InitElement {
authentication: standardS3Auth(bindOptions.instanceRole, bucket.bucketName),
};
}
}(targetDirectory, options.serviceRestartHandles);
})(targetDirectory, options.serviceRestartHandles);
}

/**
* Create an InitSource from an asset created from the given path.
*/
public static fromAsset(targetDirectory: string, path: string, options: InitSourceAssetOptions = {}): InitSource {
return new class extends InitSource {
return new (class extends InitSource {
protected _doBind(bindOptions: InitBindOptions) {
const asset = new s3_assets.Asset(bindOptions.scope, `${targetDirectory}Asset`, {
path,
Expand All @@ -953,14 +953,14 @@ export abstract class InitSource extends InitElement {
assetHash: asset.assetHash,
};
}
}(targetDirectory, options.serviceRestartHandles);
})(targetDirectory, options.serviceRestartHandles);
}

/**
* Extract a directory from an existing directory asset.
*/
public static fromExistingAsset(targetDirectory: string, asset: s3_assets.Asset, options: InitSourceOptions = {}): InitSource {
return new class extends InitSource {
return new (class extends InitSource {
protected _doBind(bindOptions: InitBindOptions) {
asset.grantRead(bindOptions.instanceRole);

Expand All @@ -970,7 +970,7 @@ export abstract class InitSource extends InitElement {
assetHash: asset.assetHash,
};
}
}(targetDirectory, options.serviceRestartHandles);
})(targetDirectory, options.serviceRestartHandles);
}

public readonly elementType = InitElementType.SOURCE.toString();
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-ec2/lib/cfn-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ function deepMerge(target?: Record<string, any>, src?: Record<string, any>) {
throw new Error(`Trying to merge array [${value}] into a non-array '${target[key]}'`);
}
target[key] = Array.from(new Set([
...target[key] ?? [],
...(target[key] ?? []),
...value,
]));
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { EC2 } from 'aws-sdk';

const ec2 = new EC2();

import AWS_EC2, { EC2 } from "@aws-sdk/client-ec2";

const ec2 = new EC2({});

/**
* The default security group ingress rule. This can be used to both revoke and authorize the rules
*/
function ingressRuleParams(groupId: string, account: string): EC2.RevokeSecurityGroupIngressRequest | EC2.AuthorizeSecurityGroupIngressRequest {
function ingressRuleParams(groupId: string, account: string): AWS_EC2.RevokeSecurityGroupIngressCommandInput | AWS_EC2.AuthorizeSecurityGroupIngressCommandInput {
return {
GroupId: groupId,
IpPermissions: [{
Expand All @@ -22,7 +24,7 @@ function ingressRuleParams(groupId: string, account: string): EC2.RevokeSecurity
/**
* The default security group egress rule. This can be used to both revoke and authorize the rules
*/
function egressRuleParams(groupId: string): EC2.RevokeSecurityGroupEgressRequest | EC2.AuthorizeSecurityGroupEgressRequest {
function egressRuleParams(groupId: string): AWS_EC2.RevokeSecurityGroupEgressCommandInput | AWS_EC2.AuthorizeSecurityGroupEgressCommandInput {
return {
GroupId: groupId,
IpPermissions: [{
Expand Down Expand Up @@ -67,16 +69,16 @@ async function onUpdate(event: AWSLambda.CloudFormationCustomResourceUpdateEvent
* Revoke both ingress and egress rules
*/
async function revokeRules(groupId: string, account: string): Promise<void> {
await ec2.revokeSecurityGroupEgress(egressRuleParams(groupId)).promise();
await ec2.revokeSecurityGroupIngress(ingressRuleParams(groupId, account)).promise();
await ec2.revokeSecurityGroupEgress(egressRuleParams(groupId));
await ec2.revokeSecurityGroupIngress(ingressRuleParams(groupId, account));
return;
}

/**
* Authorize both ingress and egress rules
*/
async function authorizeRules(groupId: string, account: string): Promise<void> {
await ec2.authorizeSecurityGroupIngress(ingressRuleParams(groupId, account)).promise();
await ec2.authorizeSecurityGroupEgress(egressRuleParams(groupId)).promise();
await ec2.authorizeSecurityGroupIngress(ingressRuleParams(groupId, account));
await ec2.authorizeSecurityGroupEgress(egressRuleParams(groupId));
return;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import { ACM } from 'aws-sdk'; // eslint-disable-line import/no-extraneous-dependencies
import { ACM } from "@aws-sdk/client-acm";

const acm = new ACM();
const acm = new ACM({});

export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent) {
switch (event.RequestType) {
Expand All @@ -12,7 +12,7 @@ export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent
Certificate: fs.readFileSync('./server.crt'),
PrivateKey: fs.readFileSync('./server.key'),
CertificateChain: fs.readFileSync('./ca.crt'),
}).promise();
});
}

let clientImport;
Expand All @@ -21,7 +21,7 @@ export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent
Certificate: fs.readFileSync('./client1.domain.tld.crt'),
PrivateKey: fs.readFileSync('./client1.domain.tld.key'),
CertificateChain: fs.readFileSync('./ca.crt'),
}).promise();
});
}

return {
Expand All @@ -36,12 +36,12 @@ export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent
if (event.ResourceProperties.ServerCertificateArn) {
await acm.deleteCertificate({
CertificateArn: event.ResourceProperties.ServerCertificateArn,
}).promise();
});
}
if (event.ResourceProperties.ClientCertificateArn) {
await acm.deleteCertificate({
CertificateArn: event.ResourceProperties.ClientCertificateArn,
}).promise();
});
}
return;
}
Expand Down
18 changes: 5 additions & 13 deletions packages/aws-cdk-lib/aws-ec2/test/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1417,39 +1417,31 @@ describe('volume', () => {
availabilityZone: 'us-east-1a',
size: cdk.Size.gibibytes(min - 1),
volumeType,
...iops
? { iops }
: {},
...(iops ? { iops } : {}),
});
}).toThrow(/volumes must be between/);
expect(() => {
new Volume(stack, `Volume${idx++}`, {
availabilityZone: 'us-east-1a',
size: cdk.Size.gibibytes(min),
volumeType,
...iops
? { iops }
: {},
...(iops ? { iops } : {}),
});
}).not.toThrow();
expect(() => {
new Volume(stack, `Volume${idx++}`, {
availabilityZone: 'us-east-1a',
size: cdk.Size.gibibytes(max),
volumeType,
...iops
? { iops }
: {},
...(iops ? { iops } : {}),
});
}).not.toThrow();
expect(() => {
new Volume(stack, `Volume${idx++}`, {
availabilityZone: 'us-east-1a',
size: cdk.Size.gibibytes(max + 1),
volumeType,
...iops
? { iops }
: {},
...(iops ? { iops } : {}),
});
}).toThrow(/volumes must be between/);
}
Expand Down Expand Up @@ -1480,7 +1472,7 @@ describe('volume', () => {
availabilityZone: 'us-east-1a',
size: cdk.Size.gibibytes(125),
volumeType,
...iops ? { iops }: {},
...(iops ? { iops } : {}),
throughput: 125,
});
}).toThrow(/throughput property requires volumeType: EbsDeviceVolumeType.GP3/);
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
"@aws-sdk/client-account": "^3.382.0",
"@aws-sdk/client-codepipeline": "^3.382.0",
"@aws-sdk/client-dynamodb": "^3.382.0",
"@aws-sdk/client-ec2": "^3.382.0",
"@aws-sdk/client-ecr": "^3.382.0",
"@aws-sdk/client-eks": "^3.382.0",
"@aws-sdk/client-lambda": "^3.382.0",
Expand Down

0 comments on commit 2ce84cf

Please sign in to comment.