github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/uniter/settings.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter
     5  
     6  import (
     7  	"github.com/juju/juju/apiserver/params"
     8  )
     9  
    10  // This module implements a subset of the interface provided by
    11  // state.Settings, as needed by the uniter API.
    12  
    13  // Settings manages changes to unit settings in a relation.
    14  type Settings struct {
    15  	st          *State
    16  	relationTag string
    17  	unitTag     string
    18  	settings    params.Settings
    19  }
    20  
    21  func newSettings(st *State, relationTag, unitTag string, settings params.Settings) *Settings {
    22  	if settings == nil {
    23  		settings = make(params.Settings)
    24  	}
    25  	return &Settings{
    26  		st:          st,
    27  		relationTag: relationTag,
    28  		unitTag:     unitTag,
    29  		settings:    settings,
    30  	}
    31  }
    32  
    33  // Map returns all keys and values of the node.
    34  //
    35  // TODO(dimitern): This differes from state.Settings.Map() - it does
    36  // not return map[string]interface{}, but since all values are
    37  // expected to be strings anyway, we need to fix the uniter code
    38  // accordingly when migrating to the API.
    39  func (s *Settings) Map() params.Settings {
    40  	settingsCopy := make(params.Settings)
    41  	for k, v := range s.settings {
    42  		if v != "" {
    43  			// Skip deleted keys.
    44  			settingsCopy[k] = v
    45  		}
    46  	}
    47  	return settingsCopy
    48  }
    49  
    50  // Set sets key to value.
    51  //
    52  // TODO(dimitern): value must be a string. Change the code that uses
    53  // this accordingly.
    54  func (s *Settings) Set(key, value string) {
    55  	s.settings[key] = value
    56  }
    57  
    58  // Delete removes key.
    59  func (s *Settings) Delete(key string) {
    60  	// Keys are only marked as deleted, because we need to report them
    61  	// back to the server for deletion on Write().
    62  	s.settings[key] = ""
    63  }
    64  
    65  // Write writes changes made to s back onto its node. Keys set to
    66  // empty values will be deleted, others will be updated to the new
    67  // value.
    68  //
    69  // TODO(dimitern): 2013-09-06 bug 1221798
    70  // Once the machine addressability changes lands, we may need to
    71  // revise the logic here to take into account that the
    72  // "private-address" setting for a unit can be changed outside of the
    73  // uniter's control. So we may need to send diffs of what has changed
    74  // to make sure we update the address (and other settings) correctly,
    75  // without overwritting.
    76  func (s *Settings) Write() error {
    77  	// First make a copy of the map, including deleted keys.
    78  	settingsCopy := make(params.Settings)
    79  	for k, v := range s.settings {
    80  		settingsCopy[k] = v
    81  	}
    82  
    83  	var result params.ErrorResults
    84  	args := params.RelationUnitsSettings{
    85  		RelationUnits: []params.RelationUnitSettings{{
    86  			Relation: s.relationTag,
    87  			Unit:     s.unitTag,
    88  			Settings: settingsCopy,
    89  		}},
    90  	}
    91  	err := s.st.facade.FacadeCall("UpdateSettings", args, &result)
    92  	if err != nil {
    93  		return err
    94  	}
    95  	return result.OneError()
    96  }