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: Add automatic deferred action support for unknown provider configuration #1335

Merged
merged 31 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
efdc771
update plugin-go
austinvalle Apr 19, 2024
d9c6993
quick implementation of automatic deferral (no opt-in currently)
austinvalle Apr 19, 2024
b7c02ce
add opt-in logic with ResourceBehavior
austinvalle Apr 22, 2024
f44c9fe
update naming on exported structs/fields
austinvalle Apr 22, 2024
1f768dc
add data source tests
austinvalle Apr 23, 2024
07e816a
add readresource tests
austinvalle Apr 23, 2024
39bf469
refactor planresourcechange tests
austinvalle Apr 23, 2024
d16367c
remove configure from tests
austinvalle Apr 23, 2024
90c72f9
add new PlanResourceChange RPC tests
austinvalle Apr 23, 2024
0cac590
update import logic + add tests for ImportResourceState
austinvalle Apr 23, 2024
c8c3e47
Merge branch 'main' into av/dfa
austinvalle Apr 24, 2024
fe1ea31
update sdkv2 to match new protocol changes + pass deferral allowed to…
austinvalle Apr 24, 2024
bf74443
update terraform-plugin-go
austinvalle May 3, 2024
4dcc2ad
update plugin-go
austinvalle May 6, 2024
e3e7e45
name changes + doc updates + remove duplicate diags
austinvalle May 6, 2024
be070c1
add logging
austinvalle May 6, 2024
eb08273
error message update
austinvalle May 6, 2024
9450487
pkg doc updates
austinvalle May 6, 2024
3ad892a
add changelogs
austinvalle May 6, 2024
8251ffc
add copywrite header
austinvalle May 6, 2024
276a421
go mod tidy :)
austinvalle May 6, 2024
11c5a6d
Merge branch 'main' into av/dfa
austinvalle May 6, 2024
30c6157
Merge branch 'main' into av/dfa
austinvalle May 6, 2024
aed628d
replace TODO comment on import
austinvalle May 9, 2024
0598563
replace if check for deferralAllowed
austinvalle May 9, 2024
bd21c5e
replace logging key with a constant
austinvalle May 9, 2024
c31bc00
use the proper error returning method for test assertions
austinvalle May 9, 2024
207c950
add experimental verbiage
austinvalle May 9, 2024
470473d
DeferredResponse -> Deferred
austinvalle May 10, 2024
e216dec
Merge branch 'main' into av/dfa
austinvalle May 17, 2024
ec6e0a2
Merge branch 'main' into av/dfa
austinvalle May 17, 2024
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
6 changes: 6 additions & 0 deletions .changes/unreleased/FEATURES-20240506-152018.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: FEATURES
body: 'helper/schema: Added `(Provider).ConfigureProvider` function for configuring
providers that support additional features, such as deferred actions.'
time: 2024-05-06T15:20:18.393505-04:00
custom:
Issue: "1335"
6 changes: 6 additions & 0 deletions .changes/unreleased/FEATURES-20240506-152135.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: FEATURES
body: 'helper/schema: Added `(Resource).ResourceBehavior` to allow additional control
over deferred action behavior during plan modification.'
time: 2024-05-06T15:21:35.304825-04:00
custom:
Issue: "1335"
7 changes: 7 additions & 0 deletions .changes/unreleased/NOTES-20240509-134945.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: NOTES
body: This release contains support for deferred actions, which is an experimental
feature only available in prerelease builds of Terraform 1.9 and later. This functionality
is subject to change and is not protected by version compatibility guarantees.
time: 2024-05-09T13:49:45.38523-04:00
custom:
Issue: "1335"
45 changes: 45 additions & 0 deletions helper/schema/deferred.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package schema

// MAINTAINER NOTE: Only PROVIDER_CONFIG_UNKNOWN (enum value 2 in the plugin-protocol) is relevant
// for SDKv2. Since (Deferred).Reason is mapped directly to the plugin-protocol,
// the other enum values are intentionally omitted here.
const (
// DeferredReasonUnknown is used to indicate an invalid `DeferredReason`.
// Provider developers should not use it.
DeferredReasonUnknown DeferredReason = 0

// DeferredReasonProviderConfigUnknown represents a deferred reason caused
// by unknown provider configuration.
DeferredReasonProviderConfigUnknown DeferredReason = 2
)

