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