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 ( |
| 4 | "github.com/mitchellh/mapstructure" |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud" |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 8 | |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 9 | // GetResult temporarily stores a Get response. |
| 10 | type GetResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 11 | gophercloud.Result |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 12 | } |
| 13 | |
| 14 | // Extract interprets a GetResult as an Image. |
| 15 | func (gr GetResult) Extract() (*Image, error) { |
| 16 | if gr.Err != nil { |
| 17 | return nil, gr.Err |
| 18 | } |
| 19 | |
| 20 | var decoded struct { |
| 21 | Image Image `mapstructure:"image"` |
| 22 | } |
| 23 | |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 24 | err := mapstructure.Decode(gr.Body, &decoded) |
Ash Wilson | d2f8703 | 2014-09-25 11:34:41 -0400 | [diff] [blame] | 25 | return &decoded.Image, err |
| 26 | } |
| 27 | |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 28 | // Image is used for JSON (un)marshalling. |
| 29 | // It provides a description of an OS image. |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 30 | type Image struct { |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 31 | // ID contains the image's unique identifier. |
| 32 | ID string |
| 33 | |
| 34 | Created string |
| 35 | |
| 36 | // MinDisk and MinRAM specify the minimum resources a server must provide to be able to install the image. |
| 37 | MinDisk int |
| 38 | MinRAM int |
| 39 | |
| 40 | // Name provides a human-readable moniker for the OS image. |
| 41 | Name string |
| 42 | |
| 43 | // The Progress and Status fields indicate image-creation status. |
| 44 | // Any usable image will have 100% progress. |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 45 | Progress int |
| 46 | Status string |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 47 | |
| 48 | Updated string |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 51 | // ImagePage contains a single page of results from a List operation. |
| 52 | // Use ExtractImages to convert it into a slice of usable structs. |
| 53 | type ImagePage struct { |
| 54 | pagination.LinkedPageBase |
| 55 | } |
| 56 | |
| 57 | // IsEmpty returns true if a page contains no Image results. |
| 58 | func (page ImagePage) IsEmpty() (bool, error) { |
| 59 | images, err := ExtractImages(page) |
| 60 | if err != nil { |
| 61 | return true, err |
| 62 | } |
| 63 | return len(images) == 0, nil |
| 64 | } |
| 65 | |
| 66 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
| 67 | func (page ImagePage) NextPageURL() (string, error) { |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 68 | type resp struct { |
Jamie Hannaford | a581acd | 2014-10-08 17:14:13 +0200 | [diff] [blame] | 69 | Links []gophercloud.Link `mapstructure:"images_links"` |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | var r resp |
| 73 | err := mapstructure.Decode(page.Body, &r) |
| 74 | if err != nil { |
| 75 | return "", err |
| 76 | } |
| 77 | |
Jamie Hannaford | a581acd | 2014-10-08 17:14:13 +0200 | [diff] [blame] | 78 | return gophercloud.ExtractNextURL(r.Links) |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 79 | } |
| 80 | |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 81 | // ExtractImages converts a page of List results into a slice of usable Image structs. |
| 82 | func ExtractImages(page pagination.Page) ([]Image, error) { |
Ash Wilson | 501b4f3 | 2014-09-25 15:16:02 -0400 | [diff] [blame] | 83 | casted := page.(ImagePage).Body |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 84 | var results struct { |
| 85 | Images []Image `mapstructure:"images"` |
| 86 | } |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 87 | |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame] | 88 | err := mapstructure.Decode(casted, &results) |
| 89 | return results.Images, err |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 90 | } |