github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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 func (s *stateShim) MongoSession() mongoSession { 37 return mongoSessionShim{s.State.MongoSession()} 38 } 39 40 func (m *machineShim) APIHostPorts() []network.HostPort { 41 return network.AddressesWithPort(m.Addresses(), m.apiPort) 42 } 43 44 func (m *machineShim) MongoHostPorts() []network.HostPort { 45 return network.AddressesWithPort(m.Addresses(), m.mongoPort) 46 } 47 48 type machineShim struct { 49 *state.Machine 50 mongoPort int 51 apiPort int 52 } 53 54 type mongoSessionShim struct { 55 session *mgo.Session 56 } 57 58 func (s mongoSessionShim) CurrentStatus() (*replicaset.Status, error) { 59 return replicaset.CurrentStatus(s.session) 60 } 61 62 func (s mongoSessionShim) CurrentMembers() ([]replicaset.Member, error) { 63 return replicaset.CurrentMembers(s.session) 64 } 65 66 func (s mongoSessionShim) Set(members []replicaset.Member) error { 67 return replicaset.Set(s.session, members) 68 }