launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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  	"launchpad.net/errgo/errors"
     8  	"launchpad.net/juju-core/state/api/base"
     9  	"launchpad.net/juju-core/state/api/params"
    10  	"launchpad.net/juju-core/state/api/watcher"
    11  )
    12  
    13  // State provides access to an logger worker's view of the state.
    14  type State struct {
    15  	caller base.Caller
    16  }
    17  
    18  // NewState returns a version of the state that provides functionality
    19  // required by the logger worker.
    20  func NewState(caller base.Caller) *State {
    21  	return &State{caller}
    22  }
    23  
    24  // LoggingConfig returns the loggo configuration string for the agent
    25  // specified by agentTag.
    26  func (st *State) LoggingConfig(agentTag string) (string, error) {
    27  	var results params.StringResults
    28  	args := params.Entities{
    29  		Entities: []params.Entity{{Tag: agentTag}},
    30  	}
    31  	err := st.caller.Call("Logger", "", "LoggingConfig", args, &results)
    32  	if err != nil {
    33  		// TODO: Not directly tested
    34  		return "", base.WrapError(err)
    35  	}
    36  	if len(results.Results) != 1 {
    37  		// TODO: Not directly tested
    38  		return "", errors.Newf("expected one result, got %d", len(results.Results))
    39  	}
    40  	result := results.Results[0]
    41  	if err := result.Error; err != nil {
    42  		return "", base.WrapError(err)
    43  	}
    44  	return result.Result, nil
    45  }
    46  
    47  // WatchLoggingConfig returns a notify watcher that looks for changes in the
    48  // logging-config for the agent specifed by agentTag.
    49  func (st *State) WatchLoggingConfig(agentTag string) (watcher.NotifyWatcher, error) {
    50  	var results params.NotifyWatchResults
    51  	args := params.Entities{
    52  		Entities: []params.Entity{{Tag: agentTag}},
    53  	}
    54  	err := st.caller.Call("Logger", "", "WatchLoggingConfig", args, &results)
    55  	if err != nil {
    56  		// TODO: Not directly tested
    57  		return nil, base.WrapError(err)
    58  	}
    59  	if len(results.Results) != 1 {
    60  		// TODO: Not directly tested
    61  		return nil, errors.Newf("expected one result, got %d", len(results.Results))
    62  	}
    63  	result := results.Results[0]
    64  	if result.Error != nil {
    65  		//  TODO: Not directly tested
    66  		return nil, result.Error
    67  	}
    68  	w := watcher.NewNotifyWatcher(st.caller, result)
    69  	return w, nil
    70  }