github.com/rhenning/terraform@v0.8.0-beta2/terraform/graph_config_node_provider.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/config"
     7  	"github.com/hashicorp/terraform/dag"
     8  )
     9  
    10  // GraphNodeConfigProvider represents a configured provider within the
    11  // configuration graph. These are only immediately in the graph when an
    12  // explicit `provider` configuration block is in the configuration.
    13  type GraphNodeConfigProvider struct {
    14  	Provider *config.ProviderConfig
    15  }
    16  
    17  func (n *GraphNodeConfigProvider) Name() string {
    18  	return fmt.Sprintf("provider.%s", n.ProviderName())
    19  }
    20  
    21  func (n *GraphNodeConfigProvider) ConfigType() GraphNodeConfigType {
    22  	return GraphNodeConfigTypeProvider
    23  }
    24  
    25  func (n *GraphNodeConfigProvider) DependableName() []string {
    26  	return []string{n.Name()}
    27  }
    28  
    29  func (n *GraphNodeConfigProvider) DependentOn() []string {
    30  	vars := n.Provider.RawConfig.Variables
    31  	result := make([]string, 0, len(vars))
    32  	for _, v := range vars {
    33  		if vn := varNameForVar(v); vn != "" {
    34  			result = append(result, vn)
    35  		}
    36  	}
    37  
    38  	return result
    39  }
    40  
    41  // GraphNodeEvalable impl.
    42  func (n *GraphNodeConfigProvider) EvalTree() EvalNode {
    43  	return ProviderEvalTree(n.ProviderName(), n.Provider.RawConfig)
    44  }
    45  
    46  // GraphNodeProvider implementation
    47  func (n *GraphNodeConfigProvider) ProviderName() string {
    48  	if n.Provider.Alias == "" {
    49  		return n.Provider.Name
    50  	} else {
    51  		return fmt.Sprintf("%s.%s", n.Provider.Name, n.Provider.Alias)
    52  	}
    53  }
    54  
    55  // GraphNodeProvider implementation
    56  func (n *GraphNodeConfigProvider) ProviderConfig() *config.RawConfig {
    57  	return n.Provider.RawConfig
    58  }
    59  
    60  // GraphNodeDotter impl.
    61  func (n *GraphNodeConfigProvider) DotNode(name string, opts *dag.DotOpts) *dag.DotNode {
    62  	return &dag.DotNode{
    63  		Name: name,
    64  		Attrs: map[string]string{
    65  			"label": n.Name(),
    66  			"shape": "diamond",
    67  		},
    68  	}
    69  }
    70  
    71  // GraphNodeDotterOrigin impl.
    72  func (n *GraphNodeConfigProvider) DotOrigin() bool {
    73  	return true
    74  }
    75  
    76  // GraphNodeFlattenable impl.
    77  func (n *GraphNodeConfigProvider) Flatten(p []string) (dag.Vertex, error) {
    78  	return &GraphNodeConfigProviderFlat{
    79  		GraphNodeConfigProvider: n,
    80  		PathValue:               p,
    81  	}, nil
    82  }
    83  
    84  // Same as GraphNodeConfigProvider, but for flattening
    85  type GraphNodeConfigProviderFlat struct {
    86  	*GraphNodeConfigProvider
    87  
    88  	PathValue []string
    89  }
    90  
    91  func (n *GraphNodeConfigProviderFlat) Name() string {
    92  	return fmt.Sprintf(
    93  		"%s.%s", modulePrefixStr(n.PathValue), n.GraphNodeConfigProvider.Name())
    94  }
    95  
    96  func (n *GraphNodeConfigProviderFlat) Path() []string {
    97  	return n.PathValue
    98  }
    99  
   100  func (n *GraphNodeConfigProviderFlat) DependableName() []string {
   101  	return modulePrefixList(
   102  		n.GraphNodeConfigProvider.DependableName(),
   103  		modulePrefixStr(n.PathValue))
   104  }
   105  
   106  func (n *GraphNodeConfigProviderFlat) DependentOn() []string {
   107  	prefixed := modulePrefixList(
   108  		n.GraphNodeConfigProvider.DependentOn(),
   109  		modulePrefixStr(n.PathValue))
   110  
   111  	result := make([]string, len(prefixed), len(prefixed)+1)
   112  	copy(result, prefixed)
   113  
   114  	// If we're in a module, then depend on our parent's provider
   115  	if len(n.PathValue) > 1 {
   116  		prefix := modulePrefixStr(n.PathValue[:len(n.PathValue)-1])
   117  		if prefix != "" {
   118  			prefix += "."
   119  		}
   120  
   121  		result = append(result, fmt.Sprintf(
   122  			"%s%s",
   123  			prefix, n.GraphNodeConfigProvider.Name()))
   124  	}
   125  
   126  	return result
   127  }
   128  
   129  func (n *GraphNodeConfigProviderFlat) ProviderName() string {
   130  	return fmt.Sprintf(
   131  		"%s.%s", modulePrefixStr(n.PathValue),
   132  		n.GraphNodeConfigProvider.ProviderName())
   133  }