-
Notifications
You must be signed in to change notification settings - Fork 103
/
default_value.go
101 lines (77 loc) · 3.18 KB
/
default_value.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package attribute_plan_modifier
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// defaultValueAttributePlanModifier specifies a default value (attr.Value) for an attribute.
type defaultValueAttributePlanModifier struct {
DefaultValue attr.Value
}
// DefaultValue is an helper to instantiate a defaultValueAttributePlanModifier.
func DefaultValue(v attr.Value) tfsdk.AttributePlanModifier {
return &defaultValueAttributePlanModifier{v}
}
var _ tfsdk.AttributePlanModifier = (*defaultValueAttributePlanModifier)(nil)
func (apm *defaultValueAttributePlanModifier) Description(ctx context.Context) string {
return apm.MarkdownDescription(ctx)
}
func (apm *defaultValueAttributePlanModifier) MarkdownDescription(ctx context.Context) string {
return fmt.Sprintf("Sets the default value %q (%s) if the attribute is not set", apm.DefaultValue, apm.DefaultValue.Type(ctx))
}
func (apm *defaultValueAttributePlanModifier) Modify(_ context.Context, req tfsdk.ModifyAttributePlanRequest, res *tfsdk.ModifyAttributePlanResponse) {
// If the attribute configuration is not null, we are done here
if !req.AttributeConfig.IsNull() {
return
}
// If the attribute plan is "known" and "not null", then a previous plan modifier in the sequence
// has already been applied, and we don't want to interfere.
if !req.AttributePlan.IsUnknown() && !req.AttributePlan.IsNull() {
return
}
res.AttributePlan = apm.DefaultValue
}
// readyForRenewalAttributePlanModifier determines whether the certificate is ready for renewal.
type readyForRenewalAttributePlanModifier struct {
}
// ReadyForRenewal is an helper to instantiate a defaultValueAttributePlanModifier.
func ReadyForRenewal() tfsdk.AttributePlanModifier {
return &readyForRenewalAttributePlanModifier{}
}
var _ tfsdk.AttributePlanModifier = (*readyForRenewalAttributePlanModifier)(nil)
func (apm *readyForRenewalAttributePlanModifier) Description(ctx context.Context) string {
return apm.MarkdownDescription(ctx)
}
func (apm *readyForRenewalAttributePlanModifier) MarkdownDescription(ctx context.Context) string {
return "Sets the value of ready_for_renewal depending on value of validity_period_hours and early_renewal_hours"
}
func (apm *readyForRenewalAttributePlanModifier) Modify(ctx context.Context, req tfsdk.ModifyAttributePlanRequest, res *tfsdk.ModifyAttributePlanResponse) {
var validityPeriodHours types.Int64
res.Diagnostics.Append(req.Plan.GetAttribute(ctx, path.Root("validity_period_hours"), &validityPeriodHours)...)
if res.Diagnostics.HasError() {
return
}
if validityPeriodHours.Value == 0 {
res.AttributePlan = types.Bool{
Value: true,
}
return
}
var earlyRenewalHours types.Int64
res.Diagnostics.Append(req.Plan.GetAttribute(ctx, path.Root("early_renewal_hours"), &earlyRenewalHours)...)
if res.Diagnostics.HasError() {
return
}
if earlyRenewalHours.IsNull() || earlyRenewalHours.IsUnknown() {
return
}
if earlyRenewalHours.Value >= validityPeriodHours.Value {
res.AttributePlan = types.Bool{
Value: true,
}
return
}
}