Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 1 | package images |
| 2 | |
| 3 | import ( |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 6 | |
| 7 | "github.com/racker/perigee" |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 8 | ) |
| 9 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 10 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 11 | // List request. |
| 12 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 13 | ToImageListQuery() (string, error) |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 14 | } |
| 15 | |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame] | 16 | // ListOpts contain options for limiting the number of Images returned from a call to ListDetail. |
| 17 | type ListOpts struct { |
| 18 | // When the image last changed status (in date-time format). |
| 19 | ChangesSince string `q:"changes-since"` |
| 20 | // The number of Images to return. |
| 21 | Limit int `q:"limit"` |
| 22 | // UUID of the Image at which to set a marker. |
| 23 | Marker string `q:"marker"` |
| 24 | // The name of the Image. |
| 25 | Name string `q:"name:"` |
| 26 | // The name of the Server (in URL format). |
| 27 | Server string `q:"server"` |
| 28 | // The current status of the Image. |
| 29 | Status string `q:"status"` |
| 30 | // The value of the type of image (e.g. BASE, SERVER, ALL) |
| 31 | Type string `q:"type"` |
| 32 | } |
| 33 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 34 | // ToImageListQuery formats a ListOpts into a query string. |
| 35 | func (opts ListOpts) ToImageListQuery() (string, error) { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 36 | q, err := gophercloud.BuildQueryString(opts) |
| 37 | if err != nil { |
| 38 | return "", err |
| 39 | } |
| 40 | return q.String(), nil |
| 41 | } |
| 42 | |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame] | 43 | // ListDetail enumerates the available images. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 44 | func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame] | 45 | url := listDetailURL(client) |
| 46 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 47 | query, err := opts.ToImageListQuery() |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame] | 48 | if err != nil { |
| 49 | return pagination.Pager{Err: err} |
| 50 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 51 | url += query |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame] | 52 | } |
| 53 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 54 | createPage := func(r pagination.PageResult) pagination.Page { |
| 55 | return ImagePage{pagination.LinkedPageBase{PageResult: r}} |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame] | 58 | return pagination.NewPager(client, url, createPage) |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 59 | } |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 60 | |
| 61 | // Get acquires additional detail about a specific image by ID. |
Alex Gaynor | a6d5f9f | 2014-10-27 10:52:32 -0700 | [diff] [blame^] | 62 | // Use ExtractImage() to interpret the result as an openstack Image. |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 63 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 64 | var result GetResult |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame] | 65 | _, result.Err = perigee.Request("GET", getURL(client, id), perigee.Options{ |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 66 | MoreHeaders: client.AuthenticatedHeaders(), |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 67 | Results: &result.Body, |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 68 | OkCodes: []int{200}, |
| 69 | }) |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 70 | return result |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 71 | } |