github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/remoterelations/manifold.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package remoterelations 5 6 import ( 7 "github.com/juju/clock" 8 "github.com/juju/errors" 9 "gopkg.in/juju/worker.v1" 10 "gopkg.in/juju/worker.v1/dependency" 11 12 "github.com/juju/juju/agent" 13 "github.com/juju/juju/api" 14 "github.com/juju/juju/api/base" 15 "github.com/juju/juju/worker/apicaller" 16 ) 17 18 // ManifoldConfig defines the names of the manifolds on which a 19 // Worker manifold will depend. 20 type ManifoldConfig struct { 21 AgentName string 22 APICallerName string 23 24 NewControllerConnection apicaller.NewExternalControllerConnectionFunc 25 NewRemoteRelationsFacade func(base.APICaller) (RemoteRelationsFacade, 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.NewControllerConnection == nil { 38 return errors.NotValidf("nil NewControllerConnection") 39 } 40 if config.NewRemoteRelationsFacade == nil { 41 return errors.NotValidf("nil NewRemoteRelationsFacade") 42 } 43 if config.NewWorker == nil { 44 return errors.NotValidf("nil NewWorker") 45 } 46 return nil 47 } 48 49 // start is a StartFunc for a Worker manifold. 50 func (config ManifoldConfig) start(context dependency.Context) (worker.Worker, error) { 51 if err := config.Validate(); err != nil { 52 return nil, errors.Trace(err) 53 } 54 55 var agent agent.Agent 56 if err := context.Get(config.AgentName, &agent); err != nil { 57 return nil, errors.Trace(err) 58 } 59 var apiConn api.Connection 60 if err := context.Get(config.APICallerName, &apiConn); err != nil { 61 return nil, errors.Trace(err) 62 } 63 facade, err := config.NewRemoteRelationsFacade(apiConn) 64 if err != nil { 65 return nil, errors.Trace(err) 66 } 67 68 w, err := config.NewWorker(Config{ 69 ModelUUID: agent.CurrentConfig().Model().Id(), 70 RelationsFacade: facade, 71 NewRemoteModelFacadeFunc: remoteRelationsFacadeForModelFunc(config.NewControllerConnection), 72 Clock: clock.WallClock, 73 }) 74 if err != nil { 75 return nil, errors.Trace(err) 76 } 77 return w, nil 78 } 79 80 // Manifold packages a Worker for use in a dependency.Engine. 81 func Manifold(config ManifoldConfig) dependency.Manifold { 82 return dependency.Manifold{ 83 Inputs: []string{ 84 config.AgentName, 85 config.APICallerName, 86 }, 87 Start: config.start, 88 } 89 }