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