blob: 00461f8d5a55a191816538b4f3e94048cbc670ee [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.
Ash Wilsond2f87032014-09-25 11:34:41 -040055func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060056 var r GetResult
57 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
58 return r
Ash Wilson7ddf0362014-09-17 10:59:09 -040059}
Jesse Nelsonab02e572015-04-14 16:28:21 -070060
61// Delete deletes the specified image ID.
62func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060063 var r DeleteResult
64 _, r.Err = client.Delete(deleteURL(client, id), nil)
65 return r
Jesse Nelsonab02e572015-04-14 16:28:21 -070066}
Jon Perrittad5f1cb2015-05-20 10:38:13 -060067
68// IDFromName is a convienience function that returns an image's ID given its name.
69func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -060070 count := 0
71 id := ""
Jon Perrittf094fef2016-03-07 01:41:59 -060072 allPages, err := ListDetail(client, nil).AllPages()
73 if err != nil {
74 return "", err
75 }
Jon Perrittad5f1cb2015-05-20 10:38:13 -060076
Jon Perrittf094fef2016-03-07 01:41:59 -060077 all, err := ExtractImages(allPages)
78 if err != nil {
79 return "", err
80 }
81
82 for _, f := range all {
83 if f.Name == name {
84 count++
85 id = f.ID
86 }
87 }
88
89 switch count {
Jon Perrittad5f1cb2015-05-20 10:38:13 -060090 case 0:
Jon Perrittf094fef2016-03-07 01:41:59 -060091 err := &gophercloud.ErrResourceNotFound{}
Jon Perrittf094fef2016-03-07 01:41:59 -060092 err.ResourceType = "image"
93 err.Name = name
94 return "", err
Jon Perrittad5f1cb2015-05-20 10:38:13 -060095 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -060096 return id, nil
Jon Perrittad5f1cb2015-05-20 10:38:13 -060097 default:
Jon Perrittf094fef2016-03-07 01:41:59 -060098 err := &gophercloud.ErrMultipleResourcesFound{}
Jon Perrittf094fef2016-03-07 01:41:59 -060099 err.ResourceType = "image"
100 err.Name = name
101 err.Count = count
102 return "", err
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600103 }
104}