1
// Copyright 2012, 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
// hook provides types that define the hooks known to the Uniter
10
"gopkg.in/juju/charm.v6-unstable/hooks"
11
"gopkg.in/juju/names.v2"
14
// TODO(fwereade): move these definitions to juju/charm/hooks.
16
LeaderElected hooks.Kind = "leader-elected"
17
LeaderDeposed hooks.Kind = "leader-deposed"
18
LeaderSettingsChanged hooks.Kind = "leader-settings-changed"
21
// Info holds details required to execute a hook. Not all fields are
22
// relevant to all Kind values.
24
Kind hooks.Kind `yaml:"kind"`
26
// RelationId identifies the relation associated with the hook. It is
27
// only set when Kind indicates a relation hook.
28
RelationId int `yaml:"relation-id,omitempty"`
30
// RemoteUnit is the name of the unit that triggered the hook. It is only
31
// set when Kind indicates a relation hook other than relation-broken.
32
RemoteUnit string `yaml:"remote-unit,omitempty"`
34
// ChangeVersion identifies the most recent unit settings change
35
// associated with RemoteUnit. It is only set when RemoteUnit is set.
36
ChangeVersion int64 `yaml:"change-version,omitempty"`
38
// StorageId is the ID of the storage instance relevant to the hook.
39
StorageId string `yaml:"storage-id,omitempty"`
42
// Validate returns an error if the info is not valid.
43
func (hi Info) Validate() error {
45
case hooks.RelationJoined, hooks.RelationChanged, hooks.RelationDeparted:
46
if hi.RemoteUnit == "" {
47
return fmt.Errorf("%q hook requires a remote unit", hi.Kind)
50
case hooks.Install, hooks.Start, hooks.ConfigChanged, hooks.UpgradeCharm, hooks.Stop, hooks.RelationBroken,
51
hooks.CollectMetrics, hooks.MeterStatusChanged, hooks.UpdateStatus:
54
return fmt.Errorf("hooks.Kind Action is deprecated")
55
case hooks.StorageAttached, hooks.StorageDetaching:
56
if !names.IsValidStorage(hi.StorageId) {
57
return fmt.Errorf("invalid storage ID %q", hi.StorageId)
60
// TODO(fwereade): define these in charm/hooks...
61
case LeaderElected, LeaderDeposed, LeaderSettingsChanged:
64
return fmt.Errorf("unknown hook kind %q", hi.Kind)
67
// Committer is an interface that may be used to convey the fact that the
68
// specified hook has been successfully executed, and committed.
69
type Committer interface {
70
CommitHook(Info) error
73
// Validator is an interface that may be used to validate a hook execution
74
// request prior to executing it.
75
type Validator interface {
76
ValidateHook(Info) error