github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/worker/uniter/filter/manifold.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package filter
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/names"
    11  
    12  	"github.com/juju/juju/api/base"
    13  	"github.com/juju/juju/api/uniter"
    14  	"github.com/juju/juju/worker"
    15  	"github.com/juju/juju/worker/agent"
    16  	"github.com/juju/juju/worker/dependency"
    17  	"github.com/juju/juju/worker/util"
    18  )
    19  
    20  // ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
    21  type ManifoldConfig util.AgentApiManifoldConfig
    22  
    23  // Manifold returns a dependency manifold that runs an event filter worker, using
    24  // the resource names defined in the supplied config.
    25  func Manifold(config ManifoldConfig) dependency.Manifold {
    26  	manifold := util.AgentApiManifold(util.AgentApiManifoldConfig(config), newWorker)
    27  	manifold.Output = outputFunc
    28  	return manifold
    29  }
    30  
    31  // newWorker should eventually replace NewFilter as the package factory function,
    32  // and the Worker methods should be removed from the Filter interface; at that point
    33  // all tests can be done via the manifold. For now it's just to be patched out for
    34  // the sake of simplified testing.
    35  var newWorker = func(agent agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    36  	// TODO(fwereade): this worker was extracted from uniter, which is why it
    37  	// still uses the uniter facade. This should probably be fixed at some point.
    38  	unitTag, ok := agent.Tag().(names.UnitTag)
    39  	if !ok {
    40  		return nil, fmt.Errorf("expected a unit tag; got %q", agent.Tag())
    41  	}
    42  	uniterFacade := uniter.NewState(apiCaller, unitTag)
    43  	return NewFilter(uniterFacade, unitTag)
    44  }
    45  
    46  // outputFunc exposes a *filter as a Filter.
    47  func outputFunc(in worker.Worker, out interface{}) error {
    48  	inWorker, _ := in.(*filter)
    49  	outPointer, _ := out.(*Filter)
    50  	if inWorker == nil || outPointer == nil {
    51  		return errors.Errorf("expected %T->%T; got %T->%T", inWorker, outPointer, in, out)
    52  	}
    53  	*outPointer = inWorker
    54  	return nil
    55  }