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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package migrationminion
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/juju/agent"
     9  	"github.com/juju/juju/api/base"
    10  	"github.com/juju/juju/worker"
    11  	"github.com/juju/juju/worker/dependency"
    12  	"github.com/juju/juju/worker/fortress"
    13  )
    14  
    15  // ManifoldConfig defines the names of the manifolds on which a
    16  // Worker manifold will depend.
    17  type ManifoldConfig struct {
    18  	AgentName     string
    19  	APICallerName string
    20  	FortressName  string
    21  
    22  	NewFacade func(base.APICaller) (Facade, error)
    23  	NewWorker func(Config) (worker.Worker, error)
    24  }
    25  
    26  // validate is called by start to check for bad configuration.
    27  func (config ManifoldConfig) validate() error {
    28  	if config.AgentName == "" {
    29  		return errors.NotValidf("empty AgentName")
    30  	}
    31  	if config.APICallerName == "" {
    32  		return errors.NotValidf("empty APICallerName")
    33  	}
    34  	if config.FortressName == "" {
    35  		return errors.NotValidf("empty FortressName")
    36  	}
    37  	if config.NewFacade == nil {
    38  		return errors.NotValidf("nil NewFacade")
    39  	}
    40  	if config.NewWorker == nil {
    41  		return errors.NotValidf("nil NewWorker")
    42  	}
    43  	return nil
    44  }
    45  
    46  // start is a StartFunc for a Worker manifold.
    47  func (config ManifoldConfig) start(context dependency.Context) (worker.Worker, error) {
    48  	if err := config.validate(); err != nil {
    49  		return nil, errors.Trace(err)
    50  	}
    51  	var agent agent.Agent
    52  	if err := context.Get(config.AgentName, &agent); err != nil {
    53  		return nil, errors.Trace(err)
    54  	}
    55  	var apiCaller base.APICaller
    56  	if err := context.Get(config.APICallerName, &apiCaller); err != nil {
    57  		return nil, errors.Trace(err)
    58  	}
    59  	var guard fortress.Guard
    60  	if err := context.Get(config.FortressName, &guard); err != nil {
    61  		return nil, errors.Trace(err)
    62  	}
    63  
    64  	facade, err := config.NewFacade(apiCaller)
    65  	if err != nil {
    66  		return nil, errors.Trace(err)
    67  	}
    68  	worker, err := config.NewWorker(Config{
    69  		Agent:  agent,
    70  		Facade: facade,
    71  		Guard:  guard,
    72  	})
    73  	if err != nil {
    74  		return nil, errors.Trace(err)
    75  	}
    76  	return worker, nil
    77  }
    78  
    79  // Manifold returns a dependency manifold that runs the migration
    80  // worker.
    81  func Manifold(config ManifoldConfig) dependency.Manifold {
    82  	return dependency.Manifold{
    83  		Inputs: []string{
    84  			config.AgentName,
    85  			config.APICallerName,
    86  			config.FortressName,
    87  		},
    88  		Start: config.start,
    89  	}
    90  }