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