~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/uniter/hook/hook.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        "gopkg.in/juju/charm.v6-unstable/hooks"
 
11
        "gopkg.in/juju/names.v2"
 
12
)
 
13
 
 
14
// TODO(fwereade): move these definitions to juju/charm/hooks.
 
15
const (
 
16
        LeaderElected         hooks.Kind = "leader-elected"
 
17
        LeaderDeposed         hooks.Kind = "leader-deposed"
 
18
        LeaderSettingsChanged hooks.Kind = "leader-settings-changed"
 
19
)
 
20
 
 
21
// Info holds details required to execute a hook. Not all fields are
 
22
// relevant to all Kind values.
 
23
type Info struct {
 
24
        Kind hooks.Kind `yaml:"kind"`
 
25
 
 
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"`
 
29
 
 
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"`
 
33
 
 
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"`
 
37
 
 
38
        // StorageId is the ID of the storage instance relevant to the hook.
 
39
        StorageId string `yaml:"storage-id,omitempty"`
 
40
}
 
41
 
 
42
// Validate returns an error if the info is not valid.
 
43
func (hi Info) Validate() error {
 
44
        switch hi.Kind {
 
45
        case hooks.RelationJoined, hooks.RelationChanged, hooks.RelationDeparted:
 
46
                if hi.RemoteUnit == "" {
 
47
                        return fmt.Errorf("%q hook requires a remote unit", hi.Kind)
 
48
                }
 
49
                fallthrough
 
50
        case hooks.Install, hooks.Start, hooks.ConfigChanged, hooks.UpgradeCharm, hooks.Stop, hooks.RelationBroken,
 
51
                hooks.CollectMetrics, hooks.MeterStatusChanged, hooks.UpdateStatus:
 
52
                return nil
 
53
        case hooks.Action:
 
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)
 
58
                }
 
59
                return nil
 
60
        // TODO(fwereade): define these in charm/hooks...
 
61
        case LeaderElected, LeaderDeposed, LeaderSettingsChanged:
 
62
                return nil
 
63
        }
 
64
        return fmt.Errorf("unknown hook kind %q", hi.Kind)
 
65
}
 
66
 
 
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
 
71
}
 
72
 
 
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
 
77
}