github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/api/common/modelwatcher.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/api/base"
     8  	apiwatcher "github.com/juju/juju/api/watcher"
     9  	"github.com/juju/juju/apiserver/params"
    10  	"github.com/juju/juju/environs/config"
    11  	"github.com/juju/juju/logfwd/syslog"
    12  	"github.com/juju/juju/watcher"
    13  )
    14  
    15  // ModelWatcher provides common client-side API functions
    16  // to call into apiserver.common.ModelWatcher.
    17  type ModelWatcher struct {
    18  	facade base.FacadeCaller
    19  }
    20  
    21  // NewModelWatcher creates a ModelWatcher on the specified facade,
    22  // and uses this name when calling through the caller.
    23  func NewModelWatcher(facade base.FacadeCaller) *ModelWatcher {
    24  	return &ModelWatcher{facade}
    25  }
    26  
    27  // WatchForModelConfigChanges return a NotifyWatcher waiting for the
    28  // model configuration to change.
    29  func (e *ModelWatcher) WatchForModelConfigChanges() (watcher.NotifyWatcher, error) {
    30  	var result params.NotifyWatchResult
    31  	err := e.facade.FacadeCall("WatchForModelConfigChanges", nil, &result)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	return apiwatcher.NewNotifyWatcher(e.facade.RawAPICaller(), result), nil
    36  }
    37  
    38  // ModelConfig returns the current model configuration.
    39  func (e *ModelWatcher) ModelConfig() (*config.Config, error) {
    40  	var result params.ModelConfigResult
    41  	err := e.facade.FacadeCall("ModelConfig", nil, &result)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	conf, err := config.New(config.NoDefaults, result.Config)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	return conf, nil
    50  }
    51  
    52  // WatchForLogForwardConfigChanges return a NotifyWatcher waiting for the
    53  // log forward syslog configuration to change.
    54  func (e *ModelWatcher) WatchForLogForwardConfigChanges() (watcher.NotifyWatcher, error) {
    55  	// TODO(wallyworld) - lp:1602237 - this needs to have it's own backend implementation.
    56  	// For now, we'll piggyback off the ModelConfig API.
    57  	return e.WatchForModelConfigChanges()
    58  }
    59  
    60  // LogForwardConfig returns the current log forward syslog configuration.
    61  func (e *ModelWatcher) LogForwardConfig() (*syslog.RawConfig, bool, error) {
    62  	// TODO(wallyworld) - lp:1602237 - this needs to have it's own backend implementation.
    63  	// For now, we'll piggyback off the ModelConfig API.
    64  	modelConfig, err := e.ModelConfig()
    65  	if err != nil {
    66  		return nil, false, err
    67  	}
    68  	cfg, ok := modelConfig.LogFwdSyslog()
    69  	return cfg, ok, nil
    70  }