From a32833af2c2d655aa9eef5397e81cc9e365ac692 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 22 Sep 2014 15:37:29 -0700 Subject: [PATCH] terraform: Graph, Context, Plan all speak modules --- terraform/context.go | 24 ++-- terraform/context_test.go | 252 ++++++++++++++++++------------------ terraform/graph.go | 25 ++-- terraform/graph_test.go | 68 +++++----- terraform/plan.go | 3 +- terraform/terraform_test.go | 10 ++ 6 files changed, 203 insertions(+), 179 deletions(-) diff --git a/terraform/context.go b/terraform/context.go index 813a4c5ca..4bf324164 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -9,6 +9,7 @@ import ( "sync/atomic" "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/depgraph" "github.com/hashicorp/terraform/helper/multierror" ) @@ -24,6 +25,7 @@ type genericWalkFunc func(*Resource) error // Additionally, a context can be created from a Plan using Plan.Context. type Context struct { config *config.Config + module *module.Tree diff *Diff hooks []Hook state *State @@ -42,9 +44,9 @@ type Context struct { // ContextOpts are the user-creatable configuration structure to create // a context with NewContext. type ContextOpts struct { - Config *config.Config Diff *Diff Hooks []Hook + Module *module.Tree Parallelism int State *State Providers map[string]ResourceProviderFactory @@ -73,10 +75,15 @@ func NewContext(opts *ContextOpts) *Context { } parCh := make(chan struct{}, par) + var config *config.Config + if opts.Module != nil { + config = opts.Module.Config() + } + // Calculate all the default variables defaultVars := make(map[string]string) - if opts.Config != nil { - for _, v := range opts.Config.Variables { + if config != nil { + for _, v := range config.Variables { for k, val := range v.DefaultsMap() { defaultVars[k] = val } @@ -84,9 +91,10 @@ func NewContext(opts *ContextOpts) *Context { } return &Context{ - config: opts.Config, + config: config, diff: opts.Diff, hooks: hooks, + module: opts.Module, state: opts.State, providers: opts.Providers, provisioners: opts.Provisioners, @@ -108,8 +116,8 @@ func (c *Context) Apply() (*State, error) { defer c.releaseRun(v) g, err := Graph(&GraphOpts{ - Config: c.config, Diff: c.diff, + Module: c.module, Providers: c.providers, Provisioners: c.provisioners, State: c.state, @@ -172,7 +180,7 @@ func (c *Context) Plan(opts *PlanOpts) (*Plan, error) { defer c.releaseRun(v) g, err := Graph(&GraphOpts{ - Config: c.config, + Module: c.module, Providers: c.providers, Provisioners: c.provisioners, State: c.state, @@ -237,7 +245,7 @@ func (c *Context) Refresh() (*State, error) { c.state = c.state.deepcopy() g, err := Graph(&GraphOpts{ - Config: c.config, + Module: c.module, Providers: c.providers, Provisioners: c.provisioners, State: c.state, @@ -493,8 +501,8 @@ func (c *Context) computeResourceMultiVariable( func (c *Context) graph() (*depgraph.Graph, error) { return Graph(&GraphOpts{ - Config: c.config, Diff: c.diff, + Module: c.module, Providers: c.providers, Provisioners: c.provisioners, State: c.state, diff --git a/terraform/context_test.go b/terraform/context_test.go index 28f23bb1f..2bc97eefe 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -9,9 +9,9 @@ import ( func TestContextGraph(t *testing.T) { p := testProvider("aws") - config := testConfig(t, "validate-good") + m := testModule(t, "validate-good") c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -31,9 +31,9 @@ func TestContextGraph(t *testing.T) { func TestContextValidate(t *testing.T) { p := testProvider("aws") - config := testConfig(t, "validate-good") + m := testModule(t, "validate-good") c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -50,9 +50,9 @@ func TestContextValidate(t *testing.T) { func TestContextValidate_badVar(t *testing.T) { p := testProvider("aws") - config := testConfig(t, "validate-bad-var") + m := testModule(t, "validate-bad-var") c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -69,7 +69,7 @@ func TestContextValidate_badVar(t *testing.T) { func TestContextValidate_orphans(t *testing.T) { p := testProvider("aws") - config := testConfig(t, "validate-good") + m := testModule(t, "validate-good") state := &State{ Modules: []*ModuleState{ &ModuleState{ @@ -86,7 +86,7 @@ func TestContextValidate_orphans(t *testing.T) { }, } c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -108,10 +108,10 @@ func TestContextValidate_orphans(t *testing.T) { } func TestContextValidate_providerConfig_bad(t *testing.T) { - config := testConfig(t, "validate-bad-pc") + m := testModule(t, "validate-bad-pc") p := testProvider("aws") c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -129,10 +129,10 @@ func TestContextValidate_providerConfig_bad(t *testing.T) { } func TestContextValidate_providerConfig_badEmpty(t *testing.T) { - config := testConfig(t, "validate-bad-pc-empty") + m := testModule(t, "validate-bad-pc-empty") p := testProvider("aws") c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -150,10 +150,10 @@ func TestContextValidate_providerConfig_badEmpty(t *testing.T) { } func TestContextValidate_providerConfig_good(t *testing.T) { - config := testConfig(t, "validate-bad-pc") + m := testModule(t, "validate-bad-pc") p := testProvider("aws") c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -169,10 +169,10 @@ func TestContextValidate_providerConfig_good(t *testing.T) { } func TestContextValidate_resourceConfig_bad(t *testing.T) { - config := testConfig(t, "validate-bad-rc") + m := testModule(t, "validate-bad-rc") p := testProvider("aws") c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -190,10 +190,10 @@ func TestContextValidate_resourceConfig_bad(t *testing.T) { } func TestContextValidate_resourceConfig_good(t *testing.T) { - config := testConfig(t, "validate-bad-rc") + m := testModule(t, "validate-bad-rc") p := testProvider("aws") c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -209,9 +209,9 @@ func TestContextValidate_resourceConfig_good(t *testing.T) { } func TestContextValidate_requiredVar(t *testing.T) { - config := testConfig(t, "validate-required-var") + m := testModule(t, "validate-required-var") c := testContext(t, &ContextOpts{ - Config: config, + Module: m, }) w, e := c.Validate() @@ -224,11 +224,11 @@ func TestContextValidate_requiredVar(t *testing.T) { } func TestContextValidate_provisionerConfig_bad(t *testing.T) { - config := testConfig(t, "validate-bad-prov-conf") + m := testModule(t, "validate-bad-prov-conf") p := testProvider("aws") pr := testProvisioner() c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -249,7 +249,7 @@ func TestContextValidate_provisionerConfig_bad(t *testing.T) { } func TestContextValidate_provisionerConfig_good(t *testing.T) { - config := testConfig(t, "validate-bad-prov-conf") + m := testModule(t, "validate-bad-prov-conf") p := testProvider("aws") pr := testProvisioner() pr.ValidateFn = func(c *ResourceConfig) ([]string, []error) { @@ -259,7 +259,7 @@ func TestContextValidate_provisionerConfig_good(t *testing.T) { return nil, nil } c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -279,9 +279,9 @@ func TestContextValidate_provisionerConfig_good(t *testing.T) { func TestContextValidate_selfRef(t *testing.T) { p := testProvider("aws") - config := testConfig(t, "validate-self-ref") + m := testModule(t, "validate-self-ref") c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -298,9 +298,9 @@ func TestContextValidate_selfRef(t *testing.T) { func TestContextValidate_selfRefMulti(t *testing.T) { p := testProvider("aws") - config := testConfig(t, "validate-self-ref-multi") + m := testModule(t, "validate-self-ref-multi") c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -317,9 +317,9 @@ func TestContextValidate_selfRefMulti(t *testing.T) { func TestContextValidate_selfRefMultiAll(t *testing.T) { p := testProvider("aws") - config := testConfig(t, "validate-self-ref-multi-all") + m := testModule(t, "validate-self-ref-multi-all") c := testContext(t, &ContextOpts{ - Config: config, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -335,12 +335,12 @@ func TestContextValidate_selfRefMultiAll(t *testing.T) { } func TestContextApply(t *testing.T) { - c := testConfig(t, "apply-good") + m := testModule(t, "apply-good") p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -368,12 +368,12 @@ func TestContextApply(t *testing.T) { } func TestContextApply_Minimal(t *testing.T) { - c := testConfig(t, "apply-minimal") + m := testModule(t, "apply-minimal") p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -396,12 +396,12 @@ func TestContextApply_Minimal(t *testing.T) { } func TestContextApply_badDiff(t *testing.T) { - c := testConfig(t, "apply-good") + m := testModule(t, "apply-good") p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -427,10 +427,10 @@ func TestContextApply_badDiff(t *testing.T) { func TestContextApply_cancel(t *testing.T) { stopped := false - c := testConfig(t, "apply-cancel") + m := testModule(t, "apply-cancel") p := testProvider("aws") ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -495,12 +495,12 @@ func TestContextApply_cancel(t *testing.T) { } func TestContextApply_compute(t *testing.T) { - c := testConfig(t, "apply-compute") + m := testModule(t, "apply-compute") p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -525,12 +525,12 @@ func TestContextApply_compute(t *testing.T) { } func TestContextApply_nilDiff(t *testing.T) { - c := testConfig(t, "apply-good") + m := testModule(t, "apply-good") p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -550,7 +550,7 @@ func TestContextApply_nilDiff(t *testing.T) { } func TestContextApply_Provisioner_compute(t *testing.T) { - c := testConfig(t, "apply-provisioner-compute") + m := testModule(t, "apply-provisioner-compute") p := testProvider("aws") pr := testProvisioner() p.ApplyFn = testApplyFn @@ -564,7 +564,7 @@ func TestContextApply_Provisioner_compute(t *testing.T) { return nil } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -598,7 +598,7 @@ func TestContextApply_Provisioner_compute(t *testing.T) { } func TestContextApply_provisionerFail(t *testing.T) { - c := testConfig(t, "apply-provisioner-fail") + m := testModule(t, "apply-provisioner-fail") p := testProvider("aws") pr := testProvisioner() p.ApplyFn = testApplyFn @@ -609,7 +609,7 @@ func TestContextApply_provisionerFail(t *testing.T) { } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -638,7 +638,7 @@ func TestContextApply_provisionerFail(t *testing.T) { } func TestContextApply_provisionerResourceRef(t *testing.T) { - c := testConfig(t, "apply-provisioner-resource-ref") + m := testModule(t, "apply-provisioner-resource-ref") p := testProvider("aws") pr := testProvisioner() p.ApplyFn = testApplyFn @@ -653,7 +653,7 @@ func TestContextApply_provisionerResourceRef(t *testing.T) { } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -684,7 +684,7 @@ func TestContextApply_provisionerResourceRef(t *testing.T) { } func TestContextApply_outputDiffVars(t *testing.T) { - c := testConfig(t, "apply-good") + m := testModule(t, "apply-good") p := testProvider("aws") s := &State{ Modules: []*ModuleState{ @@ -702,7 +702,7 @@ func TestContextApply_outputDiffVars(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -743,7 +743,7 @@ func TestContextApply_outputDiffVars(t *testing.T) { } func TestContextApply_Provisioner_ConnInfo(t *testing.T) { - c := testConfig(t, "apply-provisioner-conninfo") + m := testModule(t, "apply-provisioner-conninfo") p := testProvider("aws") pr := testProvisioner() @@ -784,7 +784,7 @@ func TestContextApply_Provisioner_ConnInfo(t *testing.T) { } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -819,13 +819,13 @@ func TestContextApply_Provisioner_ConnInfo(t *testing.T) { } func TestContextApply_destroy(t *testing.T) { - c := testConfig(t, "apply-destroy") + m := testModule(t, "apply-destroy") h := new(HookRecordApplyOrder) p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Hooks: []Hook{h}, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -869,13 +869,13 @@ func TestContextApply_destroy(t *testing.T) { } func TestContextApply_destroyOutputs(t *testing.T) { - c := testConfig(t, "apply-destroy-outputs") + m := testModule(t, "apply-destroy-outputs") h := new(HookRecordApplyOrder) p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Hooks: []Hook{h}, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -910,7 +910,7 @@ func TestContextApply_destroyOutputs(t *testing.T) { } func TestContextApply_destroyOrphan(t *testing.T) { - c := testConfig(t, "apply-error") + m := testModule(t, "apply-error") p := testProvider("aws") s := &State{ Modules: []*ModuleState{ @@ -928,7 +928,7 @@ func TestContextApply_destroyOrphan(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -972,10 +972,10 @@ func TestContextApply_destroyOrphan(t *testing.T) { func TestContextApply_error(t *testing.T) { errored := false - c := testConfig(t, "apply-error") + m := testModule(t, "apply-error") p := testProvider("aws") ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1026,7 +1026,7 @@ func TestContextApply_error(t *testing.T) { func TestContextApply_errorPartial(t *testing.T) { errored := false - c := testConfig(t, "apply-error") + m := testModule(t, "apply-error") p := testProvider("aws") s := &State{ Modules: []*ModuleState{ @@ -1044,7 +1044,7 @@ func TestContextApply_errorPartial(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1096,13 +1096,13 @@ func TestContextApply_errorPartial(t *testing.T) { } func TestContextApply_hook(t *testing.T) { - c := testConfig(t, "apply-good") + m := testModule(t, "apply-good") h := new(MockHook) p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Hooks: []Hook{h}, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1126,10 +1126,10 @@ func TestContextApply_hook(t *testing.T) { } func TestContextApply_idAttr(t *testing.T) { - c := testConfig(t, "apply-idattr") + m := testModule(t, "apply-idattr") p := testProvider("aws") ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1177,12 +1177,12 @@ func TestContextApply_idAttr(t *testing.T) { } func TestContextApply_output(t *testing.T) { - c := testConfig(t, "apply-output") + m := testModule(t, "apply-output") p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1205,12 +1205,12 @@ func TestContextApply_output(t *testing.T) { } func TestContextApply_outputMulti(t *testing.T) { - c := testConfig(t, "apply-output-multi") + m := testModule(t, "apply-output-multi") p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1233,12 +1233,12 @@ func TestContextApply_outputMulti(t *testing.T) { } func TestContextApply_outputMultiIndex(t *testing.T) { - c := testConfig(t, "apply-output-multi-index") + m := testModule(t, "apply-output-multi-index") p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1261,7 +1261,7 @@ func TestContextApply_outputMultiIndex(t *testing.T) { } func TestContextApply_taint(t *testing.T) { - c := testConfig(t, "apply-taint") + m := testModule(t, "apply-taint") p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn @@ -1287,7 +1287,7 @@ func TestContextApply_taint(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1311,12 +1311,12 @@ func TestContextApply_taint(t *testing.T) { } func TestContextApply_unknownAttribute(t *testing.T) { - c := testConfig(t, "apply-unknown") + m := testModule(t, "apply-unknown") p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1339,12 +1339,12 @@ func TestContextApply_unknownAttribute(t *testing.T) { } func TestContextApply_vars(t *testing.T) { - c := testConfig(t, "apply-vars") + m := testModule(t, "apply-vars") p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1379,11 +1379,11 @@ func TestContextApply_vars(t *testing.T) { } func TestContextPlan(t *testing.T) { - c := testConfig(t, "plan-good") + m := testModule(t, "plan-good") p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1406,11 +1406,11 @@ func TestContextPlan(t *testing.T) { } func TestContextPlan_minimal(t *testing.T) { - c := testConfig(t, "plan-empty") + m := testModule(t, "plan-empty") p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1429,11 +1429,11 @@ func TestContextPlan_minimal(t *testing.T) { } func TestContextPlan_nil(t *testing.T) { - c := testConfig(t, "plan-nil") + m := testModule(t, "plan-nil") p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1464,11 +1464,11 @@ func TestContextPlan_nil(t *testing.T) { } func TestContextPlan_computed(t *testing.T) { - c := testConfig(t, "plan-computed") + m := testModule(t, "plan-computed") p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1487,11 +1487,11 @@ func TestContextPlan_computed(t *testing.T) { } func TestContextPlan_computedList(t *testing.T) { - c := testConfig(t, "plan-computed-list") + m := testModule(t, "plan-computed-list") p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1510,11 +1510,11 @@ func TestContextPlan_computedList(t *testing.T) { } func TestContextPlan_count(t *testing.T) { - c := testConfig(t, "plan-count") + m := testModule(t, "plan-count") p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1537,7 +1537,7 @@ func TestContextPlan_count(t *testing.T) { } func TestContextPlan_countDecreaseToOne(t *testing.T) { - c := testConfig(t, "plan-count-dec") + m := testModule(t, "plan-count-dec") p := testProvider("aws") p.DiffFn = testDiffFn s := &State{ @@ -1572,7 +1572,7 @@ func TestContextPlan_countDecreaseToOne(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1592,7 +1592,7 @@ func TestContextPlan_countDecreaseToOne(t *testing.T) { } func TestContextPlan_countIncreaseFromNotSet(t *testing.T) { - c := testConfig(t, "plan-count-inc") + m := testModule(t, "plan-count-inc") p := testProvider("aws") p.DiffFn = testDiffFn s := &State{ @@ -1615,7 +1615,7 @@ func TestContextPlan_countIncreaseFromNotSet(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1635,7 +1635,7 @@ func TestContextPlan_countIncreaseFromNotSet(t *testing.T) { } func TestContextPlan_countIncreaseFromOne(t *testing.T) { - c := testConfig(t, "plan-count-inc") + m := testModule(t, "plan-count-inc") p := testProvider("aws") p.DiffFn = testDiffFn s := &State{ @@ -1658,7 +1658,7 @@ func TestContextPlan_countIncreaseFromOne(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1678,7 +1678,7 @@ func TestContextPlan_countIncreaseFromOne(t *testing.T) { } func TestContextPlan_destroy(t *testing.T) { - c := testConfig(t, "plan-destroy") + m := testModule(t, "plan-destroy") p := testProvider("aws") p.DiffFn = testDiffFn s := &State{ @@ -1703,7 +1703,7 @@ func TestContextPlan_destroy(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1727,7 +1727,7 @@ func TestContextPlan_destroy(t *testing.T) { } func TestContextPlan_diffVar(t *testing.T) { - c := testConfig(t, "plan-diffvar") + m := testModule(t, "plan-diffvar") p := testProvider("aws") s := &State{ Modules: []*ModuleState{ @@ -1747,7 +1747,7 @@ func TestContextPlan_diffVar(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1785,12 +1785,12 @@ func TestContextPlan_diffVar(t *testing.T) { } func TestContextPlan_hook(t *testing.T) { - c := testConfig(t, "plan-good") + m := testModule(t, "plan-good") h := new(MockHook) p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Hooks: []Hook{h}, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -1811,7 +1811,7 @@ func TestContextPlan_hook(t *testing.T) { } func TestContextPlan_orphan(t *testing.T) { - c := testConfig(t, "plan-orphan") + m := testModule(t, "plan-orphan") p := testProvider("aws") p.DiffFn = testDiffFn s := &State{ @@ -1830,7 +1830,7 @@ func TestContextPlan_orphan(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1850,7 +1850,7 @@ func TestContextPlan_orphan(t *testing.T) { } func TestContextPlan_state(t *testing.T) { - c := testConfig(t, "plan-good") + m := testModule(t, "plan-good") p := testProvider("aws") p.DiffFn = testDiffFn s := &State{ @@ -1868,7 +1868,7 @@ func TestContextPlan_state(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1892,7 +1892,7 @@ func TestContextPlan_state(t *testing.T) { } func TestContextPlan_taint(t *testing.T) { - c := testConfig(t, "plan-taint") + m := testModule(t, "plan-taint") p := testProvider("aws") p.DiffFn = testDiffFn s := &State{ @@ -1920,7 +1920,7 @@ func TestContextPlan_taint(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1940,7 +1940,7 @@ func TestContextPlan_taint(t *testing.T) { } func TestContextPlan_multiple_taint(t *testing.T) { - c := testConfig(t, "plan-taint") + m := testModule(t, "plan-taint") p := testProvider("aws") p.DiffFn = testDiffFn s := &State{ @@ -1971,7 +1971,7 @@ func TestContextPlan_multiple_taint(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -1991,11 +1991,11 @@ func TestContextPlan_multiple_taint(t *testing.T) { } func TestContextPlan_varMultiCountOne(t *testing.T) { - c := testConfig(t, "plan-var-multi-count-one") + m := testModule(t, "plan-var-multi-count-one") p := testProvider("aws") p.DiffFn = testDiffFn ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -2015,9 +2015,9 @@ func TestContextPlan_varMultiCountOne(t *testing.T) { func TestContextRefresh(t *testing.T) { p := testProvider("aws") - c := testConfig(t, "refresh-basic") + m := testModule(t, "refresh-basic") ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -2067,9 +2067,9 @@ func TestContextRefresh(t *testing.T) { func TestContextRefresh_delete(t *testing.T) { p := testProvider("aws") - c := testConfig(t, "refresh-basic") + m := testModule(t, "refresh-basic") ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -2106,9 +2106,9 @@ func TestContextRefresh_delete(t *testing.T) { func TestContextRefresh_ignoreUncreated(t *testing.T) { p := testProvider("aws") - c := testConfig(t, "refresh-basic") + m := testModule(t, "refresh-basic") ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -2132,9 +2132,9 @@ func TestContextRefresh_ignoreUncreated(t *testing.T) { func TestContextRefresh_hook(t *testing.T) { h := new(MockHook) p := testProvider("aws") - c := testConfig(t, "refresh-basic") + m := testModule(t, "refresh-basic") ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Hooks: []Hook{h}, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -2181,7 +2181,7 @@ func TestContextRefresh_hook(t *testing.T) { func TestContextRefresh_state(t *testing.T) { p := testProvider("aws") - c := testConfig(t, "refresh-basic") + m := testModule(t, "refresh-basic") state := &State{ Modules: []*ModuleState{ &ModuleState{ @@ -2197,7 +2197,7 @@ func TestContextRefresh_state(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -2228,7 +2228,7 @@ func TestContextRefresh_state(t *testing.T) { func TestContextRefresh_tainted(t *testing.T) { p := testProvider("aws") - c := testConfig(t, "refresh-basic") + m := testModule(t, "refresh-basic") state := &State{ Modules: []*ModuleState{ &ModuleState{ @@ -2247,7 +2247,7 @@ func TestContextRefresh_tainted(t *testing.T) { }, } ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, @@ -2276,9 +2276,9 @@ func TestContextRefresh_tainted(t *testing.T) { func TestContextRefresh_vars(t *testing.T) { p := testProvider("aws") - c := testConfig(t, "refresh-vars") + m := testModule(t, "refresh-vars") ctx := testContext(t, &ContextOpts{ - Config: c, + Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), }, diff --git a/terraform/graph.go b/terraform/graph.go index d6724bcbe..ef4e1c505 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/depgraph" "github.com/hashicorp/terraform/helper/multierror" ) @@ -20,7 +21,11 @@ import ( type GraphOpts struct { // Config is the configuration from which to build the basic graph. // This is the only required item. - Config *config.Config + //Config *config.Config + + // Module is the relative root of a module tree for this graph. This + // is the only required item. + Module *module.Tree // Diff of changes that will be applied to the given state. This will // associate a ResourceDiff with applicable resources. Additionally, @@ -101,10 +106,12 @@ type GraphNodeResourceProvider struct { // configured at this point. // func Graph(opts *GraphOpts) (*depgraph.Graph, error) { - if opts.Config == nil { - return nil, errors.New("Config is required for Graph") + if opts.Module == nil { + return nil, errors.New("Module is required for Graph") } + config := opts.Module.Config() + log.Printf("[DEBUG] Creating graph...") g := new(depgraph.Graph) @@ -112,24 +119,24 @@ func Graph(opts *GraphOpts) (*depgraph.Graph, error) { // First, build the initial resource graph. This only has the resources // and no dependencies. This only adds resources that are in the config // and not "orphans" (that are in the state, but not in the config). - graphAddConfigResources(g, opts.Config, opts.State) + graphAddConfigResources(g, config, opts.State) // Add the modules that are in the configuration. - graphAddConfigModules(g, opts) + graphAddConfigModules(g, config, opts) // Add explicit dependsOn dependencies to the graph graphAddExplicitDeps(g) if opts.State != nil { // Next, add the state orphans if we have any - graphAddOrphans(g, opts.Config, opts.State) + graphAddOrphans(g, config, opts.State) // Add tainted resources if we have any. graphAddTainted(g, opts.State) } // Map the provider configurations to all of the resources - graphAddProviderConfigs(g, opts.Config) + graphAddProviderConfigs(g, config) // Setup the provisioners. These may have variable dependencies, // and must be done before dependency setup @@ -231,9 +238,7 @@ func graphInitState(s *State, g *depgraph.Graph) { // graphAddConfigModules adds the modules from a configuration structure // into the graph, expanding each to their own sub-graph. -func graphAddConfigModules(g *depgraph.Graph, opts *GraphOpts) { - c := opts.Config - +func graphAddConfigModules(g *depgraph.Graph, c *config.Config, opts *GraphOpts) { // Just short-circuit the whole thing if we don't have modules if len(c.Modules) == 0 { return diff --git a/terraform/graph_test.go b/terraform/graph_test.go index e138efb76..d2b8c2bd1 100644 --- a/terraform/graph_test.go +++ b/terraform/graph_test.go @@ -7,9 +7,9 @@ import ( ) func TestGraph(t *testing.T) { - config := testConfig(t, "graph-basic") + m := testModule(t, "graph-basic") - g, err := Graph(&GraphOpts{Config: config}) + g, err := Graph(&GraphOpts{Module: m}) if err != nil { t.Fatalf("err: %s", err) } @@ -28,9 +28,9 @@ func TestGraph_configRequired(t *testing.T) { } func TestGraph_count(t *testing.T) { - config := testConfig(t, "graph-count") + m := testModule(t, "graph-count") - g, err := Graph(&GraphOpts{Config: config}) + g, err := Graph(&GraphOpts{Module: m}) if err != nil { t.Fatalf("err: %s", err) } @@ -43,18 +43,18 @@ func TestGraph_count(t *testing.T) { } func TestGraph_cycle(t *testing.T) { - config := testConfig(t, "graph-cycle") + m := testModule(t, "graph-cycle") - _, err := Graph(&GraphOpts{Config: config}) + _, err := Graph(&GraphOpts{Module: m}) if err == nil { t.Fatal("should error") } } func TestGraph_dependsOn(t *testing.T) { - config := testConfig(t, "graph-depends-on") + m := testModule(t, "graph-depends-on") - g, err := Graph(&GraphOpts{Config: config}) + g, err := Graph(&GraphOpts{Module: m}) if err != nil { t.Fatalf("err: %s", err) } @@ -67,9 +67,9 @@ func TestGraph_dependsOn(t *testing.T) { } func TestGraph_dependsOnCount(t *testing.T) { - config := testConfig(t, "graph-depends-on-count") + m := testModule(t, "graph-depends-on-count") - g, err := Graph(&GraphOpts{Config: config}) + g, err := Graph(&GraphOpts{Module: m}) if err != nil { t.Fatalf("err: %s", err) } @@ -82,9 +82,9 @@ func TestGraph_dependsOnCount(t *testing.T) { } func TestGraph_modules(t *testing.T) { - config := testConfig(t, "graph-modules") + m := testModule(t, "graph-modules") - g, err := Graph(&GraphOpts{Config: config}) + g, err := Graph(&GraphOpts{Module: m}) if err != nil { t.Fatalf("err: %s", err) } @@ -97,7 +97,7 @@ func TestGraph_modules(t *testing.T) { } func TestGraph_state(t *testing.T) { - config := testConfig(t, "graph-basic") + m := testModule(t, "graph-basic") state := &State{ Modules: []*ModuleState{ &ModuleState{ @@ -115,7 +115,7 @@ func TestGraph_state(t *testing.T) { }, } - g, err := Graph(&GraphOpts{Config: config, State: state}) + g, err := Graph(&GraphOpts{Module: m, State: state}) if err != nil { t.Fatalf("err: %s", err) } @@ -128,7 +128,7 @@ func TestGraph_state(t *testing.T) { } func TestGraph_tainted(t *testing.T) { - config := testConfig(t, "graph-tainted") + m := testModule(t, "graph-tainted") state := &State{ Modules: []*ModuleState{ &ModuleState{ @@ -151,7 +151,7 @@ func TestGraph_tainted(t *testing.T) { }, } - g, err := Graph(&GraphOpts{Config: config, State: state}) + g, err := Graph(&GraphOpts{Module: m, State: state}) if err != nil { t.Fatalf("err: %s", err) } @@ -164,7 +164,7 @@ func TestGraph_tainted(t *testing.T) { } func TestGraph_taintedMulti(t *testing.T) { - config := testConfig(t, "graph-tainted") + m := testModule(t, "graph-tainted") state := &State{ Modules: []*ModuleState{ &ModuleState{ @@ -190,7 +190,7 @@ func TestGraph_taintedMulti(t *testing.T) { }, } - g, err := Graph(&GraphOpts{Config: config, State: state}) + g, err := Graph(&GraphOpts{Module: m, State: state}) if err != nil { t.Fatalf("err: %s", err) } @@ -220,8 +220,8 @@ func TestGraphFull(t *testing.T) { "open": testProviderFuncFixed(rpOS), } - c := testConfig(t, "graph-basic") - g, err := Graph(&GraphOpts{Config: c, Providers: ps}) + m := testModule(t, "graph-basic") + g, err := Graph(&GraphOpts{Module: m, Providers: ps}) if err != nil { t.Fatalf("err: %s", err) } @@ -276,8 +276,8 @@ func TestGraphProvisioners(t *testing.T) { "aws": testProviderFuncFixed(rpAws), } - c := testConfig(t, "graph-provisioners") - g, err := Graph(&GraphOpts{Config: c, Providers: pf, Provisioners: ps}) + m := testModule(t, "graph-provisioners") + g, err := Graph(&GraphOpts{Module: m, Providers: pf, Provisioners: ps}) if err != nil { t.Fatalf("err: %s", err) } @@ -339,7 +339,7 @@ func TestGraphProvisioners(t *testing.T) { } func TestGraphAddDiff(t *testing.T) { - config := testConfig(t, "graph-diff") + m := testModule(t, "graph-diff") diff := &Diff{ Resources: map[string]*InstanceDiff{ "aws_instance.foo": &InstanceDiff{ @@ -352,7 +352,7 @@ func TestGraphAddDiff(t *testing.T) { }, } - g, err := Graph(&GraphOpts{Config: config, Diff: diff}) + g, err := Graph(&GraphOpts{Module: m, Diff: diff}) if err != nil { t.Fatalf("err: %s", err) } @@ -375,7 +375,7 @@ func TestGraphAddDiff(t *testing.T) { } func TestGraphAddDiff_destroy(t *testing.T) { - config := testConfig(t, "graph-diff-destroy") + m := testModule(t, "graph-diff-destroy") diff := &Diff{ Resources: map[string]*InstanceDiff{ "aws_instance.foo": &InstanceDiff{ @@ -413,7 +413,7 @@ func TestGraphAddDiff_destroy(t *testing.T) { diffHash := checksumStruct(t, diff) g, err := Graph(&GraphOpts{ - Config: config, + Module: m, Diff: diff, State: state, }) @@ -445,7 +445,7 @@ func TestGraphAddDiff_destroy(t *testing.T) { } func TestGraphAddDiff_destroy_counts(t *testing.T) { - config := testConfig(t, "graph-count") + m := testModule(t, "graph-count") diff := &Diff{ Resources: map[string]*InstanceDiff{ "aws_instance.web.0": &InstanceDiff{ @@ -500,7 +500,7 @@ func TestGraphAddDiff_destroy_counts(t *testing.T) { diffHash := checksumStruct(t, diff) g, err := Graph(&GraphOpts{ - Config: config, + Module: m, Diff: diff, State: state, }) @@ -532,7 +532,7 @@ func TestGraphAddDiff_destroy_counts(t *testing.T) { } func TestGraphInitState(t *testing.T) { - config := testConfig(t, "graph-basic") + m := testModule(t, "graph-basic") state := &State{ Modules: []*ModuleState{ &ModuleState{ @@ -555,7 +555,7 @@ func TestGraphInitState(t *testing.T) { }, } - g, err := Graph(&GraphOpts{Config: config, State: state}) + g, err := Graph(&GraphOpts{Module: m, State: state}) if err != nil { t.Fatalf("err: %s", err) } @@ -576,7 +576,7 @@ func TestGraphInitState(t *testing.T) { } func TestGraphInitState_Count(t *testing.T) { - config := testConfig(t, "graph-count") + m := testModule(t, "graph-count") state := &State{ Modules: []*ModuleState{ &ModuleState{ @@ -599,7 +599,7 @@ func TestGraphInitState_Count(t *testing.T) { }, } - g, err := Graph(&GraphOpts{Config: config, State: state}) + g, err := Graph(&GraphOpts{Module: m, State: state}) if err != nil { t.Fatalf("err: %s", err) } @@ -620,7 +620,7 @@ func TestGraphInitState_Count(t *testing.T) { } func TestGraph_orphan_dependencies(t *testing.T) { - config := testConfig(t, "graph-count") + m := testModule(t, "graph-count") state := &State{ Modules: []*ModuleState{ &ModuleState{ @@ -655,7 +655,7 @@ func TestGraph_orphan_dependencies(t *testing.T) { }, } - g, err := Graph(&GraphOpts{Config: config, State: state}) + g, err := Graph(&GraphOpts{Module: m, State: state}) if err != nil { t.Fatalf("err: %s", err) } diff --git a/terraform/plan.go b/terraform/plan.go index ed845cc36..2a501637b 100644 --- a/terraform/plan.go +++ b/terraform/plan.go @@ -9,6 +9,7 @@ import ( "sync" "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/config/module" ) func init() { @@ -43,8 +44,8 @@ type Plan struct { // The following fields in opts are overridden by the plan: Config, // Diff, State, Variables. func (p *Plan) Context(opts *ContextOpts) *Context { - opts.Config = p.Config opts.Diff = p.Diff + opts.Module = module.NewTree("", p.Config) // TODO: compat opts.State = p.State opts.Variables = p.Vars return NewContext(opts) diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index c8df4c2a1..39a0f5aca 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/config/module" ) // This is the directory where our test fixtures are. @@ -39,6 +40,15 @@ func testConfig(t *testing.T, name string) *config.Config { return c } +func testModule(t *testing.T, name string) *module.Tree { + mod, err := module.NewTreeModule("", filepath.Join(fixtureDir, name)) + if err != nil { + t.Fatalf("err: %s", err) + } + + return mod +} + func testProviderFuncFixed(rp ResourceProvider) ResourceProviderFactory { return func() (ResourceProvider, error) { return rp, nil