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