|
| 1 | +package splunk |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// EventCollectorResponse is the payload returned by the HTTP Event Collector |
| 11 | +// in response to requests. |
| 12 | +// https://docs.splunk.com/Documentation/Splunk/latest/RESTREF/RESTinput#services.2Fcollector |
| 13 | +type EventCollectorResponse struct { |
| 14 | + Text string `json:"text"` |
| 15 | + Code StatusCode `json:"code"` |
| 16 | + InvalidEventNumber *int `json:"invalid-event-number"` |
| 17 | + AckID *int `json:"ackId"` |
| 18 | +} |
| 19 | + |
| 20 | +var _ error = (*EventCollectorResponse)(nil) |
| 21 | + |
| 22 | +// Error implements the error interface. |
| 23 | +func (r *EventCollectorResponse) Error() string { |
| 24 | + if r == nil { |
| 25 | + return "" |
| 26 | + } |
| 27 | + |
| 28 | + var sb strings.Builder |
| 29 | + |
| 30 | + sb.WriteString(r.Text + " (Code: " + strconv.Itoa(int(r.Code))) |
| 31 | + if r.InvalidEventNumber != nil { |
| 32 | + sb.WriteString(", InvalidEventNumber: " + strconv.Itoa(*r.InvalidEventNumber)) |
| 33 | + } |
| 34 | + if r.AckID != nil { |
| 35 | + sb.WriteString(", AckID: " + strconv.Itoa(*r.AckID)) |
| 36 | + } |
| 37 | + sb.WriteRune(')') |
| 38 | + |
| 39 | + return sb.String() |
| 40 | +} |
| 41 | + |
| 42 | +// StatusCode defines the meaning of responses returned by HTTP Event Collector |
| 43 | +// endpoints. |
| 44 | +type StatusCode int8 |
| 45 | + |
| 46 | +const ( |
| 47 | + Success StatusCode = iota |
| 48 | + TokenDisabled |
| 49 | + TokenRequired |
| 50 | + InvalidAuthz |
| 51 | + InvalidToken |
| 52 | + NoData |
| 53 | + InvalidDataFormat |
| 54 | + IncorrectIndex |
| 55 | + InternalServerError |
| 56 | + ServerBusy |
| 57 | + DataChannelMissing |
| 58 | + InvalidDataChannel |
| 59 | + EventFieldRequired |
| 60 | + EventFieldBlank |
| 61 | + ACKDisabled |
| 62 | + ErrorHandlingIndexedFields |
| 63 | + QueryStringAuthzNotEnabled |
| 64 | +) |
| 65 | + |
| 66 | +// HTTPCode returns the HTTP code corresponding to the given StatusCode. It |
| 67 | +// returns -1 and an error in case the HTTP status code can not be determined. |
| 68 | +func (c StatusCode) HTTPCode() (code int, err error) { |
| 69 | + switch c { |
| 70 | + case Success: |
| 71 | + code = http.StatusOK |
| 72 | + case TokenDisabled: |
| 73 | + code = http.StatusForbidden |
| 74 | + case TokenRequired: |
| 75 | + code = http.StatusUnauthorized |
| 76 | + case InvalidAuthz: |
| 77 | + code = http.StatusUnauthorized |
| 78 | + case InvalidToken: |
| 79 | + code = http.StatusForbidden |
| 80 | + case NoData: |
| 81 | + code = http.StatusBadRequest |
| 82 | + case InvalidDataFormat: |
| 83 | + code = http.StatusBadRequest |
| 84 | + case IncorrectIndex: |
| 85 | + code = http.StatusBadRequest |
| 86 | + case InternalServerError: |
| 87 | + code = http.StatusInternalServerError |
| 88 | + case ServerBusy: |
| 89 | + code = http.StatusServiceUnavailable |
| 90 | + case DataChannelMissing: |
| 91 | + code = http.StatusBadRequest |
| 92 | + case InvalidDataChannel: |
| 93 | + code = http.StatusBadRequest |
| 94 | + case EventFieldRequired: |
| 95 | + code = http.StatusBadRequest |
| 96 | + case EventFieldBlank: |
| 97 | + code = http.StatusBadRequest |
| 98 | + case ACKDisabled: |
| 99 | + code = http.StatusBadRequest |
| 100 | + case ErrorHandlingIndexedFields: |
| 101 | + code = http.StatusBadRequest |
| 102 | + case QueryStringAuthzNotEnabled: |
| 103 | + code = http.StatusBadRequest |
| 104 | + default: |
| 105 | + code = -1 |
| 106 | + err = fmt.Errorf("unknown status code %d", c) |
| 107 | + } |
| 108 | + |
| 109 | + return |
| 110 | +} |
0 commit comments