From c5292fa42d2203ace3752ec0e6e0fb2b310c062f Mon Sep 17 00:00:00 2001 From: iwarapter Date: Fri, 8 Mar 2024 23:26:04 +0000 Subject: [PATCH] feat: add runtime provider debugging capability --- GNUmakefile | 4 ++++ README.md | 12 ++++++++++++ main.go | 15 +++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index c903bd3100..2ca33b4622 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -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 diff --git a/README.md b/README.md index 7028bdbf62..0b5b661602 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.go b/main.go index 205bccd591..9e94983944 100644 --- a/main.go +++ b/main.go @@ -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) }