aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/terraform/terraform/transform_resource_refresh_plannable.go
blob: 35358a3180ebd8af8dcb2b0ae4d18dc530ed5866 (plain) (blame)
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
package terraform

import (
	"fmt"
	"log"
)

// ResourceRefreshPlannableTransformer is a GraphTransformer that replaces any
// nodes that don't have state yet exist in config with
// NodePlannableResourceInstance.
//
// This transformer is used when expanding count on managed resource nodes
// during the refresh phase to ensure that data sources that have
// interpolations that depend on resources existing in the graph can be walked
// properly.
type ResourceRefreshPlannableTransformer struct {
	// The full global state.
	State *State
}

// Transform implements GraphTransformer for
// ResourceRefreshPlannableTransformer.
func (t *ResourceRefreshPlannableTransformer) Transform(g *Graph) error {
nextVertex:
	for _, v := range g.Vertices() {
		addr := v.(*NodeRefreshableManagedResourceInstance).Addr

		// Find the state for this address, if there is one
		filter := &StateFilter{State: t.State}
		results, err := filter.Filter(addr.String())
		if err != nil {
			return err
		}

		// Check to see if we have a state for this resource. If we do, skip this
		// node.
		for _, result := range results {
			if _, ok := result.Value.(*ResourceState); ok {
				continue nextVertex
			}
		}
		// If we don't, convert this resource to a NodePlannableResourceInstance node
		// with all of the data we need to make it happen.
		log.Printf("[TRACE] No state for %s, converting to NodePlannableResourceInstance", addr.String())
		new := &NodePlannableResourceInstance{
			NodeAbstractResource: v.(*NodeRefreshableManagedResourceInstance).NodeAbstractResource,
		}
		// Replace the node in the graph
		if !g.Replace(v, new) {
			return fmt.Errorf("ResourceRefreshPlannableTransformer: Could not replace node %#v with %#v", v, new)
		}
	}

	return nil
}