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 ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/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 |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 50 | // ImagePage contains a single page of results from a List operation. |
| 51 | // Use ExtractImages to convert it into a slice of usable structs. |
| 52 | type ImagePage struct { |
| 53 | pagination.LinkedPageBase |
| 54 | } |
| 55 | |
| 56 | // IsEmpty returns true if a page contains no Image results. |
| 57 | func (page ImagePage) IsEmpty() (bool, error) { |
| 58 | images, err := ExtractImages(page) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 59 | return len(images) == 0, err |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
| 63 | func (page ImagePage) NextPageURL() (string, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 64 | var s struct { |
| 65 | Links []gophercloud.Link `json:"images_links"` |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 66 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 67 | err := page.ExtractInto(&s) |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 68 | if err != nil { |
| 69 | return "", err |
| 70 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 71 | return gophercloud.ExtractNextURL(s.Links) |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 74 | // ExtractImages converts a page of List results into a slice of usable Image structs. |
| 75 | func ExtractImages(page pagination.Page) ([]Image, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 76 | r := page.(ImagePage) |
| 77 | var s struct { |
| 78 | Images []Image `json:"images"` |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 79 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 80 | err := r.ExtractInto(&s) |
| 81 | return s.Images, err |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 82 | } |