Skip to content

Commit 3d1b409

Browse files
authored
all: Initial provider defined functions implementation (#209)
Reference: hashicorp/terraform-plugin-go#351 The next versions of the plugin protocol (5.5/6.5) include support for provider defined functions. This change includes initial implementation of that support including: - Temporarily pointing at terraform-plugin-go with provider function support (will be pointed at final terraform-plugin-go release before merge) - Updates to all provider server packages for new `GetFunctions` and `CallFunction` RPCs
1 parent 58143df commit 3d1b409

33 files changed

+3991
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: FEATURES
2+
body: 'all: Upgrade protocol versions to support provider-defined functions'
3+
time: 2023-11-07T14:15:09.783296-05:00
4+
custom:
5+
Issue: "209"

internal/tf5testserver/tf5testserver.go

+24
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ var _ tfprotov5.ProviderServer = &TestServer{}
1616
type TestServer struct {
1717
ApplyResourceChangeCalled map[string]bool
1818

19+
CallFunctionCalled map[string]bool
20+
1921
ConfigureProviderCalled bool
2022
ConfigureProviderResponse *tfprotov5.ConfigureProviderResponse
2123

24+
GetFunctionsCalled bool
25+
GetFunctionsResponse *tfprotov5.GetFunctionsResponse
26+
2227
GetMetadataCalled bool
2328
GetMetadataResponse *tfprotov5.GetMetadataResponse
2429

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

67+
func (s *TestServer) CallFunction(_ context.Context, req *tfprotov5.CallFunctionRequest) (*tfprotov5.CallFunctionResponse, error) {
68+
if s.CallFunctionCalled == nil {
69+
s.CallFunctionCalled = make(map[string]bool)
70+
}
71+
72+
s.CallFunctionCalled[req.Name] = true
73+
return nil, nil
74+
}
75+
6276
func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov5.ConfigureProviderRequest) (*tfprotov5.ConfigureProviderResponse, error) {
6377
s.ConfigureProviderCalled = true
6478

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

86+
func (s *TestServer) GetFunctions(_ context.Context, _ *tfprotov5.GetFunctionsRequest) (*tfprotov5.GetFunctionsResponse, error) {
87+
s.GetFunctionsCalled = true
88+
89+
if s.GetFunctionsResponse != nil {
90+
return s.GetFunctionsResponse, nil
91+
}
92+
93+
return &tfprotov5.GetFunctionsResponse{}, nil
94+
}
95+
7296
func (s *TestServer) GetMetadata(_ context.Context, _ *tfprotov5.GetMetadataRequest) (*tfprotov5.GetMetadataResponse, error) {
7397
s.GetMetadataCalled = true
7498

internal/tf6testserver/tf6testserver.go

+24
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ var _ tfprotov6.ProviderServer = &TestServer{}
1616
type TestServer struct {
1717
ApplyResourceChangeCalled map[string]bool
1818

19+
CallFunctionCalled map[string]bool
20+
1921
ConfigureProviderCalled bool
2022
ConfigureProviderResponse *tfprotov6.ConfigureProviderResponse
2123

24+
GetFunctionsCalled bool
25+
GetFunctionsResponse *tfprotov6.GetFunctionsResponse
26+
2227
GetMetadataCalled bool
2328
GetMetadataResponse *tfprotov6.GetMetadataResponse
2429

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

67+
func (s *TestServer) CallFunction(_ context.Context, req *tfprotov6.CallFunctionRequest) (*tfprotov6.CallFunctionResponse, error) {
68+
if s.CallFunctionCalled == nil {
69+
s.CallFunctionCalled = make(map[string]bool)
70+
}
71+
72+
s.CallFunctionCalled[req.Name] = true
73+
return nil, nil
74+
}
75+
6276
func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov6.ConfigureProviderRequest) (*tfprotov6.ConfigureProviderResponse, error) {
6377
s.ConfigureProviderCalled = true
6478

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

86+
func (s *TestServer) GetFunctions(_ context.Context, _ *tfprotov6.GetFunctionsRequest) (*tfprotov6.GetFunctionsResponse, error) {
87+
s.GetFunctionsCalled = true
88+
89+
if s.GetFunctionsResponse != nil {
90+
return s.GetFunctionsResponse, nil
91+
}
92+
93+
return &tfprotov6.GetFunctionsResponse{}, nil
94+
}
95+
7296
func (s *TestServer) GetMetadata(_ context.Context, _ *tfprotov6.GetMetadataRequest) (*tfprotov6.GetMetadataResponse, error) {
7397
s.GetMetadataCalled = true
7498

internal/tfprotov5tov6/tfprotov5tov6.go

+118
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,34 @@ func ApplyResourceChangeResponse(in *tfprotov5.ApplyResourceChangeResponse) *tfp
3636
}
3737
}
3838

39+
func CallFunctionRequest(in *tfprotov5.CallFunctionRequest) *tfprotov6.CallFunctionRequest {
40+
if in == nil {
41+
return nil
42+
}
43+
44+
out := &tfprotov6.CallFunctionRequest{
45+
Arguments: make([]*tfprotov6.DynamicValue, 0, len(in.Arguments)),
46+
Name: in.Name,
47+
}
48+
49+
for _, argument := range in.Arguments {
50+
out.Arguments = append(out.Arguments, DynamicValue(argument))
51+
}
52+
53+
return out
54+
}
55+
56+
func CallFunctionResponse(in *tfprotov5.CallFunctionResponse) *tfprotov6.CallFunctionResponse {
57+
if in == nil {
58+
return nil
59+
}
60+
61+
return &tfprotov6.CallFunctionResponse{
62+
Diagnostics: Diagnostics(in.Diagnostics),
63+
Result: DynamicValue(in.Result),
64+
}
65+
}
66+
3967
func ConfigureProviderRequest(in *tfprotov5.ConfigureProviderRequest) *tfprotov6.ConfigureProviderRequest {
4068
if in == nil {
4169
return nil
@@ -98,6 +126,84 @@ func DynamicValue(in *tfprotov5.DynamicValue) *tfprotov6.DynamicValue {
98126
}
99127
}
100128

129+
func Function(in *tfprotov5.Function) *tfprotov6.Function {
130+
if in == nil {
131+
return nil
132+
}
133+
134+
out := &tfprotov6.Function{
135+
DeprecationMessage: in.DeprecationMessage,
136+
Description: in.Description,
137+
DescriptionKind: StringKind(in.DescriptionKind),
138+
Parameters: make([]*tfprotov6.FunctionParameter, 0, len(in.Parameters)),
139+
Return: FunctionReturn(in.Return),
140+
Summary: in.Summary,
141+
VariadicParameter: FunctionParameter(in.VariadicParameter),
142+
}
143+
144+
for _, parameter := range in.Parameters {
145+
out.Parameters = append(out.Parameters, FunctionParameter(parameter))
146+
}
147+
148+
return out
149+
}
150+
151+
func FunctionMetadata(in tfprotov5.FunctionMetadata) tfprotov6.FunctionMetadata {
152+
return tfprotov6.FunctionMetadata{
153+
Name: in.Name,
154+
}
155+
}
156+
157+
func FunctionParameter(in *tfprotov5.FunctionParameter) *tfprotov6.FunctionParameter {
158+
if in == nil {
159+
return nil
160+
}
161+
162+
return &tfprotov6.FunctionParameter{
163+
AllowNullValue: in.AllowNullValue,
164+
AllowUnknownValues: in.AllowUnknownValues,
165+
Description: in.Description,
166+
DescriptionKind: StringKind(in.DescriptionKind),
167+
Name: in.Name,
168+
Type: in.Type,
169+
}
170+
}
171+
172+
func FunctionReturn(in *tfprotov5.FunctionReturn) *tfprotov6.FunctionReturn {
173+
if in == nil {
174+
return nil
175+
}
176+
177+
return &tfprotov6.FunctionReturn{
178+
Type: in.Type,
179+
}
180+
}
181+
182+
func GetFunctionsRequest(in *tfprotov5.GetFunctionsRequest) *tfprotov6.GetFunctionsRequest {
183+
if in == nil {
184+
return nil
185+
}
186+
187+
return &tfprotov6.GetFunctionsRequest{}
188+
}
189+
190+
func GetFunctionsResponse(in *tfprotov5.GetFunctionsResponse) *tfprotov6.GetFunctionsResponse {
191+
if in == nil {
192+
return nil
193+
}
194+
195+
functions := make(map[string]*tfprotov6.Function, len(in.Functions))
196+
197+
for name, function := range in.Functions {
198+
functions[name] = Function(function)
199+
}
200+
201+
return &tfprotov6.GetFunctionsResponse{
202+
Diagnostics: Diagnostics(in.Diagnostics),
203+
Functions: functions,
204+
}
205+
}
206+
101207
func GetMetadataRequest(in *tfprotov5.GetMetadataRequest) *tfprotov6.GetMetadataRequest {
102208
if in == nil {
103209
return nil
@@ -114,6 +220,7 @@ func GetMetadataResponse(in *tfprotov5.GetMetadataResponse) *tfprotov6.GetMetada
114220
resp := &tfprotov6.GetMetadataResponse{
115221
DataSources: make([]tfprotov6.DataSourceMetadata, 0, len(in.DataSources)),
116222
Diagnostics: Diagnostics(in.Diagnostics),
223+
Functions: make([]tfprotov6.FunctionMetadata, 0, len(in.Functions)),
117224
Resources: make([]tfprotov6.ResourceMetadata, 0, len(in.Resources)),
118225
ServerCapabilities: ServerCapabilities(in.ServerCapabilities),
119226
}
@@ -122,6 +229,10 @@ func GetMetadataResponse(in *tfprotov5.GetMetadataResponse) *tfprotov6.GetMetada
122229
resp.DataSources = append(resp.DataSources, DataSourceMetadata(datasource))
123230
}
124231

232+
for _, function := range in.Functions {
233+
resp.Functions = append(resp.Functions, FunctionMetadata(function))
234+
}
235+
125236
for _, resource := range in.Resources {
126237
resp.Resources = append(resp.Resources, ResourceMetadata(resource))
127238
}
@@ -148,6 +259,12 @@ func GetProviderSchemaResponse(in *tfprotov5.GetProviderSchemaResponse) *tfproto
148259
dataSourceSchemas[k] = Schema(v)
149260
}
150261

262+
functions := make(map[string]*tfprotov6.Function, len(in.Functions))
263+
264+
for name, function := range in.Functions {
265+
functions[name] = Function(function)
266+
}
267+
151268
resourceSchemas := make(map[string]*tfprotov6.Schema, len(in.ResourceSchemas))
152269

153270
for k, v := range in.ResourceSchemas {
@@ -157,6 +274,7 @@ func GetProviderSchemaResponse(in *tfprotov5.GetProviderSchemaResponse) *tfproto
157274
return &tfprotov6.GetProviderSchemaResponse{
158275
DataSourceSchemas: dataSourceSchemas,
159276
Diagnostics: Diagnostics(in.Diagnostics),
277+
Functions: functions,
160278
Provider: Schema(in.Provider),
161279
ProviderMeta: Schema(in.ProviderMeta),
162280
ResourceSchemas: resourceSchemas,

0 commit comments

Comments
 (0)