Skip to content

Commit 27ed47c

Browse files
committed
Refined solution based on PR feedback.
1 parent 817107f commit 27ed47c

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

cloudsmith/resource_repository_retention_rule.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ func resourceRepoRetentionRuleUpdate(d *schema.ResourceData, meta interface{}) e
4444
RetentionDaysLimit: &retentionDaysLimit,
4545
}
4646

47-
// retention_size_limit has no default, so use optionalInt64 to only send if set
48-
updateData.RetentionSizeLimit = optionalInt64(d, "retention_size_limit")
47+
// For retention_size_limit, we need to always send the value to handle
48+
// the case where users explicitly set it to 0 (which would otherwise be
49+
// indistinguishable from "not set" using GetOk)
50+
retentionSizeLimit := int64(d.Get("retention_size_limit").(int))
51+
updateData.RetentionSizeLimit = &retentionSizeLimit
4952

5053
req = req.Data(updateData)
5154

cloudsmith/resource_repository_retention_rule_test.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,27 @@ import (
77
"testing"
88

99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1011
)
1112

13+
// testCheckResourceAttrWithMessage enhances output for attribute checks
14+
func testCheckResourceAttrWithMessage(resourceName, attrName, expected string) resource.TestCheckFunc {
15+
return func(s *terraform.State) error {
16+
rs, ok := s.RootModule().Resources[resourceName]
17+
if !ok {
18+
return fmt.Errorf("Not found: %s", resourceName)
19+
}
20+
actual, ok := rs.Primary.Attributes[attrName]
21+
if !ok {
22+
return fmt.Errorf("Attribute '%s' not found in resource '%s'. State: %#v", attrName, resourceName, rs.Primary.Attributes)
23+
}
24+
if actual != expected {
25+
return fmt.Errorf("Attribute '%s' in resource '%s' expected '%s', got '%s'. Full state: %#v", attrName, resourceName, expected, actual, rs.Primary.Attributes)
26+
}
27+
return nil
28+
}
29+
}
30+
1231
func TestAccRepositoryRetentionRule_basic(t *testing.T) {
1332
t.Parallel()
1433

@@ -26,22 +45,23 @@ func TestAccRepositoryRetentionRule_basic(t *testing.T) {
2645
Config: testAccRepositoryRetentionRuleConfigBasic,
2746
Check: resource.ComposeTestCheckFunc(
2847
testAccRepositoryCheckExists("cloudsmith_repository.test-retention"),
29-
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_count_limit", "100"),
30-
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_days_limit", "28"),
31-
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_enabled", "false"),
32-
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_group_by_name", "false"),
33-
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_group_by_format", "false"),
34-
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_group_by_package_type", "false"),
35-
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_size_limit", "0"),
36-
resource.TestCheckResourceAttr("cloudsmith_repository_retention_rule.test", "retention_package_query_string", "name:test"),
48+
testCheckResourceAttrWithMessage("cloudsmith_repository_retention_rule.test", "retention_count_limit", "100"),
49+
testCheckResourceAttrWithMessage("cloudsmith_repository_retention_rule.test", "retention_days_limit", "28"),
50+
testCheckResourceAttrWithMessage("cloudsmith_repository_retention_rule.test", "retention_enabled", "false"),
51+
testCheckResourceAttrWithMessage("cloudsmith_repository_retention_rule.test", "retention_group_by_name", "false"),
52+
testCheckResourceAttrWithMessage("cloudsmith_repository_retention_rule.test", "retention_group_by_format", "false"),
53+
testCheckResourceAttrWithMessage("cloudsmith_repository_retention_rule.test", "retention_group_by_package_type", "false"),
54+
testCheckResourceAttrWithMessage("cloudsmith_repository_retention_rule.test", "retention_size_limit", "0"),
55+
testCheckResourceAttrWithMessage("cloudsmith_repository_retention_rule.test", "retention_package_query_string", "name:test"),
3756
),
3857
},
3958
{
4059
Config: testAccRepositoryRetentionRuleConfigZero,
4160
Check: resource.ComposeTestCheckFunc(
4261
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"),
62+
testCheckResourceAttrWithMessage("cloudsmith_repository_retention_rule.test", "retention_count_limit", "0"),
63+
testCheckResourceAttrWithMessage("cloudsmith_repository_retention_rule.test", "retention_days_limit", "0"),
64+
testCheckResourceAttrWithMessage("cloudsmith_repository_retention_rule.test", "retention_size_limit", "0"),
4565
),
4666
},
4767
},

docs/resources/repository_retention.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ The following arguments are supported:
4444
* `namespace` - (Required) The namespace of the repository.
4545
* `repository` - (Required) If true, the retention rules will be activated for the repository and settings will be updated.
4646
* `retention_enabled` - (Required) If true, the retention rules will be activated for the repository and settings will be updated.
47-
* `retention_count_limit` - (Optional) The maximum number of packages to retain. Must be between 0 and 10000.
47+
* `retention_count_limit` - (Optional) The maximum number of packages to retain. Must be between `0` and `10000`. Default set to 100 packages as part of repository creation.
4848
* `retention_days_limit` - (Optional) The number of days of packages to retain. Must be between `0` and `180`. Default set to 28 days as part of repository creation.
4949
* `retention_group_by_name` - (Optional) If true, retention will apply to groups of packages by name rather than all packages.
5050
* `retention_group_by_format` - (Optional) If true, retention will apply to packages by package formats rather than across all package formats.
5151
* `retention_group_by_package_type` - (Optional) If true, retention will apply to packages by package type rather than across all package types for one or more formats.
52-
* `retention_size_limit` - (Optional) The maximum total size (in bytes) of packages to retain. Must be between `0` and `21474836480` (21.47 GB / 21474.83 MB).
52+
* `retention_size_limit` - (Optional) The maximum total size (in bytes) of packages to retain. Must be between `0` and `20000000000` up to the maximum size of 20 GB (20,000,000,000 bytes).
5353
* `retention_package_query_string` - (Optional) A package search expression which, if provided, filters the packages to be deleted. For example, `name:foo` will only delete packages called 'foo'.
5454

5555
## Import

0 commit comments

Comments
 (0)