Skip to content

Commit 817107f

Browse files
committed
Fixed handling of zero values in retention attributes.
1 parent aebcdd4 commit 817107f

File tree

2 files changed

+53
-27
lines changed

2 files changed

+53
-27
lines changed

cloudsmith/resource_repository_retention_rule.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,25 @@ func resourceRepoRetentionRuleUpdate(d *schema.ResourceData, meta interface{}) e
2727
repo := requiredString(d, "repository")
2828

2929
req := pc.APIClient.ReposApi.RepoRetentionPartialUpdate(pc.Auth, namespace, repo)
30+
31+
// For integer fields with defaults, we need to always send the value to handle
32+
// the case where users explicitly set them to 0 (which would otherwise be
33+
// indistinguishable from "not set" using GetOk)
34+
retentionCountLimit := int64(d.Get("retention_count_limit").(int))
35+
retentionDaysLimit := int64(d.Get("retention_days_limit").(int))
36+
3037
updateData := cloudsmith.RepositoryRetentionRulesRequestPatch{
3138
RetentionEnabled: optionalBool(d, "retention_enabled"),
3239
RetentionGroupByName: optionalBool(d, "retention_group_by_name"),
3340
RetentionGroupByFormat: optionalBool(d, "retention_group_by_format"),
3441
RetentionGroupByPackageType: optionalBool(d, "retention_group_by_package_type"),
3542
RetentionPackageQueryString: nullableString(d, "retention_package_query_string"),
43+
RetentionCountLimit: &retentionCountLimit,
44+
RetentionDaysLimit: &retentionDaysLimit,
3645
}
3746

38-
// Explicitly set these values, even if they're zero
39-
if d.HasChange("retention_count_limit") {
40-
value := int64(d.Get("retention_count_limit").(int))
41-
updateData.RetentionCountLimit = &value
42-
}
43-
if d.HasChange("retention_days_limit") {
44-
value := int64(d.Get("retention_days_limit").(int))
45-
updateData.RetentionDaysLimit = &value
46-
}
47-
if d.HasChange("retention_size_limit") {
48-
value := int64(d.Get("retention_size_limit").(int))
49-
updateData.RetentionSizeLimit = &value
50-
}
47+
// retention_size_limit has no default, so use optionalInt64 to only send if set
48+
updateData.RetentionSizeLimit = optionalInt64(d, "retention_size_limit")
5149

5250
req = req.Data(updateData)
5351

cloudsmith/resource_repository_retention_rule_test.go

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,62 @@ func TestAccRepositoryRetentionRule_basic(t *testing.T) {
3636
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_package_query_string", "name:test"),
3737
),
3838
},
39+
{
40+
Config: testAccRepositoryRetentionRuleConfigZero,
41+
Check: resource.ComposeTestCheckFunc(
42+
testAccRepositoryCheckExists("cloudsmith_repository.test-retention"),
43+
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_count_limit", "0"),
44+
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_days_limit", "0"),
45+
),
46+
},
3947
},
4048
CheckDestroy: testAccRepositoryCheckDestroy("cloudsmith_repository.test-retention"),
4149
})
4250
}
4351

4452
var testAccRepositoryConfig = fmt.Sprintf(`
4553
resource "cloudsmith_repository" "test-retention" {
46-
name = "terraform-acc-repo-retention-rule"
47-
namespace = "%s"
54+
name = "terraform-acc-repo-retention-rule"
55+
namespace = "%s"
4856
}
4957
`, os.Getenv("CLOUDSMITH_NAMESPACE"))
5058

5159
var testAccRepositoryRetentionRuleConfigBasic = fmt.Sprintf(`
5260
resource "cloudsmith_repository" "test-retention" {
53-
name = "terraform-acc-repo-retention-rule"
54-
namespace = "%s"
61+
name = "terraform-acc-repo-retention-rule"
62+
namespace = "%s"
63+
}
64+
65+
resource "cloudsmith_repository_retention_rule" "test" {
66+
namespace = "%s"
67+
repository = cloudsmith_repository.test-retention.name
68+
retention_enabled = false
69+
retention_count_limit = 100
70+
retention_days_limit = 28
71+
retention_group_by_name = false
72+
retention_group_by_format = false
73+
retention_group_by_package_type = false
74+
retention_size_limit = 0
75+
retention_package_query_string = "name:test"
76+
}
77+
`, os.Getenv("CLOUDSMITH_NAMESPACE"), os.Getenv("CLOUDSMITH_NAMESPACE"))
78+
79+
var testAccRepositoryRetentionRuleConfigZero = fmt.Sprintf(`
80+
resource "cloudsmith_repository" "test-retention" {
81+
name = "terraform-acc-repo-retention-rule"
82+
namespace = "%s"
5583
}
5684
5785
resource "cloudsmith_repository_retention_rule" "test" {
58-
namespace = "%s"
59-
repository = cloudsmith_repository.test-retention.name
60-
retention_enabled = false
61-
retention_count_limit = 100
62-
retention_days_limit = 28
63-
retention_group_by_name = false
64-
retention_group_by_format = false
65-
retention_group_by_package_type = false
66-
retention_size_limit = 0
67-
retention_package_query_string = "name:test"
86+
namespace = "%s"
87+
repository = cloudsmith_repository.test-retention.name
88+
retention_enabled = false
89+
retention_count_limit = 0
90+
retention_days_limit = 0
91+
retention_group_by_name = false
92+
retention_group_by_format = false
93+
retention_group_by_package_type = false
94+
retention_size_limit = 0
95+
retention_package_query_string = "name:test"
6896
}
6997
`, os.Getenv("CLOUDSMITH_NAMESPACE"), os.Getenv("CLOUDSMITH_NAMESPACE"))

0 commit comments

Comments
 (0)