github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/controller/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/facade" 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 // NewAPI provides the required signature for facade registration. 19 func NewAPI(st *state.State, res facade.Resources, auth facade.Authorizer) (*Facade, error) { 20 return NewFacade(backendShim{st}, res, auth) 21 } 22 23 // backendShim wraps a *State to implement Backend without pulling in direct 24 // mongodb dependencies. It would be awesome if we were to put this in state 25 // and test it properly there, where we have no choice but to test against 26 // mongodb anyway, but that's relatively low priority... 27 // 28 // ...so long as it stays simple, and the full functionality remains tested 29 // elsewhere. 30 type backendShim struct { 31 st *state.State 32 } 33 34 // WatchScaledServices is part of the Backend interface. 35 func (shim backendShim) WatchScaledServices() state.StringsWatcher { 36 return shim.st.WatchMinUnits() 37 } 38 39 // RescaleService is part of the Backend interface. 40 func (shim backendShim) RescaleService(name string) error { 41 service, err := shim.st.Application(name) 42 if err != nil { 43 return errors.Trace(err) 44 } 45 return service.EnsureMinUnits() 46 }