github.com/hs0210/hashicorp-terraform@v0.11.12-beta1/terraform/node_output.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/terraform/config"
     8  	"github.com/hashicorp/terraform/dag"
     9  )
    10  
    11  // NodeApplyableOutput represents an output that is "applyable":
    12  // it is ready to be applied.
    13  type NodeApplyableOutput struct {
    14  	PathValue []string
    15  	Config    *config.Output // Config is the output in the config
    16  }
    17  
    18  func (n *NodeApplyableOutput) Name() string {
    19  	result := fmt.Sprintf("output.%s", n.Config.Name)
    20  	if len(n.PathValue) > 1 {
    21  		result = fmt.Sprintf("%s.%s", modulePrefixStr(n.PathValue), result)
    22  	}
    23  
    24  	return result
    25  }
    26  
    27  // GraphNodeSubPath
    28  func (n *NodeApplyableOutput) Path() []string {
    29  	return n.PathValue
    30  }
    31  
    32  // RemovableIfNotTargeted
    33  func (n *NodeApplyableOutput) RemoveIfNotTargeted() bool {
    34  	// We need to add this so that this node will be removed if
    35  	// it isn't targeted or a dependency of a target.
    36  	return true
    37  }
    38  
    39  // GraphNodeTargetDownstream
    40  func (n *NodeApplyableOutput) TargetDownstream(targetedDeps, untargetedDeps *dag.Set) bool {
    41  	// If any of the direct dependencies of an output are targeted then
    42  	// the output must always be targeted as well, so its value will always
    43  	// be up-to-date at the completion of an apply walk.
    44  	return true
    45  }
    46  
    47  // GraphNodeReferenceable
    48  func (n *NodeApplyableOutput) ReferenceableName() []string {
    49  	name := fmt.Sprintf("output.%s", n.Config.Name)
    50  	return []string{name}
    51  }
    52  
    53  // GraphNodeReferencer
    54  func (n *NodeApplyableOutput) References() []string {
    55  	var result []string
    56  	result = append(result, n.Config.DependsOn...)
    57  	result = append(result, ReferencesFromConfig(n.Config.RawConfig)...)
    58  	for _, v := range result {
    59  		split := strings.Split(v, "/")
    60  		for i, s := range split {
    61  			split[i] = s + ".destroy"
    62  		}
    63  
    64  		result = append(result, strings.Join(split, "/"))
    65  	}
    66  
    67  	return result
    68  }
    69  
    70  // GraphNodeEvalable
    71  func (n *NodeApplyableOutput) EvalTree() EvalNode {
    72  	return &EvalSequence{
    73  		Nodes: []EvalNode{
    74  			&EvalOpFilter{
    75  				// Don't let interpolation errors stop Input, since it happens
    76  				// before Refresh.
    77  				Ops: []walkOperation{walkInput},
    78  				Node: &EvalWriteOutput{
    79  					Name:          n.Config.Name,
    80  					Sensitive:     n.Config.Sensitive,
    81  					Value:         n.Config.RawConfig,
    82  					ContinueOnErr: true,
    83  				},
    84  			},
    85  			&EvalOpFilter{
    86  				Ops: []walkOperation{walkRefresh, walkPlan, walkApply, walkValidate, walkDestroy, walkPlanDestroy},
    87  				Node: &EvalWriteOutput{
    88  					Name:      n.Config.Name,
    89  					Sensitive: n.Config.Sensitive,
    90  					Value:     n.Config.RawConfig,
    91  				},
    92  			},
    93  		},
    94  	}
    95  }
    96  
    97  // NodeDestroyableOutput represents an output that is "destroybale":
    98  // its application will remove the output from the state.
    99  type NodeDestroyableOutput struct {
   100  	PathValue []string
   101  	Config    *config.Output // Config is the output in the config
   102  }
   103  
   104  func (n *NodeDestroyableOutput) Name() string {
   105  	result := fmt.Sprintf("output.%s (destroy)", n.Config.Name)
   106  	if len(n.PathValue) > 1 {
   107  		result = fmt.Sprintf("%s.%s", modulePrefixStr(n.PathValue), result)
   108  	}
   109  
   110  	return result
   111  }
   112  
   113  // GraphNodeSubPath
   114  func (n *NodeDestroyableOutput) Path() []string {
   115  	return n.PathValue
   116  }
   117  
   118  // RemovableIfNotTargeted
   119  func (n *NodeDestroyableOutput) RemoveIfNotTargeted() bool {
   120  	// We need to add this so that this node will be removed if
   121  	// it isn't targeted or a dependency of a target.
   122  	return true
   123  }
   124  
   125  // This will keep the destroy node in the graph if its corresponding output
   126  // node is also in the destroy graph.
   127  func (n *NodeDestroyableOutput) TargetDownstream(targetedDeps, untargetedDeps *dag.Set) bool {
   128  	return true
   129  }
   130  
   131  // GraphNodeReferencer
   132  func (n *NodeDestroyableOutput) References() []string {
   133  	var result []string
   134  	result = append(result, n.Config.DependsOn...)
   135  	result = append(result, ReferencesFromConfig(n.Config.RawConfig)...)
   136  	for _, v := range result {
   137  		split := strings.Split(v, "/")
   138  		for i, s := range split {
   139  			split[i] = s + ".destroy"
   140  		}
   141  
   142  		result = append(result, strings.Join(split, "/"))
   143  	}
   144  
   145  	return result
   146  }
   147  
   148  // GraphNodeEvalable
   149  func (n *NodeDestroyableOutput) EvalTree() EvalNode {
   150  	return &EvalDeleteOutput{
   151  		Name: n.Config.Name,
   152  	}
   153  }