Skip to content

Commit

Permalink
use naming templates for terraform resources
Browse files Browse the repository at this point in the history
  • Loading branch information
djelusic committed Nov 30, 2021
1 parent 3caefba commit 3792675
Show file tree
Hide file tree
Showing 32 changed files with 158 additions and 77 deletions.
2 changes: 2 additions & 0 deletions cli/controller/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ func (d *Deploy) backendRequest() dto.DeployRequest {
ResourceTags: d.stage.ResourceTags(),
WsEnv: d.stage.WsEnv(),
HasPublic: d.stage.HasPublic(),
NamingTemplate: d.stage.ResourceNamingTemplate(),
PublicBucketName: d.stage.PublicBucketName(),
}
}
return req
Expand Down
2 changes: 2 additions & 0 deletions cli/controller/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (c *Setup) create(n *domain.Node) error {
FunctionsPath: n.Functions.Path,
AuthEnv: n.AuthEnv(),
ResourceSuffix: n.ResourceSuffix(),
NamingTemplate: n.ResourceNamingTemplate(),
APIGatewayLogsRole: APIGatewayLogsRole,
ResourceTags: c.resourceTags,
}
Expand Down Expand Up @@ -222,6 +223,7 @@ func (c *Setup) upgrade(n *domain.Node) error {
FunctionsPath: n.Functions.Path,
AuthEnv: n.AuthEnv(),
ResourceSuffix: n.ResourceSuffix(),
NamingTemplate: n.ResourceNamingTemplate(),
ResourceTags: c.resourceTags,
}
if err := invoke.Lambda(c.aws.Lambda(), c.lambdaName, ui.NodeLogsSink).Do("upgrade", req, nil); err != nil {
Expand Down
13 changes: 13 additions & 0 deletions cli/ui/term/term_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build !windows

package term

import "fmt"

func HideCursor() {
fmt.Print("\033[?25l")
}

func ShowCursor() {
fmt.Print("\033[?25h")
}
63 changes: 63 additions & 0 deletions cli/ui/term/term_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//go:build windows

package term

import (
"os"
"syscall"
"unsafe"
)

var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
procGetConsoleCursorInfo = kernel32.NewProc("GetConsoleCursorInfo")
procSetConsoleCursorInfo = kernel32.NewProc("SetConsoleCursorInfo")
)

type short int16
type dword uint32
type word uint16

type coord struct {
x short
y short
}

type smallRect struct {
bottom short
left short
right short
top short
}

type consoleScreenBufferInfo struct {
size coord
cursorPosition coord
attributes word
window smallRect
maximumWindowSize coord
}

type consoleCursorInfo struct {
size dword
visible int32
}

func ShowCursor() {
handle := syscall.Handle(os.Stdout.Fd())
var cci consoleCursorInfo
_, _, _ = procGetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&cci)))
cci.visible = 1
_, _, _ = procSetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&cci)))
}

func HideCursor() {
handle := syscall.Handle(os.Stdout.Fd())
var cci consoleCursorInfo
_, _, _ = procGetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&cci)))
cci.visible = 0
_, _, _ = procSetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&cci)))
}
4 changes: 4 additions & 0 deletions domain/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ func (s *Stage) WsEndpoint() string {
return fmt.Sprintf("%s/$default", s.Endpoints.Ws)
}

func (s *Stage) PublicBucketName() string {
return fmt.Sprintf("%s-%s-public-%s", s.Project().Name, s.Name, s.Node().ResourceSuffix())
}

type WsConfig struct {
ApiToFn map[string]string `json:"apiToFn"`
}
Expand Down
4 changes: 4 additions & 0 deletions domain/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ func (n *Node) ResourceSuffix() string {
return n.ID
}

func (n *Node) ResourceNamingTemplate() string {
return "mantil-%s-" + n.ID
}

func (n *Node) SetupStackName() string {
return n.SetupLambdaName()
}
Expand Down
1 change: 1 addition & 0 deletions node/api/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (s *Setup) terraformCreate(req *dto.SetupRequest) (*dto.SetupResponse, erro
FunctionsBucket: req.FunctionsBucket,
FunctionsPath: req.FunctionsPath,
ResourceSuffix: req.ResourceSuffix,
NamingTemplate: req.NamingTemplate,
AuthEnv: req.AuthEnv,
ResourceTags: req.ResourceTags,
}
Expand Down
3 changes: 3 additions & 0 deletions node/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type StageTemplate struct {
ResourceTags map[string]string
WsEnv map[string]string
HasPublic bool
NamingTemplate string
PublicBucketName string
}

