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