github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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/params" 8 "github.com/juju/juju/environs" 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 *Resources 18 authorizer 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, environment tags are not used, so both created AuthFuncs 26 // are called with "" for tag, which means "the current environment". 27 func NewModelWatcher(st state.ModelAccessor, resources *Resources, authorizer 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 environment 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 (e *ModelWatcher) WatchForModelConfigChanges() (params.NotifyWatchResult, error) { 41 result := params.NotifyWatchResult{} 42 watch := e.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 = e.resources.Register(watch) 49 } else { 50 return result, watcher.EnsureErr(watch) 51 } 52 return result, nil 53 } 54 55 // ModelConfig returns the current environment's configuration. 56 func (e *ModelWatcher) ModelConfig() (params.ModelConfigResult, error) { 57 result := params.ModelConfigResult{} 58 59 config, err := e.st.ModelConfig() 60 if err != nil { 61 return result, err 62 } 63 allAttrs := config.AllAttrs() 64 65 if !e.authorizer.AuthModelManager() { 66 // Mask out any secrets in the environment configuration 67 // with values of the same type, so it'll pass validation. 68 // 69 // TODO(dimitern) 201309-26 bug #1231384 70 // Delete the code below and mark the bug as fixed, 71 // once it's live tested on MAAS and 1.16 compatibility 72 // is dropped. 73 provider, err := environs.Provider(config.Type()) 74 if err != nil { 75 return result, err 76 } 77 secretAttrs, err := provider.SecretAttrs(config) 78 for k := range secretAttrs { 79 allAttrs[k] = "not available" 80 } 81 } 82 result.Config = allAttrs 83 return result, nil 84 }