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

Dotnet terraform gcp core #390

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
91 changes: 91 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"settings": {
"cluster": {
"name": "",
"capacity_provider": {
"type": "fargate",
"fargate": {
"fargate_weight": 100,
"fargate_base": 0,
"fargate_spot_weight": 0
}
}
},
"service": {
"name": "",
"container_definitions": {
"image": "0000000000.dkr.ecr.eu-west-1.amazonaws.com/service-name",
"port": 3000
}
}
},
"systemSettings": {
"requireAuthenticationEntity": "false"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.prettierignore
.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
27 changes: 27 additions & 0 deletions plugins/dotnet-provisioning-terraform-aws-deployment-ecs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# @amplication/plugin-provisioning-terraform-aws-deployment-ecs

[![NPM Downloads](https://img.shields.io/npm/dt/@amplication/plugin-provisioning-terraform-aws-deployment-ecs)](https://www.npmjs.com/package/@amplication/plugin-provisioning-terraform-aws-deployment-ecs)

Adds terraform code for provisioning Amazon Web Services Elastic Container Service (ECS) as an addition to the 'core' terraform code base.

## Purpose

Adds terraform code for provisioning Amazon Web Services Elastic Container Service (ECS) as an addition to the 'core' terraform code base.

## Configuration

If a configuration is required, add it here.

## Scripts

### `build`

Running `npm run build` will bundle your plugin with Webpack for production.

### `dev`

Running `npm run dev` will watch your plugin's source code and automatically bundle it with every change.

## Usage

Explain the usage of this plugin and its effect on the final build.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@amplication/plugin-dotnet-provisioning-terraform-aws-deployment-ecs",
"version": "1.0.5",
"description": "Adds terraform code for provisioning Amazon Web Services core network infrastructure",
"main": "dist/index.js",
"nx": {},
"scripts": {
"prepublishOnly": "npm run build",
"dev": "webpack --watch",
"build": "webpack",
"prebuild": "rimraf dist"
},
"author": "Haim Bell",
"license": "Apache-2.0",
"dependencies": {},
"devDependencies": {
"@amplication/code-gen-types": "2.0.34",
"@amplication/code-gen-utils": "^0.0.9",
"@babel/parser": "^7.23.0",
"@babel/types": "^7.23.0",
"@types/lodash": "^4.14.200",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"copy-webpack-plugin": "^12.0.2",
"eslint": "^8.52.0",
"jest-mock-extended": "^3.0.5",
"lodash": "^4.17.21",
"prettier": "^2.6.2",
"rimraf": "^5.0.5",
"ts-loader": "^9.5.0",
"typescript": "^5.2.2",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4",
"webpack-node-externals": "^3.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"targets": {
"lint": {},
"npm:publish": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// generic constants
export const moduleNameEcsClusterKey = "${{ ECS_CLUSTER_MODULE_NAME }}";
export const moduleNameEcsServiceKey = "${{ ECS_SERVICE_MODULE_NAME }}";
export const moduleNameEcsAlbKey = "${{ ECS_ALB_MODULE_NAME }}";
export const moduleNameEcsSgKey = "${{ ECS_SG_MODULE_NAME }}";

// settings cluster constants
export const clusterHyphenNameKey = "${{ CLUSTER_NAME }}";
export const clusterUnderscoreNameKey = "${{ CLUSTER_NAME_UNDERSCORE }}";
export const clusterCapacityProviderKey = "${{ CLUSTER_CAPACITY_PROVIDER }}";

// settings service constants
export const serviceHyphenNameKey = "${{ SERVICE_NAME }}";
export const serviceUnderscoreNameKey = "${{ SERVICE_NAME_UNDERSCORE }}";
export const serviceContainerImage = "${{ SERVICE_CONTAINER_IMAGE }}";
export const serviceContainerPort = "${{ SERVICE_CONTAINER_PORT }}";
148 changes: 148 additions & 0 deletions plugins/dotnet-provisioning-terraform-aws-deployment-ecs/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import {
dotnetPluginEventsTypes,
dotnetPluginEventsParams as dotnet,
dotnetTypes,
FileMap,
IFile,
} from "@amplication/code-gen-types";
import { CodeBlock } from "@amplication/csharp-ast";
import {
clusterHyphenNameKey,
clusterUnderscoreNameKey,
moduleNameEcsClusterKey,
moduleNameEcsServiceKey,
moduleNameEcsAlbKey,
moduleNameEcsSgKey,
clusterCapacityProviderKey,
serviceContainerImage,
serviceContainerPort,
serviceHyphenNameKey,
serviceUnderscoreNameKey,
} from "./constants";
import { resolve } from "path";
import { getPluginSettings, getTerraformDirectory } from "./utils";
import { kebabCase, snakeCase } from "lodash";

class TerraformAwsDeploymentEcsPlugin implements dotnetTypes.AmplicationPlugin {
register(): dotnetPluginEventsTypes.DotnetEvents {
return {
LoadStaticFiles: {
after: this.afterLoadStaticFiles,
},
};
}
async afterLoadStaticFiles(
context: dotnetTypes.DsgContext,
eventParams: dotnet.LoadStaticFilesParams,
files: FileMap<CodeBlock>
): Promise<FileMap<CodeBlock>> {
context.logger.info(`Generating Terraform AWS Deployment ECS...`);

// get the name for the service, to be used as a fallback for the
// repository name
const serviceName = kebabCase(context.resourceInfo?.name);
if (!serviceName) {
throw new Error(
"TerraformAwsRepositoryEcrPlugin: Service name is undefined"
);
}

// instantiate a variable consisting of the path on the
// 'provisioning-terraform-aws-core' made up of the settings
// 'root_directory' & 'directory_name', this function will throw
// an error if the aforementioned plugin wasnt installed.
const terraformDirectory = getTerraformDirectory(
context.pluginInstallations,
context.serverDirectories.baseDirectory
);

// fetch the plugin specific settings and merge them with the defaults
const settings = getPluginSettings(context.pluginInstallations);

const templateFileName = "ecs-template.tf";
const fileNamePrefix = "ecs-";
const fileNameSuffix = ".tf";
const ecsServiceName: string = settings.service.name
? settings.service.name
: serviceName;
const ecsClusterName: string = settings.cluster.name
? settings.cluster.name
: serviceName;

const staticPath = resolve(__dirname, "./static");
const staticFiles = await context.utils.importStaticFiles(
staticPath,
terraformDirectory
);

// switch statement for determining capacity provider within the
// cluster configuration, default to fargate
let capacityProvider: string;
switch (settings.cluster.capacity_provider.type) {
default: {
capacityProvider = `fargate_capacity_providers = {
FARGATE = {
default_capacity_provider_strategy = {
weight = ${settings.cluster.capacity_provider.fargate?.fargate_weight}
base = ${settings.cluster.capacity_provider.fargate?.fargate_base}
}
}
FARGATE_SPOT = {
default_capacity_provider_strategy = {
weight = ${settings.cluster.capacity_provider.fargate?.fargate_spot_weight}
}
}
}`;
}
}

const hyphenServiceName: string = kebabCase(ecsServiceName);
const underscoreServiceName: string = snakeCase(ecsServiceName);
const hyphenClusterName: string = kebabCase(ecsClusterName);
const underscoreClusterName: string = snakeCase(ecsClusterName);
for (const item of staticFiles.getAll()) {
const newPath = item.path.replace(
templateFileName,
fileNamePrefix + kebabCase(serviceName) + fileNameSuffix
);

const newCode = item.code
.replaceAll(clusterHyphenNameKey, hyphenClusterName)
.replaceAll(clusterUnderscoreNameKey, underscoreClusterName)
.replaceAll(serviceHyphenNameKey, hyphenServiceName)
.replaceAll(serviceUnderscoreNameKey, underscoreServiceName)
.replaceAll(
moduleNameEcsClusterKey,
"ecs_cluster_" + underscoreClusterName
)
.replaceAll(
moduleNameEcsServiceKey,
"ecs_service_" + underscoreServiceName
)
.replaceAll(moduleNameEcsAlbKey, "ecs_alb_" + underscoreServiceName)
.replaceAll(moduleNameEcsSgKey, "ecs_alb_sg_" + underscoreServiceName)
.replaceAll(clusterCapacityProviderKey, capacityProvider)
.replaceAll(
serviceContainerImage,
settings.service.container_definitions.image
)
.replaceAll(
serviceContainerPort,
String(settings.service.container_definitions.port)
);
const file: IFile<CodeBlock> = {
path: newPath,
code: new CodeBlock({
code: newCode,
}),
};
files.set(file);
}

context.logger.info(`Generated Terraform AWS Deployment ECS...`);

return files;
}
}

export default TerraformAwsDeploymentEcsPlugin;
Loading
Loading