-
Notifications
You must be signed in to change notification settings - Fork 5
refactor lifecycle retention policies around quantity_to_keep #88
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
Changes from all commits
588ab66
1035f92
222736d
711c024
f53bbe4
6746323
08c7f7e
b619288
57b64db
2244fba
c73d9dc
c313a13
49b373c
bad1e9a
7ce2d26
a9b2d78
f7c23bc
84df380
e0e56be
154e62d
76b7697
3781576
6fe0ed3
92492c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package schemas | |
|
||
import ( | ||
"context" | ||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" | ||
"strings" | ||
|
||
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util" | ||
|
@@ -13,7 +14,6 @@ import ( | |
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
|
@@ -111,19 +111,16 @@ func getResourceRetentionPolicyBlockSchema() resourceSchema.ListNestedBlock { | |
Attributes: map[string]resourceSchema.Attribute{ | ||
"quantity_to_keep": util.ResourceInt64(). | ||
Optional().Computed(). | ||
Default(int64default.StaticInt64(30)). | ||
Validators(int64validator.AtLeast(0)). | ||
Description("The number of days/releases to keep. The default value is 30. If 0 then all are kept."). | ||
Description("The number of days/releases to keep. If 0 then all are kept."). | ||
Build(), | ||
"should_keep_forever": util.ResourceBool(). | ||
Optional().Computed(). | ||
Default(booldefault.StaticBool(false)). | ||
Description("Indicates if items should never be deleted. The default value is false."). | ||
Description("Indicates if items should never be deleted."). | ||
Build(), | ||
"unit": util.ResourceString(). | ||
Optional().Computed(). | ||
Default(stringdefault.StaticString("Days")). | ||
Description("The unit of quantity to keep. Valid units are Days or Items. The default value is Days."). | ||
Optional().Computed().Validators(stringvalidator.OneOfCaseInsensitive("Days", "Items")). | ||
Description("The unit of quantity to keep. Valid units are Days or Items."). | ||
Build(), | ||
}, | ||
Validators: []validator.Object{ | ||
|
@@ -206,32 +203,56 @@ func (v retentionPolicyValidator) ValidateObject(ctx context.Context, req valida | |
return | ||
} | ||
|
||
if !retentionPolicy.QuantityToKeep.IsNull() && !retentionPolicy.QuantityToKeep.IsUnknown() && !retentionPolicy.ShouldKeepForever.IsNull() && !retentionPolicy.ShouldKeepForever.IsUnknown() { | ||
quantityToKeep := retentionPolicy.QuantityToKeep.ValueInt64() | ||
shouldKeepForever := retentionPolicy.ShouldKeepForever.ValueBool() | ||
unitPresent := !retentionPolicy.Unit.IsNull() && !retentionPolicy.Unit.IsUnknown() | ||
quantityToKeepPresent := !retentionPolicy.QuantityToKeep.IsNull() && !retentionPolicy.QuantityToKeep.IsUnknown() | ||
shouldKeepForeverPresent := !retentionPolicy.ShouldKeepForever.IsNull() && !retentionPolicy.ShouldKeepForever.IsUnknown() | ||
shouldKeepForever := shouldKeepForeverPresent && retentionPolicy.ShouldKeepForever.ValueBool() | ||
quantityToKeepIsMoreThanZero := quantityToKeepPresent && retentionPolicy.QuantityToKeep.ValueInt64() > 0 | ||
|
||
if quantityToKeep == 0 && !shouldKeepForever { | ||
if !unitPresent && !quantityToKeepPresent && !shouldKeepForeverPresent { | ||
resp.Diagnostics.AddAttributeError( | ||
req.Path.AtName("strategy"), | ||
"Invalid retention policy configuration", | ||
"please either add retention policy attributes or remove the entire block", | ||
) | ||
} | ||
|
||
// count strategy validations | ||
if quantityToKeepIsMoreThanZero { | ||
if shouldKeepForever { | ||
resp.Diagnostics.AddAttributeError( | ||
req.Path.AtName("should_keep_forever"), | ||
"Invalid retention policy configuration", | ||
"should_keep_forever must be true when quantity_to_keep is 0", | ||
"should_keep_forever must be false when quantity_to_keep is greater than 0", | ||
) | ||
} else if quantityToKeep != 0 && shouldKeepForever { | ||
} | ||
if !unitPresent { | ||
resp.Diagnostics.AddAttributeError( | ||
req.Path.AtName("should_keep_forever"), | ||
req.Path.AtName("unit"), | ||
"Invalid retention policy configuration", | ||
"should_keep_forever must be false when quantity_to_keep is not 0", | ||
"unit is required when quantity_to_keep is greater than 0", | ||
) | ||
} | ||
} | ||
|
||
if !retentionPolicy.Unit.IsNull() && !retentionPolicy.Unit.IsUnknown() { | ||
unit := retentionPolicy.Unit.ValueString() | ||
if !strings.EqualFold(unit, "Days") && !strings.EqualFold(unit, "Items") { | ||
// keep forever strategy validation | ||
if !quantityToKeepIsMoreThanZero && !shouldKeepForever { | ||
resp.Diagnostics.AddAttributeError( | ||
req.Path.AtName("should_keep_forever"), | ||
"Invalid retention policy configuration", | ||
"should_keep_forever must be true when quantity_to_keep is zero or missing", | ||
) | ||
} | ||
|
||
if unitPresent && !quantityToKeepIsMoreThanZero { | ||
if strings.EqualFold(retentionPolicy.Unit.ValueString(), "Items") { | ||
// do not throw an error for backwards compatability. | ||
} else { | ||
resp.Diagnostics.AddAttributeError( | ||
// replaces a confusing state change to "unit = Items" error at the api | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing this is to deal with the case sensitive API error response for "Items", but the error message is not related to this at all, which is confusing. Could you explain what you mean here by this comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the user puts days in when the strategy isn't set to count, the server will return "unexpected result, unit was Days but is now Items" because the server returns items no matter what when a forever or default strategy is set. It won't error when people use "items" even though items isn't needed so I thought it would be good to not break instances that are doing this (even though its an unnecessary field) I was told to use (stringvalidator.OneOfCaseInsensitive("Days", "Items")) in the schema instead of letting the validation logic be in the external validator so that's why lines 233 and 234 are deleted below |
||
req.Path.AtName("unit"), | ||
"Invalid retention policy unit", | ||
"Unit must be either 'Days' or 'Items' (case insensitive)", | ||
"Invalid retention policy configuration", | ||
"unit is only used when quantity_to_keep is greater than 0", | ||
) | ||
} | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.