Skip to content
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

all: Initial provider defined functions implementation #209

Merged
merged 7 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20231107-141509.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'all: Upgrade protocol versions to support provider-defined functions'
time: 2023-11-07T14:15:09.783296-05:00
custom:
Issue: "209"
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/google/go-cmp v0.6.0
github.com/hashicorp/terraform-plugin-go v0.19.1
github.com/hashicorp/terraform-plugin-go v0.19.2-0.20231212192300-fb1d856be38c
github.com/hashicorp/terraform-plugin-log v0.9.0
google.golang.org/grpc v1.59.0
)
Expand All @@ -13,11 +13,11 @@ require (
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-plugin v1.5.2 // indirect
github.com/hashicorp/go-plugin v1.6.0 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c=
github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-plugin v1.5.2 h1:aWv8eimFqWlsEiMrYZdPYl+FdHaBJSN4AWwGWfT1G2Y=
github.com/hashicorp/go-plugin v1.5.2/go.mod h1:w1sAEES3g3PuV/RzUrgow20W2uErMly84hhD3um1WL4=
github.com/hashicorp/go-plugin v1.6.0 h1:wgd4KxHJTVGGqWBq4QPB1i5BZNEx9BR8+OFmHDmTk8A=
github.com/hashicorp/go-plugin v1.6.0/go.mod h1:lBS5MtSSBZk0SHc66KACcjjlU6WzEVP/8pwz68aMkCI=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/terraform-plugin-go v0.19.1 h1:lf/jTGTeELcz5IIbn/94mJdmnTjRYm6S6ct/JqCSr50=
github.com/hashicorp/terraform-plugin-go v0.19.1/go.mod h1:5NMIS+DXkfacX6o5HCpswda5yjkSYfKzn1Nfl9l+qRs=
github.com/hashicorp/terraform-plugin-go v0.19.2-0.20231212192300-fb1d856be38c h1:+/OS6VVVQZ3mdF1H2KbMYxKp329S61MDbNLQs4L1eCg=
github.com/hashicorp/terraform-plugin-go v0.19.2-0.20231212192300-fb1d856be38c/go.mod h1:dYHQT4x4qtYsGsD0ZmWo0JJz9rIxSv/ETdK8p+1qlhI=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI=
github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM=
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
Expand Down
24 changes: 24 additions & 0 deletions internal/tf5testserver/tf5testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ var _ tfprotov5.ProviderServer = &TestServer{}
type TestServer struct {
ApplyResourceChangeCalled map[string]bool

CallFunctionCalled map[string]bool

ConfigureProviderCalled bool
ConfigureProviderResponse *tfprotov5.ConfigureProviderResponse

GetFunctionsCalled bool
GetFunctionsResponse *tfprotov5.GetFunctionsResponse

GetMetadataCalled bool
GetMetadataResponse *tfprotov5.GetMetadataResponse

Expand Down Expand Up @@ -59,6 +64,15 @@ func (s *TestServer) ApplyResourceChange(_ context.Context, req *tfprotov5.Apply
return nil, nil
}

func (s *TestServer) CallFunction(_ context.Context, req *tfprotov5.CallFunctionRequest) (*tfprotov5.CallFunctionResponse, error) {
if s.CallFunctionCalled == nil {
s.CallFunctionCalled = make(map[string]bool)
}

s.CallFunctionCalled[req.Name] = true
return nil, nil
}

func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov5.ConfigureProviderRequest) (*tfprotov5.ConfigureProviderResponse, error) {
s.ConfigureProviderCalled = true

Expand All @@ -69,6 +83,16 @@ func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov5.Configure
return &tfprotov5.ConfigureProviderResponse{}, nil
}

func (s *TestServer) GetFunctions(_ context.Context, _ *tfprotov5.GetFunctionsRequest) (*tfprotov5.GetFunctionsResponse, error) {
s.GetFunctionsCalled = true

if s.GetFunctionsResponse != nil {
return s.GetFunctionsResponse, nil
}

return &tfprotov5.GetFunctionsResponse{}, nil
}

func (s *TestServer) GetMetadata(_ context.Context, _ *tfprotov5.GetMetadataRequest) (*tfprotov5.GetMetadataResponse, error) {
s.GetMetadataCalled = true

Expand Down
24 changes: 24 additions & 0 deletions internal/tf6testserver/tf6testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ var _ tfprotov6.ProviderServer = &TestServer{}
type TestServer struct {
ApplyResourceChangeCalled map[string]bool

CallFunctionCalled map[string]bool

ConfigureProviderCalled bool
ConfigureProviderResponse *tfprotov6.ConfigureProviderResponse

GetFunctionsCalled bool
GetFunctionsResponse *tfprotov6.GetFunctionsResponse

GetMetadataCalled bool
GetMetadataResponse *tfprotov6.GetMetadataResponse

Expand Down Expand Up @@ -59,6 +64,15 @@ func (s *TestServer) ApplyResourceChange(_ context.Context, req *tfprotov6.Apply
return nil, nil
}

func (s *TestServer) CallFunction(_ context.Context, req *tfprotov6.CallFunctionRequest) (*tfprotov6.CallFunctionResponse, error) {
if s.CallFunctionCalled == nil {
s.CallFunctionCalled = make(map[string]bool)
}

s.CallFunctionCalled[req.Name] = true
return nil, nil
}

func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov6.ConfigureProviderRequest) (*tfprotov6.ConfigureProviderResponse, error) {
s.ConfigureProviderCalled = true

Expand All @@ -69,6 +83,16 @@ func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov6.Configure
return &tfprotov6.ConfigureProviderResponse{}, nil
}

func (s *TestServer) GetFunctions(_ context.Context, _ *tfprotov6.GetFunctionsRequest) (*tfprotov6.GetFunctionsResponse, error) {
s.GetFunctionsCalled = true

if s.GetFunctionsResponse != nil {
return s.GetFunctionsResponse, nil
}

return &tfprotov6.GetFunctionsResponse{}, nil
}

func (s *TestServer) GetMetadata(_ context.Context, _ *tfprotov6.GetMetadataRequest) (*tfprotov6.GetMetadataResponse, error) {
s.GetMetadataCalled = true

Expand Down
118 changes: 118 additions & 0 deletions internal/tfprotov5tov6/tfprotov5tov6.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ func ApplyResourceChangeResponse(in *tfprotov5.ApplyResourceChangeResponse) *tfp
}
}

func CallFunctionRequest(in *tfprotov5.CallFunctionRequest) *tfprotov6.CallFunctionRequest {
if in == nil {
return nil
}

out := &tfprotov6.CallFunctionRequest{
Arguments: make([]*tfprotov6.DynamicValue, 0, len(in.Arguments)),
Name: in.Name,
}

for _, argument := range in.Arguments {
out.Arguments = append(out.Arguments, DynamicValue(argument))
}

return out
}

func CallFunctionResponse(in *tfprotov5.CallFunctionResponse) *tfprotov6.CallFunctionResponse {
if in == nil {
return nil
}

return &tfprotov6.CallFunctionResponse{
Diagnostics: Diagnostics(in.Diagnostics),
Result: DynamicValue(in.Result),
}
}

func ConfigureProviderRequest(in *tfprotov5.ConfigureProviderRequest) *tfprotov6.ConfigureProviderRequest {
if in == nil {
return nil
Expand Down Expand Up @@ -98,6 +126,84 @@ func DynamicValue(in *tfprotov5.DynamicValue) *tfprotov6.DynamicValue {
}
}

func Function(in *tfprotov5.Function) *tfprotov6.Function {
if in == nil {
return nil
}

out := &tfprotov6.Function{
DeprecationMessage: in.DeprecationMessage,
Description: in.Description,
DescriptionKind: StringKind(in.DescriptionKind),
Parameters: make([]*tfprotov6.FunctionParameter, 0, len(in.Parameters)),
Return: FunctionReturn(in.Return),
Summary: in.Summary,
VariadicParameter: FunctionParameter(in.VariadicParameter),
}

for _, parameter := range in.Parameters {
out.Parameters = append(out.Parameters, FunctionParameter(parameter))
}

return out
}

func FunctionMetadata(in tfprotov5.FunctionMetadata) tfprotov6.FunctionMetadata {
return tfprotov6.FunctionMetadata{
Name: in.Name,
}
}

func FunctionParameter(in *tfprotov5.FunctionParameter) *tfprotov6.FunctionParameter {
if in == nil {
return nil
}

return &tfprotov6.FunctionParameter{
AllowNullValue: in.AllowNullValue,
AllowUnknownValues: in.AllowUnknownValues,
Description: in.Description,
DescriptionKind: StringKind(in.DescriptionKind),
Name: in.Name,
Type: in.Type,
}
}

func FunctionReturn(in *tfprotov5.FunctionReturn) *tfprotov6.FunctionReturn {
if in == nil {
return nil
}

return &tfprotov6.FunctionReturn{
Type: in.Type,
}
}

func GetFunctionsRequest(in *tfprotov5.GetFunctionsRequest) *tfprotov6.GetFunctionsRequest {
if in == nil {
return nil
}

return &tfprotov6.GetFunctionsRequest{}
}

func GetFunctionsResponse(in *tfprotov5.GetFunctionsResponse) *tfprotov6.GetFunctionsResponse {
if in == nil {
return nil
}

functions := make(map[string]*tfprotov6.Function, len(in.Functions))

for name, function := range in.Functions {
functions[name] = Function(function)
}

return &tfprotov6.GetFunctionsResponse{
Diagnostics: Diagnostics(in.Diagnostics),
Functions: functions,
}
}

func GetMetadataRequest(in *tfprotov5.GetMetadataRequest) *tfprotov6.GetMetadataRequest {
if in == nil {
return nil
Expand All @@ -114,6 +220,7 @@ func GetMetadataResponse(in *tfprotov5.GetMetadataResponse) *tfprotov6.GetMetada
resp := &tfprotov6.GetMetadataResponse{
DataSources: make([]tfprotov6.DataSourceMetadata, 0, len(in.DataSources)),
Diagnostics: Diagnostics(in.Diagnostics),
Functions: make([]tfprotov6.FunctionMetadata, 0, len(in.Functions)),
Resources: make([]tfprotov6.ResourceMetadata, 0, len(in.Resources)),
ServerCapabilities: ServerCapabilities(in.ServerCapabilities),
}
Expand All @@ -122,6 +229,10 @@ func GetMetadataResponse(in *tfprotov5.GetMetadataResponse) *tfprotov6.GetMetada
resp.DataSources = append(resp.DataSources, DataSourceMetadata(datasource))
}

for _, function := range in.Functions {
resp.Functions = append(resp.Functions, FunctionMetadata(function))
}

for _, resource := range in.Resources {
resp.Resources = append(resp.Resources, ResourceMetadata(resource))
}
Expand All @@ -148,6 +259,12 @@ func GetProviderSchemaResponse(in *tfprotov5.GetProviderSchemaResponse) *tfproto
dataSourceSchemas[k] = Schema(v)
}

functions := make(map[string]*tfprotov6.Function, len(in.Functions))

for name, function := range in.Functions {
functions[name] = Function(function)
}

resourceSchemas := make(map[string]*tfprotov6.Schema, len(in.ResourceSchemas))

for k, v := range in.ResourceSchemas {
Expand All @@ -157,6 +274,7 @@ func GetProviderSchemaResponse(in *tfprotov5.GetProviderSchemaResponse) *tfproto
return &tfprotov6.GetProviderSchemaResponse{
DataSourceSchemas: dataSourceSchemas,
Diagnostics: Diagnostics(in.Diagnostics),
Functions: functions,
Provider: Schema(in.Provider),
ProviderMeta: Schema(in.ProviderMeta),
ResourceSchemas: resourceSchemas,
Expand Down
Loading
Loading