github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/apiserver/common/environwatcher.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 // EnvironWatcher implements two common methods for use by various 14 // facades - WatchForEnvironConfigChanges and EnvironConfig. 15 type EnvironWatcher struct { 16 st state.EnvironAccessor 17 resources *Resources 18 authorizer Authorizer 19 } 20 21 // NewEnvironWatcher returns a new EnvironWatcher. 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 NewEnvironWatcher(st state.EnvironAccessor, resources *Resources, authorizer Authorizer) *EnvironWatcher { 28 return &EnvironWatcher{ 29 st: st, 30 resources: resources, 31 authorizer: authorizer, 32 } 33 } 34 35 // WatchForEnvironConfigChanges 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 *EnvironWatcher) WatchForEnvironConfigChanges() (params.NotifyWatchResult, error) { 41 result := params.NotifyWatchResult{} 42 watch := e.st.WatchForEnvironConfigChanges() 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 // EnvironConfig returns the current environment's configuration. 56 func (e *EnvironWatcher) EnvironConfig() (params.EnvironConfigResult, error) { 57 result := params.EnvironConfigResult{} 58 59 config, err := e.st.EnvironConfig() 60 if err != nil { 61 return result, err 62 } 63 allAttrs := config.AllAttrs() 64 65 if !e.authorizer.AuthEnvironManager() { 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 env, err := environs.New(config) 74 if err != nil { 75 return result, err 76 } 77 secretAttrs, err := env.Provider().SecretAttrs(config) 78 for k := range secretAttrs { 79 allAttrs[k] = "not available" 80 } 81 } 82 result.Config = allAttrs 83 return result, nil 84 }