Skip to content

Commit 222736d

Browse files
committed
refactor validation
1 parent 1035f92 commit 222736d

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

octopusdeploy_framework/schemas/lifecycle.go

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
1414
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
1515
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
16-
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
1716
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1817
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1918
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
@@ -203,32 +202,68 @@ func (v retentionPolicyValidator) ValidateObject(ctx context.Context, req valida
203202
return
204203
}
205204

206-
if !retentionPolicy.QuantityToKeep.IsNull() && !retentionPolicy.QuantityToKeep.IsUnknown() && !retentionPolicy.ShouldKeepForever.IsNull() && !retentionPolicy.ShouldKeepForever.IsUnknown() {
207-
quantityToKeep := retentionPolicy.QuantityToKeep.ValueInt64()
208-
shouldKeepForever := retentionPolicy.ShouldKeepForever.ValueBool()
205+
unitPresent := !retentionPolicy.Unit.IsNull() && !retentionPolicy.Unit.IsUnknown()
206+
quantityToKeepPresent := !retentionPolicy.QuantityToKeep.IsNull() && !retentionPolicy.QuantityToKeep.IsUnknown()
207+
shouldKeepForeverPresent := !retentionPolicy.ShouldKeepForever.IsNull() && !retentionPolicy.ShouldKeepForever.IsUnknown()
208+
shouldKeepForeverIsTrue := shouldKeepForeverPresent && retentionPolicy.ShouldKeepForever.ValueBool() == true
209+
shouldKeepForeverIsFalse := shouldKeepForeverPresent && retentionPolicy.ShouldKeepForever.ValueBool() == false
210+
quantityToKeepIsMoreThanZero := quantityToKeepPresent && retentionPolicy.QuantityToKeep.ValueInt64() > 0
209211

210-
if quantityToKeep == 0 && !shouldKeepForever {
212+
if !unitPresent && !quantityToKeepPresent && !shouldKeepForeverPresent {
213+
resp.Diagnostics.AddAttributeError(
214+
req.Path.AtName("strategy"),
215+
"Invalid retention policy configuration",
216+
"please either add retention policy attributes or remove the entire block",
217+
)
218+
}
219+
220+
// count strategy validations
221+
if quantityToKeepIsMoreThanZero {
222+
if shouldKeepForeverIsTrue {
211223
resp.Diagnostics.AddAttributeError(
212224
req.Path.AtName("should_keep_forever"),
213225
"Invalid retention policy configuration",
214-
"should_keep_forever must be true when quantity_to_keep is 0",
226+
"should_keep_forever must be false when quantity_to_keep is greater than 0",
215227
)
216-
} else if quantityToKeep != 0 && shouldKeepForever {
228+
}
229+
if !unitPresent {
217230
resp.Diagnostics.AddAttributeError(
218-
req.Path.AtName("should_keep_forever"),
231+
req.Path.AtName("unit"),
232+
"Invalid retention policy configuration",
233+
"unit is required when quantity_to_keep is greater than 0",
234+
)
235+
}
236+
}
237+
238+
// keep forever strategy validation
239+
if !quantityToKeepIsMoreThanZero && shouldKeepForeverIsFalse {
240+
resp.Diagnostics.AddAttributeError(
241+
req.Path.AtName("should_keep_forever"),
242+
"Invalid retention policy configuration",
243+
"should_keep_forever must be true when quantity_to_keep is zero or missing",
244+
)
245+
}
246+
247+
//prevent users from inputting units when not using count
248+
if unitPresent && !quantityToKeepIsMoreThanZero {
249+
if strings.EqualFold(retentionPolicy.Unit.ValueString(), "Items") {
250+
// do not throw an error for backwards compatability.
251+
} else {
252+
resp.Diagnostics.AddAttributeError(
253+
req.Path.AtName("unit"),
219254
"Invalid retention policy configuration",
220-
"should_keep_forever must be false when quantity_to_keep is not 0",
255+
"unit is only used when quantity_to_keep is greater than 0",
221256
)
222257
}
223258
}
224259

225-
if !retentionPolicy.Unit.IsNull() && !retentionPolicy.Unit.IsUnknown() {
260+
if unitPresent {
226261
unit := retentionPolicy.Unit.ValueString()
227262
if !strings.EqualFold(unit, "Days") && !strings.EqualFold(unit, "Items") {
228263
resp.Diagnostics.AddAttributeError(
229264
req.Path.AtName("unit"),
230265
"Invalid retention policy unit",
231-
"Unit must be either 'Days' or 'Items' (case insensitive)",
266+
"Unit must be either 'Days' or 'Items'",
232267
)
233268
}
234269
}

0 commit comments

Comments
 (0)