github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/worker/uniter/relation/dyingsource.go (about) 1 // Copyright 2012-2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package relation 5 6 import ( 7 "sort" 8 9 "gopkg.in/juju/charm.v5/hooks" 10 11 "github.com/juju/juju/worker/uniter/hook" 12 ) 13 14 // NewDyingHookSource returns a new hook.Source that generates all hooks 15 // necessary to clean up the supplied initial relation hook state, while 16 // preserving the guarantees Juju makes about hook execution order. 17 func NewDyingHookSource(initial *State) hook.Source { 18 var list []hook.Info 19 20 // Honour any expected relation-changed hook. 21 if initial.ChangedPending != "" { 22 list = append(list, hook.Info{ 23 Kind: hooks.RelationChanged, 24 RelationId: initial.RelationId, 25 RemoteUnit: initial.ChangedPending, 26 ChangeVersion: initial.Members[initial.ChangedPending], 27 }) 28 } 29 30 // Depart in consistent order, mainly for testing purposes. 31 departs := []string{} 32 for name := range initial.Members { 33 departs = append(departs, name) 34 } 35 sort.Strings(departs) 36 for _, name := range departs { 37 list = append(list, hook.Info{ 38 Kind: hooks.RelationDeparted, 39 RelationId: initial.RelationId, 40 RemoteUnit: name, 41 ChangeVersion: initial.Members[name], 42 }) 43 } 44 45 // Finally break the relation. 46 list = append(list, hook.Info{ 47 Kind: hooks.RelationBroken, 48 RelationId: initial.RelationId, 49 }) 50 51 return hook.NewListSource(list) 52 }