Skip to content

Commit

Permalink
refactor code a bit to allow for ldap auth engine state upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
vinay-gopalan committed Oct 3, 2023
1 parent 3bc35d1 commit f81b315
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 34 deletions.
32 changes: 17 additions & 15 deletions internal/provider/schema_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func mustAddSchema(k string, s *schema.Schema, d map[string]*schema.Schema) {
d[k] = s
}

func MustAddMountMigrationSchema(r *schema.Resource) *schema.Resource {
func MustAddMountMigrationSchema(r *schema.Resource, customStateUpgrade bool) *schema.Resource {
MustAddSchema(r, map[string]*schema.Schema{
consts.FieldDisableRemount: {
Type: schema.TypeBool,
Expand All @@ -51,15 +51,17 @@ 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
if !customStateUpgrade {
// Enable disable_remount default 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 = defaultDisableRemountStateUpgraders()
r.SchemaVersion = 1
}

return r
}
Expand All @@ -82,7 +84,7 @@ func MustAddNamespaceSchema(d map[string]*schema.Schema) {
}
}

func secretsAuthMountDisableRemountResourceV0() *schema.Resource {
func SecretsAuthMountDisableRemountResourceV0() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
consts.FieldDisableRemount: {
Expand All @@ -96,7 +98,7 @@ func secretsAuthMountDisableRemountResourceV0() *schema.Resource {
}
}

func secretsAuthMountDisableRemountUpgradeV0(
func SecretsAuthMountDisableRemountUpgradeV0(
_ context.Context, rawState map[string]interface{}, _ interface{},
) (map[string]interface{}, error) {
if rawState[consts.FieldDisableRemount] == nil {
Expand All @@ -106,12 +108,12 @@ func secretsAuthMountDisableRemountUpgradeV0(
return rawState, nil
}

func getDisableRemountStateUpgraders() []schema.StateUpgrader {
func defaultDisableRemountStateUpgraders() []schema.StateUpgrader {
return []schema.StateUpgrader{
{
Version: 0,
Type: secretsAuthMountDisableRemountResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: secretsAuthMountDisableRemountUpgradeV0,
Type: SecretsAuthMountDisableRemountResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: SecretsAuthMountDisableRemountUpgradeV0,
},
}
}
2 changes: 1 addition & 1 deletion internal/provider/schema_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestSecretsAuthDisableRemountUpgradeV0(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := secretsAuthMountDisableRemountUpgradeV0(nil, tt.rawState, nil)
got, err := SecretsAuthMountDisableRemountUpgradeV0(nil, tt.rawState, nil)

if tt.wantErr {
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_ad_secret_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func adSecretBackendResource() *schema.Resource {
},
CustomizeDiff: getMountCustomizeDiffFunc(consts.FieldBackend),
Schema: fields,
})
}, false)
}

func createConfigResource(d *schema.ResourceData, meta interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_auth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func AuthBackendResource() *schema.Resource {

"tune": authMountTuneSchema(),
},
})
}, false)
}

func authBackendWrite(d *schema.ResourceData, meta interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_aws_secret_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func awsSecretBackendResource() *schema.Resource {
Description: "Specifies if the secret backend is local only",
},
},
})
}, false)
}

func getMountCustomizeDiffFunc(field string) schema.CustomizeDiffFunc {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_azure_secret_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func azureSecretBackendResource() *schema.Resource {
Description: "The Azure cloud environment. Valid values: AzurePublicCloud, AzureUSGovernmentCloud, AzureChinaCloud, AzureGermanCloud.",
},
},
})
}, false)
}

func azureSecretBackendCreate(d *schema.ResourceData, meta interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_consul_secret_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func consulSecretBackendResource() *schema.Resource {
Description: "Specifies if the secret backend is local only",
},
},
})
}, false)
}

func consulSecretBackendCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_gcp_auth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func gcpAuthBackendResource() *schema.Resource {
Description: "The accessor of the auth backend",
},
},
})
}, false)
}

func gcpAuthCustomEndpointSchema() map[string]*schema.Schema {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_gcp_secret_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func gcpSecretBackendResource(name string) *schema.Resource {
Description: "Local mount flag that can be explicitly set to true to enforce local mount in HA environment",
},
},
})
}, false)
}

func gcpSecretBackendCreate(d *schema.ResourceData, meta interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_github_auth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func githubAuthBackendResource() *schema.Resource {
},
Schema: fields,
CustomizeDiff: getMountCustomizeDiffFunc(consts.FieldPath),
})
}, false)
}

func githubAuthBackendCreate(d *schema.ResourceData, meta interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_jwt_auth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func jwtAuthBackendResource() *schema.Resource {

"tune": authMountTuneSchema(),
},
})
}, false)
}

func jwtCustomizeDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_kmip_secret_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func kmipSecretBackendResource() *schema.Resource {
Description: "Client certificate TTL in seconds",
},
},
})
}, false)
}

func kmipSecretBackendCreate(d *schema.ResourceData, meta interface{}) error {
Expand Down
13 changes: 10 additions & 3 deletions vault/resource_ldap_auth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,15 @@ func ldapAuthBackendResource() *schema.Resource {
addTokenFields(fields, &addTokenFieldsConfig{})

return provider.MustAddMountMigrationSchema(&schema.Resource{
SchemaVersion: 1,

SchemaVersion: 2,
// Handle custom state upgrade case since schema version was already 1
StateUpgraders: []schema.StateUpgrader{
{
Version: 1,
Type: provider.SecretsAuthMountDisableRemountResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: provider.SecretsAuthMountDisableRemountUpgradeV0,
},
},
CreateContext: ldapAuthBackendWrite,
UpdateContext: ldapAuthBackendUpdate,
ReadContext: provider.ReadContextWrapper(ldapAuthBackendRead),
Expand All @@ -185,7 +192,7 @@ func ldapAuthBackendResource() *schema.Resource {
},
CustomizeDiff: getMountCustomizeDiffFunc(consts.FieldPath),
Schema: fields,
})
}, true)
}

func ldapAuthBackendConfigPath(path string) string {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_ldap_secret_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func ldapSecretBackendResource() *schema.Resource {
},
CustomizeDiff: getMountCustomizeDiffFunc(consts.FieldPath),
Schema: fields,
})
}, false)

// Add common mount schema to the resource
provider.MustAddSchema(resource, getMountSchema("path", "type"))
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_nomad_secret_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func nomadSecretAccessBackendResource() *schema.Resource {
State: schema.ImportStatePassthrough,
},
Schema: fields,
})
}, false)
}

func createNomadAccessConfigResource(d *schema.ResourceData, meta interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_okta_auth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func oktaAuthBackendResource() *schema.Resource {
Description: "The mount accessor related to the auth mount.",
},
},
})
}, false)
}

func normalizeOktaTTL(i interface{}) string {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_rabbitmq_secret_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func rabbitMQSecretBackendResource() *schema.Resource {
Description: "Template describing how dynamic usernames are generated.",
},
},
})
}, false)
}

func rabbitMQSecretBackendCreate(d *schema.ResourceData, meta interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion vault/resource_terraform_cloud_secret_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func terraformCloudSecretBackendResource() *schema.Resource {
Description: "Maximum possible lease duration for secrets in seconds",
},
},
})
}, false)
}

func terraformCloudSecretBackendCreate(d *schema.ResourceData, meta interface{}) error {
Expand Down

0 comments on commit f81b315

Please sign in to comment.