Skip to content

Commit 3af6ad4

Browse files
authored
Merge pull request #1126 from jfrog/GH-1122-fix-unable-to-set-last-downloaded-before-in-months-to-zero
Relax validations for search_criteria attributes for cleanup policy resource
2 parents 00e8110 + 5e76e1a commit 3af6ad4

File tree

3 files changed

+100
-8
lines changed

3 files changed

+100
-8
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
## 12.4.1 (November 11, 2024). Tested on Artifactory 7.98.8 with Terraform 1.9.8 and OpenTofu 1.8.5
1+
## 12.4.1 (November 12, 2024). Tested on Artifactory 7.98.8 with Terraform 1.9.8 and OpenTofu 1.8.5
22

33
BUG FIXES:
44

55
* resource/artifactory_build_webhook, resource/artifactory_release_bundle_webhook, resource/artifactory_release_bundle_v2_webhook, resource/artifactory_artifact_webhook, resource/artifactory_artifact_property_webhook, resource/artifactory_docker_webhook, resource/artifactory_build_custom_webhook, resource/artifactory_release_bundle_custom_webhook, resource/artifactory_release_bundle_v2_custom_webhook, resource/artifactory_artifact_custom_webhook, resource/artifactory_artifact_property_custom_webhook, resource/artifactory_docker_custom_webhook, resource/artifactory_ldap_group_setting_v2, resource/artifactory_repository_layout, resource/artifactory_release_bundle_v2, resource/artifactory_vault_configuration, resource/artifactory_user, resource/artifactory_managed_user, resource/artifactory_unmanaged_user: Fix attribute validation not working for unknown value (e.g. when resource is used in a module). Issue: [#1120](https://github.com/jfrog/terraform-provider-artifactory/issues/1120) PR: [#1123](https://github.com/jfrog/terraform-provider-artifactory/pull/1123)
6+
* resource/artifactory_package_cleanup_policy: Relax validations to allow 0 for `created_before_in_months` and `last_downloaded_before_in_months` attributes. Add validation run to ensure both can't be 0 at the same time. Issue: [#1122](https://github.com/jfrog/terraform-provider-artifactory/issues/1122) PR: [#1126](https://github.com/jfrog/terraform-provider-artifactory/pull/1126)
67

78
## 12.4.0 (November 4, 2024). Tested on Artifactory 7.98.7 with Terraform 1.9.8 and OpenTofu 1.8.5
89

pkg/artifactory/resource/configuration/resource_artifactory_package_cleanup_policy.go

+37-2
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ var cleanupPolicySchemaV1 = lo.Assign(
450450
int64validator.ConflictsWith(
451451
path.MatchRelative().AtParent().AtName("keep_last_n_versions"),
452452
),
453-
int64validator.AtLeast(1),
453+
int64validator.AtLeast(0),
454454
},
455455
MarkdownDescription: "Remove packages based on when they were created. For example, remove packages that were created more than a year ago. The default value is to remove packages created more than 2 years ago.",
456456
},
@@ -463,7 +463,7 @@ var cleanupPolicySchemaV1 = lo.Assign(
463463
int64validator.ConflictsWith(
464464
path.MatchRelative().AtParent().AtName("keep_last_n_versions"),
465465
),
466-
int64validator.AtLeast(1),
466+
int64validator.AtLeast(0),
467467
},
468468
MarkdownDescription: "Removes packages based on when they were last downloaded. For example, removes packages that were not downloaded in the past year. The default value is to remove packages that were downloaded more than 2 years ago.\n\n" +
469469
"~>If a package was never downloaded, the policy will remove it based only on the age-condition (`created_before_in_months`).\n\n" +
@@ -487,6 +487,41 @@ var cleanupPolicySchemaV1 = lo.Assign(
487487
},
488488
)
489489

