Skip to content

Commit ac6a95b

Browse files
committed
Add ValidateActionConfig to provider interface and protocol
1 parent ba361f2 commit ac6a95b

File tree

24 files changed

+2536
-1608
lines changed

24 files changed

+2536
-1608
lines changed

docs/plugin-protocol/tfplugin5.proto

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ service Provider {
405405
//////// Actions
406406
rpc PlanAction(PlanAction.Request) returns (PlanAction.Response);
407407
rpc InvokeAction(InvokeAction.Request) returns (stream InvokeAction.Event);
408+
rpc ValidateActionConfig(ValidateActionConfig.Request) returns (ValidateActionConfig.Response);
408409

409410
//////// Graceful Shutdown
410411
rpc Stop(Stop.Request) returns (Stop.Response);
@@ -994,3 +995,19 @@ message InvokeAction {
994995
}
995996
}
996997
}
998+
999+
message ValidateActionConfig {
1000+
message Request {
1001+
string type_name = 1;
1002+
DynamicValue config = 2;
1003+
repeated LinkedResourceConfig linked_resources = 3;
1004+
}
1005+
message Response {
1006+
repeated Diagnostic diagnostics = 1;
1007+
}
1008+
}
1009+
1010+
message LinkedResourceConfig {
1011+
string type_name = 1;
1012+
DynamicValue config = 2;
1013+
}

docs/plugin-protocol/tfplugin6.proto

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ service Provider {
433433
//////// Actions
434434
rpc PlanAction(PlanAction.Request) returns (PlanAction.Response);
435435
rpc InvokeAction(InvokeAction.Request) returns (stream InvokeAction.Event);
436+
rpc ValidateActionConfig(ValidateActionConfig.Request) returns (ValidateActionConfig.Response);
436437

437438
//////// Graceful Shutdown
438439
rpc StopProvider(StopProvider.Request) returns (StopProvider.Response);
@@ -1033,3 +1034,19 @@ message InvokeAction {
10331034
}
10341035
}
10351036
}
1037+
1038+
message ValidateActionConfig {
1039+
message Request {
1040+
string type_name = 1;
1041+
DynamicValue config = 2;
1042+
repeated LinkedResourceConfig linked_resources = 3;
1043+
}
1044+
message Response {
1045+
repeated Diagnostic diagnostics = 1;
1046+
}
1047+
}
1048+
1049+
message LinkedResourceConfig {
1050+
string type_name = 1;
1051+
DynamicValue config = 2;
1052+
}

internal/builtin/providers/terraform/provider.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"fmt"
88
"log"
99

10+
tfaddr "github.com/hashicorp/terraform-registry-address"
1011
"github.com/zclconf/go-cty/cty"
1112

12-
tfaddr "github.com/hashicorp/terraform-registry-address"
1313
"github.com/hashicorp/terraform/internal/providers"
1414
)
1515

@@ -325,6 +325,17 @@ func (p *Provider) InvokeAction(req providers.InvokeActionRequest) providers.Inv
325325
return resp
326326
}
327327

