-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setting attributes within nested subject block to computed and to use…
… plan modifier which examines state and plan for string and list types (#284)
- Loading branch information
1 parent
7733cf3
commit 0494451
Showing
3 changed files
with
146 additions
and
9 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
internal/provider/attribute_plan_modifier/requires_replace_empty_null.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package attribute_plan_modifier | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func RequiresReplaceNullEmpty() tfsdk.AttributePlanModifier { | ||
return requiresReplaceNullEmpty{} | ||
} | ||
|
||
type requiresReplaceNullEmpty struct{} | ||
|
||
func (r requiresReplaceNullEmpty) Modify(ctx context.Context, req tfsdk.ModifyAttributePlanRequest, resp *tfsdk.ModifyAttributePlanResponse) { | ||
if req.AttributeConfig == nil || req.AttributePlan == nil || req.AttributeState == nil { | ||
// shouldn't happen, but let's not panic if it does | ||
return | ||
} | ||
|
||
emptyStateString := types.String{} | ||
emptyPlanString := types.String{ | ||
Unknown: true, | ||
} | ||
|
||
if req.AttributePlan.Equal(emptyPlanString) { | ||
resp.AttributePlan = emptyStateString | ||
return | ||
} | ||
|
||
emptyStateList := types.List{ | ||
Null: true, | ||
ElemType: types.StringType, | ||
} | ||
emptyStateListEmptyElems := types.List{ | ||
ElemType: types.StringType, | ||
Elems: []attr.Value{}, | ||
} | ||
emptyPlanList := types.List{ | ||
Unknown: true, | ||
ElemType: types.StringType, | ||
} | ||
|
||
if req.AttributePlan.Equal(emptyPlanList) && req.AttributeState.Equal(emptyStateList) { | ||
resp.AttributePlan = emptyStateList | ||
return | ||
} | ||
|
||
if req.AttributePlan.Equal(emptyPlanList) && req.AttributeState.Equal(emptyStateListEmptyElems) { | ||
resp.AttributePlan = emptyStateListEmptyElems | ||
return | ||
} | ||
|
||
if req.AttributePlan.Equal(req.AttributeState) { | ||
// if the plan and the state are in agreement, this attribute | ||
// isn't changing, don't require replace | ||
return | ||
} | ||
|
||
resp.RequiresReplace = true | ||
} | ||
|
||
func (r requiresReplaceNullEmpty) Description(ctx context.Context) string { | ||
return "If the value of this attribute changes, Terraform will destroy and recreate the resource." | ||
} | ||
|
||
func (r requiresReplaceNullEmpty) MarkdownDescription(ctx context.Context) string { | ||
return "If the value of this attribute changes, Terraform will destroy and recreate the resource." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters