github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/environ/manifold.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package environ
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/api/agent"
    10  	"github.com/juju/juju/api/base"
    11  	"github.com/juju/juju/environs"
    12  	"github.com/juju/juju/worker"
    13  	"github.com/juju/juju/worker/dependency"
    14  )
    15  
    16  // ManifoldConfig describes the resources used by a Tracker.
    17  type ManifoldConfig struct {
    18  	APICallerName  string
    19  	NewEnvironFunc NewEnvironFunc
    20  }
    21  
    22  // Manifold returns a Manifold that encapsulates a *Tracker and exposes it as
    23  // an environs.Environ resource.
    24  func Manifold(config ManifoldConfig) dependency.Manifold {
    25  	manifold := dependency.Manifold{
    26  		Inputs: []string{
    27  			config.APICallerName,
    28  		},
    29  		Output: manifoldOutput,
    30  		Start: func(context dependency.Context) (worker.Worker, error) {
    31  			var apiCaller base.APICaller
    32  			if err := context.Get(config.APICallerName, &apiCaller); err != nil {
    33  				return nil, errors.Trace(err)
    34  			}
    35  			w, err := NewTracker(Config{
    36  				Observer:       agent.NewState(apiCaller),
    37  				NewEnvironFunc: config.NewEnvironFunc,
    38  			})
    39  			if err != nil {
    40  				return nil, errors.Trace(err)
    41  			}
    42  			return w, nil
    43  		},
    44  	}
    45  	return manifold
    46  }
    47  
    48  // manifoldOutput extracts an environs.Environ resource from a *Tracker.
    49  func manifoldOutput(in worker.Worker, out interface{}) error {
    50  	inTracker, ok := in.(*Tracker)
    51  	if !ok {
    52  		return errors.Errorf("expected *environ.Tracker, got %T", in)
    53  	}
    54  	outEnviron, ok := out.(*environs.Environ)
    55  	if !ok {
    56  		return errors.Errorf("expected *environs.Environ, got %T", out)
    57  	}
    58  	*outEnviron = inTracker.Environ()
    59  	return nil
    60  }