github.com/gophercloud/gophercloud@v1.11.0/openstack/orchestration/v1/stackevents/results.go (about)

     1  package stackevents
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // Event represents a stack event.
    12  type Event struct {
    13  	// The name of the resource for which the event occurred.
    14  	ResourceName string `json:"resource_name"`
    15  	// The time the event occurred.
    16  	Time time.Time `json:"-"`
    17  	// The URLs to the event.
    18  	Links []gophercloud.Link `json:"links"`
    19  	// The logical ID of the stack resource.
    20  	LogicalResourceID string `json:"logical_resource_id"`
    21  	// The reason of the status of the event.
    22  	ResourceStatusReason string `json:"resource_status_reason"`
    23  	// The status of the event.
    24  	ResourceStatus string `json:"resource_status"`
    25  	// The physical ID of the stack resource.
    26  	PhysicalResourceID string `json:"physical_resource_id"`
    27  	// The event ID.
    28  	ID string `json:"id"`
    29  	// Properties of the stack resource.
    30  	ResourceProperties map[string]interface{} `json:"resource_properties"`
    31  }
    32  
    33  func (r *Event) UnmarshalJSON(b []byte) error {
    34  	type tmp Event
    35  	var s struct {
    36  		tmp
    37  		Time string `json:"event_time"`
    38  	}
    39  
    40  	err := json.Unmarshal(b, &s)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	*r = Event(s.tmp)
    46  
    47  	if s.Time != "" {
    48  		t, err := time.Parse(time.RFC3339, s.Time)
    49  		if err != nil {
    50  			t, err = time.Parse(gophercloud.RFC3339NoZ, s.Time)
    51  			if err != nil {
    52  				return err
    53  			}
    54  		}
    55  		r.Time = t
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  // FindResult represents the result of a Find operation.
    62  type FindResult struct {
    63  	gophercloud.Result
    64  }
    65  
    66  // Extract returns a slice of Event objects and is called after a
    67  // Find operation.
    68  func (r FindResult) Extract() ([]Event, error) {
    69  	var s struct {
    70  		Events []Event `json:"events"`
    71  	}
    72  	err := r.ExtractInto(&s)
    73  	return s.Events, err
    74  }
    75  
    76  // EventPage abstracts the raw results of making a List() request against the API.
    77  // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the
    78  // data provided through the ExtractResources call.
    79  type EventPage struct {
    80  	pagination.MarkerPageBase
    81  }
    82  
    83  // IsEmpty returns true if a page contains no Server results.
    84  func (r EventPage) IsEmpty() (bool, error) {
    85  	if r.StatusCode == 204 {
    86  		return true, nil
    87  	}
    88  
    89  	events, err := ExtractEvents(r)
    90  	return len(events) == 0, err
    91  }
    92  
    93  // LastMarker returns the last stack ID in a ListResult.
    94  func (r EventPage) LastMarker() (string, error) {
    95  	events, err := ExtractEvents(r)
    96  	if err != nil {
    97  		return "", err
    98  	}
    99  	if len(events) == 0 {
   100  		return "", nil
   101  	}
   102  	return events[len(events)-1].ID, nil
   103  }
   104  
   105  // ExtractEvents interprets the results of a single page from a List() call, producing a slice of Event entities.
   106  func ExtractEvents(r pagination.Page) ([]Event, error) {
   107  	var s struct {
   108  		Events []Event `json:"events"`
   109  	}
   110  	err := (r.(EventPage)).ExtractInto(&s)
   111  	return s.Events, err
   112  }
   113  
   114  // ExtractResourceEvents interprets the results of a single page from a
   115  // ListResourceEvents() call, producing a slice of Event entities.
   116  func ExtractResourceEvents(page pagination.Page) ([]Event, error) {
   117  	return ExtractEvents(page)
   118  }
   119  
   120  // GetResult represents the result of a Get operation.
   121  type GetResult struct {
   122  	gophercloud.Result
   123  }
   124  
   125  // Extract returns a pointer to an Event object and is called after a
   126  // Get operation.
   127  func (r GetResult) Extract() (*Event, error) {
   128  	var s struct {
   129  		Event *Event `json:"event"`
   130  	}
   131  	err := r.ExtractInto(&s)
   132  	return s.Event, err
   133  }