blob: 1422cd094a0df23d09086eea042decc162131b0e [file] [log] [blame]
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08001package images
2
3import (
Ash Wilson9ccf9b62014-09-17 10:07:52 -04004 "github.com/rackspace/gophercloud"
5 "github.com/rackspace/gophercloud/pagination"
Jon Perritt04851d32014-10-14 02:07:13 -05006
7 "github.com/racker/perigee"
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.
25 Name string `q:"name:"`
26 // 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.
62// Use ExtractImage() to intepret 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
Jon Perrittef168e62014-10-08 11:14:05 -050065 _, result.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040066 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsond3dc2542014-10-20 10:10:48 -040067 Results: &result.Body,
Ash Wilson7ddf0362014-09-17 10:59:09 -040068 OkCodes: []int{200},
69 })
Ash Wilsond2f87032014-09-25 11:34:41 -040070 return result
Ash Wilson7ddf0362014-09-17 10:59:09 -040071}