github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/state/api/logger/logger.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package logger 5 6 import ( 7 "fmt" 8 9 "github.com/juju/juju/state/api/base" 10 "github.com/juju/juju/state/api/params" 11 "github.com/juju/juju/state/api/watcher" 12 ) 13 14 // State provides access to an logger worker's view of the state. 15 type State struct { 16 caller base.Caller 17 } 18 19 func (st *State) call(method string, params, result interface{}) error { 20 return st.caller.Call("Logger", "", method, params, result) 21 } 22 23 // NewState returns a version of the state that provides functionality 24 // required by the logger worker. 25 func NewState(caller base.Caller) *State { 26 return &State{caller} 27 } 28 29 // LoggingConfig returns the loggo configuration string for the agent 30 // specified by agentTag. 31 func (st *State) LoggingConfig(agentTag string) (string, error) { 32 var results params.StringResults 33 args := params.Entities{ 34 Entities: []params.Entity{{Tag: agentTag}}, 35 } 36 err := st.call("LoggingConfig", args, &results) 37 if err != nil { 38 // TODO: Not directly tested 39 return "", err 40 } 41 if len(results.Results) != 1 { 42 // TODO: Not directly tested 43 return "", fmt.Errorf("expected 1 result, got %d", len(results.Results)) 44 } 45 result := results.Results[0] 46 if err := result.Error; err != nil { 47 return "", err 48 } 49 return result.Result, nil 50 } 51 52 // WatchLoggingConfig returns a notify watcher that looks for changes in the 53 // logging-config for the agent specifed by agentTag. 54 func (st *State) WatchLoggingConfig(agentTag string) (watcher.NotifyWatcher, error) { 55 var results params.NotifyWatchResults 56 args := params.Entities{ 57 Entities: []params.Entity{{Tag: agentTag}}, 58 } 59 err := st.call("WatchLoggingConfig", args, &results) 60 if err != nil { 61 // TODO: Not directly tested 62 return nil, err 63 } 64 if len(results.Results) != 1 { 65 // TODO: Not directly tested 66 return nil, fmt.Errorf("expected 1 result, got %d", len(results.Results)) 67 } 68 result := results.Results[0] 69 if result.Error != nil { 70 // TODO: Not directly tested 71 return nil, result.Error 72 } 73 w := watcher.NewNotifyWatcher(st.caller, result) 74 return w, nil 75 }