No description
Find a file
Bitor Tonixa Balenca eb70c87759
Added .gitignore
2023-12-19 23:31:45 +00:00
example Added http call to provider creation 2023-12-19 23:31:44 +00:00
internal/provider Added http call to provider creation 2023-12-19 23:31:44 +00:00
.gitignore Added .gitignore 2023-12-19 23:31:45 +00:00
go.mod More on the receitas resource 2023-12-19 23:31:43 +00:00
go.sum More on the receitas resource 2023-12-19 23:31:43 +00:00
main.go More on the receitas resource 2023-12-19 23:31:43 +00:00
README.md More bugfixing 2023-12-19 23:31:45 +00:00

Receita terraform example provider

What?

This is a terraform provider to serve as example. This will give an API to generate receipts based on a terraform provider.

Building?

First step in this journey was to install go language. For that you can just follow something like this.

Next we need to init this as a go module this is basically to help us to manage go dependencies.

For this first step we ended with

go mod init balhau.net/receita-provider 

the output of this command was the creation of the go.mod file.

Since the goal here is to build a terraform provider we must add the terraform libraries needed for that. This can be achieved with

go get

To be able to use this terraform plugin you'll need to build this go project with

go build

And then move the binary into your terraform plugin directory, something like this

~/.terraform.d/plugins/terraform.local/balhau/receita/1.0.0/darwin_amd64/terraform-provider-receita

Running?

Setting the http mock server

After previous steps were taken of we are ready to run the receita terraform provider. The provider will do some http calls to the endpoint defined in the provider definition. This means that we should put a service running that matches the description in the endpoint definition

provider "receita" {
  endpoint = "http://localhost:9999"
}

For that we can use http module from python3 and run

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

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

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

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

{
  "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
}