Skip to content
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

feat: Add experimental AwsTerraformAdapter to support interoperability with AWS CDK constructs #1123

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
d8f05df
feat(cli): start building support for nested_type in generator
ansgarm Aug 30, 2021
f34fc5a
feat(cli): support `optional`, `required` and `computed` on attribute…
ansgarm Sep 2, 2021
e77bd3d
feat(cli): support nested_type for computed attributes
ansgarm Sep 6, 2021
f2f7250
tests(cli): update snapshot tests
ansgarm Sep 29, 2021
95dbea5
tests(cli): add testcase for nested_type
ansgarm Sep 29, 2021
f443498
chore(cli): remove unsupported nesting type from type definition
ansgarm Sep 29, 2021
4157ceb
fix(cli): trying to fix stack size error by increasing stack size
ansgarm Sep 29, 2021
4cd1c30
fix(cli): remove obsolete nesting mode and simplify code
ansgarm Sep 29, 2021
540846a
fix(cli): trying another way to fix stack size error by increasing st…
ansgarm Sep 29, 2021
9119236
chore(cli): revert stack size setting, as that did not work
ansgarm Sep 29, 2021
129f625
test: move generator test file to new separate generator package
ansgarm Oct 5, 2021
4cd0dd9
tests: remove obsolete snapshots
ansgarm Oct 5, 2021
d438059
chore: add aws-adapter pr lint scope
ansgarm Oct 5, 2021
ecfeed2
feat(cli): start building support for nested_type in generator
ansgarm Aug 30, 2021
fe32fc1
feat(cli): support `optional`, `required` and `computed` on attribute…
ansgarm Sep 2, 2021
b6da196
chore: scaffold package @cdktf/aws-adapter
ansgarm Sep 23, 2021
60639eb
fix: add empty index file for TS to build
ansgarm Sep 23, 2021
f40e58b
feat: migrate code from proof of concept codebase to aws-adapter package
ansgarm Sep 23, 2021
1f09545
feat: switch to @cdktf/provider-aws instead of generated provider bin…
ansgarm Sep 23, 2021
98eb45e
add .gen/providers/awscc for now as it not public yet
ansgarm Sep 23, 2021
98b9868
fix: add missing package.sh file
ansgarm Sep 23, 2021
1e247ad
chore: update yarn.lock
ansgarm Sep 24, 2021
617ee90
chore: add two examples. support iam-policy. configure packages
ansgarm Sep 27, 2021
faa7599
tests: commit snapshot test result for step functions example
ansgarm Sep 27, 2021
9476f80
chore: update todo comments, extract common type, workaround for awsc…
ansgarm Sep 28, 2021
4232a4c
chore: support another intrinsic function, continue implementation
ansgarm Sep 29, 2021
46fd9ba
chore: setup jsii, adjust code to work with jsii
ansgarm Sep 30, 2021
788209b
fix: remove generated build output for awscc provider
ansgarm Sep 30, 2021
d7b6da5
feat: continue implementing aws-adapter intrinsic function support
ansgarm Oct 4, 2021
aca6614
chore: remove unused code
ansgarm Oct 5, 2021
a07d1ce
chore: update comments, remove logs
ansgarm Oct 5, 2021
9a2e219
chore: update tests, fix implementation of Fn::Sub and others
ansgarm Oct 6, 2021
1fae4b2
feat: improve tests and implementation of intrinsic function support
ansgarm Oct 7, 2021
8e617a1
feat(cdktf): Support markAsInner on Lazy values
ansgarm Oct 7, 2021
681a830
feat: start adding support for Conditions
ansgarm Oct 7, 2021
63abd3a
feat(aws-adapter): finish implementing conditions support
ansgarm Oct 8, 2021
4772a40
test(lib): add tests for new expressions, fix jsii compilation for ex…
ansgarm Oct 11, 2021
14bf721
fix(lib): fix count being IResolvable as well
ansgarm Oct 11, 2021
8ceb374
chore: remove files from aws-adapter as they move to a separate repo
ansgarm Oct 11, 2021
8fb8c8b
chore: remove generated bindings for awscc for now
ansgarm Oct 11, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/pr-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
hcl2json
hcl2cdk
provider-generator
aws-adapter
tests
examples
readme
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"packages": [
"packages/*",
"packages/@cdktf/*",
"packages/@cdktf/aws-adapter/examples/*",
"examples/*",
"examples/python/*",
"examples/typescript/*",
Expand Down
15 changes: 15 additions & 0 deletions packages/@cdktf/aws-adapter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
**/*.d.ts.map
**/*.js.map
tsconfig.tsbuildinfo
!tsconfig.json
*.d.ts
*.js
cdktf.out
cdktf.log
.terraform
coverage
!lib/awscc_schemas/schemas.js
!jest.config.js
!setupJest.js
.yalc
yalc.lock
14 changes: 14 additions & 0 deletions packages/@cdktf/aws-adapter/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

# Exclude typescript source and config
*.ts
tsconfig.json

# Include javascript files and typescript declarations
!*.js
!*.d.ts

# Exclude jsii outdir
dist

# Include .jsii
!.jsii
1 change: 1 addition & 0 deletions packages/@cdktf/aws-adapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# aws-adapter
17 changes: 17 additions & 0 deletions packages/@cdktf/aws-adapter/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/

module.exports = {
clearMocks: true,
coverageProvider: "v8",
setupFilesAfterEnv: ["./setupJest.js"],
// collectCoverage: true,
testMatch: ["**/*.test.ts"],
testPathIgnorePatterns: ["/node_modules/", "<rootDir>/examples"],
transform: {
"^.+\\.tsx?$": "ts-jest",
},
moduleFileExtensions: ["js", "ts", "tsx"],
};
121 changes: 121 additions & 0 deletions packages/@cdktf/aws-adapter/lib/obsolete-aws-terraform-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { Construct, IConstruct } from "constructs";

import {
Aspects as AWSAspects,
IAspect as IAWSAspect,
Annotations as AWSAnnotations,
Stack,
CfnElement,
} from "aws-cdk-lib";

import { Aspects } from "cdktf";
import { addCustomSynthesis } from "cdktf/lib/synthesize/synthesizer"; // NOTE: these are internals

// TODO: walk through this file, migrate todos and delete this afterwards
export class ObsoleteAwsTerraformAdapter extends Stack {
constructor(_scope: Construct, id: string) {
super(undefined, id);

// adds an Aspect to the parent hosting this Adapter which will invoke
// All AWSAspects on children of this Adapter
Aspects.of(_scope).add({
visit: (node) => {
if (node === _scope) {
invokeAWSAspects(this);
this.addResources();
}
// console.log("CDKTF ASPECT", node.node.path, {
// isCfnElement: CfnElement.isCfnElement(node),
// });
},
});

addCustomSynthesis(_scope, { onSynthesize: this._onSynth.bind(this) });
}

private addResources() {
const elements = cfnElements(this);
// TODO: maybe we want to gather intelligence on the resolve tokens before resolving to get the underlying
// dependencies from it and to reuse them when creating TF resources instead of needing to parse them from
// their resulting CloufFormation representation like Fn:Join, Fn:GetAtt etc.
const fragments = elements.map((e) => this.resolve(e._toCloudFormation()));
console.log(JSON.stringify(fragments, null, 2));
}

_onSynth(/*session: ISynthesisSession*/) {
console.log("AWS Adapter: custom synthesis");
// TODO: find out if we need this at all.. I think we don't as we are going to
// convert everything to CDKTF first and then every work will happen there
// maybe we can use this to let the synth fail or something
// but that could also be done in the conversion already
// so while this is cool we might not need it at all and this is fine, too
}
}

/**
* Invoke aspects on the given construct tree.
*
* originally from https://github.com/aws/aws-cdk/blob/dcae3eead0dbf9acb1ed80ba95bb104c64cb1bd7/packages/@aws-cdk/core/lib/private/synthesis.ts#L99-L137
*/
function invokeAWSAspects(root: IConstruct) {
const invokedByPath: { [nodePath: string]: IAWSAspect[] } = {};

let nestedAspectWarning = false;
recurse(root, []);

console.log(`invoked AWSAspects: `, invokedByPath);

function recurse(construct: IConstruct, inheritedAspects: IAWSAspect[]) {
const node = construct.node;
const aspects = AWSAspects.of(construct);
const allAspectsHere = [...(inheritedAspects ?? []), ...aspects.all];
const nodeAspectsCount = aspects.all.length;
for (const aspect of allAspectsHere) {
let invoked = invokedByPath[node.path];
if (!invoked) {
invoked = invokedByPath[node.path] = [];
}

if (invoked.includes(aspect)) {
continue;
}

aspect.visit(construct);

// if an aspect was added to the node while invoking another aspect it will not be invoked, emit a warning
// the `nestedAspectWarning` flag is used to prevent the warning from being emitted for every child
if (!nestedAspectWarning && nodeAspectsCount !== aspects.all.length) {
AWSAnnotations.of(construct).addWarning(
"We detected an Aspect was added via another Aspect, and will not be applied"
);
nestedAspectWarning = true;
}

// mark as invoked for this node
invoked.push(aspect);
}

for (const child of construct.node.children) {
recurse(child, allAspectsHere);
}
}
}

function cfnElements(node: IConstruct, into: CfnElement[] = []): CfnElement[] {
if (CfnElement.isCfnElement(node)) {
into.push(node);
}

for (const child of node.node.children) {
// Don't recurse into a substack
if (Stack.isStack(child)) {
throw new Error(
"Encountered nested stack in AWS CDK construct which is not supported yet by the CDK for Terraform to convert"
);
}

cfnElements(child, into);
}

return into;
}
Loading