github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv3/event.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"time"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
     8  )
     9  
    10  type Event struct {
    11  	GUID      string
    12  	CreatedAt time.Time
    13  	Type      string
    14  	ActorName string
    15  	Data      map[string]interface{}
    16  }
    17  
    18  func (e *Event) UnmarshalJSON(data []byte) error {
    19  	var ccEvent struct {
    20  		GUID      string    `json:"guid"`
    21  		CreatedAt time.Time `json:"created_at"`
    22  		Type      string    `json:"type"`
    23  		Actor     struct {
    24  			Name string `json:"name"`
    25  		} `json:"actor"`
    26  		Data map[string]interface{} `json:"data"`
    27  	}
    28  	err := cloudcontroller.DecodeJSON(data, &ccEvent)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	e.GUID = ccEvent.GUID
    34  	e.CreatedAt = ccEvent.CreatedAt
    35  	e.Type = ccEvent.Type
    36  	e.ActorName = ccEvent.Actor.Name
    37  	e.Data = ccEvent.Data
    38  
    39  	return nil
    40  }
    41  
    42  // GetEvents uses the /v3/audit_events endpoint to retrieve a list of audit events.
    43  // NOTE: This only returns the first page of results. We are intentionally not using the paginate helper to fetch all
    44  // pages here because we only needed the first page for the `cf events` output. If we need to, we can refactor this
    45  // later to fetch all pages and make `cf events` only filter down to the first page.
    46  func (client *Client) GetEvents(query ...Query) ([]Event, Warnings, error) {
    47  	request, err := client.newHTTPRequest(requestOptions{
    48  		RequestName: internal.GetEventsRequest,
    49  		Query:       query,
    50  	})
    51  
    52  	if err != nil {
    53  		return nil, nil, err // untested
    54  	}
    55  
    56  	var eventResponse struct {
    57  		Resources []Event `json:"resources"`
    58  	}
    59  
    60  	response := cloudcontroller.Response{
    61  		DecodeJSONResponseInto: &eventResponse,
    62  	}
    63  	err = client.connection.Make(request, &response)
    64  
    65  	return eventResponse.Resources, response.Warnings, err
    66  }