490+
func (r PackageCleanupPolicyResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
491+
var data PackageCleanupPolicyResourceModelV1
492+
493+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
494+
495+
if resp.Diagnostics.HasError() {
496+
return
497+
}
498+
499+
// If search_criteria is not configured, return without warning.
500+
if data.SearchCriteria.IsNull() || data.SearchCriteria.IsUnknown() {
501+
return
502+
}
503+
504+
searchCriteriaAttrs := data.SearchCriteria.Attributes()
505+
createdBeforeInMonths := searchCriteriaAttrs["created_before_in_months"].(types.Int64)
506+
lastDownloadedBeforeInMonths := searchCriteriaAttrs["last_downloaded_before_in_months"].(types.Int64)
507+
508+
if createdBeforeInMonths.IsNull() || createdBeforeInMonths.IsUnknown() {
509+
return
510+
}
511+
512+
if lastDownloadedBeforeInMonths.IsNull() || lastDownloadedBeforeInMonths.IsUnknown() {
513+
return
514+
}
515+
516+
if createdBeforeInMonths.ValueInt64() == 0 && lastDownloadedBeforeInMonths.ValueInt64() == 0 {
517+
resp.Diagnostics.AddAttributeError(
518+
path.Root("search_criteria").AtName("created_before_in_months"),
519+
"Invalid Attribute Configuration",
520+
"Both created_before_in_months and last_downloaded_before_in_months cannot be zero at the same time.",
521+
)
522+
}
523+
}
524+
490525
func (r *PackageCleanupPolicyResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
491526
resp.Schema = schema.Schema{
492527
Attributes: cleanupPolicySchemaV1,

pkg/artifactory/resource/configuration/resource_artifactory_package_cleanup_policy_test.go

+61-5
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func TestAccPackageCleanupPolicy_invalid_key(t *testing.T) {
162162
skip_trashcan = false
163163
164164
search_criteria = {
165-
repos = []
165+
repos = ["**"]
166166
package_types = ["docker"]
167167
include_all_projects = true
168168
included_packages = ["**"]
@@ -195,6 +195,62 @@ func TestAccPackageCleanupPolicy_invalid_key(t *testing.T) {
195195
}
196196
}
197197

198+
func TestAccPackageCleanupPolicy_invalid_conditions(t *testing.T) {
199+
client := acctest.GetTestResty(t)
200+
version, err := util.GetArtifactoryVersion(client)
201+
if err != nil {
202+
t.Fatal(err)
203+
}
204+
valid, err := util.CheckVersion(version, "7.90.1")
205+
if err != nil {
206+
t.Fatal(err)
207+
}
208+
if !valid {
209+
t.Skipf("Artifactory version %s is earlier than 7.90.1", version)
210+
}
211+
212+
_, _, policyName := testutil.MkNames("test-package-cleanup-policy", "artifactory_package_cleanup_policy")
213+
214+
temp := `
215+
resource "artifactory_package_cleanup_policy" "{{ .policyName }}" {
216+
key = "{{ .policyName }}"
217+
description = "Test policy"
218+
cron_expression = "0 0 2 ? * MON-SAT *"
219+
duration_in_minutes = 60
220+
enabled = true
221+
skip_trashcan = false
222+
223+
search_criteria = {
224+
repos = ["**"]
225+
package_types = ["docker"]
226+
include_all_projects = true
227+
included_packages = ["**"]
228+
excluded_packages = ["com/jfrog/latest"]
229+
created_before_in_months = 0
230+
last_downloaded_before_in_months = 0
231+
}
232+
}`
233+
234+
config := util.ExecuteTemplate(
235+
policyName,
236+
temp,
237+
map[string]string{
238+
"policyName": policyName,
239+
},
240+
)
241+
242+
resource.Test(t, resource.TestCase{
243+
PreCheck: func() { acctest.PreCheck(t) },
244+
ProtoV6ProviderFactories: acctest.ProtoV6MuxProviderFactories,
245+
Steps: []resource.TestStep{
246+
{
247+
Config: config,
248+
ExpectError: regexp.MustCompile(".*Both created_before_in_months and last_downloaded_before_in_months cannot be\n.*zero at the same time.*"),
249+
},
250+
},
251+
})
252+
}
253+
198254
func TestAccPackageCleanupPolicy_full(t *testing.T) {
199255
client := acctest.GetTestResty(t)
200256
version, err := util.GetArtifactoryVersion(client)
@@ -247,7 +303,7 @@ func TestAccPackageCleanupPolicy_full(t *testing.T) {
247303
included_projects = [project.myproject.key]
248304
included_packages = ["**"]
249305
excluded_packages = ["com/jfrog/latest"]
250-
created_before_in_months = 1
306+
created_before_in_months = 0
251307
last_downloaded_before_in_months = 6
252308
}
253309
}`
@@ -274,7 +330,7 @@ func TestAccPackageCleanupPolicy_full(t *testing.T) {
274330
excluded_packages = ["com/jfrog/latest"]
275331
include_all_projects = true
276332
created_before_in_months = 12
277-
last_downloaded_before_in_months = 24
333+
last_downloaded_before_in_months = 0
278334
}
279335
}`
280336

@@ -323,7 +379,7 @@ func TestAccPackageCleanupPolicy_full(t *testing.T) {
323379
resource.TestCheckResourceAttr(fqrn, "search_criteria.included_packages.0", "**"),
324380
resource.TestCheckResourceAttr(fqrn, "search_criteria.excluded_packages.#", "1"),
325381
resource.TestCheckResourceAttr(fqrn, "search_criteria.excluded_packages.0", "com/jfrog/latest"),
326-
resource.TestCheckResourceAttr(fqrn, "search_criteria.created_before_in_months", "1"),
382+
resource.TestCheckResourceAttr(fqrn, "search_criteria.created_before_in_months", "0"),
327383
resource.TestCheckResourceAttr(fqrn, "search_criteria.last_downloaded_before_in_months", "6"),
328384
),
329385
},
@@ -349,7 +405,7 @@ func TestAccPackageCleanupPolicy_full(t *testing.T) {
349405
resource.TestCheckResourceAttr(fqrn, "search_criteria.excluded_packages.#", "1"),
350406
resource.TestCheckResourceAttr(fqrn, "search_criteria.excluded_packages.0", "com/jfrog/latest"),
351407
resource.TestCheckResourceAttr(fqrn, "search_criteria.created_before_in_months", "12"),
352-
resource.TestCheckResourceAttr(fqrn, "search_criteria.last_downloaded_before_in_months", "24"),
408+
resource.TestCheckResourceAttr(fqrn, "search_criteria.last_downloaded_before_in_months", "0"),
353409
),
354410
},
355411
{

0 commit comments

Comments
 (0)