From 60e6ba183148d1bdbac22ae299798c14f10f4c68 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Thu, 29 Apr 2021 02:45:33 +0300
Subject: [PATCH] Scaffolding.

---
 .gitignore              |  3 ++
 .gitlab-ci.yml          | 56 ++++++++++++++++++++++++++
 .pre-commit-config.yaml | 88 +++++++++++++++++++++++++++++++++++++++++
 Makefile                | 12 ++++++
 main.tf                 | 19 +++++++++
 outputs.tf              | 14 +++++++
 requirements.txt        |  1 +
 src/function.py         |  3 ++
 variables.tf            |  4 ++
 9 files changed, 200 insertions(+)
 create mode 100644 .gitlab-ci.yml
 create mode 100644 .pre-commit-config.yaml
 create mode 100644 Makefile
 create mode 100644 main.tf
 create mode 100644 outputs.tf
 create mode 100644 requirements.txt
 create mode 100644 src/function.py
 create mode 100644 variables.tf

diff --git a/.gitignore b/.gitignore
index 62c0b50..11d1643 100644
--- a/.gitignore
+++ b/.gitignore
@@ -48,5 +48,8 @@ dist/
 .bundle/
 !Pipfile.lock
 .terraform
+.terraform.*
 tfplan
 *.tfstate*
+payload/*
+payload.zip
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..4af2668
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,56 @@
+---
+include:
+  - project: shore/ci-templates
+    file: templates/pre-commit.yml
+
+variables:
+  TF_ADDRESS: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/default
+
+stages:
+  - test
+  - build
+  - plan
+  - apply
+
+Generate payload:
+  stage: build
+  image: registry.shore.co.il/ci-images:python3
+  variables:
+    XDG_CACHE_HOME: "$CI_PROJECT_DIR/.cache"
+  before_script:
+    - apt-get update
+    - apt-get install -y zip
+  script:
+    make payload.zip
+  artifacts:
+    paths:
+      - payload.zip
+  cache:
+    paths:
+      - .cache/
+
+Terraform plan:
+  stage: plan
+  image: &tf_image registry.gitlab.com/gitlab-org/terraform-images/stable:latest
+  script:
+    - gitlab-terraform plan
+    - gitlab-terraform plan-json
+  dependencies:
+    - Generate payload
+  artifacts:
+    name: plan
+    paths:
+      - plan.cache
+    reports:
+      terraform: plan.json
+
+Terraform apply:
+  stage: apply
+  image: *tf_image
+  script:
+    - gitlab-terraform apply
+  dependencies: &tf_apply_dependecies
+    - Generate payload
+    - Terraform plan
+  when: manual
+  needs: *tf_apply_dependecies
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
new file mode 100644
index 0000000..9c8f669
--- /dev/null
+++ b/.pre-commit-config.yaml
@@ -0,0 +1,88 @@
+---
+repos:
+  - repo: https://github.com/pre-commit/pre-commit-hooks
+    rev: v3.4.0
+    hooks:
+      - id: check-merge-conflict
+      - id: trailing-whitespace
+
+  - repo: https://github.com/Yelp/detect-secrets
+    rev: v1.1.0
+    hooks:
+      - id: detect-secrets
+
+  - repo: https://github.com/adrienverge/yamllint
+    rev: v1.26.1
+    hooks:
+      - id: yamllint
+
+  - repo: https://github.com/executablebooks/mdformat.git
+    rev: 0.7.4
+    hooks:
+      - id: mdformat
+
+  - repo: https://github.com/amperser/proselint/
+    rev: 0.10.2
+    hooks:
+      - id: proselint
+        types: [plain-text]
+        exclude: LICENSE
+
+  - repo: https://github.com/ambv/black
+    rev: 20.8b1
+    hooks:
+      - id: black
+        args:
+          - |
+              --line-length=79
+
+  - repo: https://github.com/PyCQA/prospector
+    rev: 1.3.1
+    hooks:
+      - id: prospector
+        args:
+          - |-
+            --max-line-length=79
+          - |-
+            --with-tool=pyroma
+          - |-
+            --with-tool=bandit
+          - |-
+            --without-tool=pep257
+          - |-
+            --doc-warnings
+          - |-
+            --test-warnings
+          - |-
+            --full-pep8
+          - |-
+            --strictness=high
+          - |-
+            --no-autodetect
+        additional_dependencies:
+          - bandit
+          - pyroma
+
+  - repo: https://gitlab.com/pycqa/flake8.git
+    rev: 3.9.1
+    hooks:
+      - id: flake8
+        args:
+          - |-
+            --doctests
+        additional_dependencies:
+          - flake8-bugbear
+
+  - repo: https://gitlab.com/devopshq/gitlab-ci-linter
+    rev: v1.0.3
+    hooks:
+      - id: gitlab-ci-linter
+        args:
+          - "--server"
+          - https://git.shore.co.il
+
+  - repo: https://git.shore.co.il/nimrod/terraform-pre-commit.git
+    rev: v0.1.0
+    hooks:
+      - id: tf-fmt
+      - id: tf-validate
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..5602f77
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,12 @@
+src != find src/ -type f -name '*.py'
+
+payload.zip: clean requirements.txt $(src)
+	-rm -r payload.zip payload/*
+	mkdir -p payload
+	cp -a src/* payload/
+	python3 -m pip install -r requirements.txt -t payload
+	cd payload && zip -X --exclude __pycache__ --exclude "*.pyc" --exclude test --exclude bin -r ../payload.zip ./
+
+.PHONY = clean
+clean:
+	-rm -r payload.zip payload
diff --git a/main.tf b/main.tf
new file mode 100644
index 0000000..e156d2d
--- /dev/null
+++ b/main.tf
@@ -0,0 +1,19 @@
+terraform {
+  backend "http" {}
+}
+
+locals {
+  env    = terraform.workspace == "default" ? "prod" : terraform.workspace
+  module = basename(abspath(path.root))
+  common_tags = {
+    Environment = local.env
+    Module      = local.module
+  }
+  Name = "${local.module}-${local.env}"
+}
+
+provider "aws" {
+  region = var.region
+}
+
+provider "template" {}
diff --git a/outputs.tf b/outputs.tf
new file mode 100644
index 0000000..1393977
--- /dev/null
+++ b/outputs.tf
@@ -0,0 +1,14 @@
+output "env" {
+  description = "Environment (prod/dev etc.)."
+  value       = local.env
+}
+
+output "module" {
+  description = "The name of the Terraform module, used to tagging resources."
+  value       = local.module
+}
+
+output "region" {
+  description = "AWS region."
+  value       = var.region
+}
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..2f73596
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1 @@
+dnspython
diff --git a/src/function.py b/src/function.py
new file mode 100644
index 0000000..7176109
--- /dev/null
+++ b/src/function.py
@@ -0,0 +1,3 @@
+def handler(event, context):  # pylint: disable=unused-argument
+    """Lambda event handler."""
+    pass  # pylint: disable=unnecessary-pass
diff --git a/variables.tf b/variables.tf
new file mode 100644
index 0000000..d86dca0
--- /dev/null
+++ b/variables.tf
@@ -0,0 +1,4 @@
+variable "region" {
+  default     = "us-east-1"
+  description = "AWS region."
+}
-- 
GitLab