// Deferred is used to indicate to Terraform that a resource or data source is not able
// to be applied yet and should be skipped (deferred). After completing an apply that has deferred actions,
// the practitioner can then execute additional plan and apply “rounds” to eventually reach convergence
// where there are no remaining deferred actions.
//
// NOTE: This functionality is related to deferred action support, which is currently experimental and is subject
// to change or break without warning. It is not protected by version compatibility guarantees.
type Deferred struct {
// Reason represents the deferred reason.
Reason DeferredReason
}

// DeferredReason represents different reasons for deferring a change.
//
// NOTE: This functionality is related to deferred action support, which is currently experimental and is subject
// to change or break without warning. It is not protected by version compatibility guarantees.
type DeferredReason int32

func (d DeferredReason) String() string {
switch d {
case 0:
return "Unknown"
case 2:
return "Provider Config Unknown"
}
return "Unknown"
}
154 changes: 154 additions & 0 deletions helper/schema/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,12 +607,37 @@ func (s *GRPCProviderServer) ConfigureProvider(ctx context.Context, req *tfproto
// request scoped contexts, however this is a large undertaking for very large providers.
ctxHack := context.WithValue(ctx, StopContextKey, s.StopContext(context.Background()))

// NOTE: This is a hack to pass the deferral_allowed field from the Terraform client to the
// underlying (provider).Configure function, which cannot be changed because the function
// signature is public. (╯°□°)╯︵ ┻━┻
s.provider.deferralAllowed = configureDeferralAllowed(req.ClientCapabilities)

logging.HelperSchemaTrace(ctx, "Calling downstream")
diags := s.provider.Configure(ctxHack, config)
logging.HelperSchemaTrace(ctx, "Called downstream")

resp.Diagnostics = convert.AppendProtoDiag(ctx, resp.Diagnostics, diags)

if s.provider.providerDeferred != nil {
// Check if a deferred response was incorrectly set on the provider. This would cause an error during later RPCs.
if !s.provider.deferralAllowed {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Invalid Deferred Provider Response",
Detail: "Provider configured a deferred response for all resources and data sources but the Terraform request " +
"did not indicate support for deferred actions. This is an issue with the provider and should be reported to the provider developers.",
})
} else {
logging.HelperSchemaDebug(
ctx,
"Provider has configured a deferred response, all associated resources and data sources will automatically return a deferred response.",
map[string]interface{}{
logging.KeyDeferredReason: s.provider.providerDeferred.Reason.String(),
},
)
}
}

return resp, nil
}

Expand All @@ -632,6 +657,22 @@ func (s *GRPCProviderServer) ReadResource(ctx context.Context, req *tfprotov5.Re
}
schemaBlock := s.getResourceSchemaBlock(req.TypeName)

if s.provider.providerDeferred != nil {
logging.HelperSchemaDebug(
ctx,
"Provider has deferred response configured, automatically returning deferred response.",
map[string]interface{}{
logging.KeyDeferredReason: s.provider.providerDeferred.Reason.String(),
},
)

resp.NewState = req.CurrentState
resp.Deferred = &tfprotov5.Deferred{
Reason: tfprotov5.DeferredReason(s.provider.providerDeferred.Reason),
}
return resp, nil
}