328+
func (p *Provider) ValidateActionConfig(req providers.ValidateActionConfigRequest) providers.ValidateActionConfigResponse {
329+
var resp providers.ValidateActionConfigResponse
330+
331+
switch req.TypeName {
332+
default:
333+
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unsupported action %q", req.TypeName))
334+
}
335+
336+
return resp
337+
}
338+
328339
// Close is a noop for this provider, since it's run in-process.
329340
func (p *Provider) Close() error {
330341
return nil

internal/grpcwrap/provider.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,45 @@ func (p *provider) InvokeAction(req *tfplugin5.InvokeAction_Request, server tfpl
10731073
return nil
10741074
}
10751075

1076+
func (p *provider) ValidateActionConfig(_ context.Context, req *tfplugin5.ValidateActionConfig_Request) (*tfplugin5.ValidateActionConfig_Response, error) {
1077+
resp := &tfplugin5.ValidateActionConfig_Response{}
1078+
ty := p.schema.Actions[req.TypeName].ConfigSchema.ImpliedType()
1079+
1080+
configVal, err := decodeDynamicValue(req.Config, ty)
1081+
if err != nil {
1082+
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
1083+
return resp, nil
1084+
}
1085+
1086+
protoReq := providers.ValidateActionConfigRequest{
1087+
TypeName: req.TypeName,
1088+
Config: configVal,
1089+
}
1090+
1091+
lrs := make([]providers.LinkedResourceConfig, 0, len(req.LinkedResources))
1092+
for _, lr := range req.LinkedResources {
1093+
ty := p.schema.ResourceTypes[lr.TypeName].Body.ImpliedType()
1094+
1095+
configVal, err := decodeDynamicValue(lr.Config, ty)
1096+
if err != nil {
1097+
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
1098+
return resp, nil
1099+
}
1100+
lrs = append(lrs, providers.LinkedResourceConfig{
1101+
TypeName: lr.TypeName,
1102+
Config: configVal,
1103+
})
1104+
}
1105+
1106+
if len(lrs) > 0 {
1107+
protoReq.LinkedResources = lrs
1108+
}
1109+
1110+
validateResp := p.provider.ValidateActionConfig(protoReq)
1111+
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, validateResp.Diagnostics)
1112+
return resp, nil
1113+
}
1114+
10761115
func (p *provider) Stop(context.Context, *tfplugin5.Stop_Request) (*tfplugin5.Stop_Response, error) {
10771116
resp := &tfplugin5.Stop_Response{}
10781117
err := p.provider.Stop()

internal/grpcwrap/provider6.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,45 @@ func (p *provider6) InvokeAction(req *tfplugin6.InvokeAction_Request, server tfp
11451145
return nil
11461146
}
11471147

1148+
func (p *provider6) ValidateActionConfig(_ context.Context, req *tfplugin6.ValidateActionConfig_Request) (*tfplugin6.ValidateActionConfig_Response, error) {
1149+
resp := &tfplugin6.ValidateActionConfig_Response{}
1150+
ty := p.schema.Actions[req.TypeName].ConfigSchema.ImpliedType()
1151+
1152+
configVal, err := decodeDynamicValue6(req.Config, ty)
1153+
if err != nil {
1154+
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
1155+
return resp, nil
1156+
}
1157+
1158+
protoReq := providers.ValidateActionConfigRequest{
1159+
TypeName: req.TypeName,
1160+
Config: configVal,
1161+
}
1162+
1163+
lrs := make([]providers.LinkedResourceConfig, 0, len(req.LinkedResources))
1164+
for _, lr := range req.LinkedResources {
1165+
ty := p.schema.ResourceTypes[lr.TypeName].Body.ImpliedType()
1166+
1167+
configVal, err := decodeDynamicValue6(lr.Config, ty)
1168+
if err != nil {
1169+
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
1170+
return resp, nil
1171+
}
1172+
lrs = append(lrs, providers.LinkedResourceConfig{
1173+
TypeName: lr.TypeName,
1174+
Config: configVal,
1175+
})
1176+
}
1177+
1178+
if len(lrs) > 0 {
1179+
protoReq.LinkedResources = lrs
1180+
}
1181+
1182+
validateResp := p.provider.ValidateActionConfig(protoReq)
1183+
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, validateResp.Diagnostics)
1184+
return resp, nil
1185+
}
1186+
11481187
func (p *provider6) StopProvider(context.Context, *tfplugin6.StopProvider_Request) (*tfplugin6.StopProvider_Response, error) {
11491188
resp := &tfplugin6.StopProvider_Response{}
11501189
err := p.provider.Stop()

internal/plans/planfile/tfplan.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ func writeTfplan(plan *plans.Plan, w io.Writer) error {
578578
ResourceChanges: []*planproto.ResourceInstanceChange{},
579579
ResourceDrift: []*planproto.ResourceInstanceChange{},
580580
DeferredChanges: []*planproto.DeferredResourceInstanceChange{},
581-
ActionInvocations: []*planproto.ActionInvocation{},
581+
ActionInvocations: []*planproto.ActionInvocationInstance{},
582582
}
583583

584584
rawPlan.Applyable = plan.Applyable
@@ -1245,7 +1245,7 @@ func CheckResultsToPlanProto(checkResults *states.CheckResults) ([]*planproto.Ch
12451245
}
12461246
}
12471247

1248-
func actionInvocationFromTfplan(rawAction *planproto.ActionInvocation) (*plans.ActionInvocationInstanceSrc, error) {
1248+
func actionInvocationFromTfplan(rawAction *planproto.ActionInvocationInstance) (*plans.ActionInvocationInstanceSrc, error) {
12491249
if rawAction == nil {
12501250
// Should never happen in practice, since protobuf can't represent
12511251
// a nil value in a list.
@@ -1268,12 +1268,12 @@ func actionInvocationFromTfplan(rawAction *planproto.ActionInvocation) (*plans.A
12681268
return ret, nil
12691269
}
12701270

1271-
func actionInvocationToTfPlan(action *plans.ActionInvocationInstanceSrc) (*planproto.ActionInvocation, error) {
1271+
func actionInvocationToTfPlan(action *plans.ActionInvocationInstanceSrc) (*planproto.ActionInvocationInstance, error) {
12721272
if action == nil {
12731273
return nil, nil
12741274
}
12751275

1276-
ret := &planproto.ActionInvocation{
1276+
ret := &planproto.ActionInvocationInstance{
12771277
Addr: action.Addr.String(),
12781278
Provider: action.ProviderAddr.String(),
12791279
}

0 commit comments

Comments
 (0)