Jon Perritt | 7cbb42c | 2015-02-08 21:13:08 -0700 | [diff] [blame] | 1 | package stackevents |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | |
| 6 | "github.com/mitchellh/mapstructure" |
| 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 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. |
| 14 | ResourceName string `mapstructure:"resource_name"` |
| 15 | // The time the event occurred. |
| 16 | Time time.Time `mapstructure:"-"` |
| 17 | // The URLs to the event. |
| 18 | Links []gophercloud.Link `mapstructure:"links"` |
| 19 | // The logical ID of the stack resource. |
| 20 | LogicalResourceID string `mapstructure:"logical_resource_id"` |
| 21 | // The reason of the status of the event. |
| 22 | ResourceStatusReason string `mapstructure:"resource_status_reason"` |
| 23 | // The status of the event. |
| 24 | ResourceStatus string `mapstructure:"resource_status"` |
| 25 | // The physical ID of the stack resource. |
| 26 | PhysicalResourceID string `mapstructure:"physical_resource_id"` |
| 27 | // The event ID. |
| 28 | ID string `mapstructure:"id"` |
| 29 | // Properties of the stack resource. |
| 30 | ResourceProperties map[string]interface{} `mapstructure:"resource_properties"` |
Jon Perritt | 7cbb42c | 2015-02-08 21:13:08 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // FindResult represents the result of a Find operation. |
| 34 | type FindResult struct { |
| 35 | gophercloud.Result |
| 36 | } |
| 37 | |
| 38 | // Extract returns a slice of Event objects and is called after a |
| 39 | // Find operation. |
| 40 | func (r FindResult) Extract() ([]Event, error) { |
| 41 | if r.Err != nil { |
| 42 | return nil, r.Err |
| 43 | } |
| 44 | |
| 45 | var res struct { |
| 46 | Res []Event `mapstructure:"events"` |
| 47 | } |
| 48 | |
| 49 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | events := r.Body.(map[string]interface{})["events"].([]interface{}) |
| 54 | |
| 55 | for i, eventRaw := range events { |
| 56 | event := eventRaw.(map[string]interface{}) |
| 57 | if date, ok := event["event_time"]; ok && date != nil { |
| 58 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | res.Res[i].Time = t |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return res.Res, nil |
| 67 | } |
| 68 | |
| 69 | // EventPage abstracts the raw results of making a List() request against the API. |
| 70 | // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the |
| 71 | // data provided through the ExtractResources call. |
| 72 | type EventPage struct { |
| 73 | pagination.MarkerPageBase |
| 74 | } |
| 75 | |
| 76 | // IsEmpty returns true if a page contains no Server results. |
| 77 | func (r EventPage) IsEmpty() (bool, error) { |
| 78 | events, err := ExtractEvents(r) |
| 79 | if err != nil { |
| 80 | return true, err |
| 81 | } |
| 82 | return len(events) == 0, nil |
| 83 | } |
| 84 | |
| 85 | // LastMarker returns the last stack ID in a ListResult. |
| 86 | func (r EventPage) LastMarker() (string, error) { |
| 87 | events, err := ExtractEvents(r) |
| 88 | if err != nil { |
| 89 | return "", err |
| 90 | } |
| 91 | if len(events) == 0 { |
| 92 | return "", nil |
| 93 | } |
| 94 | return events[len(events)-1].ID, nil |
| 95 | } |
| 96 | |
| 97 | // ExtractEvents interprets the results of a single page from a List() call, producing a slice of Event entities. |
| 98 | func ExtractEvents(page pagination.Page) ([]Event, error) { |
| 99 | casted := page.(EventPage).Body |
| 100 | |
| 101 | var res struct { |
| 102 | Res []Event `mapstructure:"events"` |
| 103 | } |
| 104 | |
| 105 | if err := mapstructure.Decode(casted, &res); err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | |
| 109 | events := casted.(map[string]interface{})["events"].([]interface{}) |
| 110 | |
| 111 | for i, eventRaw := range events { |
| 112 | event := eventRaw.(map[string]interface{}) |
| 113 | if date, ok := event["event_time"]; ok && date != nil { |
| 114 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 115 | if err != nil { |
| 116 | return nil, err |
| 117 | } |
| 118 | res.Res[i].Time = t |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return res.Res, nil |
| 123 | } |
| 124 | |
Jon Perritt | 64c053f | 2015-02-09 15:42:18 -0700 | [diff] [blame] | 125 | // ExtractResourceEvents interprets the results of a single page from a |
| 126 | // ListResourceEvents() call, producing a slice of Event entities. |
| 127 | func ExtractResourceEvents(page pagination.Page) ([]Event, error) { |
| 128 | return ExtractEvents(page) |
| 129 | } |
| 130 | |
Jon Perritt | 7cbb42c | 2015-02-08 21:13:08 -0700 | [diff] [blame] | 131 | // GetResult represents the result of a Get operation. |
| 132 | type GetResult struct { |
| 133 | gophercloud.Result |
| 134 | } |
| 135 | |
| 136 | // Extract returns a pointer to an Event object and is called after a |
| 137 | // Get operation. |
| 138 | func (r GetResult) Extract() (*Event, error) { |
| 139 | if r.Err != nil { |
| 140 | return nil, r.Err |
| 141 | } |
| 142 | |
| 143 | var res struct { |
| 144 | Res *Event `mapstructure:"event"` |
| 145 | } |
| 146 | |
| 147 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 148 | return nil, err |
| 149 | } |
| 150 | |
| 151 | event := r.Body.(map[string]interface{})["event"].(map[string]interface{}) |
| 152 | |
| 153 | if date, ok := event["event_time"]; ok && date != nil { |
| 154 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 155 | if err != nil { |
| 156 | return nil, err |
| 157 | } |
| 158 | res.Res.Time = t |
| 159 | } |
| 160 | |
| 161 | return res.Res, nil |
| 162 | } |