Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting default idle_session_lifetime on tenant import #849

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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