Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 1 | package images |
| 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" |
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) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 35 | return q.String(), err |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame] | 38 | // ListDetail enumerates the available images. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 39 | func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame] | 40 | url := listDetailURL(client) |
| 41 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 42 | query, err := opts.ToImageListQuery() |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame] | 43 | if err != nil { |
| 44 | return pagination.Pager{Err: err} |
| 45 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 46 | url += query |
Jon Perritt | ef168e6 | 2014-10-08 11:14:05 -0500 | [diff] [blame] | 47 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 48 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 49 | return ImagePage{pagination.LinkedPageBase{PageResult: r}} |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 50 | }) |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 51 | } |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 52 | |
| 53 | // Get acquires additional detail about a specific image by ID. |
Alex Gaynor | a6d5f9f | 2014-10-27 10:52:32 -0700 | [diff] [blame] | 54 | // Use ExtractImage() to interpret the result as an openstack Image. |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 55 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 56 | var r GetResult |
| 57 | _, r.Err = client.Get(getURL(client, id), &r.Body, nil) |
| 58 | return r |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 59 | } |
Jesse Nelson | ab02e57 | 2015-04-14 16:28:21 -0700 | [diff] [blame] | 60 | |
| 61 | // Delete deletes the specified image ID. |
| 62 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 63 | var r DeleteResult |
| 64 | _, r.Err = client.Delete(deleteURL(client, id), nil) |
| 65 | return r |
Jesse Nelson | ab02e57 | 2015-04-14 16:28:21 -0700 | [diff] [blame] | 66 | } |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 67 | |
| 68 | // IDFromName is a convienience function that returns an image's ID given its name. |
| 69 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 70 | count := 0 |
| 71 | id := "" |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 72 | allPages, err := ListDetail(client, nil).AllPages() |
| 73 | if err != nil { |
| 74 | return "", err |
| 75 | } |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 76 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 77 | all, err := ExtractImages(allPages) |
| 78 | if err != nil { |
| 79 | return "", err |
| 80 | } |
| 81 | |
| 82 | for _, f := range all { |
| 83 | if f.Name == name { |
| 84 | count++ |
| 85 | id = f.ID |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | switch count { |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 90 | case 0: |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 91 | err := &gophercloud.ErrResourceNotFound{} |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 92 | err.ResourceType = "image" |
| 93 | err.Name = name |
| 94 | return "", err |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 95 | case 1: |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 96 | return id, nil |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 97 | default: |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 98 | err := &gophercloud.ErrMultipleResourcesFound{} |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 99 | err.ResourceType = "image" |
| 100 | err.Name = name |
| 101 | err.Count = count |
| 102 | return "", err |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 103 | } |
| 104 | } |