blob: 482e7d6f6ea1be9c99fb1f3e3ce030ad28085f7f [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
jrperritt524bceb2016-05-09 14:35:39 -050054
55 Metadata map[string]string
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080056}
57
Ash Wilson501b4f32014-09-25 15:16:02 -040058// ImagePage contains a single page of results from a List operation.
59// Use ExtractImages to convert it into a slice of usable structs.
60type ImagePage struct {
61 pagination.LinkedPageBase
62}
63
64// IsEmpty returns true if a page contains no Image results.
65func (page ImagePage) IsEmpty() (bool, error) {
66 images, err := ExtractImages(page)
67 if err != nil {
68 return true, err
69 }
70 return len(images) == 0, nil
71}
72
73// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
74func (page ImagePage) NextPageURL() (string, error) {
Ash Wilson501b4f32014-09-25 15:16:02 -040075 type resp struct {
Jamie Hannaforda581acd2014-10-08 17:14:13 +020076 Links []gophercloud.Link `mapstructure:"images_links"`
Ash Wilson501b4f32014-09-25 15:16:02 -040077 }
78
79 var r resp
80 err := mapstructure.Decode(page.Body, &r)
81 if err != nil {
82 return "", err
83 }
84
Jamie Hannaforda581acd2014-10-08 17:14:13 +020085 return gophercloud.ExtractNextURL(r.Links)
Ash Wilson501b4f32014-09-25 15:16:02 -040086}
87
Ash Wilson9ccf9b62014-09-17 10:07:52 -040088// ExtractImages converts a page of List results into a slice of usable Image structs.
89func ExtractImages(page pagination.Page) ([]Image, error) {
Ash Wilson501b4f32014-09-25 15:16:02 -040090 casted := page.(ImagePage).Body
Ash Wilsonfaf006d2014-09-24 17:10:58 -040091 var results struct {
92 Images []Image `mapstructure:"images"`
93 }
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080094
Ash Wilsonfaf006d2014-09-24 17:10:58 -040095 err := mapstructure.Decode(casted, &results)
96 return results.Images, err
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070097}