terraform: use the proper diff for apply time

This commit is contained in:
Mitchell Hashimoto 2015-02-12 19:57:27 -08:00
parent aea6b0a7e1
commit abc68a89a8
2 changed files with 9 additions and 177 deletions

View File

@ -2916,13 +2916,14 @@ func TestContextApply_cancel(t *testing.T) {
t.Fatalf("bad: \n%s", actual)
}
}
*/
func TestContextApply_compute(t *testing.T) {
func TestContext2Apply_compute(t *testing.T) {
m := testModule(t, "apply-compute")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext(t, &ContextOpts{
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
@ -2947,6 +2948,7 @@ func TestContextApply_compute(t *testing.T) {
}
}
/*
func TestContextApply_countDecrease(t *testing.T) {
m := testModule(t, "apply-count-dec")
p := testProvider("aws")
@ -4630,177 +4632,6 @@ func TestContextApply_singleDestroy(t *testing.T) {
}
}
func testContext(t *testing.T, opts *ContextOpts) *Context {
return NewContext(opts)
}
func testApplyFn(
info *InstanceInfo,
s *InstanceState,
d *InstanceDiff) (*InstanceState, error) {
if d.Destroy {
return nil, nil
}
id := "foo"
if idAttr, ok := d.Attributes["id"]; ok && !idAttr.NewComputed {
id = idAttr.New
}
result := &InstanceState{
ID: id,
}
if d != nil {
result = result.MergeDiff(d)
}
return result, nil
}
func testDiffFn(
info *InstanceInfo,
s *InstanceState,
c *ResourceConfig) (*InstanceDiff, error) {
var diff InstanceDiff
diff.Attributes = make(map[string]*ResourceAttrDiff)
for k, v := range c.Raw {
if _, ok := v.(string); !ok {
continue
}
if k == "nil" {
return nil, nil
}
// This key is used for other purposes
if k == "compute_value" {
continue
}
if k == "compute" {
attrDiff := &ResourceAttrDiff{
Old: "",
New: "",
NewComputed: true,
}
if cv, ok := c.Config["compute_value"]; ok {
if cv.(string) == "1" {
attrDiff.NewComputed = false
attrDiff.New = fmt.Sprintf("computed_%s", v.(string))
}
}
diff.Attributes[v.(string)] = attrDiff
continue
}
// If this key is not computed, then look it up in the
// cleaned config.
found := false
for _, ck := range c.ComputedKeys {
if ck == k {
found = true
break
}
}
if !found {
v = c.Config[k]
}
attrDiff := &ResourceAttrDiff{
Old: "",
New: v.(string),
}
if k == "require_new" {
attrDiff.RequiresNew = true
}
diff.Attributes[k] = attrDiff
}
for _, k := range c.ComputedKeys {
diff.Attributes[k] = &ResourceAttrDiff{
Old: "",
NewComputed: true,
}
}
for k, v := range diff.Attributes {
if v.NewComputed {
continue
}
old, ok := s.Attributes[k]
if !ok {
continue
}
if old == v.New {
delete(diff.Attributes, k)
}
}
if !diff.Empty() {
diff.Attributes["type"] = &ResourceAttrDiff{
Old: "",
New: info.Type,
}
}
return &diff, nil
}
func testProvider(prefix string) *MockResourceProvider {
p := new(MockResourceProvider)
p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) {
return s, nil
}
p.ResourcesReturn = []ResourceType{
ResourceType{
Name: fmt.Sprintf("%s_instance", prefix),
},
}
return p
}
func testProvisioner() *MockResourceProvisioner {
p := new(MockResourceProvisioner)
return p
}
const testContextGraph = `
root: root
aws_instance.bar
aws_instance.bar -> provider.aws
aws_instance.foo
aws_instance.foo -> provider.aws
provider.aws
root
root -> aws_instance.bar
root -> aws_instance.foo
`
const testContextRefreshModuleStr = `
aws_instance.web: (1 tainted)
ID = <not created>
Tainted ID 1 = bar
module.child:
aws_instance.web:
ID = new
`
const testContextRefreshOutputPartialStr = `
<no state>
`
const testContextRefreshTaintedStr = `
aws_instance.web: (1 tainted)
ID = <not created>
Tainted ID 1 = foo
`
*/
func testContext2(t *testing.T, opts *ContextOpts) *Context2 {

View File

@ -98,7 +98,7 @@ func (n *graphNodeExpandedResource) ProvidedBy() []string {
// GraphNodeEvalable impl.
func (n *graphNodeExpandedResource) EvalTree() EvalNode {
var diff, diff2 *InstanceDiff
var diff *InstanceDiff
var state *InstanceState
// Build the resource. If we aren't part of a multi-resource, then
@ -221,6 +221,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
// Diff the resource for destruction
var provider ResourceProvider
var diffApply *InstanceDiff
seq.Nodes = append(seq.Nodes, &EvalOpFilter{
Ops: []walkOperation{walkApply},
Node: &EvalSequence{
@ -231,7 +232,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
Config: interpolateNode,
Provider: &EvalGetProvider{Name: n.ProvidedBy()[0]},
State: &EvalReadState{Name: n.stateId()},
Output: &diff2,
Output: &diffApply,
},
// Get the saved diff
@ -244,7 +245,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
&EvalCompareDiff{
Info: info,
One: &diff,
Two: &diff2,
Two: &diffApply,
},
&EvalGetProvider{
@ -258,7 +259,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
&EvalApply{
Info: info,
State: &state,
Diff: &diff,
Diff: &diffApply,
Provider: &provider,
Output: &state,
},