github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/jujud/agent/util/api.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package util
     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  }