github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/migration/precheck_shim.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package migration 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/version" 9 10 "github.com/juju/juju/state" 11 ) 12 13 // PrecheckShim wraps a *state.State to implement PrecheckBackend. 14 func PrecheckShim(st *state.State) PrecheckBackend { 15 return &precheckShim{st} 16 } 17 18 // precheckShim is untested, but is simple enough to be verified by 19 // inspection. 20 type precheckShim struct { 21 *state.State 22 } 23 24 // Model implements PrecheckBackend. 25 func (s *precheckShim) Model() (PrecheckModel, error) { 26 model, err := s.State.Model() 27 if err != nil { 28 return nil, errors.Trace(err) 29 } 30 return model, nil 31 } 32 33 // AllModels implements PrecheckBackend. 34 func (s *precheckShim) AllModels() ([]PrecheckModel, error) { 35 models, err := s.State.AllModels() 36 if err != nil { 37 return nil, errors.Trace(err) 38 } 39 out := make([]PrecheckModel, 0, len(models)) 40 for _, model := range models { 41 out = append(out, model) 42 } 43 return out, nil 44 } 45 46 // IsMigrationActive implements PrecheckBackend. 47 func (s *precheckShim) IsMigrationActive(modelUUID string) (bool, error) { 48 return state.IsMigrationActive(s.State, modelUUID) 49 } 50 51 // AgentVersion implements PrecheckBackend. 52 func (s *precheckShim) AgentVersion() (version.Number, error) { 53 cfg, err := s.State.ModelConfig() 54 if err != nil { 55 return version.Zero, errors.Trace(err) 56 } 57 vers, ok := cfg.AgentVersion() 58 if !ok { 59 return version.Zero, errors.New("no model agent version") 60 } 61 return vers, nil 62 } 63 64 // AllMachines implements PrecheckBackend. 65 func (s *precheckShim) AllMachines() ([]PrecheckMachine, error) { 66 machines, err := s.State.AllMachines() 67 if err != nil { 68 return nil, errors.Trace(err) 69 } 70 out := make([]PrecheckMachine, 0, len(machines)) 71 for _, machine := range machines { 72 out = append(out, machine) 73 } 74 return out, nil 75 } 76 77 // AllApplications implements PrecheckBackend. 78 func (s *precheckShim) AllApplications() ([]PrecheckApplication, error) { 79 apps, err := s.State.AllApplications() 80 if err != nil { 81 return nil, errors.Trace(err) 82 } 83 out := make([]PrecheckApplication, 0, len(apps)) 84 for _, app := range apps { 85 out = append(out, &precheckAppShim{app}) 86 } 87 return out, nil 88 } 89 90 // ControllerBackend implements PrecheckBackend. 91 func (s *precheckShim) ControllerBackend() (PrecheckBackend, error) { 92 model, err := s.State.ControllerModel() 93 if err != nil { 94 return nil, errors.Trace(err) 95 } 96 st, err := s.State.ForModel(model.ModelTag()) 97 if err != nil { 98 return nil, errors.Trace(err) 99 } 100 return PrecheckShim(st), nil 101 } 102 103 // precheckAppShim implements PrecheckApplication. 104 type precheckAppShim struct { 105 *state.Application 106 } 107 108 // AllUnits implements PrecheckApplication. 109 func (s *precheckAppShim) AllUnits() ([]PrecheckUnit, error) { 110 units, err := s.Application.AllUnits() 111 if err != nil { 112 return nil, errors.Trace(err) 113 } 114 out := make([]PrecheckUnit, 0, len(units)) 115 for _, unit := range units { 116 out = append(out, unit) 117 } 118 return out, nil 119 }