github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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  	"github.com/juju/names"
     9  
    10  	"github.com/juju/juju/agent"
    11  	"github.com/juju/juju/api/base"
    12  	"github.com/juju/juju/api/uniter"
    13  	"github.com/juju/juju/worker"
    14  	"github.com/juju/juju/worker/dependency"
    15  	"github.com/juju/juju/worker/util"
    16  )
    17  
    18  // ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
    19  type ManifoldConfig util.AgentApiManifoldConfig
    20  
    21  // Manifold returns a dependency manifold that runs an API address updater worker,
    22  // using the resource names defined in the supplied config.
    23  func Manifold(config ManifoldConfig) dependency.Manifold {
    24  	return util.AgentApiManifold(util.AgentApiManifoldConfig(config), newWorker)
    25  }
    26  
    27  // newWorker trivially wraps NewAPIAddressUpdater for use in a util.AgentApiManifold.
    28  // It's not tested at the moment, because the scaffolding necessary to test these 5
    29  // lines outweighs them by several times for very little confirmatory power; in the
    30  // long term, all APIAddressUpdaters should be constructed via a manifold, and the
    31  // tests can be updated to reflect that.
    32  var newWorker = func(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    33  	// TODO(fwereade): why on *earth* do we use the *uniter* facade for this
    34  	// worker? This code really ought to work anywhere...
    35  	tag := a.CurrentConfig().Tag()
    36  	unitTag, ok := tag.(names.UnitTag)
    37  	if !ok {
    38  		return nil, errors.Errorf("expected a unit tag; got %q", tag)
    39  	}
    40  	return NewAPIAddressUpdater(uniter.NewState(apiCaller, unitTag), agent.APIHostPortsSetter{a}), nil
    41  }