-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tfsdk: Added Debug field to ServeOpts for running providers via debugger and testing processes #243
Conversation
go.mod
Outdated
@@ -4,6 +4,7 @@ go 1.17 | |||
|
|||
require ( | |||
github.com/google/go-cmp v0.5.6 | |||
github.com/hashicorp/go-plugin v1.4.3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This is temporary until terraform-plugin-go is updated: hashicorp/terraform-plugin-go#123
tfsdk/debug.go
Outdated
// Debug runs the provider in a mode acceptable for debugging and testing | ||
// processes, such as delve, by managing the process lifecycle. Information | ||
// needed for Terraform CLI to connect to the provider is output to stdout. | ||
// os.Interrupt (Ctrl-c) can be used to stop the provider. | ||
func Debug(ctx context.Context, providerFunc func() Provider, opts ServeOpts) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside: Instead of introducing an exported function, we could also implement this by adding a Debug bool
field to ServeOpts
, then triggering this logic via an unexported function. Thinking about that again, maybe that would be a cleaner implementation for providers rather than forcing them to implement the conditional between serving functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opted to go with the ServeOpts
approach!
Switched this to using a Verified with this main function: func main() {
var debug bool
flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()
ctx := context.Background()
serveOpts := tfsdk.ServeOpts{
Debug: debug,
Name: "registry.terraform.io/bflad/framework",
}
err := tfsdk.Serve(ctx, provider.New, serveOpts)
if err != nil {
fmt.Printf("Error serving provider: %s", err)
os.Exit(1)
}
} |
1e4ca4c
to
db5395b
Compare
Will this work with a muxed provider? |
terraform-plugin-mux would likely require its own debug functionality, since providers are served using the terraform-plugin-go server implementations, rather than the SDK or framework implementations. I'll open an issue to investigate that further there, thanks for the call out! |
Almost all of this code could be removed via an upstream implementation such as: hashicorp/terraform-plugin-go#137 func Serve(ctx context.Context, providerFunc func() Provider, opts ServeOpts) error {
var tf6serverOpts []tf6server.ServeOpt
if opts.Debug {
tf6serverOpts = append(tf6serverOpts, tf6server.WithManagedDebug())
}
return tf6server.Serve(opts.Name, func() tfprotov6.ProviderServer {
return &server{
p: providerFunc(),
}
}, tf6serverOpts...)
} It may be worth getting that in before recreating the implementation here. |
…sting processes Reference: #239 Reference: https://www.terraform.io/plugin/sdkv2/debugging Similar to Terraform Plugin SDK, providers need to be executable via debugger and testing processes for instrumenting the provider code. In this workflow, the caller manages the provider lifecycle, rather than Terraform CLI. The Terraform CLI reattach configuration is output to stdout.
db5395b
to
fb224a9
Compare
Rebased on terraform-plugin-go@v0.6.0 and ready for review. 👍 |
LGTM 🚀. |
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. |
Closes #239
Reference: https://www.terraform.io/plugin/sdkv2/debugging
Similar to Terraform Plugin SDK, providers need to be executable via debugger and testing processes for instrumenting the provider code. In this workflow, the caller manages the provider lifecycle, rather than Terraform CLI. The Terraform CLI reattach configuration is output to stdout.
Verified in a framework-based provider by:
go mod edit -replace=github.com/hashicorp/terraform-plugin-framework=/Users/bflad/src/github.com/hashicorp/terraform-plugin-framework
(use this worktree)main
function code to.vscode/launch.json
(refer also to terraform-provider-scaffolding)