core: tweaks from code review

This commit is contained in:
Paul Hinze 2015-03-05 10:11:14 -06:00
parent 6e13aacefa
commit f1c9e32fa0
2 changed files with 25 additions and 23 deletions

View File

@ -22,7 +22,8 @@ func (n *EvalReadState) Eval(ctx EvalContext) (interface{}, error) {
type EvalReadStateTainted struct {
Name string
Output **InstanceState
// Index indicates which instance in the Tainted list to target, or -1 for the last item.
// Index indicates which instance in the Tainted list to target, or -1 for
// the last item.
Index int
}
@ -46,7 +47,8 @@ func (n *EvalReadStateTainted) Eval(ctx EvalContext) (interface{}, error) {
type EvalReadStateDeposed struct {
Name string
Output **InstanceState
// Index indicates which instance in the Deposed list to target, or -1 for the last item.
// Index indicates which instance in the Deposed list to target, or -1 for
// the last item.
Index int
}
@ -72,7 +74,7 @@ func readInstanceFromState(
ctx EvalContext,
resourceName string,
output **InstanceState,
reader func(*ResourceState) (*InstanceState, error),
readerFn func(*ResourceState) (*InstanceState, error),
) (*InstanceState, error) {
state, lock := ctx.State()
@ -93,7 +95,7 @@ func readInstanceFromState(
}
// Use the delegate function to get the instance state from the resource state
is, err := reader(rs)
is, err := readerFn(rs)
if err != nil {
return nil, err
}
@ -224,7 +226,7 @@ func writeInstanceToState(
resourceName string,
resourceType string,
dependencies []string,
writer func(*ResourceState) error,
writerFn func(*ResourceState) error,
) (*InstanceState, error) {
state, lock := ctx.State()
if state == nil {
@ -251,7 +253,7 @@ func writeInstanceToState(
rs.Type = resourceType
rs.Dependencies = dependencies
if err := writer(rs); err != nil {
if err := writerFn(rs); err != nil {
return nil, err
}
@ -361,8 +363,9 @@ func (n *EvalUndeposeState) Eval(ctx EvalContext) (interface{}, error) {
}
// Undepose
rs.Primary = rs.Deposed[len(rs.Deposed)-1]
rs.Deposed = rs.Deposed[:len(rs.Deposed)-1]
idx := len(rs.Deposed) - 1
rs.Primary = rs.Deposed[idx]
rs.Deposed[idx] = nil
return nil, nil
}

View File

@ -169,13 +169,10 @@ func TestEvalWriteState(t *testing.T) {
t.Fatalf("Got err: %#v", err)
}
rs := state.ModuleByPath(ctx.Path()).Resources["restype.resname"]
if rs.Type != "restype" {
t.Fatalf("expected type 'restype': %#v", rs)
}
if rs.Primary.ID != "i-abc123" {
t.Fatalf("expected primary instance to have ID 'i-abc123': %#v", rs)
}
checkStateString(t, state, `
restype.resname:
ID = i-abc123
`)
}
func TestEvalWriteStateTainted(t *testing.T) {
@ -197,10 +194,11 @@ func TestEvalWriteStateTainted(t *testing.T) {
t.Fatalf("Got err: %#v", err)
}
rs := state.ModuleByPath(ctx.Path()).Resources["restype.resname"]
if len(rs.Tainted) != 1 || rs.Tainted[0].ID != "i-abc123" {
t.Fatalf("expected tainted instance to have ID 'i-abc123': %#v", rs)
}
checkStateString(t, state, `
restype.resname: (1 tainted)
ID = <not created>
Tainted ID 1 = i-abc123
`)
}
func TestEvalWriteStateDeposed(t *testing.T) {
@ -222,8 +220,9 @@ func TestEvalWriteStateDeposed(t *testing.T) {
t.Fatalf("Got err: %#v", err)
}
rs := state.ModuleByPath(ctx.Path()).Resources["restype.resname"]
if len(rs.Deposed) != 1 || rs.Deposed[0].ID != "i-abc123" {
t.Fatalf("expected deposed instance to have ID 'i-abc123': %#v", rs)
}
checkStateString(t, state, `
restype.resname: (1 deposed)
ID = <not created>
Deposed ID 1 = i-abc123
`)
}