blob: f9ebc69e98d72867cbf32edf518d2a6d7c642f3a [file] [log] [blame]
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08001package images
2
Ash Wilson9ccf9b62014-09-17 10:07:52 -04003import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Ash Wilson9ccf9b62014-09-17 10:07:52 -04006)
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08007
Ash Wilsond2f87032014-09-25 11:34:41 -04008// GetResult temporarily stores a Get response.
9type GetResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040010 gophercloud.Result
Ash Wilsond2f87032014-09-25 11:34:41 -040011}
12
Jesse Nelsonab02e572015-04-14 16:28:21 -070013// DeleteResult represents the result of an image.Delete operation.
14type DeleteResult struct {
15 gophercloud.ErrResult
16}
17
Ash Wilsond2f87032014-09-25 11:34:41 -040018// Extract interprets a GetResult as an Image.
Jon Perritt12395212016-02-24 10:41:17 -060019func (r GetResult) Extract() (*Image, error) {
20 var s struct {
21 Image *Image `json:"image"`
Ash Wilsond2f87032014-09-25 11:34:41 -040022 }
Jon Perritt12395212016-02-24 10:41:17 -060023 err := r.ExtractInto(&s)
24 return s.Image, err
Ash Wilsond2f87032014-09-25 11:34:41 -040025}
26
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080027// Image is used for JSON (un)marshalling.
28// It provides a description of an OS image.
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080029type Image struct {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040030 // ID contains the image's unique identifier.
31 ID string
32
33 Created string
34
35 // MinDisk and MinRAM specify the minimum resources a server must provide to be able to install the image.
36 MinDisk int
37 MinRAM int
38
39 // Name provides a human-readable moniker for the OS image.
40 Name string
41
42 // The Progress and Status fields indicate image-creation status.
43 // Any usable image will have 100% progress.
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080044 Progress int
45 Status string
Ash Wilson9ccf9b62014-09-17 10:07:52 -040046
47 Updated string
jrperritt5cb543c2017-02-20 14:03:36 -060048
Joe Topjianafdc3502017-03-09 18:51:37 -070049 Metadata map[string]interface{}
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080050}
51
Ash Wilson501b4f32014-09-25 15:16:02 -040052// ImagePage contains a single page of results from a List operation.
53// Use ExtractImages to convert it into a slice of usable structs.
54type ImagePage struct {
55 pagination.LinkedPageBase
56}
57
58// IsEmpty returns true if a page contains no Image results.
59func (page ImagePage) IsEmpty() (bool, error) {
60 images, err := ExtractImages(page)
Jon Perritt12395212016-02-24 10:41:17 -060061 return len(images) == 0, err
Ash Wilson501b4f32014-09-25 15:16:02 -040062}
63
64// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
65func (page ImagePage) NextPageURL() (string, error) {
Jon Perritt12395212016-02-24 10:41:17 -060066 var s struct {
67 Links []gophercloud.Link `json:"images_links"`
Ash Wilson501b4f32014-09-25 15:16:02 -040068 }
Jon Perritt12395212016-02-24 10:41:17 -060069 err := page.ExtractInto(&s)
Ash Wilson501b4f32014-09-25 15:16:02 -040070 if err != nil {
71 return "", err
72 }
Jon Perritt12395212016-02-24 10:41:17 -060073 return gophercloud.ExtractNextURL(s.Links)
Ash Wilson501b4f32014-09-25 15:16:02 -040074}
75
Ash Wilson9ccf9b62014-09-17 10:07:52 -040076// ExtractImages converts a page of List results into a slice of usable Image structs.
Jon Perritt31b66462016-02-25 22:25:30 -060077func ExtractImages(r pagination.Page) ([]Image, error) {
Jon Perritt12395212016-02-24 10:41:17 -060078 var s struct {
79 Images []Image `json:"images"`
Ash Wilsonfaf006d2014-09-24 17:10:58 -040080 }
Jon Perritt31b66462016-02-25 22:25:30 -060081 err := (r.(ImagePage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -060082 return s.Images, err
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070083}