Skip to content

Commit

Permalink
feat: add runtime provider debugging capability
Browse files Browse the repository at this point in the history
  • Loading branch information
iwarapter committed Mar 8, 2024
1 parent 79f6519 commit c5292fa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ tools:
build: fmtcheck
go install

debug: fmtcheck
go build -gcflags="all=-N -l" -trimpath -o terraform-provider-azuread
dlv exec --listen=:51000 --headless=true --api-version=2 --accept-multiclient --continue terraform-provider-azuread -- -debug

fumpt:
@echo "==> Fixing source code with gofmt..."
# This logic should match the search logic in scripts/gofmtcheck.sh
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ $ $GOPATH/bin/terraform-provider-azuread
...
```

To compile the provider for attached debugging run `make debug`.

```sh
$ make debug
...
Provider started. To attach Terraform CLI, set the TF_REATTACH_PROVIDERS environment variable with the following:
TF_REATTACH_PROVIDERS='{"registry.terraform.io/hashicorp/azuread":{"Protocol":"grpc","ProtocolVersion":5,"Pid":16227,"Test":true,"Addr":{"Network":"unix","String":"/var/folders/dy/r91ps1bx7fscm_v64qbwd0nh0000gn/T/plugin1540622971"}}}'
```

See the [documentation](https://developer.hashicorp.com/terraform/plugin/debugging#starting-a-provider-in-debug-mode) for attaching a debugger.


In order to test the provider, you can simply run `make test`.

```sh
Expand Down
15 changes: 13 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
package main

import (
"flag"

"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
var debug bool

flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()

opts := &plugin.ServeOpts{
Debug: debug,
ProviderAddr: "registry.terraform.io/hashicorp/azuread",
ProviderFunc: Provider,
})
}

plugin.Serve(opts)
}

0 comments on commit c5292fa

Please sign in to comment.