github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/worker/uniter/runner/action.go (about)

     1  // Copyright 2012-2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package runner
     5  
     6  import (
     7  	"github.com/juju/names"
     8  )
     9  
    10  // ActionData contains the tag, parameters, and results of an Action.
    11  type ActionData struct {
    12  	ActionName     string
    13  	ActionTag      names.ActionTag
    14  	ActionParams   map[string]interface{}
    15  	ActionFailed   bool
    16  	ResultsMessage string
    17  	ResultsMap     map[string]interface{}
    18  }
    19  
    20  // NewActionData builds a suitable ActionData struct with no nil members.
    21  // this should only be called in the event that an Action hook is being requested.
    22  func newActionData(name string, tag *names.ActionTag, params map[string]interface{}) *ActionData {
    23  	return &ActionData{
    24  		ActionName:   name,
    25  		ActionTag:    *tag,
    26  		ActionParams: params,
    27  		ResultsMap:   map[string]interface{}{},
    28  	}
    29  }
    30  
    31  // actionStatus messages define the possible states of a completed Action.
    32  const (
    33  	actionStatusInit   = "init"
    34  	actionStatusFailed = "fail"
    35  )
    36  
    37  // addValueToMap adds the given value to the map on which the method is run.
    38  // This allows us to merge maps such as {foo: {bar: baz}} and {foo: {baz: faz}}
    39  // into {foo: {bar: baz, baz: faz}}.
    40  func addValueToMap(keys []string, value string, target map[string]interface{}) {
    41  	next := target
    42  
    43  	for i := range keys {
    44  		// if we are on last key set the value.
    45  		// shouldn't be a problem.  overwrites existing vals.
    46  		if i == len(keys)-1 {
    47  			next[keys[i]] = value
    48  			break
    49  		}
    50  
    51  		if iface, ok := next[keys[i]]; ok {
    52  			switch typed := iface.(type) {
    53  			case map[string]interface{}:
    54  				// If we already had a map inside, keep
    55  				// stepping through.
    56  				next = typed
    57  			default:
    58  				// If we didn't, then overwrite value
    59  				// with a map and iterate with that.
    60  				m := map[string]interface{}{}
    61  				next[keys[i]] = m
    62  				next = m
    63  			}
    64  			continue
    65  		}
    66  
    67  		// Otherwise, it wasn't present, so make it and step
    68  		// into.
    69  		m := map[string]interface{}{}
    70  		next[keys[i]] = m
    71  		next = m
    72  	}
    73  }