Skip to content

Commit

Permalink
Fix offline_cleanup_timeout_hours validation (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymarcus93 authored May 23, 2023
1 parent 297cd4b commit 3ffc724
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20230420-215304.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'resource/environment: Fix `offline_cleanup_timeout_hours` validation to match
behavior of the BastionZero API'
time: 2023-04-20T21:53:04.52468-04:00
custom:
Issues: "15"
15 changes: 11 additions & 4 deletions bastionzero/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ const (
// DefaultOfflineCleanupTimeoutHours is the default cleanup value to use if
// practitioner does not provide one
DefaultOfflineCleanupTimeoutHours = 24 * 90
// MaxOfflineCleanupTimeoutHours is the max value
// offline_cleanup_timeout_hours can be set to
MaxOfflineCleanupTimeoutHours = 24 * 180
// MinOfflineCleanupTimeoutHours is the min value
// offline_cleanup_timeout_hours can be set to
MinOfflineCleanupTimeoutHours = 0
)

// environmentModel maps the environment schema data.
Expand Down Expand Up @@ -140,13 +146,14 @@ func makeEnvironmentResourceSchema() map[string]schema.Attribute {
Description: fmt.Sprintf("The time this environment was created in BastionZero %s.", internal.PrettyRFC3339Timestamp()),
},
"offline_cleanup_timeout_hours": schema.Int64Attribute{
Optional: true,
Computed: true,
Description: "The amount of time (in hours) to wait until offline targets are automatically removed by BastionZero (Defaults to `2160` hours [90 days]).",
Optional: true,
Computed: true,
Description: "The amount of time (in hours) to wait until offline targets are automatically removed by BastionZero (Defaults to `2160` hours [90 days])." +
" If this value is `0`, then offline target cleanup is disabled.",
// Default to 90 days like in webapp
Default: int64default.StaticInt64(DefaultOfflineCleanupTimeoutHours),
Validators: []validator.Int64{
int64validator.AtLeast(1),
int64validator.Between(MinOfflineCleanupTimeoutHours, MaxOfflineCleanupTimeoutHours),
},
},
"targets": schema.MapNestedAttribute{
Expand Down
37 changes: 34 additions & 3 deletions bastionzero/environment/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,32 @@ func TestAccEnvironment_OfflineCleanupTimeoutHours(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "offline_cleanup_timeout_hours", strconv.Itoa(timeout2)),
),
},
// Try setting it to the max value
{
Config: testAccEnvironmentConfigOfflineCleanupTimeoutHours(rName, strconv.Itoa(environment.MaxOfflineCleanupTimeoutHours)),
Check: resource.ComposeTestCheckFunc(
testAccCheckEnvironmentExists(resourceName, &env),
testAccCheckEnvironmentAttributes(&env, &expectedEnvironment{
Name: &rName,
OfflineCleanupTimeoutHours: bastionzero.PtrTo(environment.MaxOfflineCleanupTimeoutHours),
}),
testAccCheckResourceEnvironmentComputedAttr(resourceName),
resource.TestCheckResourceAttr(resourceName, "offline_cleanup_timeout_hours", strconv.Itoa(environment.MaxOfflineCleanupTimeoutHours)),
),
},
// Try setting it to the min value
{
Config: testAccEnvironmentConfigOfflineCleanupTimeoutHours(rName, strconv.Itoa(environment.MinOfflineCleanupTimeoutHours)),
Check: resource.ComposeTestCheckFunc(
testAccCheckEnvironmentExists(resourceName, &env),
testAccCheckEnvironmentAttributes(&env, &expectedEnvironment{
Name: &rName,
OfflineCleanupTimeoutHours: bastionzero.PtrTo(environment.MinOfflineCleanupTimeoutHours),
}),
testAccCheckResourceEnvironmentComputedAttr(resourceName),
resource.TestCheckResourceAttr(resourceName, "offline_cleanup_timeout_hours", strconv.Itoa(environment.MinOfflineCleanupTimeoutHours)),
),
},
},
})
}
Expand Down Expand Up @@ -244,9 +270,14 @@ func TestEnvironment_InvalidOfflineCleanupTimeoutHours(t *testing.T) {
ProtoV6ProviderFactories: acctest.TestProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
// Hours cannot be 0
Config: testAccEnvironmentConfigOfflineCleanupTimeoutHours("test", "0"),
ExpectError: regexp.MustCompile(`must be at least 1`),
// Hours cannot go below the min
Config: testAccEnvironmentConfigOfflineCleanupTimeoutHours("test", strconv.Itoa(environment.MinOfflineCleanupTimeoutHours-1)),
ExpectError: regexp.MustCompile(`Invalid Attribute Value`),
},
{
// Hours cannot go above the max
Config: testAccEnvironmentConfigOfflineCleanupTimeoutHours("test", strconv.Itoa(environment.MaxOfflineCleanupTimeoutHours+1)),
ExpectError: regexp.MustCompile(`Invalid Attribute Value`),
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion docs/data-sources/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ output "example_env_targets" {
- `description` (String) The environment's description.
- `is_default` (Boolean) If `true`, this environment is the default environment; `false` otherwise.
- `name` (String) The environment's name.
- `offline_cleanup_timeout_hours` (Number) The amount of time (in hours) to wait until offline targets are automatically removed by BastionZero (Defaults to `2160` hours [90 days]).
- `offline_cleanup_timeout_hours` (Number) The amount of time (in hours) to wait until offline targets are automatically removed by BastionZero (Defaults to `2160` hours [90 days]). If this value is `0`, then offline target cleanup is disabled.
- `organization_id` (String) The environment's organization's ID.
- `targets` (Attributes Map) Map of targets that belong to this environment. The map is keyed by a target's unique ID. (see [below for nested schema](#nestedatt--targets))
- `time_created` (String) The time this environment was created in BastionZero formatted as a UTC timestamp string in [RFC 3339](https://datatracker.ietf.org/doc/html/rfc3339) format.
Expand Down
2 changes: 1 addition & 1 deletion docs/data-sources/environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Read-Only:
- `id` (String) The environment's unique ID.
- `is_default` (Boolean) If `true`, this environment is the default environment; `false` otherwise.
- `name` (String) The environment's name.
- `offline_cleanup_timeout_hours` (Number) The amount of time (in hours) to wait until offline targets are automatically removed by BastionZero (Defaults to `2160` hours [90 days]).
- `offline_cleanup_timeout_hours` (Number) The amount of time (in hours) to wait until offline targets are automatically removed by BastionZero (Defaults to `2160` hours [90 days]). If this value is `0`, then offline target cleanup is disabled.
- `organization_id` (String) The environment's organization's ID.
- `targets` (Attributes Map) Map of targets that belong to this environment. The map is keyed by a target's unique ID. (see [below for nested schema](#nestedatt--environments--targets))
- `time_created` (String) The time this environment was created in BastionZero formatted as a UTC timestamp string in [RFC 3339](https://datatracker.ietf.org/doc/html/rfc3339) format.
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ resource "bastionzero_environment" "example" {
### Optional

- `description` (String) The environment's description.
- `offline_cleanup_timeout_hours` (Number) The amount of time (in hours) to wait until offline targets are automatically removed by BastionZero (Defaults to `2160` hours [90 days]).
- `offline_cleanup_timeout_hours` (Number) The amount of time (in hours) to wait until offline targets are automatically removed by BastionZero (Defaults to `2160` hours [90 days]). If this value is `0`, then offline target cleanup is disabled.

### Read-Only

Expand Down

0 comments on commit 3ffc724

Please sign in to comment.