github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/pubsub/centralhub/centralhub.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package centralhub 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/loggo" 9 "github.com/juju/pubsub" 10 "github.com/juju/utils" 11 "gopkg.in/juju/names.v2" 12 "gopkg.in/yaml.v2" 13 ) 14 15 // New returns a new structured hub using yaml marshalling with an origin 16 // specified. The post processing ensures that the maps all have string keys 17 // so they messages can be marshalled between apiservers. 18 func New(origin names.MachineTag) *pubsub.StructuredHub { 19 20 return pubsub.NewStructuredHub( 21 &pubsub.StructuredHubConfig{ 22 Logger: loggo.GetLogger("juju.centralhub"), 23 Marshaller: &yamlMarshaller{}, 24 Annotations: map[string]interface{}{ 25 "origin": origin.String(), 26 }, 27 PostProcess: ensureStringMaps, 28 }) 29 } 30 31 type yamlMarshaller struct{} 32 33 // Marshal implements Marshaller. 34 func (*yamlMarshaller) Marshal(v interface{}) ([]byte, error) { 35 return yaml.Marshal(v) 36 } 37 38 // Unmarshal implements Marshaller. 39 func (*yamlMarshaller) Unmarshal(data []byte, v interface{}) error { 40 return yaml.Unmarshal(data, v) 41 } 42 43 func ensureStringMaps(in map[string]interface{}) (map[string]interface{}, error) { 44 out, err := utils.ConformYAML(in) 45 if err != nil { 46 return nil, errors.Trace(err) 47 } 48 return out.(map[string]interface{}), nil 49 }