github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/core/relation"
    12  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    13  )
    14  
    15  type RelationInfo struct {
    16  	RelationUnit *uniter.RelationUnit
    17  	MemberNames  []string
    18  }
    19  
    20  // ContextRelation is the implementation of hooks.ContextRelation.
    21  type ContextRelation struct {
    22  	ru           *uniter.RelationUnit
    23  	relationId   int
    24  	endpointName string
    25  
    26  	// settings allows read and write access to the relation unit settings.
    27  	settings *uniter.Settings
    28  
    29  	// cache holds remote unit membership and settings.
    30  	cache *RelationCache
    31  }
    32  
    33  // NewContextRelation creates a new context for the given relation unit.
    34  // The unit-name keys of members supplies the initial membership.
    35  func NewContextRelation(ru *uniter.RelationUnit, cache *RelationCache) *ContextRelation {
    36  	return &ContextRelation{
    37  		ru:           ru,
    38  		relationId:   ru.Relation().Id(),
    39  		endpointName: ru.Endpoint().Name,
    40  		cache:        cache,
    41  	}
    42  }
    43  
    44  func (ctx *ContextRelation) Id() int {
    45  	return ctx.relationId
    46  }
    47  
    48  func (ctx *ContextRelation) Name() string {
    49  	return ctx.endpointName
    50  }
    51  
    52  func (ctx *ContextRelation) FakeId() string {
    53  	return fmt.Sprintf("%s:%d", ctx.endpointName, ctx.relationId)
    54  }
    55  
    56  func (ctx *ContextRelation) UnitNames() []string {
    57  	return ctx.cache.MemberNames()
    58  }
    59  
    60  func (ctx *ContextRelation) ReadSettings(unit string) (settings params.Settings, err error) {
    61  	return ctx.cache.Settings(unit)
    62  }
    63  
    64  func (ctx *ContextRelation) Settings() (jujuc.Settings, error) {
    65  	if ctx.settings == nil {
    66  		node, err := ctx.ru.Settings()
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  		ctx.settings = node
    71  	}
    72  	return ctx.settings, nil
    73  }
    74  
    75  // WriteSettings persists all changes made to the unit's relation settings.
    76  func (ctx *ContextRelation) WriteSettings() (err error) {
    77  	if ctx.settings != nil {
    78  		err = ctx.settings.Write()
    79  	}
    80  	return
    81  }
    82  
    83  // Suspended returns true if the relation is suspended.
    84  func (ctx *ContextRelation) Suspended() bool {
    85  	return ctx.ru.Relation().Suspended()
    86  }
    87  
    88  // SetStatus sets the relation's status.
    89  func (ctx *ContextRelation) SetStatus(status relation.Status) error {
    90  	return ctx.ru.Relation().SetStatus(status)
    91  }