forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_events.go
50 lines (42 loc) · 1.51 KB
/
app_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
package api
import (
"github.com/cloudfoundry/cli/cf/api/resources"
"github.com/cloudfoundry/cli/cf/api/strategy"
"github.com/cloudfoundry/cli/cf/configuration"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
type AppEventsRepository interface {
RecentEvents(appGuid string, limit uint64) ([]models.EventFields, error)
}
type CloudControllerAppEventsRepository struct {
config configuration.Reader
gateway net.Gateway
strategy strategy.EndpointStrategy
}
func NewCloudControllerAppEventsRepository(config configuration.Reader, gateway net.Gateway, strategy strategy.EndpointStrategy) CloudControllerAppEventsRepository {
return CloudControllerAppEventsRepository{
config: config,
gateway: gateway,
strategy: strategy,
}
}
func (repo CloudControllerAppEventsRepository) RecentEvents(appGuid string, limit uint64) ([]models.EventFields, error) {
count := uint64(0)
events := make([]models.EventFields, 0, limit)
apiErr := repo.listEvents(appGuid, limit, func(eventField models.EventFields) bool {
count++
events = append(events, eventField)
return count < limit
})
return events, apiErr
}
func (repo CloudControllerAppEventsRepository) listEvents(appGuid string, limit uint64, cb func(models.EventFields) bool) error {
return repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
repo.strategy.EventsURL(appGuid, limit),
repo.strategy.EventsResource(),
func(resource interface{}) bool {
return cb(resource.(resources.EventResource).ToFields())
})
}