github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/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  	"github.com/juju/juju/environs/config"
     8  	"github.com/juju/juju/state/api/base"
     9  	"github.com/juju/juju/state/api/params"
    10  	"github.com/juju/juju/state/api/watcher"
    11  )
    12  
    13  // EnvironWatcher provides common client-side API functions
    14  // to call into apiserver.common.EnvironWatcher.
    15  type EnvironWatcher struct {
    16  	facadeName string
    17  	caller     base.Caller
    18  }
    19  
    20  // NewEnvironWatcher creates a EnvironWatcher on the specified facade,
    21  // and uses this name when calling through the caller.
    22  func NewEnvironWatcher(facadeName string, caller base.Caller) *EnvironWatcher {
    23  	return &EnvironWatcher{facadeName, 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.facadeName, "", "WatchForEnvironConfigChanges", nil, &result)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	return watcher.NewNotifyWatcher(e.caller, result), nil
    35  }
    36  
    37  // EnvironConfig returns the current environment configuration.
    38  func (e *EnvironWatcher) EnvironConfig() (*config.Config, error) {
    39  	var result params.EnvironConfigResult
    40  	err := e.caller.Call(e.facadeName, "", "EnvironConfig", nil, &result)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	conf, err := config.New(config.NoDefaults, result.Config)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	return conf, nil
    49  }