github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"gopkg.in/juju/worker.v1"
     9  	"gopkg.in/juju/worker.v1/dependency"
    10  
    11  	"github.com/juju/juju/agent"
    12  	"github.com/juju/juju/api"
    13  	"github.com/juju/juju/api/base"
    14  	"github.com/juju/juju/worker/fortress"
    15  )
    16  
    17  // ManifoldConfig defines the names of the manifolds on which a
    18  // Worker manifold will depend.
    19  type ManifoldConfig struct {
    20  	AgentName         string
    21  	APICallerName     string
    22  	FortressName      string
    23  	APIOpen           func(*api.Info, api.DialOpts) (api.Connection, error)
    24  	ValidateMigration func(base.APICaller) error
    25  
    26  	NewFacade func(base.APICaller) (Facade, error)
    27  	NewWorker func(Config) (worker.Worker, error)
    28  }
    29  
    30  // validate is called by start to check for bad configuration.
    31  func (config ManifoldConfig) validate() error {
    32  	if config.AgentName == "" {
    33  		return errors.NotValidf("empty AgentName")
    34  	}
    35  	if config.APICallerName == "" {
    36  		return errors.NotValidf("empty APICallerName")
    37  	}
    38  	if config.FortressName == "" {
    39  		return errors.NotValidf("empty FortressName")
    40  	}
    41  	if config.APIOpen == nil {
    42  		return errors.NotValidf("nil APIOpen")
    43  	}
    44  	if config.ValidateMigration == nil {
    45  		return errors.NotValidf("nil ValidateMigration")
    46  	}
    47  	if config.NewFacade == nil {
    48  		return errors.NotValidf("nil NewFacade")
    49  	}
    50  	if config.NewWorker == nil {
    51  		return errors.NotValidf("nil NewWorker")
    52  	}
    53  	return nil
    54  }
    55  
    56  // start is a StartFunc for a Worker manifold.
    57  func (config ManifoldConfig) start(context dependency.Context) (worker.Worker, error) {
    58  	if err := config.validate(); err != nil {
    59  		return nil, errors.Trace(err)
    60  	}
    61  	var agent agent.Agent
    62  	if err := context.Get(config.AgentName, &agent); err != nil {
    63  		return nil, errors.Trace(err)
    64  	}
    65  	var apiCaller base.APICaller
    66  	if err := context.Get(config.APICallerName, &apiCaller); err != nil {
    67  		return nil, errors.Trace(err)
    68  	}
    69  	var guard fortress.Guard
    70  	if err := context.Get(config.FortressName, &guard); err != nil {
    71  		return nil, errors.Trace(err)
    72  	}
    73  
    74  	facade, err := config.NewFacade(apiCaller)
    75  	if err != nil {
    76  		return nil, errors.Trace(err)
    77  	}
    78  	worker, err := config.NewWorker(Config{
    79  		Agent:             agent,
    80  		Facade:            facade,
    81  		Guard:             guard,
    82  		APIOpen:           config.APIOpen,
    83  		ValidateMigration: config.ValidateMigration,
    84  	})
    85  	if err != nil {
    86  		return nil, errors.Trace(err)
    87  	}
    88  	return worker, nil
    89  }
    90  
    91  // Manifold returns a dependency manifold that runs the migration
    92  // worker.
    93  func Manifold(config ManifoldConfig) dependency.Manifold {
    94  	return dependency.Manifold{
    95  		Inputs: []string{
    96  			config.AgentName,
    97  			config.APICallerName,
    98  			config.FortressName,
    99  		},
   100  		Start: config.start,
   101  	}
   102  }