Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 1 | package images |
| 2 | |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 3 | import ( |
Krzysztof Szukiełojć | 3f41d08 | 2017-05-07 14:43:06 +0200 | [diff] [blame^] | 4 | "gerrit.mcp.mirantis.net/debian/gophercloud.git" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame] | 5 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 6 | ) |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 7 | |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 8 | // GetResult temporarily stores a Get response. |
| 9 | type GetResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 10 | gophercloud.Result |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 11 | } |
| 12 | |
Jesse Nelson | ab02e57 | 2015-04-14 16:28:21 -0700 | [diff] [blame] | 13 | // DeleteResult represents the result of an image.Delete operation. |
| 14 | type DeleteResult struct { |
| 15 | gophercloud.ErrResult |
| 16 | } |
| 17 | |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 18 | // Extract interprets a GetResult as an Image. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 19 | func (r GetResult) Extract() (*Image, error) { |
| 20 | var s struct { |
| 21 | Image *Image `json:"image"` |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 22 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 23 | err := r.ExtractInto(&s) |
| 24 | return s.Image, err |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 25 | } |
| 26 | |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 27 | // Image is used for JSON (un)marshalling. |
| 28 | // It provides a description of an OS image. |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 29 | type Image struct { |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 30 | // ID contains the image's unique identifier. |
| 31 | ID string |
| 32 | |
| 33 | Created string |
| 34 | |
| 35 | // MinDisk and MinRAM specify the minimum resources a server must provide to be able to install the image. |
| 36 | MinDisk int |
| 37 | MinRAM int |
| 38 | |
| 39 | // Name provides a human-readable moniker for the OS image. |
| 40 | Name string |
| 41 | |
| 42 | // The Progress and Status fields indicate image-creation status. |
| 43 | // Any usable image will have 100% progress. |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 44 | Progress int |
| 45 | Status string |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 46 | |
| 47 | Updated string |
jrperritt | 5cb543c | 2017-02-20 14:03:36 -0600 | [diff] [blame] | 48 | |
Joe Topjian | afdc350 | 2017-03-09 18:51:37 -0700 | [diff] [blame] | 49 | Metadata map[string]interface{} |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 52 | // ImagePage contains a single page of results from a List operation. |
| 53 | // Use ExtractImages to convert it into a slice of usable structs. |
| 54 | type ImagePage struct { |
| 55 | pagination.LinkedPageBase |
| 56 | } |
| 57 | |
| 58 | // IsEmpty returns true if a page contains no Image results. |
| 59 | func (page ImagePage) IsEmpty() (bool, error) { |
| 60 | images, err := ExtractImages(page) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 61 | return len(images) == 0, err |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
| 65 | func (page ImagePage) NextPageURL() (string, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 66 | var s struct { |
| 67 | Links []gophercloud.Link `json:"images_links"` |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 68 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 69 | err := page.ExtractInto(&s) |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 70 | if err != nil { |
| 71 | return "", err |
| 72 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 73 | return gophercloud.ExtractNextURL(s.Links) |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 76 | // ExtractImages converts a page of List results into a slice of usable Image structs. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 77 | func ExtractImages(r pagination.Page) ([]Image, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 78 | var s struct { |
| 79 | Images []Image `json:"images"` |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 80 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 81 | err := (r.(ImagePage)).ExtractInto(&s) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 82 | return s.Images, err |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 83 | } |