github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/api/cloudcontroller/ccv2/event.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"time"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
    10  )
    11  
    12  // Event represents a Cloud Controller Event
    13  type Event struct {
    14  	// GUID is the unique event identifier.
    15  	GUID string
    16  
    17  	// Type is the type of event.
    18  	Type constant.EventType
    19  
    20  	// ActorGUID is the GUID of the actor initiating an event.
    21  	ActorGUID string
    22  
    23  	// ActorType is the type of actor initiating an event.
    24  	ActorType string
    25  
    26  	// ActorName is the name of the actor initiating an event.
    27  	ActorName string
    28  
    29  	// ActeeGUID is the GUID of the cc object affected by an event.
    30  	ActeeGUID string
    31  
    32  	// ActeeType is the type of the cc object affected by an event.
    33  	ActeeType string
    34  
    35  	// ActeeName is the name of the cc object affected by an event.
    36  	ActeeName string
    37  
    38  	// Timestamp is the event creation time.
    39  	Timestamp time.Time
    40  
    41  	// Metadata contains additional information about the event.
    42  	Metadata map[string]interface{}
    43  }
    44  
    45  // UnmarshalJSON helps unmarshal a Cloud Controller Event response.
    46  func (event *Event) UnmarshalJSON(data []byte) error {
    47  	var ccEvent struct {
    48  		Metadata internal.Metadata `json:"metadata"`
    49  		Entity   struct {
    50  			Type      string                 `json:"type,omitempty"`
    51  			ActorGUID string                 `json:"actor,omitempty"`
    52  			ActorType string                 `json:"actor_type,omitempty"`
    53  			ActorName string                 `json:"actor_name,omitempty"`
    54  			ActeeGUID string                 `json:"actee,omitempty"`
    55  			ActeeType string                 `json:"actee_type,omitempty"`
    56  			ActeeName string                 `json:"actee_name,omitempty"`
    57  			Timestamp *time.Time             `json:"timestamp"`
    58  			Metadata  map[string]interface{} `json:"metadata"`
    59  		} `json:"entity"`
    60  	}
    61  
    62  	err := cloudcontroller.DecodeJSON(data, &ccEvent)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	event.GUID = ccEvent.Metadata.GUID
    68  	event.Type = constant.EventType(ccEvent.Entity.Type)
    69  	event.ActorGUID = ccEvent.Entity.ActorGUID
    70  	event.ActorType = ccEvent.Entity.ActorType
    71  	event.ActorName = ccEvent.Entity.ActorName
    72  	event.ActeeGUID = ccEvent.Entity.ActeeGUID
    73  	event.ActeeType = ccEvent.Entity.ActeeType
    74  	event.ActeeName = ccEvent.Entity.ActeeName
    75  	if ccEvent.Entity.Timestamp != nil {
    76  		event.Timestamp = *ccEvent.Entity.Timestamp
    77  	}
    78  	event.Metadata = ccEvent.Entity.Metadata
    79  
    80  	return nil
    81  }
    82  
    83  // GetEvents returns back a list of Events based off of the provided queries.
    84  func (client *Client) GetEvents(filters ...Filter) ([]Event, Warnings, error) {
    85  	request, err := client.newHTTPRequest(requestOptions{
    86  		RequestName: internal.GetEventsRequest,
    87  		Query:       ConvertFilterParameters(filters),
    88  	})
    89  	if err != nil {
    90  		return nil, nil, err
    91  	}
    92  
    93  	var fullEventsList []Event
    94  	warnings, err := client.paginate(request, Event{}, func(item interface{}) error {
    95  		if event, ok := item.(Event); ok {
    96  			fullEventsList = append(fullEventsList, event)
    97  		} else {
    98  			return ccerror.UnknownObjectInListError{
    99  				Expected:   Event{},
   100  				Unexpected: item,
   101  			}
   102  		}
   103  		return nil
   104  	})
   105  
   106  	return fullEventsList, warnings, err
   107  }