Merge pull request #3 from Balhau/OnMakefile

Added makefile
This commit is contained in:
Bitor Tonixa Biriato Balença 2023-12-20 20:29:39 +00:00 committed by GitHub
commit 8cf3bc1903
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 78 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@
receita-provider
backend
*.tfstate.backup
bin/

41
Makefile Normal file
View file

@ -0,0 +1,41 @@
provider_directory=~/.terraform.d/plugins/terraform.local/balhau/receita/1.0.0/darwin_amd64/
.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 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 all the generated resources
.PHONY: terraform-init
terraform-init: clean-terraform-state ## Execute terraform-init on example terraform directory
cd example; terraform init
.PHONY: terraform-apply
terraform-apply: ## Execute terraform-apply on example directory
cd example; terraform apply
.PHONY: 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 in the terraform plugin directory
mkdir -p $(provider_directory)
cp bin/terraform-provider-receita $(provider_directory)
.PHONY: run-backend
run-backend: build ## Start the backend web server
bin/backend

View file

@ -49,87 +49,23 @@ 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
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
```
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
}
```