github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/context/relation.go (about) 1 // Copyright 2012-2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package context 5 6 import ( 7 "fmt" 8 9 "github.com/juju/juju/api/uniter" 10 "github.com/juju/juju/apiserver/params" 11 "github.com/juju/juju/worker/uniter/runner/jujuc" 12 ) 13 14 type RelationInfo struct { 15 RelationUnit *uniter.RelationUnit 16 MemberNames []string 17 } 18 19 // ContextRelation is the implementation of jujuc.ContextRelation. 20 type ContextRelation struct { 21 ru *uniter.RelationUnit 22 relationId int 23 endpointName string 24 25 // settings allows read and write access to the relation unit settings. 26 settings *uniter.Settings 27 28 // cache holds remote unit membership and settings. 29 cache *RelationCache 30 } 31 32 // NewContextRelation creates a new context for the given relation unit. 33 // The unit-name keys of members supplies the initial membership. 34 func NewContextRelation(ru *uniter.RelationUnit, cache *RelationCache) *ContextRelation { 35 return &ContextRelation{ 36 ru: ru, 37 relationId: ru.Relation().Id(), 38 endpointName: ru.Endpoint().Name, 39 cache: cache, 40 } 41 } 42 43 func (ctx *ContextRelation) Id() int { 44 return ctx.relationId 45 } 46 47 func (ctx *ContextRelation) Name() string { 48 return ctx.endpointName 49 } 50 51 func (ctx *ContextRelation) FakeId() string { 52 return fmt.Sprintf("%s:%d", ctx.endpointName, ctx.relationId) 53 } 54 55 func (ctx *ContextRelation) UnitNames() []string { 56 return ctx.cache.MemberNames() 57 } 58 59 func (ctx *ContextRelation) ReadSettings(unit string) (settings params.Settings, err error) { 60 return ctx.cache.Settings(unit) 61 } 62 63 func (ctx *ContextRelation) Settings() (jujuc.Settings, error) { 64 if ctx.settings == nil { 65 node, err := ctx.ru.Settings() 66 if err != nil { 67 return nil, err 68 } 69 ctx.settings = node 70 } 71 return ctx.settings, nil 72 } 73 74 // WriteSettings persists all changes made to the unit's relation settings. 75 func (ctx *ContextRelation) WriteSettings() (err error) { 76 if ctx.settings != nil { 77 err = ctx.settings.Write() 78 } 79 return 80 }