blob: c9832d44eb0a2f692d5676d74aff7d7f49654669 [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
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080048}
49
Ash Wilson501b4f32014-09-25 15:16:02 -040050// ImagePage contains a single page of results from a List operation.
51// Use ExtractImages to convert it into a slice of usable structs.
52type ImagePage struct {
53 pagination.LinkedPageBase
54}
55
56// IsEmpty returns true if a page contains no Image results.
57func (page ImagePage) IsEmpty() (bool, error) {
58 images, err := ExtractImages(page)
Jon Perritt12395212016-02-24 10:41:17 -060059 return len(images) == 0, err
Ash Wilson501b4f32014-09-25 15:16:02 -040060}
61
62// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
63func (page ImagePage) NextPageURL() (string, error) {
Jon Perritt12395212016-02-24 10:41:17 -060064 var s struct {
65 Links []gophercloud.Link `json:"images_links"`
Ash Wilson501b4f32014-09-25 15:16:02 -040066 }
Jon Perritt12395212016-02-24 10:41:17 -060067 err := page.ExtractInto(&s)
Ash Wilson501b4f32014-09-25 15:16:02 -040068 if err != nil {
69 return "", err
70 }
Jon Perritt12395212016-02-24 10:41:17 -060071 return gophercloud.ExtractNextURL(s.Links)
Ash Wilson501b4f32014-09-25 15:16:02 -040072}
73
Ash Wilson9ccf9b62014-09-17 10:07:52 -040074// ExtractImages converts a page of List results into a slice of usable Image structs.
75func ExtractImages(page pagination.Page) ([]Image, error) {
Jon Perritt12395212016-02-24 10:41:17 -060076 r := page.(ImagePage)
77 var s struct {
78 Images []Image `json:"images"`
Ash Wilsonfaf006d2014-09-24 17:10:58 -040079 }
Jon Perritt12395212016-02-24 10:41:17 -060080 err := r.ExtractInto(&s)
81 return s.Images, err
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070082}