Skip to content

Commit b2a9f09

Browse files
authored
feat: support and document attaching a debugger to the provider (#617)
* feat: support and document attaching a debugger to the provider * chore: fix documentation
1 parent f04ced9 commit b2a9f09

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

CONTRIBUTION.md

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -389,40 +389,49 @@ If you want to onboard resources of a STACKIT service `foo` that was not yet in
389389
To test your changes locally, you have to compile the provider (requires Go 1.22) and configure the Terraform CLI to use the local version.
390390

391391
1. Clone the repository.
392-
2. Set the provider address to a custom address for local development. It must correspond to the same address that is included in the dev_overrides block, in step 4.
393-
In `main.go` replace the address `registry.terraform.io/providers/stackitcloud/stackit` with `local-dev.com/stackit/stackit`.
394-
3. Go to the repository root and compile the provider locally to any location by running `go build -o <PATH_TO_BINARY>`. The binary name must start with `terraform-provider`, e.g. `terraform-provider-stackit`.
395-
4. Create a `.terraformrc` config file in your home directory (`~`) for the terraform CLI with the following content:
396-
397-
```
398-
provider_installation {
399-
dev_overrides {
400-
"local-dev.com/stackit/stackit" = "<PATH_TO_BINARY>"
401-
}
392+
1. Create a `.terraformrc` config file in your home directory (`~`) for the terraform CLI with the following content:
393+
394+
```
395+
provider_installation {
396+
dev_overrides {
397+
"registry.terraform.io/stackitcloud/stackit" = "<PATH_TO_BINARY>"
398+
}
399+
400+
# For all other providers, install them directly from their origin provider
401+
# registries as normal. If you omit this, Terraform will _only_ use
402+
# the dev_overrides block, and so no other providers will be available.
403+
direct {}
404+
}
405+
```
406+
1. Copy one of the folders in the [examples](examples/) folder to a location of your choosing, and define the Terraform variables according to its README. The main.tf file needs some additional configuration to use the local provider:
407+
408+
```
409+
terraform {
410+
required_providers {
411+
stackit = {
412+
source = "registry.terraform.io/stackitcloud/stackit"
413+
}
414+
}
415+
}
416+
```
402417

403-
# For all other providers, install them directly from their origin provider
404-
# registries as normal. If you omit this, Terraform will _only_ use
405-
# the dev_overrides block, and so no other providers will be available.
406-
direct {}
407-
}
408-
```
418+
1. Go to the copied example and initialize Terraform by running `terraform init -reconfigure -upgrade`. This will throw an error ("Failed to query available provider packages") which can be ignored since we are using the local provider build.
419+
> Note: Terraform will store its resources' states locally. To allow multiple people to use the same resources, check [Setup for multi-person usage](#setup-centralized-terraform-state)
420+
1. Setup authentication by setting the env var `STACKIT_SERVICE_ACCOUNT_TOKEN` as a valid token (see [Authentication](#authentication) for more details on how to autenticate).
421+
1. Run `terraform plan` or `terraform apply` commands.
422+
1. To debug the terraform provider, execute the following steps:
423+
* install the compiled terraform provider to binary path defined in the .terraformrc file
424+
* run the terraform provider from your IDE with the `-debug` flag set
425+
* The provider will emit the setting for the env variable `TF_REATTACH_PROVIDERS`, e.g.
409426

410-
4. Copy one of the folders in the [examples](examples/) folder to a location of your choosing, and define the Terraform variables according to its README. The main.tf file needs some additional configuration to use the local provider:
427+
```shell
428+
TF_REATTACH_PROVIDERS='{"registry.terraform.io/stackitcloud/stackit":{"Protocol":"grpc","ProtocolVersion":6,"Pid":123456,"Test":true,"Addr":{"Network":"unix","String":"/tmp/plugin47110815"}}}'
429+
```
411430

412-
```
413-
terraform {
414-
required_providers {
415-
stackit = {
416-
source = "local-dev.com/stackit/stackit"
417-
}
418-
}
419-
}
420-
```
431+
Starting terraform with this environment variable set will automatically connect to the running IDE session, instead of starting a new GRPC server with the plugin. This allows to set
432+
breakpoints and inspect the state of the provider.
433+
421434

422-
5. Go to the copied example and initialize Terraform by running `terraform init -reconfigure -upgrade`. This will throw an error ("Failed to query available provider packages") which can be ignored since we are using the local provider build.
423-
> Note: Terraform will store its resources' states locally. To allow multiple people to use the same resources, check [Setup for multi-person usage](#setup-centralized-terraform-state)
424-
6. Setup authentication by setting the env var `STACKIT_SERVICE_ACCOUNT_TOKEN` as a valid token (see [Authentication](#authentication) for more details on how to autenticate).
425-
7. Run `terraform plan` or `terraform apply` commands.
426435

427436
#### Setup centralized Terraform state
428437

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"flag"
56
"log"
67

78
"github.com/hashicorp/terraform-plugin-framework/providerserver"
@@ -14,8 +15,12 @@ var (
1415
)
1516

1617
func main() {
18+
var debug bool
19+
flag.BoolVar(&debug, "debug", false, "allows debugging the provider")
20+
flag.Parse()
1721
err := providerserver.Serve(context.Background(), stackit.New(version), providerserver.ServeOpts{
1822
Address: "registry.terraform.io/stackitcloud/stackit",
23+
Debug: debug,
1924
})
2025
if err != nil {
2126
log.Fatal(err.Error())

0 commit comments

Comments
 (0)