github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/api/agent/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/rpc/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 relationTag string 16 unitTag string 17 settings params.Settings 18 dirty bool 19 } 20 21 func newSettings(relationTag, unitTag string, settings params.Settings) *Settings { 22 if settings == nil { 23 settings = make(params.Settings) 24 } 25 return &Settings{ 26 relationTag: relationTag, 27 unitTag: unitTag, 28 settings: settings, 29 } 30 } 31 32 // Map returns all keys and values of the node. 33 func (s *Settings) Map() params.Settings { 34 settingsCopy := make(params.Settings) 35 for k, v := range s.settings { 36 if v != "" { 37 // Skip deleted keys. 38 settingsCopy[k] = v 39 } 40 } 41 return settingsCopy 42 } 43 44 // Set sets key to value. 45 func (s *Settings) Set(key, value string) { 46 s.settings[key] = value 47 s.dirty = true 48 } 49 50 // Delete removes key. 51 func (s *Settings) Delete(key string) { 52 // Keys are only marked as deleted, because we need to report them 53 // back to the server for deletion on Write(). 54 s.settings[key] = "" 55 s.dirty = true 56 } 57 58 // FinalResult returns a params.Settings with the final updates applied. 59 // This includes entries that were deleted. 60 func (s *Settings) FinalResult() params.Settings { 61 // First make a copy of the map, including deleted keys. 62 settingsCopy := make(params.Settings) 63 for k, v := range s.settings { 64 settingsCopy[k] = v 65 } 66 return settingsCopy 67 } 68 69 func (s *Settings) IsDirty() bool { 70 return s.dirty 71 }