example_function.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) HashiCorp, Inc.
  2. // SPDX-License-Identifier: MPL-2.0
  3. package provider
  4. import (
  5. "context"
  6. "github.com/hashicorp/terraform-plugin-framework/function"
  7. )
  8. var (
  9. _ function.Function = ExampleFunction{}
  10. )
  11. func NewExampleFunction() function.Function {
  12. return ExampleFunction{}
  13. }
  14. type ExampleFunction struct{}
  15. func (r ExampleFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
  16. resp.Name = "example"
  17. }
  18. func (r ExampleFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) {
  19. resp.Definition = function.Definition{
  20. Summary: "Example function",
  21. MarkdownDescription: "Echoes given argument as result",
  22. Parameters: []function.Parameter{
  23. function.StringParameter{
  24. Name: "input",
  25. MarkdownDescription: "String to echo",
  26. },
  27. },
  28. Return: function.StringReturn{},
  29. }
  30. }
  31. func (r ExampleFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
  32. var data string
  33. resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &data))
  34. if resp.Error != nil {
  35. return
  36. }
  37. resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, data))
  38. }