github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/worker/apiaddressupdater/manifold.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package apiaddressupdater
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"gopkg.in/juju/names.v2"
     9  
    10  	"github.com/juju/juju/agent"
    11  	"github.com/juju/juju/api/base"
    12  	"github.com/juju/juju/api/machiner"
    13  	"github.com/juju/juju/api/uniter"
    14  	"github.com/juju/juju/cmd/jujud/agent/engine"
    15  	"github.com/juju/juju/worker"
    16  	"github.com/juju/juju/worker/dependency"
    17  )
    18  
    19  // ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
    20  type ManifoldConfig engine.AgentAPIManifoldConfig
    21  
    22  // Manifold returns a dependency manifold that runs an API address updater worker,
    23  // using the resource names defined in the supplied config.
    24  func Manifold(config ManifoldConfig) dependency.Manifold {
    25  	typedConfig := engine.AgentAPIManifoldConfig(config)
    26  	return engine.AgentAPIManifold(typedConfig, newWorker)
    27  }
    28  
    29  // newWorker trivially wraps NewAPIAddressUpdater for use in a engine.AgentAPIManifold.
    30  // It's not tested at the moment, because the scaffolding necessary is too
    31  // unwieldy/distracting to introduce at this point.
    32  var newWorker = func(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    33  	tag := a.CurrentConfig().Tag()
    34  
    35  	// TODO(fwereade): use appropriate facade!
    36  	var facade APIAddresser
    37  	switch apiTag := tag.(type) {
    38  	case names.UnitTag:
    39  		facade = uniter.NewState(apiCaller, apiTag)
    40  	case names.MachineTag:
    41  		facade = machiner.NewState(apiCaller)
    42  	default:
    43  		return nil, errors.Errorf("expected a unit or machine tag; got %q", tag)
    44  	}
    45  
    46  	setter := agent.APIHostPortsSetter{a}
    47  	w, err := NewAPIAddressUpdater(facade, setter)
    48  	if err != nil {
    49  		return nil, errors.Trace(err)
    50  	}
    51  	return w, nil
    52  }