Skip to content

Commit

Permalink
Setting default idle_session_lifetime on tenant import (#849)
Browse files Browse the repository at this point in the history
* Using default value for tenant idle_session_lifetime

* Abstracting-out defaults into their global values

* Requested changes

---------

Co-authored-by: Will Vedder <will.vedder@okta.com>
  • Loading branch information
willvedd and willvedd authored Sep 28, 2023
1 parent b4d4962 commit 7f04a8e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
8 changes: 8 additions & 0 deletions internal/auth0/tenant/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

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"), 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)
})
}
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 7f04a8e

Please sign in to comment.