blob: 80808b10b1e178ba03703663f6ee4c5efa2d032a [file] [log] [blame]
Jon Perritt7cbb42c2015-02-08 21:13:08 -07001package stackevents
2
3import (
4 "github.com/racker/perigee"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
7)
8
Jon Perritta37ecf42015-02-11 12:51:40 -07009// Find retrieves stack events for the given stack name.
Jon Perritt7cbb42c2015-02-08 21:13:08 -070010func Find(c *gophercloud.ServiceClient, stackName string) FindResult {
11 var res FindResult
12
13 _, res.Err = perigee.Request("GET", findURL(c, stackName), perigee.Options{
14 MoreHeaders: c.AuthenticatedHeaders(),
15 Results: &res.Body,
16 OkCodes: []int{200},
17 })
18 return res
19}
20
21// SortDir is a type for specifying in which direction to sort a list of events.
22type SortDir string
23
24// SortKey is a type for specifying by which key to sort a list of events.
25type SortKey string
26
27// ResourceStatus is a type for specifying by which resource status to filter a
28// list of events.
29type ResourceStatus string
30
31// ResourceAction is a type for specifying by which resource action to filter a
32// list of events.
33type ResourceAction string
34
35var (
Jon Perritt12c04a42015-02-12 11:45:10 -070036 // ResourceStatusInProgress is used to filter a List request by the 'IN_PROGRESS' status.
37 ResourceStatusInProgress ResourceStatus = "IN_PROGRESS"
38 // ResourceStatusComplete is used to filter a List request by the 'COMPLETE' status.
39 ResourceStatusComplete ResourceStatus = "COMPLETE"
40 // ResourceStatusFailed is used to filter a List request by the 'FAILED' status.
41 ResourceStatusFailed ResourceStatus = "FAILED"
42
43 // ResourceActionCreate is used to filter a List request by the 'CREATE' action.
44 ResourceActionCreate ResourceAction = "CREATE"
45 // ResourceActionDelete is used to filter a List request by the 'DELETE' action.
46 ResourceActionDelete ResourceAction = "DELETE"
47 // ResourceActionUpdate is used to filter a List request by the 'UPDATE' action.
48 ResourceActionUpdate ResourceAction = "UPDATE"
49 // ResourceActionRollback is used to filter a List request by the 'ROLLBACK' action.
50 ResourceActionRollback ResourceAction = "ROLLBACK"
51 // ResourceActionSuspend is used to filter a List request by the 'SUSPEND' action.
52 ResourceActionSuspend ResourceAction = "SUSPEND"
53 // ResourceActionResume is used to filter a List request by the 'RESUME' action.
54 ResourceActionResume ResourceAction = "RESUME"
55 // ResourceActionAbandon is used to filter a List request by the 'ABANDON' action.
56 ResourceActionAbandon ResourceAction = "ABANDON"
57
Jon Perritt7cbb42c2015-02-08 21:13:08 -070058 // SortAsc is used to sort a list of stacks in ascending order.
59 SortAsc SortDir = "asc"
60 // SortDesc is used to sort a list of stacks in descending order.
61 SortDesc SortDir = "desc"
62
63 // SortName is used to sort a list of stacks by name.
64 SortName SortKey = "name"
65 // SortResourceType is used to sort a list of stacks by resource type.
66 SortResourceType SortKey = "resource_type"
67 // SortCreatedAt is used to sort a list of stacks by date created.
68 SortCreatedAt SortKey = "created_at"
69)
70
71// ListOptsBuilder allows extensions to add additional parameters to the
72// List request.
73type ListOptsBuilder interface {
74 ToStackEventListQuery() (string, error)
75}
76
77// ListOpts allows the filtering and sorting of paginated collections through
78// the API. Marker and Limit are used for pagination.
79type ListOpts struct {
80 // The stack resource ID with which to start the listing.
81 Marker string `q:"marker"`
82 // Integer value for the limit of values to return.
83 Limit int `q:"limit"`
84 // Filters the event list by the specified ResourceAction. You can use this
85 // filter multiple times to filter by multiple resource actions: CREATE, DELETE,
86 // UPDATE, ROLLBACK, SUSPEND, RESUME or ADOPT.
Jon Perritt12c04a42015-02-12 11:45:10 -070087 ResourceActions []ResourceAction `q:"resource_action"`
Jon Perritt7cbb42c2015-02-08 21:13:08 -070088 // Filters the event list by the specified resource_status. You can use this
89 // filter multiple times to filter by multiple resource statuses: IN_PROGRESS,
90 // COMPLETE or FAILED.
Jon Perritt12c04a42015-02-12 11:45:10 -070091 ResourceStatuses []ResourceStatus `q:"resource_status"`
Jon Perritt7cbb42c2015-02-08 21:13:08 -070092 // Filters the event list by the specified resource_name. You can use this
93 // filter multiple times to filter by multiple resource names.
94 ResourceNames []string `q:"resource_name"`
95 // Filters the event list by the specified resource_type. You can use this
96 // filter multiple times to filter by multiple resource types: OS::Nova::Server,
97 // OS::Cinder::Volume, and so on.
98 ResourceTypes []string `q:"resource_type"`
99 // Sorts the event list by: resource_type or created_at.
100 SortKey SortKey `q:"sort_keys"`
101 // The sort direction of the event list. Which is asc (ascending) or desc (descending).
102 SortDir SortDir `q:"sort_dir"`
103}
104
105// ToStackEventListQuery formats a ListOpts into a query string.
106func (opts ListOpts) ToStackEventListQuery() (string, error) {
107 q, err := gophercloud.BuildQueryString(opts)
108 if err != nil {
109 return "", err
110 }
111 return q.String(), nil
112}
113
114// List makes a request against the API to list resources for the given stack.
115func List(client *gophercloud.ServiceClient, stackName, stackID string, opts ListOptsBuilder) pagination.Pager {
116 url := listURL(client, stackName, stackID)
117
118 if opts != nil {
119 query, err := opts.ToStackEventListQuery()
120 if err != nil {
121 return pagination.Pager{Err: err}
122 }
123 url += query
124 }
125
126 createPageFn := func(r pagination.PageResult) pagination.Page {
127 p := EventPage{pagination.MarkerPageBase{PageResult: r}}
128 p.MarkerPageBase.Owner = p
129 return p
130 }
131
132 return pagination.NewPager(client, url, createPageFn)
133}
134
135// ListResourceEventsOptsBuilder allows extensions to add additional parameters to the
136// ListResourceEvents request.
137type ListResourceEventsOptsBuilder interface {
138 ToResourceEventListQuery() (string, error)
139}
140
141// ListResourceEventsOpts allows the filtering and sorting of paginated resource events through
142// the API. Marker and Limit are used for pagination.
143type ListResourceEventsOpts struct {
144 // The stack resource ID with which to start the listing.
145 Marker string `q:"marker"`
146 // Integer value for the limit of values to return.
147 Limit int `q:"limit"`
148 // Filters the event list by the specified ResourceAction. You can use this
149 // filter multiple times to filter by multiple resource actions: CREATE, DELETE,
150 // UPDATE, ROLLBACK, SUSPEND, RESUME or ADOPT.
151 ResourceActions []string `q:"resource_action"`
152 // Filters the event list by the specified resource_status. You can use this
153 // filter multiple times to filter by multiple resource statuses: IN_PROGRESS,
154 // COMPLETE or FAILED.
155 ResourceStatuses []string `q:"resource_status"`
156 // Filters the event list by the specified resource_name. You can use this
157 // filter multiple times to filter by multiple resource names.
158 ResourceNames []string `q:"resource_name"`
159 // Filters the event list by the specified resource_type. You can use this
160 // filter multiple times to filter by multiple resource types: OS::Nova::Server,
161 // OS::Cinder::Volume, and so on.
162 ResourceTypes []string `q:"resource_type"`
163 // Sorts the event list by: resource_type or created_at.
164 SortKey SortKey `q:"sort_keys"`
165 // The sort direction of the event list. Which is asc (ascending) or desc (descending).
166 SortDir SortDir `q:"sort_dir"`
167}
168
169// ToResourceEventsListQuery formats a ListOpts into a query string.
170func (opts ListOpts) ToResourceEventsListQuery() (string, error) {
171 q, err := gophercloud.BuildQueryString(opts)
172 if err != nil {
173 return "", err
174 }
175 return q.String(), nil
176}
177
178// ListResourceEvents makes a request against the API to list resources for the given stack.
179func ListResourceEvents(client *gophercloud.ServiceClient, stackName, stackID, resourceName string, opts ListResourceEventsOptsBuilder) pagination.Pager {
180 url := listResourceEventsURL(client, stackName, stackID, resourceName)
181
182 if opts != nil {
183 query, err := opts.ToResourceEventListQuery()
184 if err != nil {
185 return pagination.Pager{Err: err}
186 }
187 url += query
188 }
189
190 createPageFn := func(r pagination.PageResult) pagination.Page {
191 p := EventPage{pagination.MarkerPageBase{PageResult: r}}
192 p.MarkerPageBase.Owner = p
193 return p
194 }
195
196 return pagination.NewPager(client, url, createPageFn)
197}
198
199// Get retreives data for the given stack resource.
200func Get(c *gophercloud.ServiceClient, stackName, stackID, resourceName, eventID string) GetResult {
201 var res GetResult
202 _, res.Err = perigee.Request("GET", getURL(c, stackName, stackID, resourceName, eventID), perigee.Options{
203 MoreHeaders: c.AuthenticatedHeaders(),
204 Results: &res.Body,
205 OkCodes: []int{200},
206 })
207 return res
208}