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

hackathon Oct 2023 #3209

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions examples/typescript/building-blocks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.d.ts
*.js
node_modules
cdktf.out
cdktf.log
*terraform.*.tfstate*
.gen
.terraform
tsconfig.tsbuildinfo
!jest.config.js
!setup.js
87 changes: 87 additions & 0 deletions examples/typescript/building-blocks/__tests__/main-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) HashiCorp, Inc
// SPDX-License-Identifier: MPL-2.0
// import { Testing } from "cdktf";
// import "cdktf/lib/testing/adapters/jest";

describe("My CDKTF Application", () => {
it.todo("should be tested");

// // All Unit testst test the synthesised terraform code, it does not create real-world resources
// describe("Unit testing using assertions", () => {
// it("should contain a resource", () => {
// // import { Image,Container } from "./.gen/providers/docker"
// expect(
// Testing.synthScope((scope) => {
// new MyApplicationsAbstraction(scope, "my-app", {});
// })
// ).toHaveResource(Container);

// expect(
// Testing.synthScope((scope) => {
// new MyApplicationsAbstraction(scope, "my-app", {});
// })
// ).toHaveResourceWithProperties(Image, { name: "ubuntu:latest" });
// });
// });

// describe("Unit testing using snapshots", () => {
// it("Tests the snapshot", () => {
// const app = Testing.app();
// const stack = new TerraformStack(app, "test");

// new TestProvider(stack, "provider", {
// accessKey: "1",
// });

// new TestResource(stack, "test", {
// name: "my-resource",
// });

// expect(Testing.synth(stack)).toMatchSnapshot();
// });

// it("Tests a combination of resources", () => {
// expect(
// Testing.synthScope((stack) => {
// new TestDataSource(stack, "test-data-source", {
// name: "foo",
// });

// new TestResource(stack, "test-resource", {
// name: "bar",
// });
// })
// ).toMatchInlineSnapshot();
// });
// });

// describe("Checking validity", () => {
// it("check if the produced terraform configuration is valid", () => {
// const app = Testing.app();
// const stack = new TerraformStack(app, "test");

// new TestDataSource(stack, "test-data-source", {
// name: "foo",
// });

// new TestResource(stack, "test-resource", {
// name: "bar",
// });
// expect(Testing.fullSynth(app)).toBeValidTerraform();
// });

// it("check if this can be planned", () => {
// const app = Testing.app();
// const stack = new TerraformStack(app, "test");

// new TestDataSource(stack, "test-data-source", {
// name: "foo",
// });

// new TestResource(stack, "test-resource", {
// name: "bar",
// });
// expect(Testing.fullSynth(app)).toPlanSuccessfully();
// });
// });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { Construct } from "constructs";
import {
IamRolePolicyAttachment,
IamRolePolicyAttachmentConfig,
} from "../.gen/providers/aws/iam-role-policy-attachment";

import { IamRole } from "../.gen/providers/aws/iam-role";

/**
* A Building Block for IamRolePolicyAttachment
* @defaults Uses the following defaults:
* {
* "policyArn": "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
* }
*/
export class LambdaDBIamRolePolicyAttachment {
public defaultValues = {
policyArn:
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
};
public valueExtractorMap = {
role: (role: IamRole | any) => {
return role.name;
},
};
constructor(
scope: Construct,
id: string,
config: TypedIamRolePolicyAttachmentConfig
) {
const resourceConfig = this._convertConfig(config);

new IamRolePolicyAttachment(
scope,
id,
resourceConfig as IamRolePolicyAttachmentConfig
);
}

private _deepMerge(newConfig: any, defaultConfig: any): any {
if (!newConfig && !defaultConfig) {
return;
}
if (Array.isArray(newConfig) && Array.isArray(defaultConfig)) {
return [
{
...newConfig[0],
...defaultConfig[0],
},
this._deepMerge(newConfig.shift(), defaultConfig.shift()),
];
} else {
return {
...newConfig,
...defaultConfig,
};
}
}

private _convertConfig(typedConfig: any) {
const userSpecifiedConfig =
this._userSpecifiedAttributesConvert(typedConfig);
const newConfig = this._populateDefaultValues(userSpecifiedConfig);
return newConfig;
}

private _userSpecifiedAttributesConvert(typedConfig: any) {
if (!typedConfig) {
return;
}
type NewConfig = { [key: string]: any };
let newConfig: NewConfig = {};
for (const key in typedConfig) {
type TypedConfigKey = keyof typeof typedConfig;
const typeConfigKey = key as TypedConfigKey;
if (key in this.valueExtractorMap) {
type ValueExtractorKey = keyof typeof this.valueExtractorMap;
const valueExtractorKey = key as ValueExtractorKey;
newConfig[key] = this.valueExtractorMap[valueExtractorKey](
typedConfig[typeConfigKey]
);
}
// need to deal with the case that its an array as well
else if (typeof typedConfig[key] === "object") {
if (Array.isArray(typedConfig[key])) {
const attributeArray = [];
for (const attribute of typedConfig[key]) {
attributeArray.push(
this._userSpecifiedAttributesConvert(attribute)
);
}
newConfig[key] = attributeArray;
} else {
newConfig[key] = this._userSpecifiedAttributesConvert(
typedConfig[key]
);
}
} else {
newConfig[key] = typedConfig[key];
}
}
return newConfig;
}
// still isn't working correctly, first element of array is repeated
private _populateDefaultValues(userSpecifiedConfig: any) {
type NewConfig = { [key: string]: any };
let newConfig: NewConfig = userSpecifiedConfig;
for (const key in this.defaultValues) {
type DefaultValueKey = keyof typeof this.defaultValues;
const defaultValueKey = key as DefaultValueKey;
if (!newConfig.hasOwnProperty(key)) {
newConfig[key] = this.defaultValues[defaultValueKey];
} else if (
newConfig.hasOwnProperty(key) &&
typeof newConfig[key] === "object"
) {
newConfig[key] = this._deepMerge(
newConfig[key],
this.defaultValues[defaultValueKey]
);
}
}
return newConfig;
}
}

export interface TypedIamRolePolicyAttachmentConfig {
id?: string | undefined;
policyArn?: string;
role: IamRole;
}
19 changes: 19 additions & 0 deletions examples/typescript/building-blocks/cdktf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"language": "typescript",
"app": "npx ts-node main.ts",
"terraformProviders": [
"aws@~> 5.0"
],
"terraformModules": [
{
"name": "aurora",
"source": "terraform-aws-modules/rds-aurora/aws",
"version": "~> 8.0"
},
{
"name": "vpc",
"source": "terraform-aws-modules/vpc/aws",
"version": "~> 5.1.1"
}
]
}
34 changes: 34 additions & 0 deletions examples/typescript/building-blocks/help
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
========================================================================================================

Your cdktf typescript project is ready!

cat help Print this message

Compile:
npm run compile Compile typescript code to javascript (or "yarn watch")
npm run watch Watch for changes and compile typescript in the background
npm run build cdktf get and compile typescript

Synthesize:
cdktf synth Synthesize Terraform resources from stacks to cdktf.out/ (ready for 'terraform apply')

Diff:
cdktf diff Perform a diff (terraform plan) for the given stack

Deploy:
cdktf deploy Deploy the given stack

Destroy:
cdktf destroy Destroy the stack

Test:
npm run test Runs unit tests (edit __tests__/main-test.ts to add your own tests)
npm run test:watch Watches the tests and reruns them on change


Upgrades:
npm run get Import/update Terraform providers and modules (you should check-in this directory)
npm run upgrade Upgrade cdktf modules to latest version
npm run upgrade:next Upgrade cdktf modules to latest "@next" version (last commit)

========================================================================================================
16 changes: 16 additions & 0 deletions examples/typescript/building-blocks/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/

/*
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/

module.exports = {
clearMocks: true,
coverageProvider: "v8",
setupFilesAfterEnv: ["./setup.js"],
};

Loading
Loading