github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/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  	"github.com/juju/worker/v3"
    10  	"github.com/juju/worker/v3/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  // Logger represents the methods used by the worker to log details.
    19  type Logger interface {
    20  	Tracef(string, ...interface{})
    21  	Debugf(string, ...interface{})
    22  	Criticalf(string, ...interface{})
    23  	Infof(string, ...interface{})
    24  	Warningf(string, ...interface{})
    25  	Errorf(string, ...interface{})
    26  }
    27  
    28  // ManifoldConfig defines the names of the manifolds on which a
    29  // Worker manifold will depend.
    30  type ManifoldConfig struct {
    31  	AgentName     string
    32  	APICallerName string
    33  
    34  	NewControllerConnection  apicaller.NewExternalControllerConnectionFunc
    35  	NewRemoteRelationsFacade func(base.APICaller) RemoteRelationsFacade
    36  	NewWorker                func(Config) (worker.Worker, error)
    37  	Logger                   Logger
    38  }
    39  
    40  // Validate is called by start to check for bad configuration.
    41  func (config ManifoldConfig) Validate() error {
    42  	if config.AgentName == "" {
    43  		return errors.NotValidf("empty AgentName")
    44  	}
    45  	if config.APICallerName == "" {
    46  		return errors.NotValidf("empty APICallerName")
    47  	}
    48  	if config.NewControllerConnection == nil {
    49  		return errors.NotValidf("nil NewControllerConnection")
    50  	}
    51  	if config.NewRemoteRelationsFacade == nil {
    52  		return errors.NotValidf("nil NewRemoteRelationsFacade")
    53  	}
    54  	if config.NewWorker == nil {
    55  		return errors.NotValidf("nil NewWorker")
    56  	}
    57  	if config.Logger == nil {
    58  		return errors.NotValidf("nil Logger")
    59  	}
    60  	return nil
    61  }
    62  
    63  // start is a StartFunc for a Worker manifold.
    64  func (config ManifoldConfig) start(context dependency.Context) (worker.Worker, error) {
    65  	if err := config.Validate(); err != nil {
    66  		return nil, errors.Trace(err)
    67  	}
    68  
    69  	var agent agent.Agent
    70  	if err := context.Get(config.AgentName, &agent); err != nil {
    71  		return nil, errors.Trace(err)
    72  	}
    73  	var apiConn api.Connection
    74  	if err := context.Get(config.APICallerName, &apiConn); err != nil {
    75  		return nil, errors.Trace(err)
    76  	}
    77  
    78  	w, err := config.NewWorker(Config{
    79  		ModelUUID:                agent.CurrentConfig().Model().Id(),
    80  		RelationsFacade:          config.NewRemoteRelationsFacade(apiConn),
    81  		NewRemoteModelFacadeFunc: remoteRelationsFacadeForModelFunc(config.NewControllerConnection),
    82  		Clock:                    clock.WallClock,
    83  		Logger:                   config.Logger,
    84  	})
    85  	if err != nil {
    86  		return nil, errors.Trace(err)
    87  	}
    88  	return w, nil
    89  }
    90  
    91  // Manifold packages a Worker for use in a dependency.Engine.
    92  func Manifold(config ManifoldConfig) dependency.Manifold {
    93  	return dependency.Manifold{
    94  		Inputs: []string{
    95  			config.AgentName,
    96  			config.APICallerName,
    97  		},
    98  		Start: config.start,
    99  	}
   100  }