Skip to content

Commit

Permalink
remove nop task from thunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Rebecca Skinner committed Dec 20, 2016
1 parent 32870a2 commit 2eb4f12
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 49 deletions.
51 changes: 12 additions & 39 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ package render
import (
"crypto/rand"
"fmt"
"reflect"

"github.com/asteris-llc/converge/executor"
"github.com/asteris-llc/converge/graph"
"github.com/asteris-llc/converge/graph/node"
"github.com/asteris-llc/converge/graph/node/conditional"
"github.com/asteris-llc/converge/helpers/fakerenderer"
"github.com/asteris-llc/converge/resource"
"github.com/asteris-llc/converge/resource/module"
multierror "github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -106,14 +104,7 @@ func (p pipelineGen) prepareNode(ctx context.Context, idi interface{}) (interfac

if merged != nil {
if errIsUnresolvable(merged) {
// Get a resource with a fake renderer so that we can have a stub value to
// track the expected return type of the thunk
fakePrep, fakePrepErr := getTypedResourcePointer(ctx, res)
if fakePrepErr != nil {
return nil, fakePrepErr
}

return createThunk(fakePrep, func(factory *Factory) (resource.Task, error) {
return createThunk(func(factory *Factory) (resource.Task, error) {
dynamicRenderer, rendErr := factory.GetRenderer(p.ID)
if rendErr != nil {
return nil, rendErr
Expand Down Expand Up @@ -197,44 +188,26 @@ func typeError(where, what, expected string, actual interface{}) error {

// PrepareThunk returns a possibly lazily evaluated preparer
type PrepareThunk struct {
resource.Task
// prevent hashing thunks into a single value
Data []byte
Thunk func(*Factory) (resource.Task, error) `hash:"ignore"`
}

func createThunk(task resource.Task, f func(*Factory) (resource.Task, error)) *PrepareThunk {
// Check allows thunk to implement resource.Task
func (p *PrepareThunk) Check(context.Context, resource.Renderer) (resource.TaskStatus, error) {
return nil, errors.New("Unresolved thunk: cannot be evaluated")
}

// Apply allows thunk to implement resource.Task
func (p *PrepareThunk) Apply(context.Context) (resource.TaskStatus, error) {
return nil, errors.New("Unresolved thunk: cannot be evaluated")
}

func createThunk(f func(*Factory) (resource.Task, error)) *PrepareThunk {
junk := make([]byte, 32)
rand.Read(junk)
return &PrepareThunk{
Task: task,
Thunk: f,
Data: junk,
}
}

func getTypedResourcePointer(ctx context.Context, r resource.Resource) (resource.Task, error) {
fakeTask, err := r.Prepare(ctx, fakerenderer.New())
if err != nil {
return nil, err
}
val := reflect.ValueOf(fakeTask)
if val.Kind() != reflect.Ptr {
return fakeTask, nil
}

zero := reflect.Zero(val.Type())
asTask, ok := zero.Interface().(resource.Task)
if !ok {
return nil, fmt.Errorf("%s does not implement resource.Task", val.Type())
}
return asTask, nil
}

// FakeTaskFromPreparer provides a wrapper around the internal
// getTypedResourcePointer function. It should be removed in a future refactor
// but is in place until the code for dealing with thunked branch nodes has
// stabilized.
func FakeTaskFromPreparer(ctx context.Context, r resource.Resource) (resource.Task, error) {
return getTypedResourcePointer(ctx, r)
}
8 changes: 4 additions & 4 deletions render/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ func (r *Renderer) paramRawValue(name string) (interface{}, error) {
return "", errors.New("param not found")
}

if _, ok = task.(*PrepareThunk); ok {
r.resolverErr = true
return "", ErrUnresolvable{}
}
// if _, ok = task.(*PrepareThunk); ok {
// r.resolverErr = true
// return "", ErrUnresolvable{}
// }

// grab the value
param, ok := task.(*param.Param)
Expand Down
8 changes: 3 additions & 5 deletions resource/thunktask.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
// ThunkTask represents an abstract task over a thunk, used when we need to
// serialized a thunked value.
type ThunkTask struct {
Name string
ThunkedType Task
Name string
}

// Check returns a task status with thunk information
Expand All @@ -46,9 +45,8 @@ func (t *ThunkTask) ToStatus() *Status {
}

// NewThunkedTask generates a ThunkTask from a PrepareThunk
func NewThunkedTask(name string, thunkedType Task) *ThunkTask {
func NewThunkedTask(name string) *ThunkTask {
return &ThunkTask{
ThunkedType: thunkedType,
Name: name,
Name: name,
}
}
2 changes: 1 addition & 1 deletion rpc/grapher.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (g *grapher) Graph(in *pb.LoadRequest, stream pb.Grapher_GraphServer) error
func resolveVertex(id string, vertex interface{}) (resource.Task, error) {
switch v := vertex.(type) {
case *render.PrepareThunk:
return resource.NewThunkedTask(id, v.Task), nil
return resource.NewThunkedTask(id), nil
case *resource.TaskWrapper:
if resolved, ok := resource.ResolveTask(v); ok {
return resolved, nil
Expand Down

0 comments on commit 2eb4f12

Please sign in to comment.