Skip to content

Commit

Permalink
add disable_remount state upgrader
Browse files Browse the repository at this point in the history
  • Loading branch information
vinay-gopalan committed Oct 3, 2023
1 parent 7f71cc3 commit 117e1a3
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
45 changes: 45 additions & 0 deletions internal/provider/schema_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -50,6 +51,16 @@ func MustAddMountMigrationSchema(r *schema.Resource) *schema.Resource {
},
})

// Enable disable_remount V0 state upgrade
// Since we are adding a new boolean parameter that is expected
// to be set to a default upon upgrading, we update the TF state
// and set disable_remount to 'false' ONLY if it was previously 'nil'
//
// This case should only occur when upgrading from a version that
// does not support the disable_remount parameter (<v3.9.0)
r.StateUpgraders = getDisableRemountStateUpgraders()
r.SchemaVersion = 1

return r
}

Expand All @@ -70,3 +81,37 @@ func MustAddNamespaceSchema(d map[string]*schema.Schema) {
mustAddSchema(k, s, d)
}
}

func secretsAuthMountDisableRemountResourceV0() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
consts.FieldDisableRemount: {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "If set, opts out of mount migration " +
"on path updates.",
},
},
}
}

func secretsAuthMountDisableRemountUpgradeV0(
_ context.Context, rawState map[string]interface{}, _ interface{},
) (map[string]interface{}, error) {
if rawState[consts.FieldDisableRemount] == nil {
rawState[consts.FieldDisableRemount] = false
}

return rawState, nil
}

func getDisableRemountStateUpgraders() []schema.StateUpgrader {
return []schema.StateUpgrader{
{
Version: 0,
Type: secretsAuthMountDisableRemountResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: secretsAuthMountDisableRemountUpgradeV0,
},
}
}
45 changes: 45 additions & 0 deletions internal/provider/schema_util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"reflect"
"testing"

"github.com/hashicorp/terraform-provider-vault/internal/consts"
)

func TestSecretsAuthDisableRemountUpgradeV0(t *testing.T) {
tests := []struct {
name string
rawState map[string]interface{}
want map[string]interface{}
wantErr bool
}{
{
name: "basic",
rawState: map[string]interface{}{
consts.FieldDisableRemount: nil,
},
want: map[string]interface{}{
consts.FieldDisableRemount: false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := secretsAuthMountDisableRemountUpgradeV0(nil, tt.rawState, nil)

if tt.wantErr {
if err == nil {
t.Fatalf("SecretsAuthMountDisableRemountUpgradeV0() error = %#v, wantErr %#v", err, tt.wantErr)
}
}

if !reflect.DeepEqual(got, tt.want) {
t.Errorf("SecretsAuthMountDisableRemountUpgradeV0() got = %#v, want %#v", got, tt.want)
}
})
}
}

0 comments on commit 117e1a3

Please sign in to comment.