1package images
2
3import (
4	"github.com/gophercloud/gophercloud"
5	"github.com/gophercloud/gophercloud/pagination"
6)
7
8// GetResult is the response from a Get operation. Call its Extract method to
9// interpret it as an Image.
10type GetResult struct {
11	gophercloud.Result
12}
13
14// DeleteResult is the result from a Delete operation. Call its ExtractErr
15// method to determine if the call succeeded or failed.
16type DeleteResult struct {
17	gophercloud.ErrResult
18}
19
20// Extract interprets a GetResult as an Image.
21func (r GetResult) Extract() (*Image, error) {
22	var s struct {
23		Image *Image `json:"image"`
24	}
25	err := r.ExtractInto(&s)
26	return s.Image, err
27}
28
29// Image represents an Image returned by the Compute API.
30type Image struct {
31	// ID is the unique ID of an image.
32	ID string
33
34	// Created is the date when the image was created.
35	Created string
36
37	// MinDisk is the minimum amount of disk a flavor must have to be able
38	// to create a server based on the image, measured in GB.
39	MinDisk int
40
41	// MinRAM is the minimum amount of RAM a flavor must have to be able
42	// to create a server based on the image, measured in MB.
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	Progress int
50
51	// Status is the current status of the image.
52	Status string
53
54	// Update is the date when the image was updated.
55	Updated string
56
57	// Metadata provides free-form key/value pairs that further describe the
58	// image.
59	Metadata map[string]interface{}
60}
61
62// ImagePage contains a single page of all Images returne from a ListDetail
63// operation. Use ExtractImages to convert it into a slice of usable structs.
64type ImagePage struct {
65	pagination.LinkedPageBase
66}
67
68// IsEmpty returns true if an ImagePage contains no Image results.
69func (page ImagePage) IsEmpty() (bool, error) {
70	images, err := ExtractImages(page)
71	return len(images) == 0, err
72}
73
74// NextPageURL uses the response's embedded link reference to navigate to the
75// next page of results.
76func (page ImagePage) NextPageURL() (string, error) {
77	var s struct {
78		Links []gophercloud.Link `json:"images_links"`
79	}
80	err := page.ExtractInto(&s)
81	if err != nil {
82		return "", err
83	}
84	return gophercloud.ExtractNextURL(s.Links)
85}
86
87// ExtractImages converts a page of List results into a slice of usable Image
88// structs.
89func ExtractImages(r pagination.Page) ([]Image, error) {
90	var s struct {
91		Images []Image `json:"images"`
92	}
93	err := (r.(ImagePage)).ExtractInto(&s)
94	return s.Images, err
95}
96