github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/terraform/node_resource_abstract.go (about)

     1  package terraform
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/config"
     5  	"github.com/hashicorp/terraform/dag"
     6  )
     7  
     8  // ConcreteResourceNodeFunc is a callback type used to convert an
     9  // abstract resource to a concrete one of some type.
    10  type ConcreteResourceNodeFunc func(*NodeAbstractResource) dag.Vertex
    11  
    12  // GraphNodeResource is implemented by any nodes that represent a resource.
    13  // The type of operation cannot be assumed, only that this node represents
    14  // the given resource.
    15  type GraphNodeResource interface {
    16  	ResourceAddr() *ResourceAddress
    17  }
    18  
    19  // NodeAbstractResource represents a resource that has no associated
    20  // operations. It registers all the interfaces for a resource that common
    21  // across multiple operation types.
    22  type NodeAbstractResource struct {
    23  	Addr *ResourceAddress // Addr is the address for this resource
    24  
    25  	// The fields below will be automatically set using the Attach
    26  	// interfaces if you're running those transforms, but also be explicitly
    27  	// set if you already have that information.
    28  
    29  	Config        *config.Resource // Config is the resource in the config
    30  	ResourceState *ResourceState   // ResourceState is the ResourceState for this
    31  
    32  	Targets []ResourceAddress // Set from GraphNodeTargetable
    33  }
    34  
    35  func (n *NodeAbstractResource) Name() string {
    36  	return n.Addr.String()
    37  }
    38  
    39  // GraphNodeSubPath
    40  func (n *NodeAbstractResource) Path() []string {
    41  	return n.Addr.Path
    42  }
    43  
    44  // GraphNodeReferenceable
    45  func (n *NodeAbstractResource) ReferenceableName() []string {
    46  	if n.Config == nil {
    47  		return nil
    48  	}
    49  
    50  	return []string{n.Config.Id()}
    51  }
    52  
    53  // GraphNodeReferencer
    54  func (n *NodeAbstractResource) References() []string {
    55  	// If we have a config, that is our source of truth
    56  	if c := n.Config; c != nil {
    57  		// Grab all the references
    58  		var result []string
    59  		result = append(result, c.DependsOn...)
    60  		result = append(result, ReferencesFromConfig(c.RawCount)...)
    61  		result = append(result, ReferencesFromConfig(c.RawConfig)...)
    62  		for _, p := range c.Provisioners {
    63  			result = append(result, ReferencesFromConfig(p.ConnInfo)...)
    64  			result = append(result, ReferencesFromConfig(p.RawConfig)...)
    65  		}
    66  
    67  		return result
    68  	}
    69  
    70  	// If we have state, that is our next source
    71  	if s := n.ResourceState; s != nil {
    72  		return s.Dependencies
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  // GraphNodeProviderConsumer
    79  func (n *NodeAbstractResource) ProvidedBy() []string {
    80  	// If we have a config we prefer that above all else
    81  	if n.Config != nil {
    82  		return []string{resourceProvider(n.Config.Type, n.Config.Provider)}
    83  	}
    84  
    85  	// If we have state, then we will use the provider from there
    86  	if n.ResourceState != nil && n.ResourceState.Provider != "" {
    87  		return []string{n.ResourceState.Provider}
    88  	}
    89  
    90  	// Use our type
    91  	return []string{resourceProvider(n.Addr.Type, "")}
    92  }
    93  
    94  // GraphNodeProvisionerConsumer
    95  func (n *NodeAbstractResource) ProvisionedBy() []string {
    96  	// If we have no configuration, then we have no provisioners
    97  	if n.Config == nil {
    98  		return nil
    99  	}
   100  
   101  	// Build the list of provisioners we need based on the configuration.
   102  	// It is okay to have duplicates here.
   103  	result := make([]string, len(n.Config.Provisioners))
   104  	for i, p := range n.Config.Provisioners {
   105  		result[i] = p.Type
   106  	}
   107  
   108  	return result
   109  }
   110  
   111  // GraphNodeResource, GraphNodeAttachResourceState
   112  func (n *NodeAbstractResource) ResourceAddr() *ResourceAddress {
   113  	return n.Addr
   114  }
   115  
   116  // GraphNodeAddressable, TODO: remove, used by target, should unify
   117  func (n *NodeAbstractResource) ResourceAddress() *ResourceAddress {
   118  	return n.ResourceAddr()
   119  }
   120  
   121  // GraphNodeTargetable
   122  func (n *NodeAbstractResource) SetTargets(targets []ResourceAddress) {
   123  	n.Targets = targets
   124  }
   125  
   126  // GraphNodeAttachResourceState
   127  func (n *NodeAbstractResource) AttachResourceState(s *ResourceState) {
   128  	n.ResourceState = s
   129  }
   130  
   131  // GraphNodeAttachResourceConfig
   132  func (n *NodeAbstractResource) AttachResourceConfig(c *config.Resource) {
   133  	n.Config = c
   134  }