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