github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/peergrouper/shim.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package peergrouper 5 6 import ( 7 "github.com/juju/replicaset" 8 "gopkg.in/mgo.v2" 9 10 "github.com/juju/juju/network" 11 "github.com/juju/juju/state" 12 ) 13 14 // This file holds code that translates from State 15 // to the interface expected internally by the 16 // worker. 17 18 type stateShim struct { 19 *state.State 20 mongoPort int 21 apiPort int 22 } 23 24 func (s *stateShim) Machine(id string) (stateMachine, error) { 25 m, err := s.State.Machine(id) 26 if err != nil { 27 return nil, err 28 } 29 return &machineShim{ 30 Machine: m, 31 mongoPort: s.mongoPort, 32 apiPort: s.apiPort, 33 }, nil 34 } 35 36 type SpaceReader interface { 37 Name() string 38 } 39 40 func (st *stateShim) Space(name string) (SpaceReader, error) { 41 return st.Space(name) 42 } 43 44 func (s *stateShim) MongoSession() mongoSession { 45 return mongoSessionShim{s.State.MongoSession()} 46 } 47 48 func (m *machineShim) APIHostPorts() []network.HostPort { 49 return network.AddressesWithPort(m.Addresses(), m.apiPort) 50 } 51 52 func (m *machineShim) MongoHostPorts() []network.HostPort { 53 return network.AddressesWithPort(m.Addresses(), m.mongoPort) 54 } 55 56 type machineShim struct { 57 *state.Machine 58 mongoPort int 59 apiPort int 60 } 61 62 type mongoSessionShim struct { 63 session *mgo.Session 64 } 65 66 func (s mongoSessionShim) CurrentStatus() (*replicaset.Status, error) { 67 return replicaset.CurrentStatus(s.session) 68 } 69 70 func (s mongoSessionShim) CurrentMembers() ([]replicaset.Member, error) { 71 return replicaset.CurrentMembers(s.session) 72 } 73 74 func (s mongoSessionShim) Set(members []replicaset.Member) error { 75 return replicaset.Set(s.session, members) 76 }