github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/controller/logfwd/lastsent.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package logfwd
     5  
     6  import (
     7  	"io"
     8  
     9  	"github.com/juju/errors"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	"github.com/juju/juju/apiserver/common"
    13  	"github.com/juju/juju/apiserver/facade"
    14  	"github.com/juju/juju/apiserver/params"
    15  	"github.com/juju/juju/state"
    16  )
    17  
    18  // NewFacade creates a new LogForwardingAPI. It is used for API registration.
    19  func NewFacade(st *state.State, _ facade.Resources, auth facade.Authorizer) (*LogForwardingAPI, error) {
    20  	return NewLogForwardingAPI(&stateAdapter{st}, auth)
    21  }
    22  
    23  // LastSentTracker exposes the functionality of state.LastSentTracker.
    24  type LastSentTracker interface {
    25  	io.Closer
    26  
    27  	// Get retrieves the record ID and timestamp.
    28  	Get() (recID int64, recTimestamp int64, err error)
    29  
    30  	// Set records the record ID and timestamp.
    31  	Set(recID int64, recTimestamp int64) error
    32  }
    33  
    34  // LogForwardingState supports interacting with state for the
    35  // LogForwarding facade.
    36  type LogForwardingState interface {
    37  	// NewLastSentTracker creates a new tracker for the given model
    38  	// and log sink.
    39  	NewLastSentTracker(tag names.ModelTag, sink string) LastSentTracker
    40  }
    41  
    42  // LogForwardingAPI is the concrete implementation of the api end point.
    43  type LogForwardingAPI struct {
    44  	state LogForwardingState
    45  }
    46  
    47  // NewLogForwardingAPI creates a new server-side logger API end point.
    48  func NewLogForwardingAPI(st LogForwardingState, auth facade.Authorizer) (*LogForwardingAPI, error) {
    49  	if !auth.AuthController() {
    50  		return nil, common.ErrPerm
    51  	}
    52  	api := &LogForwardingAPI{
    53  		state: st,
    54  	}
    55  	return api, nil
    56  }
    57  
    58  // GetLastSent is a bulk call that gets the log forwarding "last sent"
    59  // record ID for each requested target.
    60  func (api *LogForwardingAPI) GetLastSent(args params.LogForwardingGetLastSentParams) params.LogForwardingGetLastSentResults {
    61  	results := make([]params.LogForwardingGetLastSentResult, len(args.IDs))
    62  	for i, id := range args.IDs {
    63  		results[i] = api.get(id)
    64  	}
    65  	return params.LogForwardingGetLastSentResults{
    66  		Results: results,
    67  	}
    68  }
    69  
    70  func (api *LogForwardingAPI) get(id params.LogForwardingID) params.LogForwardingGetLastSentResult {
    71  	var res params.LogForwardingGetLastSentResult
    72  	lst, err := api.newLastSentTracker(id)
    73  	if err != nil {
    74  		res.Error = common.ServerError(err)
    75  		return res
    76  	}
    77  	defer lst.Close()
    78  
    79  	recID, recTimestamp, err := lst.Get()
    80  	if err != nil {
    81  		res.Error = common.ServerError(err)
    82  		if errors.Cause(err) == state.ErrNeverForwarded {
    83  			res.Error.Code = params.CodeNotFound
    84  		}
    85  		return res
    86  	}
    87  	res.RecordID = recID
    88  	res.RecordTimestamp = recTimestamp
    89  	return res
    90  }
    91  
    92  // SetLastSent is a bulk call that sets the log forwarding "last sent"
    93  // record ID for each requested target.
    94  func (api *LogForwardingAPI) SetLastSent(args params.LogForwardingSetLastSentParams) params.ErrorResults {
    95  	results := make([]params.ErrorResult, len(args.Params), len(args.Params))
    96  	for i, arg := range args.Params {
    97  		results[i].Error = api.set(arg)
    98  	}
    99  	return params.ErrorResults{
   100  		Results: results,
   101  	}
   102  }
   103  
   104  func (api *LogForwardingAPI) set(arg params.LogForwardingSetLastSentParam) *params.Error {
   105  	lst, err := api.newLastSentTracker(arg.LogForwardingID)
   106  	if err != nil {
   107  		return common.ServerError(err)
   108  	}
   109  	defer lst.Close()
   110  
   111  	err = lst.Set(arg.RecordID, arg.RecordTimestamp)
   112  	return common.ServerError(err)
   113  }
   114  
   115  func (api *LogForwardingAPI) newLastSentTracker(id params.LogForwardingID) (LastSentTracker, error) {
   116  	tag, err := names.ParseModelTag(id.ModelTag)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	tracker := api.state.NewLastSentTracker(tag, id.Sink)
   121  	return tracker, nil
   122  }
   123  
   124  type stateAdapter struct {
   125  	*state.State
   126  }
   127  
   128  // NewLastSentTracker implements LogForwardingState.
   129  func (st stateAdapter) NewLastSentTracker(tag names.ModelTag, sink string) LastSentTracker {
   130  	return state.NewLastSentLogTracker(st, tag.Id(), sink)
   131  }