From 2cf26575015dbd5f3df5bc6481c55da71f42be5a Mon Sep 17 00:00:00 2001 From: Bitor Tonixa Balenca Date: Wed, 20 Dec 2023 20:01:56 +0000 Subject: [PATCH 1/5] Added makefile --- .gitignore | 1 + Makefile | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index 846b484..dca5e23 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ receita-provider backend *.tfstate.backup +bin/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..73a6bfc --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +provider_directory=~/.terraform.d/plugins/terraform.local/balhau/receita/1.0.0/darwin_amd64/ + +.PHONY: clean-bin +clean-bin: + rm -rf bin + +.PHONY: clean-terraform-state +clean-terraform-state: + rm -rf example/.terraform terraform.tfstate.backup .terraform.lock.hcl terraform.tfstate + +.PHONY: clean +clean: clean-bin clean-terraform-state + +.PHONY: build +build: + mkdir -p bin + go build -o bin/terraform-provider-receita + go build -o bin/backend api/backend.go + +.PHONY: install-provider +install-provider: + mkdir -p $(provider_directory) + cp bin/terraform-provider-receita $(provider_directory) + From 144646a2c19bf88f606579f61ea38b49d6747e43 Mon Sep 17 00:00:00 2001 From: Bitor Tonixa Balenca Date: Wed, 20 Dec 2023 20:13:32 +0000 Subject: [PATCH 2/5] Fix makefile. Added terraform and backend server steps --- Makefile | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 73a6bfc..19e4e02 100644 --- a/Makefile +++ b/Makefile @@ -6,11 +6,19 @@ clean-bin: .PHONY: clean-terraform-state clean-terraform-state: - rm -rf example/.terraform terraform.tfstate.backup .terraform.lock.hcl terraform.tfstate + rm -rf example/.terraform example/terraform.tfstate.backup example/.terraform.lock.hcl example/terraform.tfstate .PHONY: clean clean: clean-bin clean-terraform-state +.PHONY: terraform-init +terraform-init: clean-terraform-state + cd example; terraform init + +.PHONY: terraform-apply +terraform-apply: + cd example; terraform apply + .PHONY: build build: mkdir -p bin @@ -22,3 +30,7 @@ install-provider: mkdir -p $(provider_directory) cp bin/terraform-provider-receita $(provider_directory) +.PHONY: run-backend +run-backend: build + bin/backend + From 9b6dd4cc6162c77aeedfdc07ea4a8172bc4e096e Mon Sep 17 00:00:00 2001 From: Bitor Tonixa Balenca Date: Wed, 20 Dec 2023 20:26:14 +0000 Subject: [PATCH 3/5] On makefile documentation --- Makefile | 32 +++++++++++++++------ README.md | 85 ++++--------------------------------------------------- 2 files changed, 28 insertions(+), 89 deletions(-) diff --git a/Makefile b/Makefile index 19e4e02..af1711c 100644 --- a/Makefile +++ b/Makefile @@ -1,36 +1,50 @@ + +## ---------------------------------------------------------------------- +## This is a help comment. The purpose of this Makefile is to demonstrate +## a simple help mechanism that uses comments defined alongside the rules +## they describe without the need of additional help files or echoing of +## descriptions. Help comments are displayed in the order defined within +## the Makefile. +## ---------------------------------------------------------------------- + + provider_directory=~/.terraform.d/plugins/terraform.local/balhau/receita/1.0.0/darwin_amd64/ -.PHONY: clean-bin -clean-bin: +.PHONY: help +help: ## Show this help. + @sed -ne 's/^\([^[:space:]]*\):.*##/\1:\t/p' $(MAKEFILE_LIST) | column -t -s $$'\t' + +.PHONY: clean-bin +clean-bin: ## Clean all the generated binaries rm -rf bin .PHONY: clean-terraform-state -clean-terraform-state: +clean-terraform-state: ## Clean all the terraform state generated by terraform-init/terraform-apply rm -rf example/.terraform example/terraform.tfstate.backup example/.terraform.lock.hcl example/terraform.tfstate .PHONY: clean -clean: clean-bin clean-terraform-state +clean: clean-bin clean-terraform-state ## Clean all the generated resources .PHONY: terraform-init -terraform-init: clean-terraform-state +terraform-init: clean-terraform-state ## Execute terraform-init on example terraform directory cd example; terraform init .PHONY: terraform-apply -terraform-apply: +terraform-apply: ## Execute terraform-apply on example directory cd example; terraform apply .PHONY: build -build: +build: ## Build binaries, both the backed dummy server as the terraform provider binary mkdir -p bin go build -o bin/terraform-provider-receita go build -o bin/backend api/backend.go .PHONY: install-provider -install-provider: +install-provider: ## Install provider in the terraform plugin directory mkdir -p $(provider_directory) cp bin/terraform-provider-receita $(provider_directory) .PHONY: run-backend -run-backend: build +run-backend: build ## Start the backend web server bin/backend diff --git a/README.md b/README.md index e98e2b4..a534bda 100644 --- a/README.md +++ b/README.md @@ -49,87 +49,12 @@ provider "receita" { } ``` +### Running this -For that we can use http module from python3 and run +Managing terraform providers involves a bit of typing in the command line. To avoid typing to much we have here a `Makefile` with most of the command line instructions we need. + +To figure out how to use this makefile just type ```sh -python3 -m http.server 9999 - -``` - -This will open a http server in the port `9999`. This will not implement the endpoints defined in the code but will be enough to log the calls and this will be enough for us to validate the provider lifecycle. - -### Running the provider - -To run this provider we should jump into `example` directory and initialize `terraform` in the folder. For this it is enough to run - - -```sh -terraform init -``` - -If all steps run properly unti this moment we should end up with a success command. Next step is the `plan` - -For this we just - -```sh -terraform plan -out tf_plan.state -``` - -The end result should be a `binary` file containing the terraform state to be executed. -To finally apply the terraform changes we need to run - -```sh -terraform apply tf_plan.state -``` - -If all went good you should have something like this - -``` -receita_receita.receita_one: Creating... -receita_receita.receita_one: Creation complete after 0s [id=500c3203-7210-45e0-8599-6a4048b78179] - -Apply complete! Resources: 1 added, 0 changed, 0 destroyed. -``` - -as output. - -At the same time the mock server that we just started should log a call to an endpoint - -``` -code 404, message File not found -"GET /create HTTP/1.1" 404 - -``` - -You should have also a `terraform.tfstate` file. Which is a human readable `json` representation of the current terraform state. -Should look something like - -```json -{ - "version": 4, - "terraform_version": "1.4.6", - "serial": 1, - "lineage": "bef05e13-6bcf-44f0-a62e-131ade285463", - "outputs": {}, - "resources": [ - { - "mode": "managed", - "type": "receita_receita", - "name": "receita_one", - "provider": "provider[\"terraform.local/balhau/receita\"]", - "instances": [ - { - "schema_version": 0, - "attributes": { - "author": "Maria Bacalhau", - "id": "500c3203-7210-45e0-8599-6a4048b78179", - "name": "Bola de carne" - }, - "sensitive_attributes": [] - } - ] - } - ], - "check_results": null -} +make help ``` From 623651e49becefa2bf8586b1d736218628b5b155 Mon Sep 17 00:00:00 2001 From: Bitor Tonixa Balenca Date: Wed, 20 Dec 2023 20:27:20 +0000 Subject: [PATCH 4/5] More on documentation --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index a534bda..9cca1a4 100644 --- a/README.md +++ b/README.md @@ -57,4 +57,15 @@ To figure out how to use this makefile just type ```sh make help + +help: Show this help. +clean-bin: Clean all the generated binaries +clean-terraform-state: Clean all the terraform state generated by terraform-init/terraform-apply +clean: Clean all the generated resources +terraform-init: Execute terraform-init on example terraform directory +terraform-apply: Execute terraform-apply on example directory +build: Build binaries, both the backed dummy server as the terraform provider binary +install-provider: Install provider in the terraform plugin directory +run-backend: Start the backend web server ``` + From e0d6c4b52693aa1c12c6e617ba90aa3af367d406 Mon Sep 17 00:00:00 2001 From: Bitor Tonixa Balenca Date: Wed, 20 Dec 2023 20:28:45 +0000 Subject: [PATCH 5/5] Clean makefile --- Makefile | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Makefile b/Makefile index af1711c..0c62439 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,4 @@ -## ---------------------------------------------------------------------- -## This is a help comment. The purpose of this Makefile is to demonstrate -## a simple help mechanism that uses comments defined alongside the rules -## they describe without the need of additional help files or echoing of -## descriptions. Help comments are displayed in the order defined within -## the Makefile. -## ---------------------------------------------------------------------- - - provider_directory=~/.terraform.d/plugins/terraform.local/balhau/receita/1.0.0/darwin_amd64/ .PHONY: help