
Reference: https://developer.hashicorp.com/terraform/plugin/framework/functions Reference: https://github.com/hashicorp/terraform-providers-devex-internal/issues/175 Terraform 1.8 and later introduce the concept of provider-defined functions and this adds an example function that accepts a string argument and returns the same value as a string return. Dependencies updated via: ``` go get github.com/hashicorp/terraform-plugin-framework@v1.6.0 go get github.com/hashicorp/terraform-plugin-docs@v0.18.0 go mod tidy ```
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/function"
|
|
)
|
|
|
|
var (
|
|
_ function.Function = ExampleFunction{}
|
|
)
|
|
|
|
func NewExampleFunction() function.Function {
|
|
return ExampleFunction{}
|
|
}
|
|
|
|
type ExampleFunction struct{}
|
|
|
|
func (r ExampleFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
|
|
resp.Name = "example"
|
|
}
|
|
|
|
func (r ExampleFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) {
|
|
resp.Definition = function.Definition{
|
|
Summary: "Example function",
|
|
MarkdownDescription: "Echoes given argument as result",
|
|
Parameters: []function.Parameter{
|
|
function.StringParameter{
|
|
Name: "input",
|
|
MarkdownDescription: "String to echo",
|
|
},
|
|
},
|
|
Return: function.StringReturn{},
|
|
}
|
|
}
|
|
|
|
func (r ExampleFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
|
|
var data string
|
|
|
|
resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &data))
|
|
|
|
if resp.Error != nil {
|
|
return
|
|
}
|
|
|
|
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, data))
|
|
}
|