type Function struct {
Expand Down Expand Up @@ -78,6 +80,7 @@ type SetupRequest struct {
FunctionsBucket string
FunctionsPath string
ResourceSuffix string
NamingTemplate string
APIGatewayLogsRole string
AuthEnv map[string]string
ResourceTags map[string]string
Expand Down
2 changes: 1 addition & 1 deletion node/terraform/modules/api/auth.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
locals {
authorizer_lambda_name = "${var.prefix}-authorizer-${var.suffix}"
authorizer_lambda_name = format(var.naming_template, "authorizer")
authorizer_lambda_s3_key = "${var.functions_s3_path}/authorizer.zip"
}

Expand Down
4 changes: 2 additions & 2 deletions node/terraform/modules/api/iam.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
resource "aws_iam_role" "authorizer" {
count = var.authorizer == null ? 0 : 1
name = "${var.prefix}-authorizer-${var.suffix}"
name = format(var.naming_template, "authorizer")
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
Expand Down Expand Up @@ -33,7 +33,7 @@ data "aws_iam_policy_document" "authorizer" {

resource "aws_iam_role_policy" "authorizer" {
count = var.authorizer == null ? 0 : 1
name = "${var.prefix}-authorizer-${var.suffix}"
name = format(var.naming_template, "authorizer")
role = aws_iam_role.authorizer[0].id
policy = data.aws_iam_policy_document.authorizer[0].json
}
9 changes: 4 additions & 5 deletions node/terraform/modules/api/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ terraform {
}

module "http_api" {
source = "../http-api"
prefix = var.prefix
suffix = var.suffix
integrations = var.integrations
source = "../http-api"
naming_template = var.naming_template
integrations = var.integrations
authorizer = var.authorizer == null ? null : {
authorization_header = var.authorizer.authorization_header
arn = aws_lambda_function.authorizer[0].arn
Expand All @@ -17,8 +16,8 @@ module "http_api" {
module "ws_api" {
count = var.ws_enabled ? 1 : 0
source = "../ws-api"
prefix = var.prefix
suffix = var.suffix
naming_template = var.naming_template
functions_bucket = var.functions_bucket
functions_s3_path = var.functions_s3_path
ws_env = var.ws_env
Expand Down
4 changes: 2 additions & 2 deletions node/terraform/modules/api/variables.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
variable "prefix" {
variable "suffix" {
type = string
}

variable "suffix" {
variable "naming_template" {
type = string
}

Expand Down
2 changes: 1 addition & 1 deletion node/terraform/modules/cli-role/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
locals {
name = "${var.prefix}-cli-user-${var.suffix}"
name = format(var.naming_template, "cli-user")
}

resource "aws_iam_role" "cli_role" {
Expand Down
6 changes: 3 additions & 3 deletions node/terraform/modules/cli-role/variables.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
variable "prefix" {
variable "suffix" {
type = string
}

variable "suffix" {
variable "naming_template" {
type = string
}
}
9 changes: 4 additions & 5 deletions node/terraform/modules/functions-node/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ locals {
}

module "functions" {
source = "../functions"
functions = local.functions
s3_bucket = var.functions_bucket
prefix = "mantil"
suffix = var.suffix
source = "../functions"
functions = local.functions
s3_bucket = var.functions_bucket
naming_template = var.naming_template
}
3 changes: 3 additions & 0 deletions node/terraform/modules/functions-node/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ variable "suffix" {
type = string
}

variable "naming_template" {
type = string
}
variable "region" {
type = string
}
Expand Down
14 changes: 7 additions & 7 deletions node/terraform/modules/functions/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ locals {
{
s3_key : try(f.s3_key, "")

function_name : "${var.prefix}-${k}-${var.suffix}" // prefix functions name with project name
runtime : try(f.runtime, "provided.al2") // default runtime is go
handler : try(f.handler, "bootstrap") // default handler for go is 'bootstrap'
memory_size : try(f.memory_size, 128) // default memory size
timeout : try(f.timeout, 900) // default timeout
path : try(f.path, k) // default path is function's name
architecture : try(f.architecture, "arm64") // default architecture is arm64
function_name : format(var.naming_template, k) // prefix functions name with project name
runtime : try(f.runtime, "provided.al2") // default runtime is go
handler : try(f.handler, "bootstrap") // default handler for go is 'bootstrap'
memory_size : try(f.memory_size, 128) // default memory size
timeout : try(f.timeout, 900) // default timeout
path : try(f.path, k) // default path is function's name
architecture : try(f.architecture, "arm64") // default architecture is arm64
env : length(try(f.env, {})) == 0 ? null : try(f.env, {})
layers : try(f.layers, [])
policy : try(f.policy, jsonencode({
Expand Down
6 changes: 1 addition & 5 deletions node/terraform/modules/functions/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ variable "s3_bucket" {
description = "S3 bucket containing functions' deployment package."
}

variable "prefix" {
type = string
}

variable "suffix" {
variable "naming_template" {
type = string
}
6 changes: 3 additions & 3 deletions node/terraform/modules/http-api/api.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
resource "aws_apigatewayv2_api" "http" {
name = "${var.prefix}-http-${var.suffix}"
name = format(var.naming_template, "http")
protocol_type = "HTTP"
cors_configuration {
allow_origins = toset(["*"])
}
}

resource "aws_cloudwatch_log_group" "http_access_logs" {
name = "/aws/vendedlogs/${var.prefix}-http-access-logs-${var.suffix}"
name = "/aws/vendedlogs/${format(var.naming_template, "http-access-logs")}"
retention_in_days = 14
}

Expand Down Expand Up @@ -151,6 +151,6 @@ resource "aws_apigatewayv2_authorizer" "http" {
authorizer_uri = var.authorizer.invoke_arn
identity_sources = ["$request.header.${var.authorizer.authorization_header}"]
authorizer_payload_format_version = "1.0"
name = "${var.prefix}-http-authorizer-${var.suffix}"
name = format(var.naming_template, "http-authorizer")
authorizer_result_ttl_in_seconds = 0
}
6 changes: 1 addition & 5 deletions node/terraform/modules/http-api/variables.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
variable "prefix" {
type = string
}

variable "suffix" {
variable "naming_template" {
type = string
}

Expand Down
2 changes: 1 addition & 1 deletion node/terraform/modules/public-site/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_s3_bucket" "public" {
bucket = "${var.prefix}-${var.suffix}"
bucket = var.bucket_name
acl = "public-read"
force_destroy = true

Expand Down
6 changes: 1 addition & 5 deletions node/terraform/modules/public-site/variables.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
variable "prefix" {
type = string
}

variable "suffix" {
variable "bucket_name" {
type = string
}
6 changes: 3 additions & 3 deletions node/terraform/modules/ws-api/api.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
resource "aws_apigatewayv2_api" "ws" {
name = "${var.prefix}-ws-${var.suffix}"
name = format(var.naming_template, "ws")
protocol_type = "WEBSOCKET"

route_selection_expression = "\\$default"
}

resource "aws_cloudwatch_log_group" "ws_access_logs" {
name = "/aws/vendedlogs/${var.prefix}-ws-access-logs-${var.suffix}"
name = "/aws/vendedlogs/${format(var.naming_template, "ws-access-logs")}"
retention_in_days = 14
}

Expand Down Expand Up @@ -155,5 +155,5 @@ resource "aws_apigatewayv2_authorizer" "ws" {
authorizer_type = "REQUEST"
authorizer_uri = var.authorizer.invoke_arn
identity_sources = ["route.request.header.${var.authorizer.authorization_header}"]
name = "${var.prefix}-ws-authorizer-${var.suffix}"
name = format(var.naming_template, "ws-authorizer")
}
10 changes: 5 additions & 5 deletions node/terraform/modules/ws-api/iam.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_iam_role" "ws_handler" {
name = "${var.prefix}-ws-handler-${var.suffix}"
name = format(var.naming_template, "ws-handler")
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
Expand All @@ -18,7 +18,7 @@ data "aws_iam_policy_document" "ws_handler" {
statement {
effect = "Allow"
actions = ["lambda:InvokeFunction"]
resources = ["arn:aws:lambda:*:*:function:${var.prefix}-*-${var.suffix}"]
resources = ["arn:aws:lambda:*:*:function:${format(var.naming_template, "*")}"]
}
statement {
effect = "Allow"
Expand Down Expand Up @@ -47,13 +47,13 @@ data "aws_iam_policy_document" "ws_handler" {
}

resource "aws_iam_role_policy" "ws_handler" {
name = "${var.prefix}-ws-handler-${var.suffix}"
name = format(var.naming_template, "ws-handler")
role = aws_iam_role.ws_handler.id
policy = data.aws_iam_policy_document.ws_handler.json
}

resource "aws_iam_role" "ws_forwarder" {
name = "${var.prefix}-ws-forwarder-${var.suffix}"
name = format(var.naming_template, "ws-forwarder")
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
Expand Down Expand Up @@ -101,7 +101,7 @@ data "aws_iam_policy_document" "ws_forwarder" {
}

resource "aws_iam_role_policy" "ws_forwarder" {
name = "${var.prefix}-ws-forwarder-${var.suffix}"
name = format(var.naming_template, "ws-forwarder")
role = aws_iam_role.ws_forwarder.id
policy = data.aws_iam_policy_document.ws_forwarder.json
}
Loading

0 comments on commit 3792675

Please sign in to comment.