github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/api/appevents/app_events.go (about)

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