github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/client/applicationoffers/state.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package applicationoffers 5 6 import ( 7 "github.com/juju/errors" 8 "gopkg.in/juju/charm.v6" 9 "gopkg.in/juju/names.v2" 10 11 commoncrossmodel "github.com/juju/juju/apiserver/common/crossmodel" 12 "github.com/juju/juju/core/crossmodel" 13 "github.com/juju/juju/network" 14 "github.com/juju/juju/permission" 15 "github.com/juju/juju/state" 16 ) 17 18 // StatePool provides the subset of a state pool. 19 type StatePool interface { 20 // Get returns a State for a given model from the pool. 21 Get(modelUUID string) (Backend, func(), error) 22 23 // Get returns a Model from the pool. 24 GetModel(modelUUID string) (Model, func(), error) 25 } 26 27 var GetStatePool = func(sp *state.StatePool) StatePool { 28 return &statePoolShim{sp} 29 30 } 31 32 type statePoolShim struct { 33 *state.StatePool 34 } 35 36 func (pool statePoolShim) Get(modelUUID string) (Backend, func(), error) { 37 st, err := pool.StatePool.Get(modelUUID) 38 if err != nil { 39 return nil, nil, errors.Trace(err) 40 } 41 return &stateShim{ 42 st: st.State, 43 Backend: commoncrossmodel.GetBackend(st.State), 44 }, func() { st.Release() }, nil 45 } 46 47 func (pool statePoolShim) GetModel(modelUUID string) (Model, func(), error) { 48 m, ph, err := pool.StatePool.GetModel(modelUUID) 49 if err != nil { 50 return nil, nil, errors.Trace(err) 51 } 52 return &modelShim{m}, func() { ph.Release() }, nil 53 } 54 55 // Backend provides selected methods off the state.State struct. 56 type Backend interface { 57 commoncrossmodel.Backend 58 Charm(*charm.URL) (commoncrossmodel.Charm, error) 59 ApplicationOffer(name string) (*crossmodel.ApplicationOffer, error) 60 Model() (Model, error) 61 OfferConnections(string) ([]OfferConnection, error) 62 Space(string) (Space, error) 63 User(names.UserTag) (User, error) 64 65 CreateOfferAccess(offer names.ApplicationOfferTag, user names.UserTag, access permission.Access) error 66 UpdateOfferAccess(offer names.ApplicationOfferTag, user names.UserTag, access permission.Access) error 67 RemoveOfferAccess(offer names.ApplicationOfferTag, user names.UserTag) error 68 GetOfferUsers(offerUUID string) (map[string]permission.Access, error) 69 } 70 71 var GetStateAccess = func(st *state.State) Backend { 72 return &stateShim{ 73 st: st, 74 Backend: commoncrossmodel.GetBackend(st), 75 } 76 } 77 78 type stateShim struct { 79 commoncrossmodel.Backend 80 st *state.State 81 } 82 83 func (s stateShim) CreateOfferAccess(offer names.ApplicationOfferTag, user names.UserTag, access permission.Access) error { 84 return s.st.CreateOfferAccess(offer, user, access) 85 } 86 87 func (s stateShim) UpdateOfferAccess(offer names.ApplicationOfferTag, user names.UserTag, access permission.Access) error { 88 return s.st.UpdateOfferAccess(offer, user, access) 89 } 90 91 func (s stateShim) RemoveOfferAccess(offer names.ApplicationOfferTag, user names.UserTag) error { 92 return s.st.RemoveOfferAccess(offer, user) 93 } 94 95 func (s stateShim) GetOfferUsers(offerUUID string) (map[string]permission.Access, error) { 96 return s.st.GetOfferUsers(offerUUID) 97 } 98 99 func (s *stateShim) Space(name string) (Space, error) { 100 sp, err := s.st.Space(name) 101 return &spaceShim{sp}, err 102 } 103 104 func (s *stateShim) Model() (Model, error) { 105 m, err := s.st.Model() 106 return &modelShim{m}, err 107 } 108 109 type stateCharmShim struct { 110 *state.Charm 111 } 112 113 func (s stateShim) Charm(curl *charm.URL) (commoncrossmodel.Charm, error) { 114 ch, err := s.st.Charm(curl) 115 if err != nil { 116 return nil, err 117 } 118 return stateCharmShim{ch}, nil 119 } 120 121 func (s *stateShim) ApplicationOffer(name string) (*crossmodel.ApplicationOffer, error) { 122 offers := state.NewApplicationOffers(s.st) 123 return offers.ApplicationOffer(name) 124 } 125 126 var GetApplicationOffers = func(backend interface{}) crossmodel.ApplicationOffers { 127 switch st := backend.(type) { 128 case *state.State: 129 return state.NewApplicationOffers(st) 130 case *stateShim: 131 return state.NewApplicationOffers(st.st) 132 } 133 return nil 134 } 135 136 type Subnet interface { 137 CIDR() string 138 VLANTag() int 139 ProviderId() network.Id 140 ProviderNetworkId() network.Id 141 AvailabilityZones() []string 142 } 143 144 type subnetShim struct { 145 *state.Subnet 146 } 147 148 func (s *subnetShim) AvailabilityZones() []string { 149 return []string{s.Subnet.AvailabilityZone()} 150 } 151 152 type Space interface { 153 Name() string 154 Subnets() ([]Subnet, error) 155 ProviderId() network.Id 156 } 157 158 type spaceShim struct { 159 *state.Space 160 } 161 162 func (s *spaceShim) Subnets() ([]Subnet, error) { 163 subnets, err := s.Space.Subnets() 164 if err != nil { 165 return nil, errors.Trace(err) 166 } 167 result := make([]Subnet, len(subnets)) 168 for i, subnet := range subnets { 169 result[i] = &subnetShim{subnet} 170 } 171 return result, nil 172 } 173 174 type Model interface { 175 UUID() string 176 ModelTag() names.ModelTag 177 Name() string 178 Type() state.ModelType 179 Owner() names.UserTag 180 } 181 182 type modelShim struct { 183 *state.Model 184 } 185 186 func (s *stateShim) OfferConnections(offerUUID string) ([]OfferConnection, error) { 187 conns, err := s.st.OfferConnections(offerUUID) 188 if err != nil { 189 return nil, err 190 } 191 result := make([]OfferConnection, len(conns)) 192 for i, oc := range conns { 193 result[i] = offerConnectionShim{oc} 194 } 195 return result, nil 196 } 197 198 type OfferConnection interface { 199 SourceModelUUID() string 200 UserName() string 201 RelationKey() string 202 RelationId() int 203 } 204 205 type offerConnectionShim struct { 206 *state.OfferConnection 207 } 208 209 func (s *stateShim) User(tag names.UserTag) (User, error) { 210 return s.st.User(tag) 211 } 212 213 type User interface { 214 DisplayName() string 215 }