blob: aa099591bcef2e4b568032bb6539470b4c404547 [file] [log] [blame]
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08001package images
2
3import (
Jon Perrittad5f1cb2015-05-20 10:38:13 -06004 "fmt"
5
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud"
7 "github.com/gophercloud/gophercloud/pagination"
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08008)
9
Jon Perritt04851d32014-10-14 02:07:13 -050010// ListOptsBuilder allows extensions to add additional parameters to the
11// List request.
12type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050013 ToImageListQuery() (string, error)
Jon Perritt04851d32014-10-14 02:07:13 -050014}
15
Jon Perrittef168e62014-10-08 11:14:05 -050016// ListOpts contain options for limiting the number of Images returned from a call to ListDetail.
17type ListOpts struct {
18 // When the image last changed status (in date-time format).
19 ChangesSince string `q:"changes-since"`
20 // The number of Images to return.
21 Limit int `q:"limit"`
22 // UUID of the Image at which to set a marker.
23 Marker string `q:"marker"`
24 // The name of the Image.
Joe Topjian661a3c82015-02-11 03:56:12 +000025 Name string `q:"name"`
Jon Perrittef168e62014-10-08 11:14:05 -050026 // The name of the Server (in URL format).
27 Server string `q:"server"`
28 // The current status of the Image.
29 Status string `q:"status"`
30 // The value of the type of image (e.g. BASE, SERVER, ALL)
31 Type string `q:"type"`
32}
33
Jon Perritt26780d52014-10-14 11:35:58 -050034// ToImageListQuery formats a ListOpts into a query string.
35func (opts ListOpts) ToImageListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050036 q, err := gophercloud.BuildQueryString(opts)
37 if err != nil {
38 return "", err
39 }
40 return q.String(), nil
41}
42
Jon Perrittef168e62014-10-08 11:14:05 -050043// ListDetail enumerates the available images.
Jon Perritt04851d32014-10-14 02:07:13 -050044func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perrittef168e62014-10-08 11:14:05 -050045 url := listDetailURL(client)
46 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050047 query, err := opts.ToImageListQuery()
Jon Perrittef168e62014-10-08 11:14:05 -050048 if err != nil {
49 return pagination.Pager{Err: err}
50 }
Jon Perritt04851d32014-10-14 02:07:13 -050051 url += query
Jon Perrittef168e62014-10-08 11:14:05 -050052 }
53
Ash Wilsonb8b16f82014-10-20 10:19:49 -040054 createPage := func(r pagination.PageResult) pagination.Page {
55 return ImagePage{pagination.LinkedPageBase{PageResult: r}}
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080056 }
57
Jon Perrittef168e62014-10-08 11:14:05 -050058 return pagination.NewPager(client, url, createPage)
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080059}
Ash Wilson7ddf0362014-09-17 10:59:09 -040060
61// Get acquires additional detail about a specific image by ID.
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070062// Use ExtractImage() to interpret the result as an openstack Image.
Ash Wilsond2f87032014-09-25 11:34:41 -040063func Get(client *gophercloud.ServiceClient, id string) GetResult {
Ash Wilson7ddf0362014-09-17 10:59:09 -040064 var result GetResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010065 _, result.Err = client.Get(getURL(client, id), &result.Body, nil)
Ash Wilsond2f87032014-09-25 11:34:41 -040066 return result
Ash Wilson7ddf0362014-09-17 10:59:09 -040067}
Jesse Nelsonab02e572015-04-14 16:28:21 -070068
69// Delete deletes the specified image ID.
70func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
71 var result DeleteResult
Jesse Nelson8c1e0372015-04-16 10:54:40 -070072 _, result.Err = client.Delete(deleteURL(client, id), nil)
Jesse Nelsonab02e572015-04-14 16:28:21 -070073 return result
74}
Jon Perrittad5f1cb2015-05-20 10:38:13 -060075
76// IDFromName is a convienience function that returns an image's ID given its name.
77func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
78 imageCount := 0
79 imageID := ""
80 if name == "" {
81 return "", fmt.Errorf("An image name must be provided.")
82 }
83 pager := ListDetail(client, &ListOpts{
84 Name: name,
85 })
86 pager.EachPage(func(page pagination.Page) (bool, error) {
87 imageList, err := ExtractImages(page)
88 if err != nil {
89 return false, err
90 }
91
92 for _, i := range imageList {
93 if i.Name == name {
94 imageCount++
95 imageID = i.ID
96 }
97 }
98 return true, nil
99 })
100
101 switch imageCount {
102 case 0:
103 return "", fmt.Errorf("Unable to find image: %s", name)
104 case 1:
105 return imageID, nil
106 default:
107 return "", fmt.Errorf("Found %d images matching %s", imageCount, name)
108 }
109}