-
Notifications
You must be signed in to change notification settings - Fork 91
/
requires_replace_if.go
73 lines (60 loc) · 2.29 KB
/
requires_replace_if.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package int64planmodifier
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
)
// RequiresReplaceIf returns a plan modifier that conditionally requires
// resource replacement if:
//
// - The resource is planned for update.
// - The plan and state values are not equal.
// - The given function returns true. Returning false will not unset any
// prior resource replacement.
//
// Use RequiresReplace if the resource replacement should always occur on value
// changes. Use RequiresReplaceIfConfigured if the resource replacement should
// occur on value changes, but only if there is a configuration value (ignore
// unconfigured drift detection changes).
func RequiresReplaceIf(f RequiresReplaceIfFunc, description, markdownDescription string) planmodifier.Int64 {
return requiresReplaceIfModifier{
ifFunc: f,
description: description,
markdownDescription: markdownDescription,
}
}
// requiresReplaceIfModifier is an plan modifier that sets RequiresReplace
// on the attribute if a given function is true.
type requiresReplaceIfModifier struct {
ifFunc RequiresReplaceIfFunc
description string
markdownDescription string
}
// Description returns a human-readable description of the plan modifier.
func (m requiresReplaceIfModifier) Description(_ context.Context) string {
return m.description
}
// MarkdownDescription returns a markdown description of the plan modifier.
func (m requiresReplaceIfModifier) MarkdownDescription(_ context.Context) string {
return m.markdownDescription
}
// PlanModifyInt64 implements the plan modification logic.
func (m requiresReplaceIfModifier) PlanModifyInt64(ctx context.Context, req planmodifier.Int64Request, resp *planmodifier.Int64Response) {
// Do not replace on resource creation.
if req.State.Raw.IsNull() {
return
}
// Do not replace on resource destroy.
if req.Plan.Raw.IsNull() {
return
}
// Do not replace if the plan and state values are equal.
if req.PlanValue.Equal(req.StateValue) {
return
}
ifFuncResp := &RequiresReplaceIfFuncResponse{}
m.ifFunc(ctx, req, ifFuncResp)
resp.Diagnostics.Append(ifFuncResp.Diagnostics...)
resp.RequiresReplace = ifFuncResp.RequiresReplace
}