diff --git a/internal/providers/functions.go b/internal/providers/functions.go new file mode 100644 index 0000000000..55f6ccc03a --- /dev/null +++ b/internal/providers/functions.go @@ -0,0 +1,30 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package providers + +import ( + "github.com/zclconf/go-cty/cty" + + "github.com/hashicorp/terraform/internal/configs/configschema" +) + +type FunctionDecl struct { + Parameters []FunctionParam + VariadicParameter *FunctionParam + ReturnType cty.Type + + Description string + DescriptionKind configschema.StringKind +} + +type FunctionParam struct { + Name string // Only for documentation and UI, because arguments are positional + Type cty.Type + + Nullable bool + AllowUnknownValues bool + + Description string + DescriptionKind configschema.StringKind +} diff --git a/internal/providers/provider.go b/internal/providers/provider.go index bfaf2daa40..0702f8e45e 100644 --- a/internal/providers/provider.go +++ b/internal/providers/provider.go @@ -70,6 +70,9 @@ type Interface interface { // ReadDataSource returns the data source's current state. ReadDataSource(ReadDataSourceRequest) ReadDataSourceResponse + // CallFunction calls a provider-contributed function. + CallFunction(CallFunctionRequest) CallFunctionResponse + // Close shuts down the plugin process if applicable. Close() error } @@ -92,6 +95,10 @@ type GetProviderSchemaResponse struct { // DataSources maps the data source name to that data source's schema. DataSources map[string]Schema + // Functions maps from local function name (not including an namespace + // prefix) to the declaration of a function. + Functions map[string]FunctionDecl + // Diagnostics contains any warnings or errors from the method call. Diagnostics tfdiags.Diagnostics @@ -417,3 +424,35 @@ type ReadDataSourceResponse struct { // Diagnostics contains any warnings or errors from the method call. Diagnostics tfdiags.Diagnostics } + +type CallFunctionRequest struct { + // FunctionName is the local name of the function to call, as it was + // declared by the provider in its schema and without any + // externally-imposed namespace prefixes. + FunctionName string + + // Arguments are the positional argument values given at the call site. + // + // Provider functions are required to behave as pure functions, and so + // if all of the argument values are known then two separate calls with the + // same arguments must always return an identical value, without performing + // any externally-visible side-effects. + Arguments []cty.Value +} + +type CallFunctionResponse struct { + // Result is the successful result of the function call. + // + // If all of the arguments in the call were known then the result must + // also be known. If any arguments were unknown then the result may + // optionally be unknown. The type of the returned value must conform + // to the return type constraint for this function as declared in the + // provider schema. + // + // If Diagnostics contains any errors, this field will be ignored and + // so can be left as cty.NilVal to represent the absense of a value. + Result cty.Value + + // Diagnostics contains any warnings or errors from the function call. + Diagnostics tfdiags.Diagnostics +}