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

add custom diff for namespace parameter on resources #2065

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions internal/provider/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ func NewProviderMeta(d *schema.ResourceData) (interface{}, error) {
namespace = tokenNamespace
// set the namespace on the provider to ensure that all child
// namespace paths are properly honoured.
if err := d.Set(consts.FieldNamespace, namespace); err != nil {
return nil, err
}
//if err := d.Set(consts.FieldNamespace, namespace); err != nil {
// return nil, err
//}
}

if namespace != "" {
Expand Down
36 changes: 36 additions & 0 deletions internal/provider/schema_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,42 @@ func MustAddMountMigrationSchema(r *schema.Resource, customStateUpgrade bool) *s
return r
}

func NamespacePathCustomizeDiffFunc() schema.CustomizeDiffFunc {
return func(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
field := consts.FieldNamespace
if !diff.HasChange(field) {
return nil
}

o, n := diff.GetChange(field)
// if namespace is just set, ignore
if o == "" {
return nil
}

// get namespace set in provider block
client := meta.(*ProviderMeta).GetClient()
parentNS := client.Namespace()

// construct provider block ns + resource block ns path
constructedNamespace := fmt.Sprintf("%s/%s", parentNS, n)

fmt.Printf("[VINAY]: parent NS: %s\n", parentNS)
fmt.Printf("[VINAY]: constructed NS: %s\n", constructedNamespace)
fmt.Printf("[VINAY]: old NS in TF state: %s\n", o)
fmt.Printf("[VINAY]: new NS in TF config: %s\n", n)

// compare fully qualified path in TF state with new constructed namespace
if o.(string) == constructedNamespace {
return nil
}

// New NS path does not match existing namespace in state
// Destroy and recreate resource
return diff.ForceNew(field)
}
}

func GetNamespaceSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
consts.FieldNamespace: {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
func main() {
p := schema.NewProvider(vault.Provider())
for name, resource := range generated.DataSourceRegistry {
p.RegisterDataSource(name, vault.UpdateSchemaResource(resource))
p.RegisterDataSource(name, vault.UpdateSchemaResource(resource, false))
}
for name, resource := range generated.ResourceRegistry {
p.RegisterResource(name, vault.UpdateSchemaResource(resource))
p.RegisterResource(name, vault.UpdateSchemaResource(resource, false))
}

serveOpts := &plugin.ServeOpts{
Expand Down
Loading
Loading