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

[WIP] Support "hiding" ignored parameters from state. #7237

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
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type ResourceLifecycle struct {
CreateBeforeDestroy bool `mapstructure:"create_before_destroy"`
PreventDestroy bool `mapstructure:"prevent_destroy"`
IgnoreChanges []string `mapstructure:"ignore_changes"`
HideIgnored bool `mapstructure:"hide_ignored"`
}

// Copy returns a copy of this ResourceLifecycle
Expand Down
2 changes: 1 addition & 1 deletion config/loader_hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ func loadManagedResourcesHcl(list *ast.ObjectList) ([]*Resource, error) {
var lifecycle ResourceLifecycle
if o := listVal.Filter("lifecycle"); len(o.Items) > 0 {
// Check for invalid keys
valid := []string{"create_before_destroy", "ignore_changes", "prevent_destroy"}
valid := []string{"create_before_destroy", "ignore_changes", "hide_ignored", "prevent_destroy"}
if err := checkHCLKeys(o.Items[0].Val, valid); err != nil {
return nil, multierror.Prefix(err, fmt.Sprintf(
"%s[%s]:", t, k))
Expand Down
40 changes: 40 additions & 0 deletions terraform/eval_hide_ignored.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package terraform

import (
"strings"

"github.com/hashicorp/terraform/config"
)

// EvalHideIgnored is an EvalNode implementation that removes ignored
// attributes from the state.
type EvalHideIgnored struct {
Resource *config.Resource
State **InstanceState
}

func (n *EvalHideIgnored) Eval(ctx EvalContext) (interface{}, error) {
if n.State == nil || *n.State == nil || n.Resource == nil || n.Resource.Id() == "" {
return nil, nil
}
var state *InstanceState = *n.State

if !n.Resource.Lifecycle.HideIgnored {
return nil, nil
}

ignoreChanges := n.Resource.Lifecycle.IgnoreChanges
if len(ignoreChanges) == 0 {
return nil, nil
}

for _, ignoredName := range ignoreChanges {
for name := range state.Attributes {
if strings.HasPrefix(name, ignoredName) {
delete(state.Attributes, name)
}
}
}

return nil, nil
}
16 changes: 16 additions & 0 deletions terraform/transform_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ func (n *graphNodeExpandedResource) managedResourceEvalNodes(resource *Resource,
State: &state,
Output: &state,
},
&EvalHideIgnored{
Resource: n.Resource,
State: &state,
},
&EvalWriteState{
Name: n.stateId(),
ResourceType: n.Resource.Type,
Expand Down Expand Up @@ -348,6 +352,10 @@ func (n *graphNodeExpandedResource) managedResourceEvalNodes(resource *Resource,
Resource: n.Resource,
Diff: &diff,
},
&EvalHideIgnored{
Resource: n.Resource,
State: &state,
},
&EvalWriteState{
Name: n.stateId(),
ResourceType: n.Resource.Type,
Expand Down Expand Up @@ -499,6 +507,10 @@ func (n *graphNodeExpandedResource) managedResourceEvalNodes(resource *Resource,
Error: &err,
CreateNew: &createNew,
},
&EvalHideIgnored{
Resource: n.Resource,
State: &state,
},
&EvalWriteState{
Name: n.stateId(),
ResourceType: n.Resource.Type,
Expand Down Expand Up @@ -922,6 +934,10 @@ func (n *graphNodeExpandedResourceDestroy) EvalTree() EvalNode {
Error: &err,
},
},
&EvalHideIgnored{
Resource: n.Resource,
State: &state,
},
&EvalWriteState{
Name: n.stateId(),
ResourceType: n.Resource.Type,
Expand Down