blob: 9b2b9859455d943303497cbdb963450ef3fac91b [file] [log] [blame]
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08001package images
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08006)
7
Jon Perritt04851d32014-10-14 02:07:13 -05008// ListOptsBuilder allows extensions to add additional parameters to the
9// List request.
10type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050011 ToImageListQuery() (string, error)
Jon Perritt04851d32014-10-14 02:07:13 -050012}
13
Jon Perrittef168e62014-10-08 11:14:05 -050014// ListOpts contain options for limiting the number of Images returned from a call to ListDetail.
15type ListOpts struct {
16 // When the image last changed status (in date-time format).
17 ChangesSince string `q:"changes-since"`
18 // The number of Images to return.
19 Limit int `q:"limit"`
20 // UUID of the Image at which to set a marker.
21 Marker string `q:"marker"`
22 // The name of the Image.
Joe Topjian661a3c82015-02-11 03:56:12 +000023 Name string `q:"name"`
Jon Perrittef168e62014-10-08 11:14:05 -050024 // The name of the Server (in URL format).
25 Server string `q:"server"`
26 // The current status of the Image.
27 Status string `q:"status"`
28 // The value of the type of image (e.g. BASE, SERVER, ALL)
29 Type string `q:"type"`
30}
31
Jon Perritt26780d52014-10-14 11:35:58 -050032// ToImageListQuery formats a ListOpts into a query string.
33func (opts ListOpts) ToImageListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050034 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060035 return q.String(), err
Jon Perritt04851d32014-10-14 02:07:13 -050036}
37
Jon Perrittef168e62014-10-08 11:14:05 -050038// ListDetail enumerates the available images.
Jon Perritt04851d32014-10-14 02:07:13 -050039func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perrittef168e62014-10-08 11:14:05 -050040 url := listDetailURL(client)
41 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050042 query, err := opts.ToImageListQuery()
Jon Perrittef168e62014-10-08 11:14:05 -050043 if err != nil {
44 return pagination.Pager{Err: err}
45 }
Jon Perritt04851d32014-10-14 02:07:13 -050046 url += query
Jon Perrittef168e62014-10-08 11:14:05 -050047 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060048 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Ash Wilsonb8b16f82014-10-20 10:19:49 -040049 return ImagePage{pagination.LinkedPageBase{PageResult: r}}
Jon Perrittdb0ae142016-03-13 00:33:41 -060050 })
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080051}
Ash Wilson7ddf0362014-09-17 10:59:09 -040052
53// Get acquires additional detail about a specific image by ID.
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070054// Use ExtractImage() to interpret the result as an openstack Image.
Jon Perritt3860b512016-03-29 12:01:48 -050055func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060056 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
Ash Wilson7ddf0362014-09-17 10:59:09 -040057}
Jesse Nelsonab02e572015-04-14 16:28:21 -070058
59// Delete deletes the specified image ID.
Jon Perritt3860b512016-03-29 12:01:48 -050060func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060061 _, r.Err = client.Delete(deleteURL(client, id), nil)
Jesse Nelsonab02e572015-04-14 16:28:21 -070062}
Jon Perrittad5f1cb2015-05-20 10:38:13 -060063
64// IDFromName is a convienience function that returns an image's ID given its name.
65func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -060066 count := 0
67 id := ""
Jon Perrittf094fef2016-03-07 01:41:59 -060068 allPages, err := ListDetail(client, nil).AllPages()
69 if err != nil {
70 return "", err
71 }
Jon Perrittad5f1cb2015-05-20 10:38:13 -060072
Jon Perrittf094fef2016-03-07 01:41:59 -060073 all, err := ExtractImages(allPages)
74 if err != nil {
75 return "", err
76 }
77
78 for _, f := range all {
79 if f.Name == name {
80 count++
81 id = f.ID
82 }
83 }
84
85 switch count {
Jon Perrittad5f1cb2015-05-20 10:38:13 -060086 case 0:
Jon Perrittf094fef2016-03-07 01:41:59 -060087 err := &gophercloud.ErrResourceNotFound{}
Jon Perrittf094fef2016-03-07 01:41:59 -060088 err.ResourceType = "image"
89 err.Name = name
90 return "", err
Jon Perrittad5f1cb2015-05-20 10:38:13 -060091 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -060092 return id, nil
Jon Perrittad5f1cb2015-05-20 10:38:13 -060093 default:
Jon Perrittf094fef2016-03-07 01:41:59 -060094 err := &gophercloud.ErrMultipleResourcesFound{}
Jon Perrittf094fef2016-03-07 01:41:59 -060095 err.ResourceType = "image"
96 err.Name = name
97 err.Count = count
98 return "", err
Jon Perrittad5f1cb2015-05-20 10:38:13 -060099 }
100}