Skip to content

Commit

Permalink
Abstracting-out defaults into their global values
Browse files Browse the repository at this point in the history
  • Loading branch information
willvedd committed Sep 27, 2023
1 parent 816c557 commit 730ec82
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
8 changes: 6 additions & 2 deletions internal/auth0/tenant/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func flattenTenant(data *schema.ResourceData, tenant *management.Tenant) error {
data.Set("support_email", tenant.GetSupportEmail()),
data.Set("support_url", tenant.GetSupportURL()),
data.Set("allowed_logout_urls", tenant.GetAllowedLogoutURLs()),
data.Set("session_lifetime", tenant.GetSessionLifetime()),
data.Set("sandbox_version", tenant.GetSandboxVersion()),
data.Set("enabled_locales", tenant.GetEnabledLocales()),
data.Set("flags", flattenTenantFlags(tenant.GetFlags())),
Expand All @@ -26,12 +25,17 @@ func flattenTenant(data *schema.ResourceData, tenant *management.Tenant) error {
)

if tenant.GetIdleSessionLifetime() == 0 {
idleSessionLifetimeDefault := NewResource().Schema["idle_session_lifetime"].Default
result = multierror.Append(result, data.Set("idle_session_lifetime", idleSessionLifetimeDefault))
} else {
result = multierror.Append(result, data.Set("idle_session_lifetime", tenant.GetIdleSessionLifetime()))
}

if tenant.GetSessionLifetime() == 0 {
result = multierror.Append(result, data.Set("session_lifetime", sessionLifetimeDefault))
} else {
result = multierror.Append(result, data.Set("session_lifetime", tenant.GetSessionLifetime()))
}

return result.ErrorOrNil()
}

Expand Down
40 changes: 40 additions & 0 deletions internal/auth0/tenant/flatten_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tenant

import (
"testing"

"github.com/auth0/go-auth0"
"github.com/auth0/go-auth0/management"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/assert"
)

func TestFlattenTenant(t *testing.T) {
mockResourceData := schema.TestResourceDataRaw(t, NewResource().Schema, map[string]interface{}{})

t.Run("it sets default values if remote tenant does not have them set", func(t *testing.T) {
tenant := management.Tenant{
IdleSessionLifetime: nil,
SessionLifetime: nil,
}

err := flattenTenant(mockResourceData, &tenant)

assert.NoError(t, err)
assert.Equal(t, mockResourceData.Get("idle_session_lifetime"), idleSessionLifetimeDefault)
assert.Equal(t, mockResourceData.Get("session_lifetime"), sessionLifetimeDefault)
})

t.Run("it does not set default values if remote tenant has values set", func(t *testing.T) {
tenant := management.Tenant{
IdleSessionLifetime: auth0.Float64(73.5),
SessionLifetime: auth0.Float64(169.5),
}

err := flattenTenant(mockResourceData, &tenant)

assert.NoError(t, err)
assert.Equal(t, mockResourceData.Get("idle_session_lifetime"), 73.5)
assert.Equal(t, mockResourceData.Get("session_lifetime"), 169.5)
})
}
9 changes: 7 additions & 2 deletions internal/auth0/tenant/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
internalValidation "github.com/auth0/terraform-provider-auth0/internal/validation"
)

const (
idleSessionLifetimeDefault = 72.00
sessionLifetimeDefault = 168.00
)

// NewResource will return a new auth0_tenant resource.
func NewResource() *schema.Resource {
return &schema.Resource{
Expand Down Expand Up @@ -82,14 +87,14 @@ func NewResource() *schema.Resource {
"session_lifetime": {
Type: schema.TypeFloat,
Optional: true,
Default: 168,
Default: sessionLifetimeDefault,
ValidateFunc: validation.FloatAtLeast(0.01),
Description: "Number of hours during which a session will stay valid.",
},
"idle_session_lifetime": {
Type: schema.TypeFloat,
Optional: true,
Default: 72,
Default: idleSessionLifetimeDefault,
ValidateFunc: validation.FloatAtLeast(0.01),
Description: "Number of hours during which a session can be inactive before the user must log in again.",
},
Expand Down

0 comments on commit 730ec82

Please sign in to comment.