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