From 7f04a8e1b706a7b66be8e6817a859f592cbb1dc6 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Thu, 28 Sep 2023 15:26:21 -0400 Subject: [PATCH] Setting default `idle_session_lifetime` on tenant import (#849) * Using default value for tenant idle_session_lifetime * Abstracting-out defaults into their global values * Requested changes --------- Co-authored-by: Will Vedder --- internal/auth0/tenant/flatten.go | 8 ++++++ internal/auth0/tenant/flatten_test.go | 40 +++++++++++++++++++++++++++ internal/auth0/tenant/resource.go | 9 ++++-- 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 internal/auth0/tenant/flatten_test.go 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.", },