Skip to content

Commit

Permalink
all: Add automatic deferred action support for unknown provider confi…
Browse files Browse the repository at this point in the history
…guration (#1335)

* update plugin-go

* quick implementation of automatic deferral (no opt-in currently)

* add opt-in logic with ResourceBehavior

* update naming on exported structs/fields

* add data source tests

* add readresource tests

* refactor planresourcechange tests

* remove configure from tests

* add new PlanResourceChange RPC tests

* update import logic + add tests for ImportResourceState

* update sdkv2 to match new protocol changes + pass deferral allowed to configureprovider

* update terraform-plugin-go

* update plugin-go

* name changes + doc updates + remove duplicate diags

* add logging

* error message update

* pkg doc updates

* add changelogs

* add copywrite header

* go mod tidy :)

* replace TODO comment on import

* replace if check for deferralAllowed

* replace logging key with a constant

* use the proper error returning method for test assertions

* add experimental verbiage

* DeferredResponse -> Deferred
  • Loading branch information
austinvalle authored May 17, 2024
1 parent b76c8ef commit 02c429c
Show file tree
Hide file tree
Showing 9 changed files with 2,452 additions and 192 deletions.
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
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

0 comments on commit 02c429c

Please sign in to comment.