blob: 40e814d1de0ceecc598b433e20876e909dc37a89 [file] [log] [blame]
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08001package images
2
Ash Wilson9ccf9b62014-09-17 10:07:52 -04003import (
4 "github.com/mitchellh/mapstructure"
Ash Wilsond2f87032014-09-25 11:34:41 -04005 "github.com/rackspace/gophercloud"
Ash Wilson9ccf9b62014-09-17 10:07:52 -04006 "github.com/rackspace/gophercloud/pagination"
7)
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08008
Ash Wilsond2f87032014-09-25 11:34:41 -04009// GetResult temporarily stores a Get response.
10type GetResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040011 gophercloud.Result
Ash Wilsond2f87032014-09-25 11:34:41 -040012}
13
Jesse Nelsonab02e572015-04-14 16:28:21 -070014// DeleteResult represents the result of an image.Delete operation.
15type DeleteResult struct {
16 gophercloud.ErrResult
17}
18
Ash Wilsond2f87032014-09-25 11:34:41 -040019// Extract interprets a GetResult as an Image.
20func (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 Wilsond3dc2542014-10-20 10:10:48 -040029 err := mapstructure.Decode(gr.Body, &decoded)
Ash Wilsond2f87032014-09-25 11:34:41 -040030 return &decoded.Image, err
31}
32
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080033// Image is used for JSON (un)marshalling.
34// It provides a description of an OS image.
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080035type Image struct {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040036 // 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 IIe246ac02014-02-13 23:20:09 -080050 Progress int
51 Status string
Ash Wilson9ccf9b62014-09-17 10:07:52 -040052
53 Updated string
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080054}
55
Ash Wilson501b4f32014-09-25 15:16:02 -040056// ImagePage contains a single page of results from a List operation.
57// Use ExtractImages to convert it into a slice of usable structs.
58type ImagePage struct {
59 pagination.LinkedPageBase
60}
61
62// IsEmpty returns true if a page contains no Image results.
63func (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.
72func (page ImagePage) NextPageURL() (string, error) {
Ash Wilson501b4f32014-09-25 15:16:02 -040073 type resp struct {
Jamie Hannaforda581acd2014-10-08 17:14:13 +020074 Links []gophercloud.Link `mapstructure:"images_links"`
Ash Wilson501b4f32014-09-25 15:16:02 -040075 }
76
77 var r resp
78 err := mapstructure.Decode(page.Body, &r)
79 if err != nil {
80 return "", err
81 }
82
Jamie Hannaforda581acd2014-10-08 17:14:13 +020083 return gophercloud.ExtractNextURL(r.Links)
Ash Wilson501b4f32014-09-25 15:16:02 -040084}
85
Ash Wilson9ccf9b62014-09-17 10:07:52 -040086// ExtractImages converts a page of List results into a slice of usable Image structs.
87func ExtractImages(page pagination.Page) ([]Image, error) {
Ash Wilson501b4f32014-09-25 15:16:02 -040088 casted := page.(ImagePage).Body
Ash Wilsonfaf006d2014-09-24 17:10:58 -040089 var results struct {
90 Images []Image `mapstructure:"images"`
91 }
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080092
Ash Wilsonfaf006d2014-09-24 17:10:58 -040093 err := mapstructure.Decode(casted, &results)
94 return results.Images, err
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070095}