github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/state/statemetrics/state.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 package statemetrics 4 5 import ( 6 "github.com/juju/errors" 7 "gopkg.in/juju/names.v2" 8 9 "github.com/juju/juju/core/status" 10 "github.com/juju/juju/permission" 11 "github.com/juju/juju/state" 12 ) 13 14 // State represents the global state managed by the Juju controller. 15 type State interface { 16 AllMachines() ([]Machine, error) 17 AllModelUUIDs() ([]string, error) 18 AllUsers() ([]User, error) 19 ControllerTag() names.ControllerTag 20 UserAccess(names.UserTag, names.Tag) (permission.UserAccess, error) 21 } 22 23 // PooledState is a wrapper for State that includes methods to negotiate with 24 // the pool that supplied it. 25 type PooledState interface { 26 state.PoolHelper 27 State 28 } 29 30 // StatePool represents a pool of State objects. 31 type StatePool interface { 32 SystemState() State 33 Get(modelUUID string) (PooledState, error) 34 GetModel(modelUUID string) (Model, state.PoolHelper, error) 35 } 36 37 // Machine represents a machine in a Juju model. 38 type Machine interface { 39 InstanceStatus() (status.StatusInfo, error) 40 Life() state.Life 41 Status() (status.StatusInfo, error) 42 } 43 44 // Model represents a Juju model. 45 type Model interface { 46 Life() state.Life 47 ModelTag() names.ModelTag 48 Status() (status.StatusInfo, error) 49 } 50 51 // User represents a user known to the Juju controller. 52 type User interface { 53 IsDeleted() bool 54 IsDisabled() bool 55 UserTag() names.UserTag 56 } 57 58 type statePoolShim struct { 59 pool *state.StatePool 60 } 61 62 // NewStatePool takes a *state.StatePool, and returns 63 // a StatePool value backed by it. 64 func NewStatePool(pool *state.StatePool) StatePool { 65 return statePoolShim{pool} 66 } 67 68 type stateShim struct { 69 *state.State 70 } 71 72 type pooledStateShim struct { 73 *state.PooledState 74 } 75 76 func (p statePoolShim) SystemState() State { 77 return stateShim{p.pool.SystemState()} 78 } 79 80 func (p statePoolShim) Get(modelUUID string) (PooledState, error) { 81 st, err := p.pool.Get(modelUUID) 82 if err != nil { 83 return nil, err 84 } 85 return pooledStateShim{st}, nil 86 } 87 88 func (p statePoolShim) GetModel(modelUUID string) (Model, state.PoolHelper, error) { 89 model, ph, err := p.pool.GetModel(modelUUID) 90 if err != nil { 91 return nil, nil, err 92 } 93 return model, ph, err 94 } 95 96 func (s stateShim) AllMachines() ([]Machine, error) { 97 return allMachines(s.State) 98 } 99 100 func (s pooledStateShim) AllMachines() ([]Machine, error) { 101 return allMachines(s.State) 102 } 103 104 func allMachines(st *state.State) ([]Machine, error) { 105 machines, err := st.AllMachines() 106 if err != nil { 107 return nil, errors.Trace(err) 108 } 109 out := make([]Machine, len(machines)) 110 for i, m := range machines { 111 if m != nil { 112 out[i] = m 113 } 114 } 115 return out, nil 116 } 117 118 func (s stateShim) AllUsers() ([]User, error) { 119 return allUsers(s.State) 120 } 121 122 func (s pooledStateShim) AllUsers() ([]User, error) { 123 return allUsers(s.State) 124 } 125 126 func allUsers(st *state.State) ([]User, error) { 127 users, err := st.AllUsers(true) 128 if err != nil { 129 return nil, errors.Trace(err) 130 } 131 out := make([]User, len(users)) 132 for i, u := range users { 133 if u != nil { 134 out[i] = u 135 } 136 } 137 return out, nil 138 }