github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"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/base"
    14  	"github.com/juju/juju/api/caasoperator"
    15  	"github.com/juju/juju/api/machiner"
    16  	"github.com/juju/juju/api/uniter"
    17  	"github.com/juju/juju/cmd/jujud/agent/engine"
    18  )
    19  
    20  // ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
    21  type ManifoldConfig engine.AgentAPIManifoldConfig
    22  
    23  // Manifold returns a dependency manifold that runs an API address updater worker,
    24  // using the resource names defined in the supplied config.
    25  func Manifold(config ManifoldConfig) dependency.Manifold {
    26  	typedConfig := engine.AgentAPIManifoldConfig(config)
    27  	return engine.AgentAPIManifold(typedConfig, newWorker)
    28  }
    29  
    30  // newWorker trivially wraps NewAPIAddressUpdater for use in a engine.AgentAPIManifold.
    31  // It's not tested at the moment, because the scaffolding necessary is too
    32  // unwieldy/distracting to introduce at this point.
    33  var newWorker = func(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    34  	tag := a.CurrentConfig().Tag()
    35  
    36  	// TODO(fwereade): use appropriate facade!
    37  	var facade APIAddresser
    38  	switch apiTag := tag.(type) {
    39  	case names.UnitTag:
    40  		facade = uniter.NewState(apiCaller, apiTag)
    41  	case names.ApplicationTag:
    42  		facade = caasoperator.NewClient(apiCaller)
    43  	case names.MachineTag:
    44  		facade = machiner.NewState(apiCaller)
    45  	default:
    46  		return nil, errors.Errorf("expected a unit or machine tag; got %q", tag)
    47  	}
    48  
    49  	setter := agent.APIHostPortsSetter{a}
    50  	w, err := NewAPIAddressUpdater(facade, setter)
    51  	if err != nil {
    52  		return nil, errors.Trace(err)
    53  	}
    54  	return w, nil
    55  }