-
Notifications
You must be signed in to change notification settings - Fork 2
/
log_sets.go
176 lines (151 loc) · 5.01 KB
/
log_sets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package logentries_goclient
import (
"errors"
"fmt"
)
// The Log Set resource allows you to interact with Logs in your account. The following operations are supported:
// - Get details of an existing Log Set
// - Get details of a list of all Log Sets
// - Create a new Log Set
// - Update an existing Log Set
// - Delete a Log Set
// LogSets represents the logsets interface by which user can interact with logentries logsets API
type LogSets struct {
client *client `json:"-"`
}
// newLogSets creates a new LogSets struct that exposes LogSets CRUD operations
func newLogSets(c *client) LogSets {
return LogSets{c}
}
// Structs meant for clients
// PostLogSet represents the entity used to create a new logset to the logentries API
type PostLogSet struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
UserData map[string]string `json:"user_data,omitempty"`
LogsInfo []PostLogInfo `json:"logs_info,omitempty"`
}
type PostLogSetInfo struct {
Id string `json:"id"`
}
// PostLogSet represents the entity used to update an existing logset to the logentries API
type PutLogSet struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
UserData map[string]string `json:"user_data,omitempty"`
LogsInfo []LogInfo `json:"logs_info,omitempty"`
}
// LogSet represents the entity used to get an existing log from the logentries API
type LogSet struct {
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
UserData map[string]string `json:"user_data"`
LogsInfo []LogInfo `json:"logs_info"`
}
// userData represents user metadata
type userData struct {
LogEntriesDistName string `json:"le_distname"`
LogEntriesDistVer string `json:"le_distver"`
LogEntriesNameIntr string `json:"le_nameintr"`
}
// LogSetInfo represent information about the logset
type LogSetInfo struct {
Id string `json:"id"`
Name string `json:"name"`
Links []link `json:"links"`
}
// link represents link metadata of the logset
type link struct {
Href string `json:"href"`
Rel string `json:"rel"`
}
// Structs meant for marshalling/un-marshalling purposes
// logSetCollection represents a wrapper struct for marshalling/unmarshalling purposes
type logSetCollection struct {
LogSets []LogSet `json:"logsets"`
}
// getLogSet represents a wrapper struct for marshalling/unmarshalling purposes
type getLogSet struct {
LogSet LogSet `json:"logset"`
}
// postLogSet represents a wrapper struct for marshalling/unmarshalling purposes
type postLogSet struct {
PostLogSet PostLogSet `json:"logset"`
}
// putLogSet represents a wrapper struct for marshalling/unmarshalling purposes
type putLogSet struct {
PutLogSet PutLogSet `json:"logset"`
}
// GetLogSet gets details of a list of all Log Sets
func (l *LogSets) GetLogSets() ([]LogSet, error) {
logSets := &logSetCollection{}
if err := l.client.get(l.getPath(), logSets); err != nil {
return nil, err
}
return logSets.LogSets, nil
}
// GetLogSets gets details of an existing Log Set
func (l *LogSets) GetLogSet(logSetId string) (LogSet, LogSetInfo, error) {
if logSetId == "" {
return LogSet{}, LogSetInfo{}, errors.New("logSetId input parameter is mandatory")
}
logSet := &getLogSet{}
if err := l.client.get(l.getLogSetEndPoint(logSetId), logSet); err != nil {
return LogSet{}, LogSetInfo{}, err
}
logSetInfo := logSet.LogSet.logSetInfo(l)
return logSet.LogSet, logSetInfo, nil
}
// PostLogSet creates a new Log Set
func (l *LogSets) PostLogSet(p PostLogSet) (LogSet, error) {
logSet := &getLogSet{}
postLogSet := &postLogSet{p}
if err := l.client.post(l.getPath(), postLogSet, logSet); err != nil {
return LogSet{}, err
}
return logSet.LogSet, nil
}
// PutLogSet updates an existing Log Set
func (l *LogSets) PutLogSet(logSetId string, p PutLogSet) (LogSet, error) {
if logSetId == "" {
return LogSet{}, errors.New("logSetId input parameter is mandatory")
}
logSet := &getLogSet{}
putLogSet := &putLogSet{p}
if err := l.client.put(l.getLogSetEndPoint(logSetId), putLogSet, logSet); err != nil {
return LogSet{}, err
}
return logSet.LogSet, nil
}
// DeleteLogSet deletes a Log Set
func (l *LogSets) DeleteLogSet(logSetId string) error {
if logSetId == "" {
return errors.New("logSetId input parameter is mandatory")
}
var err error
if err = l.client.delete(l.getLogSetEndPoint(logSetId)); err != nil {
return err
}
return nil
}
func (l *LogSet) logSetInfo(c *LogSets) LogSetInfo {
return LogSetInfo{
Id: l.Id,
Name: l.Name,
Links: []link{
{
Href: fmt.Sprintf("%s%s", c.client.logEntriesUrl, c.getLogSetEndPoint(l.Id)),
Rel: "Self",
},
},
}
}
// getPath returns the rest end point for logsets
func (l *LogSets) getPath() string {
return "/management/logsets"
}
// getLogSetEndPoint returns the rest end point to retrieve an individual logsets
func (l *LogSets) getLogSetEndPoint(logSetId string) string {
return fmt.Sprintf("%s/%s", l.getPath(), logSetId)
}