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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package applicationscaler
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/apiserver/common"
    10  	"github.com/juju/juju/apiserver/facade"
    11  	"github.com/juju/juju/state"
    12  )
    13  
    14  // This file contains untested shims to let us wrap state in a sensible
    15  // interface and avoid writing tests that depend on mongodb. If you were
    16  // to change any part of it so that it were no longer *obviously* and
    17  // *trivially* correct, you would be Doing It Wrong.
    18  
    19  func init() {
    20  	common.RegisterStandardFacade("ApplicationScaler", 1, newFacade)
    21  }
    22  
    23  // newFacade wraps the supplied *state.State for the use of the Facade.
    24  func newFacade(st *state.State, res facade.Resources, auth facade.Authorizer) (*Facade, error) {
    25  	return NewFacade(backendShim{st}, res, auth)
    26  }
    27  
    28  // backendShim wraps a *State to implement Backend without pulling in direct
    29  // mongodb dependencies. It would be awesome if we were to put this in state
    30  // and test it properly there, where we have no choice but to test against
    31  // mongodb anyway, but that's relatively low priority...
    32  //
    33  // ...so long as it stays simple, and the full functionality remains tested
    34  // elsewhere.
    35  type backendShim struct {
    36  	st *state.State
    37  }
    38  
    39  // WatchScaledServices is part of the Backend interface.
    40  func (shim backendShim) WatchScaledServices() state.StringsWatcher {
    41  	return shim.st.WatchMinUnits()
    42  }
    43  
    44  // RescaleService is part of the Backend interface.
    45  func (shim backendShim) RescaleService(name string) error {
    46  	service, err := shim.st.Application(name)
    47  	if err != nil {
    48  		return errors.Trace(err)
    49  	}
    50  	return service.EnsureMinUnits()
    51  }