github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/api/appevents/app_events.go (about)

     1  package appevents
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/api/resources"
     5  	"github.com/cloudfoundry/cli/cf/api/strategy"
     6  	"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
     7  	"github.com/cloudfoundry/cli/cf/models"
     8  	"github.com/cloudfoundry/cli/cf/net"
     9  )
    10  
    11  //go:generate counterfeiter . Repository
    12  
    13  type Repository interface {
    14  	RecentEvents(appGUID string, limit int64) ([]models.EventFields, error)
    15  }
    16  
    17  type CloudControllerAppEventsRepository struct {
    18  	config   coreconfig.Reader
    19  	gateway  net.Gateway
    20  	strategy strategy.EndpointStrategy
    21  }
    22  
    23  func NewCloudControllerAppEventsRepository(config coreconfig.Reader, gateway net.Gateway, strategy strategy.EndpointStrategy) CloudControllerAppEventsRepository {
    24  	return CloudControllerAppEventsRepository{
    25  		config:   config,
    26  		gateway:  gateway,
    27  		strategy: strategy,
    28  	}
    29  }
    30  
    31  func (repo CloudControllerAppEventsRepository) RecentEvents(appGUID string, limit int64) ([]models.EventFields, error) {
    32  	count := int64(0)
    33  	events := make([]models.EventFields, 0, limit)
    34  	apiErr := repo.listEvents(appGUID, limit, func(eventField models.EventFields) bool {
    35  		count++
    36  		events = append(events, eventField)
    37  		return count < limit
    38  	})
    39  
    40  	return events, apiErr
    41  }
    42  
    43  func (repo CloudControllerAppEventsRepository) listEvents(appGUID string, limit int64, cb func(models.EventFields) bool) error {
    44  	return repo.gateway.ListPaginatedResources(
    45  		repo.config.APIEndpoint(),
    46  		repo.strategy.EventsURL(appGUID, limit),
    47  		repo.strategy.EventsResource(),
    48  
    49  		func(resource interface{}) bool {
    50  			return cb(resource.(resources.EventResource).ToFields())
    51  		})
    52  }