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 customDiff for sensitive config vars #389

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
38 changes: 38 additions & 0 deletions heroku/resource_heroku_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func resourceHerokuApp() *schema.Resource {
Computed: true,
},
},
CustomizeDiff: customDiffSensitiveConfigVars(),
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Expand Down Expand Up @@ -1011,6 +1012,7 @@ func resourceHerokuAppV0() *schema.Resource {
Computed: true,
},
},
CustomizeDiff: customDiffSensitiveConfigVars(),
}
}

Expand All @@ -1032,3 +1034,39 @@ func resourceHerokuAppStateUpgradeV0(ctx context.Context, rawState map[string]in

return rawState, nil
}

func customDiffSensitiveConfigVars() schema.CustomizeDiffFunc {
return func(ctx context.Context, d *schema.ResourceDiff, m interface{}) error {
old, new := d.GetChange("sensitive_config_vars")

changedKeys := diffMapKeys(old.(map[string]interface{}), new.(map[string]interface{}))

if len(changedKeys) > 0 {
d.SetNewComputed("sensitive_config_vars")
d.SetNew("changed_sensitive_config_var_keys", changedKeys)
}

return nil
}
}

// diffMapKeys returns the list of keys that differ between two maps
func diffMapKeys(oldMap, newMap map[string]interface{}) []string {
changedKeys := []string{}

// Check for added or updated keys
for k, newVal := range newMap {
if oldVal, ok := oldMap[k]; !ok || oldVal != newVal {
changedKeys = append(changedKeys, k)
}
}

// Check for removed keys
for k := range oldMap {
if _, ok := newMap[k]; !ok {
changedKeys = append(changedKeys, k)
}
}

return changedKeys
}
Loading