github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/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 "time" 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/params" 14 ) 15 16 // LastSentID is the data that identifies a log forwarding 17 // "last sent" value. The controller has a mapping from a set of IDs 18 // to a record ID (for each ID). The record ID corresponds to the last 19 // log record that the specific log forwarding machinery sent to the 20 // identified "sink" (for a given model). 21 type LastSentID struct { 22 // ModelTag identifies the model associated with the log record. 23 Model names.ModelTag 24 25 // Sink is the name of the log forwarding target to which a log 26 // record was last sent. 27 Sink string 28 } 29 30 // LastSentInfo holds the info about a "last sent" value. 31 type LastSentInfo struct { 32 LastSentID 33 34 // RecordID identifies the last log record that was forwarded 35 // for a given model and sink. 36 RecordID int64 37 38 // RecordTimestamp identifies the last log record that was forwarded 39 // for a given model and sink. 40 RecordTimestamp time.Time 41 } 42 43 // LastSentResult holds a single result from a bulk API call. 44 type LastSentResult struct { 45 LastSentInfo 46 47 // Error holds the error, if any, that resulted while handling the 48 // request for the ID. 49 Error error 50 } 51 52 // FacadeCaller provides the functionality to call methods on a facade. 53 type FacadeCaller interface { 54 // FacadeCall is the same method as on base.FacadeCaller. 55 FacadeCall(request string, params, response interface{}) error 56 } 57 58 // LastSentClient exposes the "last sent" methods of the LogForwarding 59 // API facade. 60 type LastSentClient struct { 61 caller FacadeCaller 62 } 63 64 // NewLastSentClient creates a new API client for the facade. 65 func NewLastSentClient(newFacadeCaller func(string) FacadeCaller) *LastSentClient { 66 return &LastSentClient{ 67 caller: newFacadeCaller("LogForwarding"), 68 } 69 } 70 71 // GetLastSent makes a "GetLastSent" call on the facade and returns the 72 // results in the same order. 73 func (c LastSentClient) GetLastSent(ids []LastSentID) ([]LastSentResult, error) { 74 var args params.LogForwardingGetLastSentParams 75 args.IDs = make([]params.LogForwardingID, len(ids)) 76 for i, id := range ids { 77 args.IDs[i] = params.LogForwardingID{ 78 ModelTag: id.Model.String(), 79 Sink: id.Sink, 80 } 81 } 82 83 var apiResults params.LogForwardingGetLastSentResults 84 err := c.caller.FacadeCall("GetLastSent", args, &apiResults) 85 if err != nil { 86 return nil, errors.Trace(err) 87 } 88 89 results := make([]LastSentResult, len(ids)) 90 for i, apiRes := range apiResults.Results { 91 results[i] = LastSentResult{ 92 LastSentInfo: LastSentInfo{ 93 LastSentID: ids[i], 94 RecordID: apiRes.RecordID, 95 }, 96 Error: common.RestoreError(apiRes.Error), 97 } 98 if apiRes.RecordTimestamp > 0 { 99 results[i].RecordTimestamp = time.Unix(0, apiRes.RecordTimestamp) 100 } 101 } 102 return results, nil 103 } 104 105 // SetLastSent makes a "SetLastSent" call on the facade and returns the 106 // results in the same order. 107 func (c LastSentClient) SetLastSent(reqs []LastSentInfo) ([]LastSentResult, error) { 108 var args params.LogForwardingSetLastSentParams 109 args.Params = make([]params.LogForwardingSetLastSentParam, len(reqs)) 110 for i, req := range reqs { 111 args.Params[i] = params.LogForwardingSetLastSentParam{ 112 LogForwardingID: params.LogForwardingID{ 113 ModelTag: req.Model.String(), 114 Sink: req.Sink, 115 }, 116 RecordID: req.RecordID, 117 RecordTimestamp: req.RecordTimestamp.UnixNano(), 118 } 119 } 120 121 var apiResults params.ErrorResults 122 err := c.caller.FacadeCall("SetLastSent", args, &apiResults) 123 if err != nil { 124 return nil, errors.Trace(err) 125 } 126 127 results := make([]LastSentResult, len(reqs)) 128 for i, apiRes := range apiResults.Results { 129 results[i] = LastSentResult{ 130 LastSentInfo: reqs[i], 131 Error: common.RestoreError(apiRes.Error), 132 } 133 } 134 return results, nil 135 }