blob: f93c90ca6c47da91983bb5e56a4773f994342d6a [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 {
11 gophercloud.CommonResult
12}
13
14// Extract interprets a GetResult as an Image.
15func (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
24 err := mapstructure.Decode(gr.Resp, &decoded)
25 return &decoded.Image, err
26}
27
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080028// Image is used for JSON (un)marshalling.
29// It provides a description of an OS image.
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080030type Image struct {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040031 // 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 IIe246ac02014-02-13 23:20:09 -080045 Progress int
46 Status string
Ash Wilson9ccf9b62014-09-17 10:07:52 -040047
48 Updated string
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080049}
50
Ash Wilson501b4f32014-09-25 15:16:02 -040051// ImagePage contains a single page of results from a List operation.
52// Use ExtractImages to convert it into a slice of usable structs.
53type ImagePage struct {
54 pagination.LinkedPageBase
55}
56
57// IsEmpty returns true if a page contains no Image results.
58func (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.
67func (page ImagePage) NextPageURL() (string, error) {
68 type link struct {
69 Href string `mapstructure:"href"`
70 Rel string `mapstructure:"rel"`
71 }
72 type resp struct {
73 Links []link `mapstructure:"images_links"`
74 }
75
76 var r resp
77 err := mapstructure.Decode(page.Body, &r)
78 if err != nil {
79 return "", err
80 }
81
82 var url string
83 for _, l := range r.Links {
84 if l.Rel == "next" {
85 url = l.Href
86 }
87 }
88 if url == "" {
89 return "", nil
90 }
91
92 return url, nil
93}
94
Ash Wilson9ccf9b62014-09-17 10:07:52 -040095// ExtractImages converts a page of List results into a slice of usable Image structs.
96func ExtractImages(page pagination.Page) ([]Image, error) {
Ash Wilson501b4f32014-09-25 15:16:02 -040097 casted := page.(ImagePage).Body
Ash Wilsonfaf006d2014-09-24 17:10:58 -040098 var results struct {
99 Images []Image `mapstructure:"images"`
100 }
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -0800101
Ash Wilsonfaf006d2014-09-24 17:10:58 -0400102 err := mapstructure.Decode(casted, &results)
103 return results.Images, err
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700104}