github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/worker/uniter/hook/hook.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 // hook provides types that define the hooks known to the Uniter 5 package hook 6 7 import ( 8 "fmt" 9 10 "github.com/juju/juju/charm/hooks" 11 ) 12 13 // Info holds details required to execute a hook. Not all fields are 14 // relevant to all Kind values. 15 type Info struct { 16 Kind hooks.Kind 17 18 // RelationId identifies the relation associated with the hook. It is 19 // only set when Kind indicates a relation hook. 20 RelationId int `yaml:"relation-id,omitempty"` 21 22 // RemoteUnit is the name of the unit that triggered the hook. It is only 23 // set when Kind inicates a relation hook other than relation-broken. 24 RemoteUnit string `yaml:"remote-unit,omitempty"` 25 26 // ChangeVersion identifies the most recent unit settings change 27 // associated with RemoteUnit. It is only set when RemoteUnit is set. 28 ChangeVersion int64 `yaml:"change-version,omitempty"` 29 } 30 31 // Validate returns an error if the info is not valid. 32 func (hi Info) Validate() error { 33 switch hi.Kind { 34 case hooks.RelationJoined, hooks.RelationChanged, hooks.RelationDeparted: 35 if hi.RemoteUnit == "" { 36 return fmt.Errorf("%q hook requires a remote unit", hi.Kind) 37 } 38 fallthrough 39 case hooks.Install, hooks.Start, hooks.ConfigChanged, hooks.UpgradeCharm, hooks.Stop, hooks.RelationBroken: 40 return nil 41 } 42 return fmt.Errorf("unknown hook kind %q", hi.Kind) 43 }