github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/common/modelwatcher.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common 5 6 import ( 7 "github.com/juju/juju/apiserver/facade" 8 "github.com/juju/juju/apiserver/params" 9 "github.com/juju/juju/state" 10 "github.com/juju/juju/state/watcher" 11 ) 12 13 // ModelWatcher implements two common methods for use by various 14 // facades - WatchForModelConfigChanges and ModelConfig. 15 type ModelWatcher struct { 16 st state.ModelAccessor 17 resources facade.Resources 18 authorizer facade.Authorizer 19 } 20 21 // NewModelWatcher returns a new ModelWatcher. Active watchers 22 // will be stored in the provided Resources. The two GetAuthFunc 23 // callbacks will be used on each invocation of the methods to 24 // determine current permissions. 25 // Right now, model tags are not used, so both created AuthFuncs 26 // are called with "" for tag, which means "the current model". 27 func NewModelWatcher(st state.ModelAccessor, resources facade.Resources, authorizer facade.Authorizer) *ModelWatcher { 28 return &ModelWatcher{ 29 st: st, 30 resources: resources, 31 authorizer: authorizer, 32 } 33 } 34 35 // WatchForModelConfigChanges returns a NotifyWatcher that observes 36 // changes to the model configuration. 37 // Note that although the NotifyWatchResult contains an Error field, 38 // it's not used because we are only returning a single watcher, 39 // so we use the regular error return. 40 func (m *ModelWatcher) WatchForModelConfigChanges() (params.NotifyWatchResult, error) { 41 result := params.NotifyWatchResult{} 42 watch := m.st.WatchForModelConfigChanges() 43 // Consume the initial event. Technically, API 44 // calls to Watch 'transmit' the initial event 45 // in the Watch response. But NotifyWatchers 46 // have no state to transmit. 47 if _, ok := <-watch.Changes(); ok { 48 result.NotifyWatcherId = m.resources.Register(watch) 49 } else { 50 return result, watcher.EnsureErr(watch) 51 } 52 return result, nil 53 } 54 55 // ModelConfig returns the current model's configuration. 56 func (m *ModelWatcher) ModelConfig() (params.ModelConfigResult, error) { 57 result := params.ModelConfigResult{} 58 config, err := m.st.ModelConfig() 59 if err != nil { 60 return result, err 61 } 62 result.Config = config.AllAttrs() 63 return result, nil 64 }