From 1f9d6c34d0b55c4d456de63e53c536ab9147262c Mon Sep 17 00:00:00 2001 From: Kendra Neil <53584728+TheRealAmazonKendra@users.noreply.github.com> Date: Wed, 20 Sep 2023 13:36:39 -0700 Subject: [PATCH 1/2] chore(migrate): add go and update tests --- packages/aws-cdk/lib/commands/migrate.ts | 4 +- packages/aws-cdk/package.json | 2 +- .../aws-cdk/test/commands/migrate.test.ts | 16 ++++ .../test-resources/stacks/S3Stack.java | 2 +- .../test/commands/test-resources/stacks/s3.go | 87 +++++++++++++++++++ .../test-resources/stacks/s3_stack.py | 4 +- yarn.lock | 8 +- 7 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 packages/aws-cdk/test/commands/test-resources/stacks/s3.go diff --git a/packages/aws-cdk/lib/commands/migrate.ts b/packages/aws-cdk/lib/commands/migrate.ts index bf1beae16f208..a1ca3248b9b35 100644 --- a/packages/aws-cdk/lib/commands/migrate.ts +++ b/packages/aws-cdk/lib/commands/migrate.ts @@ -53,7 +53,9 @@ export async function generateCdkApp(stackName: string, stack: string, language: case 'csharp': stackFileName = `${resolvedOutputPath}/src/${camelCase(formattedStackName, { pascalCase: true })}/${camelCase(formattedStackName, { pascalCase: true })}Stack.cs`; break; - // TODO: Add Go support + case 'go': + stackFileName = `${resolvedOutputPath}/${formattedStackName}.go`; + break; default: throw new Error(`${language} is not supported by CDK Migrate. Please choose from: ${MIGRATE_SUPPORTED_LANGUAGES.join(', ')}`); } diff --git a/packages/aws-cdk/package.json b/packages/aws-cdk/package.json index 158112185dfae..f96fd8abf52e4 100644 --- a/packages/aws-cdk/package.json +++ b/packages/aws-cdk/package.json @@ -105,7 +105,7 @@ "aws-sdk": "^2.1451.0", "camelcase": "^6.3.0", "cdk-assets": "0.0.0", - "cdk-from-cfn": "^0.11.0", + "cdk-from-cfn": "^0.17.0", "chalk": "^4", "chokidar": "^3.5.3", "decamelize": "^5.0.1", diff --git a/packages/aws-cdk/test/commands/migrate.test.ts b/packages/aws-cdk/test/commands/migrate.test.ts index 6e5416a5367c1..0b40011348e6f 100644 --- a/packages/aws-cdk/test/commands/migrate.test.ts +++ b/packages/aws-cdk/test/commands/migrate.test.ts @@ -98,6 +98,12 @@ describe('Migrate Function Tests', () => { expect(stack).toEqual(fs.readFileSync(path.join(...stackPath, 'S3Stack.cs'), 'utf8')); }); + // TODO: fix with actual go template + test('generateStack generates the expected stack string when called for go', () => { + const stack = generateStack(validTemplate, 'GoodGo', 'go'); + expect(stack).toEqual(fs.readFileSync(path.join(...stackPath, 's3.go'), 'utf8')); + }); + test('generateStack throws error when called for other language', () => { expect(() => generateStack(validTemplate, 'BadBadBad', 'php')).toThrowError('stack generation failed due to error \'unreachable\''); }); @@ -178,6 +184,16 @@ describe('Migrate Function Tests', () => { expect(replacedStack).toEqual(fs.readFileSync(path.join(...stackPath, 'S3Stack.cs'))); }); + cliTest('generatedCdkApp generates the expected cdk app when called for go', async (workDir) => { + const stack = generateStack(validTemplate, 'GoodGo', 'go'); + await generateCdkApp('GoodGo', stack, 'go', workDir); + + expect(fs.pathExists(path.join(workDir, 's3.go'))).toBeTruthy(); + const app = fs.readFileSync(path.join(workDir, 'GoodGo', 'good_go.go'), 'utf8').split('\n'); + expect(app.map(line => line.match(/func NewGoodGoStack\(scope constructs.Construct, id string, props GoodGoStackProps\) \*GoodGoStack \{/)).filter(line => line).length).toEqual(1); + expect(app.map(line => line.match(/ NewGoodGoStack\(app, "GoodGo", &GoodGoStackProps\{/))); + }); + cliTest('generatedCdkApp generates a zip file when --compress is used', async (workDir) => { const stack = generateStack(validTemplate, 'GoodTypeScript', 'typescript'); await generateCdkApp('GoodTypeScript', stack, 'typescript', workDir, true); diff --git a/packages/aws-cdk/test/commands/test-resources/stacks/S3Stack.java b/packages/aws-cdk/test/commands/test-resources/stacks/S3Stack.java index caf47dfcf3dbe..d41acc77d5211 100644 --- a/packages/aws-cdk/test/commands/test-resources/stacks/S3Stack.java +++ b/packages/aws-cdk/test/commands/test-resources/stacks/S3Stack.java @@ -1,4 +1,4 @@ -package com.acme.test.simple; +package com.myorg; import software.constructs.Construct; diff --git a/packages/aws-cdk/test/commands/test-resources/stacks/s3.go b/packages/aws-cdk/test/commands/test-resources/stacks/s3.go new file mode 100644 index 0000000000000..2dbcdc6b98a3f --- /dev/null +++ b/packages/aws-cdk/test/commands/test-resources/stacks/s3.go @@ -0,0 +1,87 @@ +package main + +import ( + cdk "github.com/aws/aws-cdk-go/awscdk/v2" + s3 "github.com/aws/aws-cdk-go/awscdk/v2/awss3" + "github.com/aws/constructs-go/constructs/v10" + "github.com/aws/jsii-runtime-go" +) + +type GoodGoStackProps struct { + cdk.StackProps +} + +/// AWS CloudFormation Sample Template S3_Website_Bucket_With_Retain_On_Delete: Sample template showing how to create a publicly accessible S3 bucket configured for website access with a deletion policy of retain on delete. +type GoodGoStack struct { + cdk.Stack + /// URL for website hosted on S3 + WebsiteUrl interface{} // TODO: fix to appropriate type + /// Name of S3 bucket to hold website content + S3BucketSecureUrl interface{} // TODO: fix to appropriate type +} + +func NewGoodGoStack(scope constructs.Construct, id string, props GoodGoStackProps) *GoodGoStack { + stack := cdk.NewStack(scope, &id, &props.StackProps) + + s3Bucket := s3.NewCfnBucket( + stack, + jsii.String("S3Bucket"), + &s3.CfnBucketProps{ + AccessControl: jsii.String("PublicRead"), + WebsiteConfiguration: &WebsiteConfiguration/* FIXME */{ + IndexDocument: jsii.String("index.html"), + ErrorDocument: jsii.String("error.html"), + }, + }, + ) + + return &GoodGoStack{ + Stack: stack, + WebsiteUrl: s3Bucket.AttrWebsiteUrl(), + S3BucketSecureUrl: cdk.Fn_Join(jsii.String(""), &[]*string{ + jsii.String("https://"), + s3Bucket.AttrDomainName(), + }), + } +} + +func main() { + defer jsii.Close() + + app := awscdk.NewApp(nil) + + NewGoodGoStack(app, "GoodGo", &GoodGoStackProps{ + awscdk.StackProps{ + Env: env(), + }, + }) + + app.Synth(nil) +} + +// env determines the AWS environment (account+region) in which our stack is to +// be deployed. For more information see: https://docs.aws.amazon.com/cdk/latest/guide/environments.html +func env() *awscdk.Environment { + // If unspecified, this stack will be "environment-agnostic". + // Account/Region-dependent features and context lookups will not work, but a + // single synthesized template can be deployed anywhere. + //--------------------------------------------------------------------------- + return nil + + // Uncomment if you know exactly what account and region you want to deploy + // the stack to. This is the recommendation for production stacks. + //--------------------------------------------------------------------------- + // return &awscdk.Environment{ + // Account: jsii.String("123456789012"), + // Region: jsii.String("us-east-1"), + // } + + // Uncomment to specialize this stack for the AWS Account and Region that are + // implied by the current CLI configuration. This is recommended for dev + // stacks. + //--------------------------------------------------------------------------- + // return &awscdk.Environment{ + // Account: jsii.String(os.Getenv("CDK_DEFAULT_ACCOUNT")), + // Region: jsii.String(os.Getenv("CDK_DEFAULT_REGION")), + // } +} diff --git a/packages/aws-cdk/test/commands/test-resources/stacks/s3_stack.py b/packages/aws-cdk/test/commands/test-resources/stacks/s3_stack.py index 327db5465456b..f62d028a050b3 100644 --- a/packages/aws-cdk/test/commands/test-resources/stacks/s3_stack.py +++ b/packages/aws-cdk/test/commands/test-resources/stacks/s3_stack.py @@ -30,14 +30,14 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: s3Bucket.cfnOptions.deletionPolicy = cdk.CfnDeletionPolicy.RETAIN # Outputs - self.website_u_r_l = s3Bucket.attrwebsiteUrl + self.website_u_r_l = s3Bucket.attr_website_u_r_l cdk.CfnOutput(self, 'WebsiteURL', description = 'URL for website hosted on S3', value = self.website_u_r_l, ) self.s3_bucket_secure_u_r_l = [ 'https://', - s3Bucket.attrdomainName, + s3Bucket.attr_domain_name, ].join('') cdk.CfnOutput(self, 'S3BucketSecureURL', description = 'Name of S3 bucket to hold website content', diff --git a/yarn.lock b/yarn.lock index 12188763f0acb..562ba4c48accc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5841,10 +5841,10 @@ case@1.6.3, case@^1.6.3: resolved "https://registry.npmjs.org/case/-/case-1.6.3.tgz#0a4386e3e9825351ca2e6216c60467ff5f1ea1c9" integrity sha512-mzDSXIPaFwVDvZAHqZ9VlbyF4yyXRuX6IvB06WvPYkqJVO24kX1PPhv9bfpKNFZyxYFmmgo03HUiD8iklmJYRQ== -cdk-from-cfn@^0.11.0: - version "0.11.0" - resolved "https://registry.npmjs.org/cdk-from-cfn/-/cdk-from-cfn-0.11.0.tgz#b294dcc886323195b1b891161cda843170eba535" - integrity sha512-Pk0Sfp0LT0XXTqAf+QRLwvcY2x/2bvm6JGwV6ABLvdJAFAQa1XsGaELkSRZqz1C2iB5vp8dVw0EC0v1x3c5bHQ== +cdk-from-cfn@^0.17.0: + version "0.17.0" + resolved "https://registry.npmjs.org/cdk-from-cfn/-/cdk-from-cfn-0.17.0.tgz#46f77dc1141c21ee99255cdda59a8a2d2d428755" + integrity sha512-7sJnvEfnlFhYdIih4XZfIpUFiIy0ogdLXHk8BZEaWdG1tSpUOV9HTmk5IvZJr6YfF1j1D21kvrGLZYZNsRr7HA== cdk-generate-synthetic-examples@^0.1.291: version "0.1.291" From 92ed75b62dbc7e3be5b32edf74e1a36cab5ec0f7 Mon Sep 17 00:00:00 2001 From: Kendra Neil <53584728+TheRealAmazonKendra@users.noreply.github.com> Date: Wed, 20 Sep 2023 13:47:56 -0700 Subject: [PATCH 2/2] pkglint --- packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES | 2 +- packages/aws-cdk/THIRD_PARTY_LICENSES | 2 +- packages/aws-cdk/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES b/packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES index 6ccc890a622f1..cbae80964d512 100644 --- a/packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES +++ b/packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES @@ -461,7 +461,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI ---------------- -** cdk-from-cfn@0.11.0 - https://www.npmjs.com/package/cdk-from-cfn/v/0.11.0 | MIT OR Apache-2.0 +** cdk-from-cfn@0.17.0 - https://www.npmjs.com/package/cdk-from-cfn/v/0.17.0 | MIT OR Apache-2.0 ---------------- diff --git a/packages/aws-cdk/THIRD_PARTY_LICENSES b/packages/aws-cdk/THIRD_PARTY_LICENSES index 1e3b66230d13f..21f06673cc7ff 100644 --- a/packages/aws-cdk/THIRD_PARTY_LICENSES +++ b/packages/aws-cdk/THIRD_PARTY_LICENSES @@ -461,7 +461,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI ---------------- -** cdk-from-cfn@0.11.0 - https://www.npmjs.com/package/cdk-from-cfn/v/0.11.0 | MIT OR Apache-2.0 +** cdk-from-cfn@0.17.0 - https://www.npmjs.com/package/cdk-from-cfn/v/0.17.0 | MIT OR Apache-2.0 ---------------- diff --git a/packages/aws-cdk/package.json b/packages/aws-cdk/package.json index f96fd8abf52e4..9ca544c2da322 100644 --- a/packages/aws-cdk/package.json +++ b/packages/aws-cdk/package.json @@ -82,7 +82,7 @@ "aws-cdk-lib": "0.0.0", "aws-sdk-mock": "5.6.0", "axios": "^0.27.2", - "cdk-from-cfn": "^0.11.0", + "cdk-from-cfn": "^0.17.0", "constructs": "^10.0.0", "fast-check": "^3.12.1", "jest": "^29.6.4",