results.go 3.1 KB

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