github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/charmrevisionupdater/handlers.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package charmrevisionupdater
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/errors"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	"github.com/juju/juju/charmstore"
    13  	"github.com/juju/juju/state"
    14  )
    15  
    16  // LatestCharmHandler exposes the functionality needed to deal with
    17  // the latest info (from the store) for a charm.
    18  type LatestCharmHandler interface {
    19  	// HandleLatest deals with the given charm info, treating it as the
    20  	// most up-to-date information for the charms most recent revision.
    21  	HandleLatest(names.ApplicationTag, charmstore.CharmInfo) error
    22  }
    23  
    24  type newHandlerFunc func(*state.State) (LatestCharmHandler, error)
    25  
    26  var registeredHandlers = map[string]newHandlerFunc{}
    27  
    28  // RegisterLatestCharmHandler adds the factory func for the identified
    29  // handler to the handler registry.
    30  func RegisterLatestCharmHandler(name string, newHandler newHandlerFunc) error {
    31  	if _, ok := registeredHandlers[name]; ok {
    32  		msg := fmt.Sprintf(`"latest charm" handler %q already registered`, name)
    33  		return errors.NewAlreadyExists(nil, msg)
    34  	}
    35  	registeredHandlers[name] = newHandler
    36  	return nil
    37  }
    38  
    39  func createHandlers(st *state.State) ([]LatestCharmHandler, error) {
    40  	var handlers []LatestCharmHandler
    41  	for _, newHandler := range registeredHandlers {
    42  		handler, err := newHandler(st)
    43  		if err != nil {
    44  			return nil, errors.Trace(err)
    45  		}
    46  		handlers = append(handlers, handler)
    47  	}
    48  	return handlers, nil
    49  }