stateVal, err := msgpack.Unmarshal(req.CurrentState.MsgPack, schemaBlock.ImpliedType())
if err != nil {
resp.Diagnostics = convert.AppendProtoDiag(ctx, resp.Diagnostics, err)
Expand Down Expand Up @@ -731,6 +772,25 @@ func (s *GRPCProviderServer) PlanResourceChange(ctx context.Context, req *tfprot
resp.UnsafeToUseLegacyTypeSystem = true
}

// Provider deferred response is present and the resource hasn't opted-in to CustomizeDiff being called, return early
// with proposed new state as a best effort for PlannedState.
if s.provider.providerDeferred != nil && !res.ResourceBehavior.ProviderDeferred.EnablePlanModification {
logging.HelperSchemaDebug(
ctx,
"Provider has deferred response configured, automatically returning deferred response.",
map[string]interface{}{
logging.KeyDeferredReason: s.provider.providerDeferred.Reason.String(),
},
)

resp.PlannedState = req.ProposedNewState
austinvalle marked this conversation as resolved.
Show resolved Hide resolved
resp.PlannedPrivate = req.PriorPrivate
resp.Deferred = &tfprotov5.Deferred{
Reason: tfprotov5.DeferredReason(s.provider.providerDeferred.Reason),
}
return resp, nil
}

priorStateVal, err := msgpack.Unmarshal(req.PriorState.MsgPack, schemaBlock.ImpliedType())
if err != nil {
resp.Diagnostics = convert.AppendProtoDiag(ctx, resp.Diagnostics, err)
Expand Down Expand Up @@ -951,6 +1011,21 @@ func (s *GRPCProviderServer) PlanResourceChange(ctx context.Context, req *tfprot
resp.RequiresReplace = append(resp.RequiresReplace, pathToAttributePath(p))
}

// Provider deferred response is present, add the deferred response alongside the provider-modified plan
if s.provider.providerDeferred != nil {
logging.HelperSchemaDebug(
ctx,
"Provider has deferred response configured, returning deferred response with modified plan.",
map[string]interface{}{
logging.KeyDeferredReason: s.provider.providerDeferred.Reason.String(),
},
)

resp.Deferred = &tfprotov5.Deferred{
Reason: tfprotov5.DeferredReason(s.provider.providerDeferred.Reason),
}
}

return resp, nil
}

Expand Down Expand Up @@ -1145,6 +1220,48 @@ func (s *GRPCProviderServer) ImportResourceState(ctx context.Context, req *tfpro
Type: req.TypeName,
}

if s.provider.providerDeferred != nil {
logging.HelperSchemaDebug(
ctx,
"Provider has deferred response configured, automatically returning deferred response.",
map[string]interface{}{
logging.KeyDeferredReason: s.provider.providerDeferred.Reason.String(),
},
)

// The logic for ensuring the resource type is supported by this provider is inside of (provider).ImportState
// We need to check to ensure the resource type is supported before using the schema
_, ok := s.provider.ResourcesMap[req.TypeName]
if !ok {
resp.Diagnostics = convert.AppendProtoDiag(ctx, resp.Diagnostics, fmt.Errorf("unknown resource type: %s", req.TypeName))
return resp, nil
}

// Since we are automatically deferring, send back an unknown value for the imported object
schemaBlock := s.getResourceSchemaBlock(req.TypeName)
unknownVal := cty.UnknownVal(schemaBlock.ImpliedType())
unknownStateMp, err := msgpack.Marshal(unknownVal, schemaBlock.ImpliedType())
if err != nil {
resp.Diagnostics = convert.AppendProtoDiag(ctx, resp.Diagnostics, err)
return resp, nil
}

resp.ImportedResources = []*tfprotov5.ImportedResource{
{
TypeName: req.TypeName,
State: &tfprotov5.DynamicValue{
MsgPack: unknownStateMp,
},
},
}

resp.Deferred = &tfprotov5.Deferred{
Reason: tfprotov5.DeferredReason(s.provider.providerDeferred.Reason),
}

return resp, nil
}

newInstanceStates, err := s.provider.ImportState(ctx, info, req.ID)
if err != nil {
resp.Diagnostics = convert.AppendProtoDiag(ctx, resp.Diagnostics, err)
Expand Down Expand Up @@ -1254,6 +1371,32 @@ func (s *GRPCProviderServer) ReadDataSource(ctx context.Context, req *tfprotov5.

schemaBlock := s.getDatasourceSchemaBlock(req.TypeName)

if s.provider.providerDeferred != nil {
logging.HelperSchemaDebug(
ctx,
"Provider has deferred response configured, automatically returning deferred response.",
map[string]interface{}{
logging.KeyDeferredReason: s.provider.providerDeferred.Reason.String(),
},
)

// Send an unknown value for the data source
unknownVal := cty.UnknownVal(schemaBlock.ImpliedType())
unknownStateMp, err := msgpack.Marshal(unknownVal, schemaBlock.ImpliedType())
if err != nil {
resp.Diagnostics = convert.AppendProtoDiag(ctx, resp.Diagnostics, err)
return resp, nil
}

resp.State = &tfprotov5.DynamicValue{
MsgPack: unknownStateMp,
}
resp.Deferred = &tfprotov5.Deferred{
Reason: tfprotov5.DeferredReason(s.provider.providerDeferred.Reason),
}
return resp, nil
}

configVal, err := msgpack.Unmarshal(req.Config.MsgPack, schemaBlock.ImpliedType())
if err != nil {
resp.Diagnostics = convert.AppendProtoDiag(ctx, resp.Diagnostics, err)
Expand Down Expand Up @@ -1674,3 +1817,14 @@ func validateConfigNulls(ctx context.Context, v cty.Value, path cty.Path) []*tfp

return diags
}

// Helper function that check a ConfigureProviderClientCapabilities struct to determine if a deferred response can be
// returned to the Terraform client. If no ConfigureProviderClientCapabilities have been passed from the client, then false
// is returned.
func configureDeferralAllowed(in *tfprotov5.ConfigureProviderClientCapabilities) bool {
if in == nil {
return false
}

return in.DeferralAllowed
}
Loading