diff --git a/internal/auth0/tenant/flatten.go b/internal/auth0/tenant/flatten.go index 73344e284..8386c4c01 100644 --- a/internal/auth0/tenant/flatten.go +++ b/internal/auth0/tenant/flatten.go @@ -26,6 +26,14 @@ func flattenTenant(data *schema.ResourceData, tenant *management.Tenant) error { data.Set("allow_organization_name_in_authentication_api", tenant.GetAllowOrgNameInAuthAPI()), ) + if tenant.GetIdleSessionLifetime() == 0 { + result = multierror.Append(result, data.Set("idle_session_lifetime", idleSessionLifetimeDefault)) + } + + if tenant.GetSessionLifetime() == 0 { + result = multierror.Append(result, data.Set("session_lifetime", sessionLifetimeDefault)) + } + return result.ErrorOrNil() } diff --git a/internal/auth0/tenant/flatten_test.go b/internal/auth0/tenant/flatten_test.go new file mode 100644 index 000000000..01e7f2f0a --- /dev/null +++ b/internal/auth0/tenant/flatten_test.go @@ -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"), 72.00) + assert.Equal(t, mockResourceData.Get("session_lifetime"), 168.00) + }) + + 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) + }) +} diff --git a/internal/auth0/tenant/resource.go b/internal/auth0/tenant/resource.go index 89a34ddb2..5505d82d7 100644 --- a/internal/auth0/tenant/resource.go +++ b/internal/auth0/tenant/resource.go @@ -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{ @@ -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.", },