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