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