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