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