@@ -13,7 +13,6 @@ import (
13
13
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
14
14
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
15
15
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
16
- "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
17
16
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
18
17
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
19
18
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
@@ -203,32 +202,68 @@ func (v retentionPolicyValidator) ValidateObject(ctx context.Context, req valida
203
202
return
204
203
}
205
204
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
209
211
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 {
211
223
resp .Diagnostics .AddAttributeError (
212
224
req .Path .AtName ("should_keep_forever" ),
213
225
"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" ,
215
227
)
216
- } else if quantityToKeep != 0 && shouldKeepForever {
228
+ }
229
+ if ! unitPresent {
217
230
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" ),
219
254
"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" ,
221
256
)
222
257
}
223
258
}
224
259
225
- if ! retentionPolicy . Unit . IsNull () && ! retentionPolicy . Unit . IsUnknown () {
260
+ if unitPresent {
226
261
unit := retentionPolicy .Unit .ValueString ()
227
262
if ! strings .EqualFold (unit , "Days" ) && ! strings .EqualFold (unit , "Items" ) {
228
263
resp .Diagnostics .AddAttributeError (
229
264
req .Path .AtName ("unit" ),
230
265
"Invalid retention policy unit" ,
231
- "Unit must be either 'Days' or 'Items' (case insensitive) " ,
266
+ "Unit must be either 'Days' or 'Items'" ,
232
267
)
233
268
}
234
269
}
0 commit comments