Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 1 | package images |
| 2 | |
| 3 | import ( |
| 4 | "github.com/rackspace/gophercloud" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
| 6 | |
| 7 | "github.com/racker/perigee" |
| 8 | ) |
| 9 | |
| 10 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 11 | // List request. |
| 12 | type ListOptsBuilder interface { |
| 13 | ToImageListQuery() (string, error) |
| 14 | } |
| 15 | |
| 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 | |
| 34 | // ToImageListQuery formats a ListOpts into a query string. |
| 35 | func (opts ListOpts) ToImageListQuery() (string, error) { |
| 36 | q, err := gophercloud.BuildQueryString(opts) |
| 37 | if err != nil { |
| 38 | return "", err |
| 39 | } |
| 40 | return q.String(), nil |
| 41 | } |
| 42 | |
| 43 | // ListDetail enumerates the available images. |
| 44 | func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 45 | url := listDetailURL(client) |
| 46 | if opts != nil { |
| 47 | query, err := opts.ToImageListQuery() |
| 48 | if err != nil { |
| 49 | return pagination.Pager{Err: err} |
| 50 | } |
| 51 | url += query |
| 52 | } |
| 53 | |
| 54 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
| 55 | return ImagePage{pagination.LinkedPageBase{LastHTTPResponse: r}} |
| 56 | } |
| 57 | |
| 58 | return pagination.NewPager(client, url, createPage) |
| 59 | } |
| 60 | |
| 61 | // Get acquires additional detail about a specific image by ID. |
| 62 | // Use ExtractImage() to intepret the result as an openstack Image. |
| 63 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 64 | var result GetResult |
| 65 | _, result.Err = perigee.Request("GET", getURL(client, id), perigee.Options{ |
| 66 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 67 | Results: &result.Resp, |
| 68 | OkCodes: []int{200}, |
| 69 | }) |
| 70 | return result |
| 71 | } |