forked from cloudfoundry/bosh-cli
/
events.go
161 lines (136 loc) · 4.2 KB
/
events.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
package director
import (
"fmt"
"net/url"
"time"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type EventImpl struct {
client Client
id string
parentID string
timestamp time.Time
user string
action string
objectType string
objectName string
taskID string
deploymentName string
instance string
context map[string]interface{}
error string
}
type EventResp struct {
ID string `json:"id"`
Timestamp int64 `json:"timestamp"`
User string `json:"user"`
Action string `json:"action"`
ObjectType string `json:"object_type"`
ObjectName string `json:"object_name"`
TaskID string `json:"task"`
DeploymentName string `json:"deployment"`
Instance string `json:"instance"`
ParentID string `json:"parent_id,omitempty"`
Context map[string]interface{} `json:"context"`
Error string `json:"error"`
}
func (e EventImpl) ID() string { return e.id }
func (e EventImpl) ParentID() string { return e.parentID }
func (e EventImpl) Timestamp() time.Time { return e.timestamp }
func (e EventImpl) User() string { return e.user }
func (e EventImpl) Action() string { return e.action }
func (e EventImpl) ObjectType() string { return e.objectType }
func (e EventImpl) ObjectName() string { return e.objectName }
func (e EventImpl) TaskID() string { return e.taskID }
func (e EventImpl) DeploymentName() string { return e.deploymentName }
func (e EventImpl) Instance() string { return e.instance }
func (e EventImpl) Context() map[string]interface{} { return e.context }
func (e EventImpl) Error() string { return e.error }
func NewEventFromResp(client Client, r EventResp) EventImpl {
return EventImpl{
client: client,
id: r.ID,
parentID: r.ParentID,
timestamp: time.Unix(r.Timestamp, 0).UTC(),
user: r.User,
action: r.Action,
objectType: r.ObjectType,
objectName: r.ObjectName,
taskID: r.TaskID,
deploymentName: r.DeploymentName,
instance: r.Instance,
context: r.Context,
error: r.Error,
}
}
func (d DirectorImpl) Events(opts EventsFilter) ([]Event, error) {
events := []Event{}
eventResps, err := d.client.Events(opts)
if err != nil {
return events, err
}
for _, r := range eventResps {
events = append(events, NewEventFromResp(d.client, r))
}
return events, nil
}
func (c Client) Events(opts EventsFilter) ([]EventResp, error) {
var events []EventResp
u, err := url.Parse("/events")
if err != nil {
panic("Parse err is non-nil.")
}
q := u.Query()
if len(opts.BeforeID) > 0 {
q.Set("before_id", opts.BeforeID)
}
if len(opts.Before) > 0 {
q.Set("before_time", opts.Before)
}
if len(opts.After) > 0 {
q.Set("after_time", opts.After)
}
if len(opts.Deployment) > 0 {
q.Set("deployment", opts.Deployment)
}
if len(opts.Task) > 0 {
q.Set("task", opts.Task)
}
if len(opts.Instance) > 0 {
q.Set("instance", opts.Instance)
}
if len(opts.User) > 0 {
q.Set("user", opts.User)
}
if len(opts.Action) > 0 {
q.Set("action", opts.Action)
}
if len(opts.ObjectType) > 0 {
q.Set("object_type", opts.ObjectType)
}
if len(opts.ObjectName) > 0 {
q.Set("object_name", opts.ObjectName)
}
u.RawQuery = q.Encode()
path := u.String()
err = c.clientRequest.Get(path, &events)
if err != nil {
return events, bosherr.WrapErrorf(err, "Finding events")
}
return events, nil
}
func (d DirectorImpl) Event(id string) (Event, error) {
eventResp, err := d.client.Event(id)
if err != nil {
return EventImpl{}, err
}
return NewEventFromResp(d.client, eventResp), nil
}
func (c Client) Event(id string) (EventResp, error) {
var event EventResp
err := c.clientRequest.Get(fmt.Sprintf("/events/%s", id), &event)
if err != nil {
return event, bosherr.WrapErrorf(err, "Finding event '%s'", id)
}
return event, nil
}