github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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/juju/agent"
    10  	"github.com/juju/juju/api/base"
    11  	"github.com/juju/juju/cmd/jujud/agent/engine"
    12  	"github.com/juju/juju/worker"
    13  	"github.com/juju/juju/worker/dependency"
    14  	"gopkg.in/juju/names.v2"
    15  )
    16  
    17  // ManifoldConfig describes the dependencies of a machine action runner.
    18  type ManifoldConfig struct {
    19  	AgentName     string
    20  	APICallerName string
    21  
    22  	NewFacade func(base.APICaller) Facade
    23  	NewWorker func(WorkerConfig) (worker.Worker, error)
    24  }
    25  
    26  // start is used by engine.AgentAPIManifold to create a StartFunc.
    27  func (config ManifoldConfig) start(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    28  	machineTag, ok := a.CurrentConfig().Tag().(names.MachineTag)
    29  	if !ok {
    30  		return nil, errors.Errorf("this manifold can only be used inside a machine")
    31  	}
    32  	machineActionsFacade := config.NewFacade(apiCaller)
    33  	return config.NewWorker(WorkerConfig{
    34  		Facade:       machineActionsFacade,
    35  		MachineTag:   machineTag,
    36  		HandleAction: HandleAction,
    37  	})
    38  }
    39  
    40  // Manifold returns a dependency.Manifold as configured.
    41  func Manifold(config ManifoldConfig) dependency.Manifold {
    42  	typedConfig := engine.AgentAPIManifoldConfig{
    43  		AgentName:     config.AgentName,
    44  		APICallerName: config.APICallerName,
    45  	}
    46  	return engine.AgentAPIManifold(typedConfig, config.start)
    47  }