github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/jujud/agent/engine/api.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package engine
     5  
     6  import (
     7  	"github.com/juju/juju/api/base"
     8  	"github.com/juju/juju/worker"
     9  	"github.com/juju/juju/worker/dependency"
    10  )
    11  
    12  // Some (hopefully growing number of) manifolds completely depend on an API
    13  // connection; this type configures them.
    14  type APIManifoldConfig struct {
    15  	APICallerName string
    16  }
    17  
    18  // APIStartFunc encapsulates the behaviour that varies among APIManifolds.
    19  type APIStartFunc func(base.APICaller) (worker.Worker, error)
    20  
    21  // APIManifold returns a dependency.Manifold that calls the supplied start
    22  // func with the API resource defined in the config (once it's present).
    23  func APIManifold(config APIManifoldConfig, start APIStartFunc) dependency.Manifold {
    24  	return dependency.Manifold{
    25  		Inputs: []string{
    26  			config.APICallerName,
    27  		},
    28  		Start: func(context dependency.Context) (worker.Worker, error) {
    29  			var apiCaller base.APICaller
    30  			if err := context.Get(config.APICallerName, &apiCaller); err != nil {
    31  				return nil, err
    32  			}
    33  			return start(apiCaller)
    34  		},
    35  	}
    36  }