Skip to content

Commit 044e4d5

Browse files
Merge pull request #36269 from hashicorp/std-client-capabilities
refactoring: standardize client capabilities
2 parents 276662f + 0ff1b77 commit 044e4d5

File tree

11 files changed

+81
-81
lines changed

11 files changed

+81
-81
lines changed

internal/grpcwrap/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func (p *provider) ValidateResourceTypeConfig(_ context.Context, req *tfplugin5.
122122
TypeName: req.TypeName,
123123
Config: configVal,
124124
ClientCapabilities: providers.ClientCapabilities{
125+
DeferralAllowed: true,
125126
WriteOnlyAttributesAllowed: true,
126127
},
127128
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package stackeval
5+
6+
import "github.com/hashicorp/terraform/internal/providers"
7+
8+
// ClientCapabilities returns the client capabilities sent to the providers
9+
// for each request. They define what this terraform instance is capable of.
10+
func ClientCapabilities() providers.ClientCapabilities {
11+
return providers.ClientCapabilities{
12+
DeferralAllowed: true,
13+
WriteOnlyAttributesAllowed: true,
14+
}
15+
}

internal/stacks/stackruntime/internal/stackeval/provider_instance.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,9 @@ func (p *ProviderInstance) CheckClient(ctx context.Context, phase EvalPhase) (pr
256256
}
257257

258258
resp := client.ConfigureProvider(providers.ConfigureProviderRequest{
259-
TerraformVersion: version.SemVer.String(),
260-
Config: unmarkedArgs,
261-
ClientCapabilities: providers.ClientCapabilities{
262-
DeferralAllowed: true,
263-
WriteOnlyAttributesAllowed: true,
264-
},
259+
TerraformVersion: version.SemVer.String(),
260+
Config: unmarkedArgs,
261+
ClientCapabilities: ClientCapabilities(),
265262
})
266263
diags = diags.Append(resp.Diagnostics)
267264
if resp.Diagnostics.HasErrors() {

internal/stacks/stackruntime/internal/stackeval/provider_instance_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,7 @@ func TestProviderInstanceCheckClient(t *testing.T) {
383383
Config: cty.ObjectVal(map[string]cty.Value{
384384
"test": cty.StringVal("yep"),
385385
}),
386-
ClientCapabilities: providers.ClientCapabilities{
387-
DeferralAllowed: true,
388-
WriteOnlyAttributesAllowed: true,
389-
},
386+
ClientCapabilities: ClientCapabilities(),
390387
}
391388
if diff := cmp.Diff(want, got, ctydebug.CmpOptions); diff != "" {
392389
t.Errorf("wrong request\n%s", diff)
@@ -426,10 +423,7 @@ func TestProviderInstanceCheckClient(t *testing.T) {
426423
Config: cty.ObjectVal(map[string]cty.Value{
427424
"test": cty.StringVal("yep"),
428425
}),
429-
ClientCapabilities: providers.ClientCapabilities{
430-
DeferralAllowed: true,
431-
WriteOnlyAttributesAllowed: true,
432-
},
426+
ClientCapabilities: ClientCapabilities(),
433427
}
434428
if diff := cmp.Diff(want, got, ctydebug.CmpOptions); diff != "" {
435429
t.Errorf("wrong request\n%s", diff)

internal/terraform/eval_context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ type EvalContext interface {
191191
// perform a planned action due to an upstream dependency being deferred.
192192
Deferrals() *deferring.Deferred
193193

194+
// ClientCapabilities returns the client capabilities sent to the providers
195+
// for each request. They define what this terraform instance is capable of.
196+
ClientCapabilities() providers.ClientCapabilities
197+
194198
// MoveResults returns a map describing the results of handling any
195199
// resource instance move statements prior to the graph walk, so that
196200
// the graph walk can then record that information appropriately in other

internal/terraform/eval_context_builtin.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,3 +613,10 @@ func (ctx *BuiltinEvalContext) Forget() bool {
613613
func (ctx *BuiltinEvalContext) EphemeralResources() *ephemeral.Resources {
614614
return ctx.EphemeralResourcesValue
615615
}
616+
617+
func (ctx *BuiltinEvalContext) ClientCapabilities() providers.ClientCapabilities {
618+
return providers.ClientCapabilities{
619+
DeferralAllowed: ctx.Deferrals().DeferralAllowed(),
620+
WriteOnlyAttributesAllowed: true,
621+
}
622+
}

internal/terraform/eval_context_mock.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,10 @@ func (c *MockEvalContext) Forget() bool {
424424
c.ForgetCalled = true
425425
return c.ForgetValues
426426
}
427+
428+
func (ctx *MockEvalContext) ClientCapabilities() providers.ClientCapabilities {
429+
return providers.ClientCapabilities{
430+
DeferralAllowed: ctx.Deferrals().DeferralAllowed(),
431+
WriteOnlyAttributesAllowed: true,
432+
}
433+
}

internal/terraform/node_resource_abstract_instance.go

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -432,16 +432,13 @@ func (n *NodeAbstractResourceInstance) planDestroy(ctx EvalContext, currentState
432432
// Allow the provider to check the destroy plan, and insert any
433433
// necessary private data.
434434
resp = provider.PlanResourceChange(providers.PlanResourceChangeRequest{
435-
TypeName: n.Addr.Resource.Resource.Type,
436-
Config: nullVal,
437-
PriorState: unmarkedPriorVal,
438-
ProposedNewState: nullVal,
439-
PriorPrivate: currentState.Private,
440-
ProviderMeta: metaConfigVal,
441-
ClientCapabilities: providers.ClientCapabilities{
442-
DeferralAllowed: deferralAllowed,
443-
WriteOnlyAttributesAllowed: true,
444-
},
435+
TypeName: n.Addr.Resource.Resource.Type,
436+
Config: nullVal,
437+
PriorState: unmarkedPriorVal,
438+
ProposedNewState: nullVal,
439+
PriorPrivate: currentState.Private,
440+
ProviderMeta: metaConfigVal,
441+
ClientCapabilities: ctx.ClientCapabilities(),
445442
})
446443
deferred = resp.Deferred
447444

@@ -635,14 +632,11 @@ func (n *NodeAbstractResourceInstance) refresh(ctx EvalContext, deposedKey state
635632
}
636633
} else {
637634
resp = provider.ReadResource(providers.ReadResourceRequest{
638-
TypeName: n.Addr.Resource.Resource.Type,
639-
PriorState: priorVal,
640-
Private: state.Private,
641-
ProviderMeta: metaConfigVal,
642-
ClientCapabilities: providers.ClientCapabilities{
643-
DeferralAllowed: deferralAllowed,
644-
WriteOnlyAttributesAllowed: true,
645-
},
635+
TypeName: n.Addr.Resource.Resource.Type,
636+
PriorState: priorVal,
637+
Private: state.Private,
638+
ProviderMeta: metaConfigVal,
639+
ClientCapabilities: ctx.ClientCapabilities(),
646640
})
647641

648642
// If we don't support deferrals, but the provider reports a deferral and does not
@@ -863,11 +857,9 @@ func (n *NodeAbstractResourceInstance) plan(
863857
unmarkedConfigVal, _ := origConfigVal.UnmarkDeep()
864858
validateResp := provider.ValidateResourceConfig(
865859
providers.ValidateResourceConfigRequest{
866-
TypeName: n.Addr.Resource.Resource.Type,
867-
Config: unmarkedConfigVal,
868-
ClientCapabilities: providers.ClientCapabilities{
869-
WriteOnlyAttributesAllowed: true,
870-
},
860+
TypeName: n.Addr.Resource.Resource.Type,
861+
Config: unmarkedConfigVal,
862+
ClientCapabilities: ctx.ClientCapabilities(),
871863
},
872864
)
873865
diags = diags.Append(validateResp.Diagnostics.InConfigBody(config.Config, n.Addr.String()))
@@ -926,16 +918,13 @@ func (n *NodeAbstractResourceInstance) plan(
926918
}
927919
} else {
928920
resp = provider.PlanResourceChange(providers.PlanResourceChangeRequest{
929-
TypeName: n.Addr.Resource.Resource.Type,
930-
Config: unmarkedConfigVal,
931-
PriorState: unmarkedPriorVal,
932-
ProposedNewState: proposedNewVal,
933-
PriorPrivate: priorPrivate,
934-
ProviderMeta: metaConfigVal,
935-
ClientCapabilities: providers.ClientCapabilities{
936-
DeferralAllowed: deferralAllowed,
937-
WriteOnlyAttributesAllowed: true,
938-
},
921+
TypeName: n.Addr.Resource.Resource.Type,
922+
Config: unmarkedConfigVal,
923+
PriorState: unmarkedPriorVal,
924+
ProposedNewState: proposedNewVal,
925+
PriorPrivate: priorPrivate,
926+
ProviderMeta: metaConfigVal,
927+
ClientCapabilities: ctx.ClientCapabilities(),
939928
})
940929
// If we don't support deferrals, but the provider reports a deferral and does not
941930
// emit any error level diagnostics, we should emit an error.
@@ -1100,16 +1089,13 @@ func (n *NodeAbstractResourceInstance) plan(
11001089
}
11011090
} else {
11021091
resp = provider.PlanResourceChange(providers.PlanResourceChangeRequest{
1103-
TypeName: n.Addr.Resource.Resource.Type,
1104-
Config: unmarkedConfigVal,
1105-
PriorState: nullPriorVal,
1106-
ProposedNewState: proposedNewVal,
1107-
PriorPrivate: plannedPrivate,
1108-
ProviderMeta: metaConfigVal,
1109-
ClientCapabilities: providers.ClientCapabilities{
1110-
DeferralAllowed: deferralAllowed,
1111-
WriteOnlyAttributesAllowed: true,
1112-
},
1092+
TypeName: n.Addr.Resource.Resource.Type,
1093+
Config: unmarkedConfigVal,
1094+
PriorState: nullPriorVal,
1095+
ProposedNewState: proposedNewVal,
1096+
PriorPrivate: plannedPrivate,
1097+
ProviderMeta: metaConfigVal,
1098+
ClientCapabilities: ctx.ClientCapabilities(),
11131099
})
11141100

11151101
// If we don't support deferrals, but the provider reports a deferral and does not
@@ -1567,13 +1553,10 @@ func (n *NodeAbstractResourceInstance) readDataSource(ctx EvalContext, configVal
15671553
}
15681554
} else {
15691555
resp = provider.ReadDataSource(providers.ReadDataSourceRequest{
1570-
TypeName: n.Addr.ContainingResource().Resource.Type,
1571-
Config: configVal,
1572-
ProviderMeta: metaConfigVal,
1573-
ClientCapabilities: providers.ClientCapabilities{
1574-
DeferralAllowed: deferralAllowed,
1575-
WriteOnlyAttributesAllowed: true,
1576-
},
1556+
TypeName: n.Addr.ContainingResource().Resource.Type,
1557+
Config: configVal,
1558+
ProviderMeta: metaConfigVal,
1559+
ClientCapabilities: ctx.ClientCapabilities(),
15771560
})
15781561

15791562
// If we don't support deferrals, but the provider reports a deferral and does not

internal/terraform/node_resource_import.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,9 @@ func (n *graphNodeImportState) Execute(ctx EvalContext, op walkOperation) (diags
103103
}
104104

105105
resp := provider.ImportResourceState(providers.ImportResourceStateRequest{
106-
TypeName: n.Addr.Resource.Resource.Type,
107-
ID: n.ID,
108-
ClientCapabilities: providers.ClientCapabilities{
109-
DeferralAllowed: false,
110-
WriteOnlyAttributesAllowed: true,
111-
},
106+
TypeName: n.Addr.Resource.Resource.Type,
107+
ID: n.ID,
108+
ClientCapabilities: ctx.ClientCapabilities(),
112109
})
113110
diags = diags.Append(resp.Diagnostics)
114111
if diags.HasErrors() {

internal/terraform/node_resource_plan_instance.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,9 @@ func (n *NodePlannableResourceInstance) importState(ctx EvalContext, addr addrs.
649649
}
650650
} else {
651651
resp = provider.ImportResourceState(providers.ImportResourceStateRequest{
652-
TypeName: addr.Resource.Resource.Type,
653-
ID: importId,
654-
ClientCapabilities: providers.ClientCapabilities{
655-
DeferralAllowed: deferralAllowed,
656-
WriteOnlyAttributesAllowed: true,
657-
},
652+
TypeName: addr.Resource.Resource.Type,
653+
ID: importId,
654+
ClientCapabilities: ctx.ClientCapabilities(),
658655
})
659656
}
660657
// If we don't support deferrals, but the provider reports a deferral and does not

0 commit comments

Comments
 (0)