Skip to content

Commit

Permalink
feat: 429 add debug mode (#430)
Browse files Browse the repository at this point in the history
* feat: 429 add debug mode

* clean up

* Add const comment

* fix: make build gcflags issue
  • Loading branch information
luigibk authored and tobio committed May 10, 2022
1 parent b64c011 commit 62cec21
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
22 changes: 22 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,25 @@ $ make build
```

You can also use the `make install` target if you wish. This target will install the binary and move it to your Terraform plugin location.

### Debugging

This provider supports debugger-based debugging as described in the related [Terraform SDK documentation](https://www.terraform.io/plugin/sdkv2/debugging#debugger-based-debugging). To build a provider with the necessary Go compiler flags run:

```console
$ make build GCFLAGS='-gcflags="all=-N -l"'
```

To run the provider and connect it to your debugger run:

```console
dlv --listen=:60324 --headless=true --api-version=2 --accept-multiclient exec ./bin/terraform-provider-ec -- --debug
```

Connect your debugger using port 60234 as target. The provider should output a line containing the following variable:

```console
TF_REATTACH_PROVIDERS='{"registry.terraform.io/elastic/ec":{"Protocol":"grpc","ProtocolVersion":5,"Pid":13197,"Test":true,"Addr":{"Network":"unix","String":"/tmp/plugin977285207"}}}'
```

which you can export or use to prefix every Terraform command. If that variable is not printed as soon as you connect your debugger, pausing execution and restarting it might do the trick.
4 changes: 2 additions & 2 deletions build/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ generate: gen
## Builds the source code and saves the binary to bin/terraform-provider-ec.
.PHONY: build
build: gen
@ echo "-> Building binary in $(BINARY_LOCATION)..."
@ go build -o $(BINARY_LOCATION) .
@ echo "-> Building binary with GCFLAGS=$(GCFLAGS) in $(BINARY_LOCATION)..."
@ go build $(GCFLAGS) -o $(BINARY_LOCATION) .

## Builds the source code and moves the binary to the user's terraform plugin location.
.PHONY: install
Expand Down
24 changes: 21 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,32 @@
package main

import (
"context"
"flag"
"github.com/elastic/terraform-provider-ec/ec"
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
"log"
)

//go:generate go run ./gen/gen.go

// ProviderName contains the full name for this terraform provider.
const ProviderName = "registry.terraform.io/elastic/ec"

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: ec.Provider,
})
var debugMode bool
flag.BoolVar(&debugMode, "debug", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()

opts := &plugin.ServeOpts{ProviderFunc: ec.Provider}

if debugMode {
err := plugin.Debug(context.Background(), ProviderName, opts)
if err != nil {
log.Fatal(err.Error())
}
return
}

plugin.Serve(opts)
}

0 comments on commit 62cec21

Please sign in to comment.