github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/migrationflag/shim.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the LGPLv3, see LICENCE file for details. 3 4 package migrationflag 5 6 import ( 7 "github.com/juju/errors" 8 9 "github.com/juju/juju/apiserver/common" 10 "github.com/juju/juju/core/migration" 11 "github.com/juju/juju/state" 12 ) 13 14 func init() { 15 common.RegisterStandardFacade("MigrationFlag", 1, newFacade) 16 } 17 18 // newFacade wraps New to express the supplied *state.State as a Backend. 19 func newFacade(st *state.State, resources *common.Resources, auth common.Authorizer) (*Facade, error) { 20 facade, err := New(&backend{st}, resources, auth) 21 if err != nil { 22 return nil, errors.Trace(err) 23 } 24 return facade, nil 25 } 26 27 // backend implements Backend by wrapping a *state.State. 28 type backend struct { 29 st *state.State 30 } 31 32 // ModelUUID is part of the Backend interface. 33 func (shim *backend) ModelUUID() string { 34 return shim.st.ModelUUID() 35 } 36 37 // WatchMigrationPhase is part of the Backend interface. 38 func (shim *backend) WatchMigrationPhase() (state.NotifyWatcher, error) { 39 watcher, err := shim.st.WatchMigrationStatus() 40 if err != nil { 41 return nil, errors.Trace(err) 42 } 43 return watcher, nil 44 } 45 46 // MigrationPhase is part of the Backend interface. 47 func (shim *backend) MigrationPhase() (migration.Phase, error) { 48 mig, err := shim.st.GetModelMigration() 49 if errors.IsNotFound(err) { 50 return migration.NONE, nil 51 } else if err != nil { 52 return migration.UNKNOWN, errors.Trace(err) 53 } 54 phase, err := mig.Phase() 55 if err != nil { 56 return migration.UNKNOWN, errors.Trace(err) 57 } 58 return phase, nil 59 }