terraform: depends_on can reference entire modules

This commit is contained in:
Mitchell Hashimoto 2016-11-11 18:54:37 -08:00
parent 008b8b4c23
commit 0b87ef82c3
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
5 changed files with 177 additions and 0 deletions

View File

@ -110,6 +110,142 @@ test = []`)
}
}
func TestContext2Apply_resourceDependsOnModule(t *testing.T) {
m := testModule(t, "apply-resource-depends-on-module")
p := testProvider("aws")
p.DiffFn = testDiffFn
{
// Wait for the dependency, sleep, and verify the graph never
// called a child.
var called int32
var checked bool
p.ApplyFn = func(
info *InstanceInfo,
is *InstanceState,
id *InstanceDiff) (*InstanceState, error) {
if info.HumanId() == "module.child.aws_instance.child" {
checked = true
// Sleep to allow parallel execution
time.Sleep(50 * time.Millisecond)
// Verify that called is 0 (dep not called)
if atomic.LoadInt32(&called) != 0 {
return nil, fmt.Errorf("aws_instance.a should not be called")
}
}
atomic.AddInt32(&called, 1)
return testApplyFn(info, is, id)
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
if !checked {
t.Fatal("should check")
}
checkStateString(t, state, testTerraformApplyResourceDependsOnModuleStr)
}
}
func TestContext2Apply_resourceDependsOnModuleDestroy(t *testing.T) {
m := testModule(t, "apply-resource-depends-on-module")
p := testProvider("aws")
p.DiffFn = testDiffFn
var globalState *State
{
p.ApplyFn = testApplyFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
globalState = state
}
{
// Wait for the dependency, sleep, and verify the graph never
// called a child.
var called int32
var checked bool
p.ApplyFn = func(
info *InstanceInfo,
is *InstanceState,
id *InstanceDiff) (*InstanceState, error) {
if info.HumanId() == "aws_instance.a" {
checked = true
// Sleep to allow parallel execution
time.Sleep(50 * time.Millisecond)
// Verify that called is 0 (dep not called)
if atomic.LoadInt32(&called) != 0 {
return nil, fmt.Errorf("module child should not be called")
}
}
atomic.AddInt32(&called, 1)
return testApplyFn(info, is, id)
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
State: globalState,
Destroy: true,
})
if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
if !checked {
t.Fatal("should check")
}
checkStateString(t, state, `
<no state>
module.child:
<no state>
`)
}
}
func TestContext2Apply_mapVarBetweenModules(t *testing.T) {
m := testModule(t, "apply-map-var-through-module")
p := testProvider("null")

View File

@ -653,6 +653,18 @@ aws_instance.foo:
num = 2
`
const testTerraformApplyResourceDependsOnModuleStr = `
aws_instance.a:
ID = foo
Dependencies:
module.child
module.child:
aws_instance.child:
ID = foo
`
const testTerraformApplyTaintStr = `
aws_instance.bar:
ID = foo

View File

@ -0,0 +1 @@
resource "aws_instance" "child" {}

View File

@ -0,0 +1,7 @@
module "child" {
source = "./child"
}
resource "aws_instance" "a" {
depends_on = ["module.child"]
}

View File

@ -200,6 +200,15 @@ func NewReferenceMap(vs []dag.Vertex) *ReferenceMap {
n = prefix + n
refMap[n] = append(refMap[n], v)
}
// If there is a path, it is always referenceable by that. For
// example, if this is a referenceable thing at path []string{"foo"},
// then it can be referenced at "module.foo"
if pn, ok := v.(GraphNodeSubPath); ok {
if p := ReferenceModulePath(pn.Path()); p != "" {
refMap[p] = append(refMap[p], v)
}
}
}
// Build the lookup table for referenced by
@ -224,6 +233,18 @@ func NewReferenceMap(vs []dag.Vertex) *ReferenceMap {
return &m
}
// Returns the reference name for a module path. The path "foo" would return
// "module.foo"
func ReferenceModulePath(p []string) string {
p = normalizeModulePath(p)
if len(p) == 1 {
// Root, no name
return ""
}
return fmt.Sprintf("module.%s", strings.Join(p[1:], "."))
}
// ReferencesFromConfig returns the references that a configuration has
// based on the interpolated variables in a configuration.
func ReferencesFromConfig(c *config.RawConfig) []string {