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" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
| 6 | ) |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 7 | |
| 8 | // Image is used for JSON (un)marshalling. |
| 9 | // It provides a description of an OS image. |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 10 | type Image struct { |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 11 | // ID contains the image's unique identifier. |
| 12 | ID string |
| 13 | |
| 14 | Created string |
| 15 | |
| 16 | // MinDisk and MinRAM specify the minimum resources a server must provide to be able to install the image. |
| 17 | MinDisk int |
| 18 | MinRAM int |
| 19 | |
| 20 | // Name provides a human-readable moniker for the OS image. |
| 21 | Name string |
| 22 | |
| 23 | // The Progress and Status fields indicate image-creation status. |
| 24 | // Any usable image will have 100% progress. |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 25 | Progress int |
| 26 | Status string |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 27 | |
| 28 | Updated string |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Ash Wilson | 9ccf9b6 | 2014-09-17 10:07:52 -0400 | [diff] [blame] | 31 | // ExtractImages converts a page of List results into a slice of usable Image structs. |
| 32 | func ExtractImages(page pagination.Page) ([]Image, error) { |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 33 | casted := page.(ListPage).Body |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame^] | 34 | var results struct { |
| 35 | Images []Image `mapstructure:"images"` |
| 36 | } |
Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame] | 37 | |
Ash Wilson | faf006d | 2014-09-24 17:10:58 -0400 | [diff] [blame^] | 38 | err := mapstructure.Decode(casted, &results) |
| 39 | return results.Images, err |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 40 | } |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 41 | |
| 42 | // ExtractImage converts the result of a Get call into a more usable Image structure. |
| 43 | func ExtractImage(result GetResult) (Image, error) { |
| 44 | var decoded Image |
| 45 | err := mapstructure.Decode(result, &decoded) |
| 46 | return decoded, err |
| 47 | } |