launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/state/api/common/environwatcher.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common 5 6 import ( 7 "launchpad.net/juju-core/environs/config" 8 "launchpad.net/juju-core/state/api/base" 9 "launchpad.net/juju-core/state/api/params" 10 "launchpad.net/juju-core/state/api/watcher" 11 ) 12 13 // EnvironWatcher provides common client side api functions 14 // to call into the apiserver.common.EnvironWatcher. 15 type EnvironWatcher struct { 16 façadeName string 17 caller base.Caller 18 } 19 20 // NewEnvironWatcher creates a EnvironWatcher on the specified façade, 21 // and uses this name when calling through the caller. 22 func NewEnvironWatcher(façadeName string, caller base.Caller) *EnvironWatcher { 23 return &EnvironWatcher{façadeName, caller} 24 } 25 26 // WatchForEnvironConfigChanges return a NotifyWatcher waiting for the 27 // environment configuration to change. 28 func (e *EnvironWatcher) WatchForEnvironConfigChanges() (watcher.NotifyWatcher, error) { 29 var result params.NotifyWatchResult 30 err := e.caller.Call(e.façadeName, "", "WatchForEnvironConfigChanges", nil, &result) 31 if err != nil { 32 return nil, base.WrapError(err) 33 } 34 if err := result.Error; err != nil { 35 return nil, result.Error 36 } 37 w := watcher.NewNotifyWatcher(e.caller, result) 38 return w, nil 39 } 40 41 // EnvironConfig returns the current environment configuration. 42 func (e *EnvironWatcher) EnvironConfig() (*config.Config, error) { 43 var result params.EnvironConfigResult 44 err := e.caller.Call(e.façadeName, "", "EnvironConfig", nil, &result) 45 if err != nil { 46 return nil, base.WrapError(err) 47 } 48 if err := result.Error; err != nil { 49 return nil, base.WrapError(err) 50 } 51 conf, err := config.New(config.NoDefaults, result.Config) 52 if err != nil { 53 return nil, base.WrapError(err) 54 } 55 return conf, nil 56 }