blob: 46fb0ff088c9a5c1e4b76b686bee3d46ddc31145 [file] [log] [blame]
Jon Perritt7cbb42c2015-02-08 21:13:08 -07001package stackevents
2
3import (
jrperritt98d01622017-01-12 14:24:42 -06004 "encoding/json"
5 "time"
6
Jon Perritt27249f42016-02-18 10:35:59 -06007 "github.com/gophercloud/gophercloud"
8 "github.com/gophercloud/gophercloud/pagination"
Jon Perritt7cbb42c2015-02-08 21:13:08 -07009)
10
11// Event represents a stack event.
12type Event struct {
Jon Perritt9dce4152015-02-11 13:05:54 -070013 // The name of the resource for which the event occurred.
Jon Perritt3c166472016-02-25 03:07:41 -060014 ResourceName string `json:"resource_name"`
Jon Perritt9dce4152015-02-11 13:05:54 -070015 // The time the event occurred.
jrperritt98d01622017-01-12 14:24:42 -060016 Time time.Time `json:"-"`
Jon Perritt9dce4152015-02-11 13:05:54 -070017 // The URLs to the event.
Jon Perritt3c166472016-02-25 03:07:41 -060018 Links []gophercloud.Link `json:"links"`
Jon Perritt9dce4152015-02-11 13:05:54 -070019 // The logical ID of the stack resource.
Jon Perritt3c166472016-02-25 03:07:41 -060020 LogicalResourceID string `json:"logical_resource_id"`
Jon Perritt9dce4152015-02-11 13:05:54 -070021 // The reason of the status of the event.
Jon Perritt3c166472016-02-25 03:07:41 -060022 ResourceStatusReason string `json:"resource_status_reason"`
Jon Perritt9dce4152015-02-11 13:05:54 -070023 // The status of the event.
Jon Perritt3c166472016-02-25 03:07:41 -060024 ResourceStatus string `json:"resource_status"`
Jon Perritt9dce4152015-02-11 13:05:54 -070025 // The physical ID of the stack resource.
Jon Perritt3c166472016-02-25 03:07:41 -060026 PhysicalResourceID string `json:"physical_resource_id"`
Jon Perritt9dce4152015-02-11 13:05:54 -070027 // The event ID.
Jon Perritt3c166472016-02-25 03:07:41 -060028 ID string `json:"id"`
Jon Perritt9dce4152015-02-11 13:05:54 -070029 // Properties of the stack resource.
Jon Perritt3c166472016-02-25 03:07:41 -060030 ResourceProperties map[string]interface{} `json:"resource_properties"`
Jon Perritt7cbb42c2015-02-08 21:13:08 -070031}
32
jrperritt98d01622017-01-12 14:24:42 -060033func (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 Perritt7cbb42c2015-02-08 21:13:08 -070051// FindResult represents the result of a Find operation.
52type FindResult struct {
53 gophercloud.Result
54}
55
56// Extract returns a slice of Event objects and is called after a
57// Find operation.
58func (r FindResult) Extract() ([]Event, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060059 var s struct {
60 Events []Event `json:"events"`
Jon Perritt7cbb42c2015-02-08 21:13:08 -070061 }
Jon Perritt3c166472016-02-25 03:07:41 -060062 err := r.ExtractInto(&s)
63 return s.Events, err
Jon Perritt7cbb42c2015-02-08 21:13:08 -070064}
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.
69type EventPage struct {
70 pagination.MarkerPageBase
71}
72
73// IsEmpty returns true if a page contains no Server results.
74func (r EventPage) IsEmpty() (bool, error) {
75 events, err := ExtractEvents(r)
Jon Perritt3c166472016-02-25 03:07:41 -060076 return len(events) == 0, err
Jon Perritt7cbb42c2015-02-08 21:13:08 -070077}
78
79// LastMarker returns the last stack ID in a ListResult.
80func (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 Perritt3c166472016-02-25 03:07:41 -060092func ExtractEvents(r pagination.Page) ([]Event, error) {
93 var s struct {
94 Events []Event `json:"events"`
Jon Perritt7cbb42c2015-02-08 21:13:08 -070095 }
Jon Perritt3c166472016-02-25 03:07:41 -060096 err := (r.(EventPage)).ExtractInto(&s)
97 return s.Events, err
Jon Perritt7cbb42c2015-02-08 21:13:08 -070098}
99
Jon Perritt64c053f2015-02-09 15:42:18 -0700100// ExtractResourceEvents interprets the results of a single page from a
101// ListResourceEvents() call, producing a slice of Event entities.
102func ExtractResourceEvents(page pagination.Page) ([]Event, error) {
103 return ExtractEvents(page)
104}
105
Jon Perritt7cbb42c2015-02-08 21:13:08 -0700106// GetResult represents the result of a Get operation.
107type GetResult struct {
108 gophercloud.Result
109}
110
111// Extract returns a pointer to an Event object and is called after a
112// Get operation.
113func (r GetResult) Extract() (*Event, error) {
Jon Perritt3c166472016-02-25 03:07:41 -0600114 var s struct {
115 Event *Event `json:"event"`
Jon Perritt7cbb42c2015-02-08 21:13:08 -0700116 }
Jon Perritt3c166472016-02-25 03:07:41 -0600117 err := r.ExtractInto(&s)
118 return s.Event, err
Jon Perritt7cbb42c2015-02-08 21:13:08 -0700119}