github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/jujud/agent/engine/apiagent.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  	"gopkg.in/juju/worker.v1"
     8  	"gopkg.in/juju/worker.v1/dependency"
     9  
    10  	"github.com/juju/juju/agent"
    11  	"github.com/juju/juju/api/base"
    12  )
    13  
    14  // Many manifolds completely depend on an agent and an API connection; this
    15  // type configures them.
    16  type AgentAPIManifoldConfig struct {
    17  	AgentName     string
    18  	APICallerName string
    19  }
    20  
    21  // AgentAPIStartFunc encapsulates the behaviour that varies among AgentAPIManifolds.
    22  type AgentAPIStartFunc func(agent.Agent, base.APICaller) (worker.Worker, error)
    23  
    24  // AgentAPIManifold returns a dependency.Manifold that calls the supplied start
    25  // func with the API and agent resources defined in the config (once those
    26  // resources are present).
    27  func AgentAPIManifold(config AgentAPIManifoldConfig, start AgentAPIStartFunc) dependency.Manifold {
    28  	return dependency.Manifold{
    29  		Inputs: []string{
    30  			config.AgentName,
    31  			config.APICallerName,
    32  		},
    33  		Start: func(context dependency.Context) (worker.Worker, error) {
    34  			var agent agent.Agent
    35  			if err := context.Get(config.AgentName, &agent); err != nil {
    36  				return nil, err
    37  			}
    38  			var apiCaller base.APICaller
    39  			if err := context.Get(config.APICallerName, &apiCaller); err != nil {
    40  				return nil, err
    41  			}
    42  			return start(agent, apiCaller)
    43  		},
    44  	}
    45  }