Skip to content

Commit 459ad04

Browse files
committed
terraform: provider cache should append the provider name to it
1 parent ad595cf commit 459ad04

8 files changed

Lines changed: 109 additions & 17 deletions

File tree

‎terraform/context.go‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package terraform
22

33
import (
44
"fmt"
5+
"log"
56
"sort"
67
"sync"
78

@@ -388,19 +389,7 @@ func (c *Context) walk(operation walkOperation) (*ContextGraphWalker, error) {
388389
}
389390

390391
// Walk the graph
392+
log.Printf("[INFO] Starting graph walk: %s", operation.String())
391393
walker := &ContextGraphWalker{Context: c, Operation: operation}
392394
return walker, graph.Walk(walker)
393395
}
394-
395-
// walkOperation is an enum which tells the walkContext what to do.
396-
type walkOperation byte
397-
398-
const (
399-
walkInvalid walkOperation = iota
400-
walkInput
401-
walkApply
402-
walkPlan
403-
walkPlanDestroy
404-
walkRefresh
405-
walkValidate
406-
)

‎terraform/context_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3175,6 +3175,45 @@ func TestContext2Apply_module(t *testing.T) {
31753175
}
31763176
}
31773177

3178+
func TestContext2Apply_multiProvider(t *testing.T) {
3179+
m := testModule(t, "apply-multi-provider")
3180+
p := testProvider("aws")
3181+
p.ApplyFn = testApplyFn
3182+
p.DiffFn = testDiffFn
3183+
3184+
pDO := testProvider("do")
3185+
pDO.ApplyFn = testApplyFn
3186+
pDO.DiffFn = testDiffFn
3187+
3188+
ctx := testContext2(t, &ContextOpts{
3189+
Module: m,
3190+
Providers: map[string]ResourceProviderFactory{
3191+
"aws": testProviderFuncFixed(p),
3192+
"do": testProviderFuncFixed(pDO),
3193+
},
3194+
})
3195+
3196+
if _, err := ctx.Plan(nil); err != nil {
3197+
t.Fatalf("err: %s", err)
3198+
}
3199+
3200+
state, err := ctx.Apply()
3201+
if err != nil {
3202+
t.Fatalf("err: %s", err)
3203+
}
3204+
3205+
mod := state.RootModule()
3206+
if len(mod.Resources) < 2 {
3207+
t.Fatalf("bad: %#v", mod.Resources)
3208+
}
3209+
3210+
actual := strings.TrimSpace(state.String())
3211+
expected := strings.TrimSpace(testTerraformApplyMultiProviderStr)
3212+
if actual != expected {
3213+
t.Fatalf("bad: \n%s", actual)
3214+
}
3215+
}
3216+
31783217
func TestContext2Apply_nilDiff(t *testing.T) {
31793218
m := testModule(t, "apply-good")
31803219
p := testProvider("aws")

‎terraform/eval.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package terraform
22

33
import (
44
"log"
5+
"strings"
56
)
67

78
// EvalNode is the interface that must be implemented by graph nodes to
@@ -43,10 +44,15 @@ func Eval(n EvalNode, ctx EvalContext) (interface{}, error) {
4344
// EvalRaw is like Eval except that it returns all errors, even if they
4445
// signal something normal such as EvalEarlyExitError.
4546
func EvalRaw(n EvalNode, ctx EvalContext) (interface{}, error) {
46-
log.Printf("[DEBUG] eval: %T", n)
47+
path := "unknown"
48+
if ctx != nil {
49+
path = strings.Join(ctx.Path(), ".")
50+
}
51+
52+
log.Printf("[DEBUG] %s: eval: %T", path, n)
4753
output, err := n.Eval(ctx)
4854
if err != nil {
49-
log.Printf("[ERROR] eval: %T, err: %s", n, err)
55+
log.Printf("[ERROR] %s: eval: %T, err: %s", path, n, err)
5056
}
5157

5258
return output, err

‎terraform/eval_context_builtin.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ func (ctx *BuiltinEvalContext) InitProvider(n string) (ResourceProvider, error)
7878
return nil, err
7979
}
8080

81-
ctx.ProviderCache[PathCacheKey(ctx.Path())] = p
81+
providerPath := make([]string, len(ctx.Path())+1)
82+
copy(providerPath, ctx.Path())
83+
providerPath[len(providerPath)-1] = n
84+
85+
ctx.ProviderCache[PathCacheKey(providerPath)] = p
8286
return p, nil
8387
}
8488

@@ -88,7 +92,11 @@ func (ctx *BuiltinEvalContext) Provider(n string) ResourceProvider {
8892
ctx.ProviderLock.Lock()
8993
defer ctx.ProviderLock.Unlock()
9094

91-
return ctx.ProviderCache[PathCacheKey(ctx.Path())]
95+
providerPath := make([]string, len(ctx.Path())+1)
96+
copy(providerPath, ctx.Path())
97+
providerPath[len(providerPath)-1] = n
98+
99+
return ctx.ProviderCache[PathCacheKey(providerPath)]
92100
}
93101

94102
func (ctx *BuiltinEvalContext) ConfigureProvider(

‎terraform/graph_walk_operation.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package terraform
2+
3+
//go:generate stringer -type=walkOperation graph_walk_operation.go
4+
5+
// walkOperation is an enum which tells the walkContext what to do.
6+
type walkOperation byte
7+
8+
const (
9+
walkInvalid walkOperation = iota
10+
walkInput
11+
walkApply
12+
walkPlan
13+
walkPlanDestroy
14+
walkRefresh
15+
walkValidate
16+
)

‎terraform/terraform_test.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ module.child:
278278
type = aws_instance
279279
`
280280

281+
const testTerraformApplyMultiProviderStr = `
282+
aws_instance.bar:
283+
ID = foo
284+
foo = bar
285+
type = aws_instance
286+
do_instance.foo:
287+
ID = foo
288+
num = 2
289+
type = do_instance
290+
`
291+
281292
const testTerraformApplyProvisionerStr = `
282293
aws_instance.bar:
283294
ID = foo
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "do_instance" "foo" {
2+
num = "2"
3+
}
4+
5+
resource "aws_instance" "bar" {
6+
foo = "bar"
7+
}

‎terraform/walkoperation_string.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// generated by stringer -type=walkOperation graph_walk_operation.go; DO NOT EDIT
2+
3+
package terraform
4+
5+
import "fmt"
6+
7+
const _walkOperation_name = "walkInvalidwalkInputwalkApplywalkPlanwalkPlanDestroywalkRefreshwalkValidate"
8+
9+
var _walkOperation_index = [...]uint8{0, 11, 20, 29, 37, 52, 63, 75}
10+
11+
func (i walkOperation) String() string {
12+
if i+1 >= walkOperation(len(_walkOperation_index)) {
13+
return fmt.Sprintf("walkOperation(%d)", i)
14+
}
15+
return _walkOperation_name[_walkOperation_index[i]:_walkOperation_index[i+1]]
16+
}

0 commit comments

Comments
 (0)