github.com/dgracilieri/terraform@v0.11.12-beta1/terraform/node_provider_abstract.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  // ConcreteProviderNodeFunc is a callback type used to convert an
    12  // abstract provider to a concrete one of some type.
    13  type ConcreteProviderNodeFunc func(*NodeAbstractProvider) dag.Vertex
    14  
    15  // NodeAbstractProvider represents a provider that has no associated operations.
    16  // It registers all the common interfaces across operations for providers.
    17  type NodeAbstractProvider struct {
    18  	NameValue string
    19  	PathValue []string
    20  
    21  	// The fields below will be automatically set using the Attach
    22  	// interfaces if you're running those transforms, but also be explicitly
    23  	// set if you already have that information.
    24  
    25  	Config *config.ProviderConfig
    26  }
    27  
    28  func ResolveProviderName(name string, path []string) string {
    29  	if strings.Contains(name, "provider.") {
    30  		// already resolved
    31  		return name
    32  	}
    33  
    34  	name = fmt.Sprintf("provider.%s", name)
    35  	if len(path) >= 1 {
    36  		name = fmt.Sprintf("%s.%s", modulePrefixStr(path), name)
    37  	}
    38  
    39  	return name
    40  }
    41  
    42  func (n *NodeAbstractProvider) Name() string {
    43  	return ResolveProviderName(n.NameValue, n.PathValue)
    44  }
    45  
    46  // GraphNodeSubPath
    47  func (n *NodeAbstractProvider) Path() []string {
    48  	return n.PathValue
    49  }
    50  
    51  // RemovableIfNotTargeted
    52  func (n *NodeAbstractProvider) RemoveIfNotTargeted() bool {
    53  	// We need to add this so that this node will be removed if
    54  	// it isn't targeted or a dependency of a target.
    55  	return true
    56  }
    57  
    58  // GraphNodeReferencer
    59  func (n *NodeAbstractProvider) References() []string {
    60  	if n.Config == nil {
    61  		return nil
    62  	}
    63  
    64  	return ReferencesFromConfig(n.Config.RawConfig)
    65  }
    66  
    67  // GraphNodeProvider
    68  func (n *NodeAbstractProvider) ProviderName() string {
    69  	return n.NameValue
    70  }
    71  
    72  // GraphNodeProvider
    73  func (n *NodeAbstractProvider) ProviderConfig() *config.ProviderConfig {
    74  	if n.Config == nil {
    75  		return nil
    76  	}
    77  
    78  	return n.Config
    79  }
    80  
    81  // GraphNodeAttachProvider
    82  func (n *NodeAbstractProvider) AttachProvider(c *config.ProviderConfig) {
    83  	n.Config = c
    84  }
    85  
    86  // GraphNodeDotter impl.
    87  func (n *NodeAbstractProvider) DotNode(name string, opts *dag.DotOpts) *dag.DotNode {
    88  	return &dag.DotNode{
    89  		Name: name,
    90  		Attrs: map[string]string{
    91  			"label": n.Name(),
    92  			"shape": "diamond",
    93  		},
    94  	}
    95  }