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

tfsdk: Ensure other attribute plan modifiers are called after errors #123

Merged
merged 6 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
50 changes: 30 additions & 20 deletions tfsdk/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,16 @@ type Attribute struct {
Validators []AttributeValidator

// PlanModifiers defines a sequence of modifiers for this attribute at
// plan time.
// Please note that plan modification only applies to resources, not
// data sources. Setting PlanModifiers on a data source attribute will
// have no effect.
// plan time. Attribute-level plan modifications occur before any
// resource-level plan modifications.
//
// Any errors will prevent further execution of this sequence
// of modifiers and modifiers associated with any nested Attribute, but will not
// prevent execution of PlanModifiers on any other Attribute in the Schema.
//
// Plan modification only applies to resources, not data sources or
// providers. Setting PlanModifiers on a data source or provider attribute
// will have no effect.
Comment on lines +84 to +86
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must admit that this makes me 🤔 about Attribute being an interface and having ProviderAttribute, ResourceAttribute, and DataSourceAttribute to distinguish available functionality between these at compiler-time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good idea. I agree it's confusing having a field on the Attribute struct that sometimes does nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #132

PlanModifiers AttributePlanModifiers
}

Expand Down Expand Up @@ -387,39 +393,43 @@ func (a Attribute) validate(ctx context.Context, req ValidateAttributeRequest, r
func (a Attribute) modifyPlan(ctx context.Context, req ModifyAttributePlanRequest, resp *ModifyAttributePlanResponse) {
attrConfig, diags := req.Config.GetAttribute(ctx, req.AttributePath)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
// Only on new errors.
if diags.HasError() {
return
}
req.AttributeConfig = attrConfig

attrState, diags := req.State.GetAttribute(ctx, req.AttributePath)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
// Only on new errors.
if diags.HasError() {
return
}
req.AttributeState = attrState

attrPlan, diags := req.Plan.GetAttribute(ctx, req.AttributePath)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
// Only on new errors.
if diags.HasError() {
return
}
req.AttributePlan = attrPlan

modifyReq := ModifyAttributePlanRequest{
AttributePath: req.AttributePath,
Config: req.Config,
State: req.State,
Plan: req.Plan,
AttributeConfig: req.AttributeConfig,
AttributeState: req.AttributeState,
AttributePlan: req.AttributePlan,
ProviderMeta: req.ProviderMeta,
}
for _, planModifier := range a.PlanModifiers {
planModifier.Modify(ctx, modifyReq, resp)
modifyReq.AttributePlan = resp.AttributePlan
if resp.Diagnostics.HasError() {
modifyResp := &ModifyAttributePlanResponse{
AttributePlan: resp.AttributePlan,
RequiresReplace: resp.RequiresReplace,
}

planModifier.Modify(ctx, req, modifyResp)

req.AttributePlan = modifyResp.AttributePlan
resp.AttributePlan = modifyResp.AttributePlan
resp.Diagnostics.Append(modifyResp.Diagnostics...)
resp.RequiresReplace = modifyResp.RequiresReplace

// Only on new errors.
if modifyResp.Diagnostics.HasError() {
return
}
}
Expand Down
7 changes: 7 additions & 0 deletions tfsdk/attribute_plan_modification.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ type AttributePlanModifier interface {
// the diff that should be shown to the user for approval, and once
// during the apply phase with any unknown values from configuration
// filled in with their final values.
//
// The Modify function has access to the config, state, and plan for
// both the attribute in question and the entire resource, but it can
// only modify the value of the one attribute.
//
// Any returned errors will stop further execution of plan modifications
// for this Attribute and any nested Attribute. Other Attribute at the same
// or higher levels of the Schema will still execute any plan modifications
// to ensure all warnings and errors across all root Attribute are
// captured.
//
// Please see the documentation for ResourceWithModifyPlan#ModifyPlan
// for further details.
Modify(context.Context, ModifyAttributePlanRequest, *ModifyAttributePlanResponse)
Expand Down
Loading