blob: 384b5b628f2883f8b21f5c0d653f27048ac5e69f [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 (
36 // SortAsc is used to sort a list of stacks in ascending order.
37 SortAsc SortDir = "asc"
38 // SortDesc is used to sort a list of stacks in descending order.
39 SortDesc SortDir = "desc"
40
41 // SortName is used to sort a list of stacks by name.
42 SortName SortKey = "name"
43 // SortResourceType is used to sort a list of stacks by resource type.
44 SortResourceType SortKey = "resource_type"
45 // SortCreatedAt is used to sort a list of stacks by date created.
46 SortCreatedAt SortKey = "created_at"
47)
48
49// ListOptsBuilder allows extensions to add additional parameters to the
50// List request.
51type ListOptsBuilder interface {
52 ToStackEventListQuery() (string, error)
53}
54
55// ListOpts allows the filtering and sorting of paginated collections through
56// the API. Marker and Limit are used for pagination.
57type ListOpts struct {
58 // The stack resource ID with which to start the listing.
59 Marker string `q:"marker"`
60 // Integer value for the limit of values to return.
61 Limit int `q:"limit"`
62 // Filters the event list by the specified ResourceAction. You can use this
63 // filter multiple times to filter by multiple resource actions: CREATE, DELETE,
64 // UPDATE, ROLLBACK, SUSPEND, RESUME or ADOPT.
65 ResourceActions []string `q:"resource_action"`
66 // Filters the event list by the specified resource_status. You can use this
67 // filter multiple times to filter by multiple resource statuses: IN_PROGRESS,
68 // COMPLETE or FAILED.
69 ResourceStatuses []string `q:"resource_status"`
70 // Filters the event list by the specified resource_name. You can use this
71 // filter multiple times to filter by multiple resource names.
72 ResourceNames []string `q:"resource_name"`
73 // Filters the event list by the specified resource_type. You can use this
74 // filter multiple times to filter by multiple resource types: OS::Nova::Server,
75 // OS::Cinder::Volume, and so on.
76 ResourceTypes []string `q:"resource_type"`
77 // Sorts the event list by: resource_type or created_at.
78 SortKey SortKey `q:"sort_keys"`
79 // The sort direction of the event list. Which is asc (ascending) or desc (descending).
80 SortDir SortDir `q:"sort_dir"`
81}
82
83// ToStackEventListQuery formats a ListOpts into a query string.
84func (opts ListOpts) ToStackEventListQuery() (string, error) {
85 q, err := gophercloud.BuildQueryString(opts)
86 if err != nil {
87 return "", err
88 }
89 return q.String(), nil
90}
91
92// List makes a request against the API to list resources for the given stack.
93func List(client *gophercloud.ServiceClient, stackName, stackID string, opts ListOptsBuilder) pagination.Pager {
94 url := listURL(client, stackName, stackID)
95
96 if opts != nil {
97 query, err := opts.ToStackEventListQuery()
98 if err != nil {
99 return pagination.Pager{Err: err}
100 }
101 url += query
102 }
103
104 createPageFn := func(r pagination.PageResult) pagination.Page {
105 p := EventPage{pagination.MarkerPageBase{PageResult: r}}
106 p.MarkerPageBase.Owner = p
107 return p
108 }
109
110 return pagination.NewPager(client, url, createPageFn)
111}
112
113// ListResourceEventsOptsBuilder allows extensions to add additional parameters to the
114// ListResourceEvents request.
115type ListResourceEventsOptsBuilder interface {
116 ToResourceEventListQuery() (string, error)
117}
118
119// ListResourceEventsOpts allows the filtering and sorting of paginated resource events through
120// the API. Marker and Limit are used for pagination.
121type ListResourceEventsOpts struct {
122 // The stack resource ID with which to start the listing.
123 Marker string `q:"marker"`
124 // Integer value for the limit of values to return.
125 Limit int `q:"limit"`
126 // Filters the event list by the specified ResourceAction. You can use this
127 // filter multiple times to filter by multiple resource actions: CREATE, DELETE,
128 // UPDATE, ROLLBACK, SUSPEND, RESUME or ADOPT.
129 ResourceActions []string `q:"resource_action"`
130 // Filters the event list by the specified resource_status. You can use this
131 // filter multiple times to filter by multiple resource statuses: IN_PROGRESS,
132 // COMPLETE or FAILED.
133 ResourceStatuses []string `q:"resource_status"`
134 // Filters the event list by the specified resource_name. You can use this
135 // filter multiple times to filter by multiple resource names.
136 ResourceNames []string `q:"resource_name"`
137 // Filters the event list by the specified resource_type. You can use this
138 // filter multiple times to filter by multiple resource types: OS::Nova::Server,
139 // OS::Cinder::Volume, and so on.
140 ResourceTypes []string `q:"resource_type"`
141 // Sorts the event list by: resource_type or created_at.
142 SortKey SortKey `q:"sort_keys"`
143 // The sort direction of the event list. Which is asc (ascending) or desc (descending).
144 SortDir SortDir `q:"sort_dir"`
145}
146
147// ToResourceEventsListQuery formats a ListOpts into a query string.
148func (opts ListOpts) ToResourceEventsListQuery() (string, error) {
149 q, err := gophercloud.BuildQueryString(opts)
150 if err != nil {
151 return "", err
152 }
153 return q.String(), nil
154}
155
156// ListResourceEvents makes a request against the API to list resources for the given stack.
157func ListResourceEvents(client *gophercloud.ServiceClient, stackName, stackID, resourceName string, opts ListResourceEventsOptsBuilder) pagination.Pager {
158 url := listResourceEventsURL(client, stackName, stackID, resourceName)
159
160 if opts != nil {
161 query, err := opts.ToResourceEventListQuery()
162 if err != nil {
163 return pagination.Pager{Err: err}
164 }
165 url += query
166 }
167
168 createPageFn := func(r pagination.PageResult) pagination.Page {
169 p := EventPage{pagination.MarkerPageBase{PageResult: r}}
170 p.MarkerPageBase.Owner = p
171 return p
172 }
173
174 return pagination.NewPager(client, url, createPageFn)
175}
176
177// Get retreives data for the given stack resource.
178func Get(c *gophercloud.ServiceClient, stackName, stackID, resourceName, eventID string) GetResult {
179 var res GetResult
180 _, res.Err = perigee.Request("GET", getURL(c, stackName, stackID, resourceName, eventID), perigee.Options{
181 MoreHeaders: c.AuthenticatedHeaders(),
182 Results: &res.Body,
183 OkCodes: []int{200},
184 })
185 return res
186}