github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/apiserver/facades/client/modelupgrader/shims.go (about) 1 // Copyright 2020 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package modelupgrader 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/mgo/v3" 9 "github.com/juju/names/v5" 10 "github.com/juju/replicaset/v3" 11 "github.com/juju/version/v2" 12 13 "github.com/juju/juju/controller" 14 "github.com/juju/juju/state" 15 ) 16 17 // StatePool represents a point of use interface for getting the state from the 18 // pool. 19 type StatePool interface { 20 Get(string) (State, error) 21 ControllerModel() (Model, error) 22 MongoVersion() (string, error) 23 } 24 25 // State represents a point of use interface for modelling a current model. 26 type State interface { 27 Model() (Model, error) 28 HasUpgradeSeriesLocks() (bool, error) 29 Release() bool 30 AllModelUUIDs() ([]string, error) 31 MachineCountForBase(base ...state.Base) (map[string]int, error) 32 MongoCurrentStatus() (*replicaset.Status, error) 33 SetModelAgentVersion(newVersion version.Number, stream *string, ignoreAgentVersions bool) error 34 AbortCurrentUpgrade() error 35 ControllerConfig() (controller.Config, error) 36 AllCharmURLs() ([]*string, error) 37 } 38 39 type SystemState interface { 40 ControllerModel() (Model, error) 41 } 42 43 // Model defines a point of use interface for the model from state. 44 type Model interface { 45 IsControllerModel() bool 46 AgentVersion() (version.Number, error) 47 Owner() names.UserTag 48 Name() string 49 MigrationMode() state.MigrationMode 50 Type() state.ModelType 51 Life() state.Life 52 } 53 54 type statePoolShim struct { 55 *state.StatePool 56 } 57 58 func (s statePoolShim) ControllerModel() (Model, error) { 59 st, err := s.StatePool.SystemState() 60 if err != nil { 61 return nil, errors.Trace(err) 62 } 63 model, err := st.Model() 64 if err != nil { 65 return nil, errors.Trace(err) 66 } 67 return modelShim{ 68 Model: model, 69 }, nil 70 } 71 72 func (s statePoolShim) Get(uuid string) (State, error) { 73 st, err := s.StatePool.Get(uuid) 74 if err != nil { 75 return nil, errors.Trace(err) 76 } 77 return stateShim{ 78 PooledState: st, 79 }, nil 80 } 81 82 func (s statePoolShim) MongoVersion() (string, error) { 83 st, err := s.StatePool.SystemState() 84 if err != nil { 85 return "", errors.Trace(err) 86 } 87 return st.MongoVersion() 88 } 89 90 type stateShim struct { 91 *state.PooledState 92 mgosession *mgo.Session 93 } 94 95 func (s stateShim) Model() (Model, error) { 96 model, err := s.PooledState.Model() 97 if err != nil { 98 return nil, errors.Trace(err) 99 } 100 return modelShim{ 101 Model: model, 102 }, nil 103 } 104 105 func (s stateShim) MachineCountForBase(base ...state.Base) (map[string]int, error) { 106 count, err := s.PooledState.MachineCountForBase(base...) 107 if err != nil { 108 return nil, errors.Trace(err) 109 } 110 return count, nil 111 } 112 113 func (s stateShim) SetModelAgentVersion(newVersion version.Number, stream *string, ignoreAgentVersions bool) error { 114 return s.PooledState.SetModelAgentVersion(newVersion, stream, ignoreAgentVersions) 115 } 116 117 func (s stateShim) AbortCurrentUpgrade() error { 118 return s.PooledState.AbortCurrentUpgrade() 119 } 120 121 func (s stateShim) AllModelUUIDs() ([]string, error) { 122 allModelUUIDs, err := s.PooledState.AllModelUUIDs() 123 if err != nil { 124 return nil, errors.Trace(err) 125 } 126 return allModelUUIDs, nil 127 } 128 129 func (s stateShim) MongoCurrentStatus() (*replicaset.Status, error) { 130 if s.mgosession == nil { 131 s.mgosession = s.PooledState.MongoSession() 132 } 133 return replicaset.CurrentStatus(s.mgosession) 134 } 135 136 func (s stateShim) AllCharmURLs() ([]*string, error) { 137 return s.PooledState.AllCharmURLs() 138 } 139 140 type modelShim struct { 141 *state.Model 142 } 143 144 func (s modelShim) IsControllerModel() bool { 145 return s.Model.IsControllerModel() 146 } 147 148 func (s modelShim) MigrationMode() state.MigrationMode { 149 return s.Model.MigrationMode() 150 }