github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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/utils/featureflag"
    11  
    12  	"github.com/juju/juju/storage"
    13  	"github.com/juju/names"
    14  	"gopkg.in/juju/charm.v4/hooks"
    15  )
    16  
    17  // Info holds details required to execute a hook. Not all fields are
    18  // relevant to all Kind values.
    19  type Info struct {
    20  	Kind hooks.Kind `yaml:"kind"`
    21  
    22  	// RelationId identifies the relation associated with the hook. It is
    23  	// only set when Kind indicates a relation hook.
    24  	RelationId int `yaml:"relation-id,omitempty"`
    25  
    26  	// RemoteUnit is the name of the unit that triggered the hook. It is only
    27  	// set when Kind inicates a relation hook other than relation-broken.
    28  	RemoteUnit string `yaml:"remote-unit,omitempty"`
    29  
    30  	// ChangeVersion identifies the most recent unit settings change
    31  	// associated with RemoteUnit. It is only set when RemoteUnit is set.
    32  	ChangeVersion int64 `yaml:"change-version,omitempty"`
    33  
    34  	// ActionId is the state State.actions ID of the Action document to
    35  	// be retrieved by RunHook.
    36  	ActionId string `yaml:"action-id,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, hooks.CollectMetrics, hooks.MeterStatusChanged:
    51  		return nil
    52  	case hooks.Action:
    53  		if !names.IsValidAction(hi.ActionId) {
    54  			return fmt.Errorf("action id %q cannot be parsed as an action tag", hi.ActionId)
    55  		}
    56  		return nil
    57  	case hooks.StorageAttached, hooks.StorageDetached:
    58  		// TODO: stop checking feature flag once storage has graduated.
    59  		if featureflag.Enabled(storage.FeatureFlag) {
    60  			return nil
    61  		}
    62  	}
    63  	return fmt.Errorf("unknown hook kind %q", hi.Kind)
    64  }