forked from cloudfoundry/cli
/
event.go
107 lines (88 loc) · 3.11 KB
/
event.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
package ccv2
import (
"time"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// Event represents a Cloud Controller Event
type Event struct {
// GUID is the unique event identifier.
GUID string
// Type is the type of event.
Type constant.EventType
// ActorGUID is the GUID of the actor initiating an event.
ActorGUID string
// ActorType is the type of actor initiating an event.
ActorType string
// ActorName is the name of the actor initiating an event.
ActorName string
// ActeeGUID is the GUID of the cc object affected by an event.
ActeeGUID string
// ActeeType is the type of the cc object affected by an event.
ActeeType string
// ActeeName is the name of the cc object affected by an event.
ActeeName string
// Timestamp is the event creation time.
Timestamp time.Time
// Metadata contains additional information about the event.
Metadata map[string]interface{}
}
// UnmarshalJSON helps unmarshal a Cloud Controller Event response.
func (event *Event) UnmarshalJSON(data []byte) error {
var ccEvent struct {
Metadata internal.Metadata `json:"metadata"`
Entity struct {
Type string `json:"type,omitempty"`
ActorGUID string `json:"actor,omitempty"`
ActorType string `json:"actor_type,omitempty"`
ActorName string `json:"actor_name,omitempty"`
ActeeGUID string `json:"actee,omitempty"`
ActeeType string `json:"actee_type,omitempty"`
ActeeName string `json:"actee_name,omitempty"`
Timestamp *time.Time `json:"timestamp"`
Metadata map[string]interface{} `json:"metadata"`
} `json:"entity"`
}
err := cloudcontroller.DecodeJSON(data, &ccEvent)
if err != nil {
return err
}
event.GUID = ccEvent.Metadata.GUID
event.Type = constant.EventType(ccEvent.Entity.Type)
event.ActorGUID = ccEvent.Entity.ActorGUID
event.ActorType = ccEvent.Entity.ActorType
event.ActorName = ccEvent.Entity.ActorName
event.ActeeGUID = ccEvent.Entity.ActeeGUID
event.ActeeType = ccEvent.Entity.ActeeType
event.ActeeName = ccEvent.Entity.ActeeName
if ccEvent.Entity.Timestamp != nil {
event.Timestamp = *ccEvent.Entity.Timestamp
}
event.Metadata = ccEvent.Entity.Metadata
return nil
}
// GetEvents returns back a list of Events based off of the provided queries.
func (client *Client) GetEvents(filters ...Filter) ([]Event, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetEventsRequest,
Query: ConvertFilterParameters(filters),
})
if err != nil {
return nil, nil, err
}
var fullEventsList []Event
warnings, err := client.paginate(request, Event{}, func(item interface{}) error {
if event, ok := item.(Event); ok {
fullEventsList = append(fullEventsList, event)
} else {
return ccerror.UnknownObjectInListError{
Expected: Event{},
Unexpected: item,
}
}
return nil
})
return fullEventsList, warnings, err
}