github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/machineactions/manifold.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Copyright 2016 Cloudbase Solutions SRL
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  package machineactions
     6  
     7  import (
     8  	"github.com/juju/errors"
     9  	"github.com/juju/names/v5"
    10  	"github.com/juju/worker/v3"
    11  	"github.com/juju/worker/v3/dependency"
    12  
    13  	"github.com/juju/juju/agent"
    14  	"github.com/juju/juju/api/base"
    15  	"github.com/juju/juju/cmd/jujud/agent/engine"
    16  	"github.com/juju/juju/core/machinelock"
    17  )
    18  
    19  // ManifoldConfig describes the dependencies of a machine action runner.
    20  type ManifoldConfig struct {
    21  	AgentName     string
    22  	APICallerName string
    23  	MachineLock   machinelock.Lock
    24  
    25  	NewFacade func(base.APICaller) Facade
    26  	NewWorker func(WorkerConfig) (worker.Worker, error)
    27  }
    28  
    29  // start is used by engine.AgentAPIManifold to create a StartFunc.
    30  func (config ManifoldConfig) start(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    31  	machineTag, ok := a.CurrentConfig().Tag().(names.MachineTag)
    32  	if !ok {
    33  		return nil, errors.Errorf("this manifold can only be used inside a machine")
    34  	}
    35  	machineActionsFacade := config.NewFacade(apiCaller)
    36  	return config.NewWorker(WorkerConfig{
    37  		Facade:       machineActionsFacade,
    38  		MachineTag:   machineTag,
    39  		MachineLock:  config.MachineLock,
    40  		HandleAction: HandleAction,
    41  	})
    42  }
    43  
    44  // Manifold returns a dependency.Manifold as configured.
    45  func Manifold(config ManifoldConfig) dependency.Manifold {
    46  	typedConfig := engine.AgentAPIManifoldConfig{
    47  		AgentName:     config.AgentName,
    48  		APICallerName: config.APICallerName,
    49  	}
    50  	return engine.AgentAPIManifold(typedConfig, config.start)
    51  }