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) |
| 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 | } |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 73 | |
| 74 | // IDFromName is a convienience function that returns an image's ID given its name. |
| 75 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame^] | 76 | count := 0 |
| 77 | id := "" |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 78 | if name == "" { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame^] | 79 | err := &gophercloud.ErrMissingInput{} |
| 80 | err.Function = "images.IDFromName" |
| 81 | err.Argument = "name" |
| 82 | return "", err |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 83 | } |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 84 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame^] | 85 | allPages, err := ListDetail(client, nil).AllPages() |
| 86 | if err != nil { |
| 87 | return "", err |
| 88 | } |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 89 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame^] | 90 | all, err := ExtractImages(allPages) |
| 91 | if err != nil { |
| 92 | return "", err |
| 93 | } |
| 94 | |
| 95 | for _, f := range all { |
| 96 | if f.Name == name { |
| 97 | count++ |
| 98 | id = f.ID |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | switch count { |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 103 | case 0: |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame^] | 104 | err := &gophercloud.ErrResourceNotFound{} |
| 105 | err.Function = "images.IDFromName" |
| 106 | err.ResourceType = "image" |
| 107 | err.Name = name |
| 108 | return "", err |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 109 | case 1: |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame^] | 110 | return id, nil |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 111 | default: |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame^] | 112 | err := &gophercloud.ErrMultipleResourcesFound{} |
| 113 | err.Function = "images.IDFromName" |
| 114 | err.ResourceType = "image" |
| 115 | err.Name = name |
| 116 | err.Count = count |
| 117 | return "", err |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 118 | } |
| 119 | } |