Skip to content

Commit

Permalink
chore(migrate): add go and update tests (#27226)
Browse files Browse the repository at this point in the history
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
TheRealAmazonKendra authored Sep 20, 2023
1 parent a2a4f68 commit a32674a
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES
Original file line number Diff line number Diff line change
Expand Up @@ -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

----------------

Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/THIRD_PARTY_LICENSES
Original file line number Diff line number Diff line change
Expand Up @@ -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

----------------

Expand Down
4 changes: 3 additions & 1 deletion packages/aws-cdk/lib/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')}`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
16 changes: 16 additions & 0 deletions packages/aws-cdk/test/commands/migrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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\'');
});
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.acme.test.simple;
package com.myorg;

import software.constructs.Construct;

Expand Down
87 changes: 87 additions & 0 deletions packages/aws-cdk/test/commands/test-resources/stacks/s3.go
Original file line number Diff line number Diff line change
@@ -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")),
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a32674a

Please sign in to comment.