blob: a0b2cbf4541406ae66157555d9cc83f3f5ddea59 [file] [log] [blame]
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08001package images
2
3import (
Ash Wilson71ff2fe2014-09-25 13:39:27 -04004 "github.com/mitchellh/mapstructure"
Ash Wilson7ddf0362014-09-17 10:59:09 -04005 "github.com/racker/perigee"
Ash Wilson9ccf9b62014-09-17 10:07:52 -04006 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08008)
9
Ash Wilsonfd043792014-09-17 10:40:17 -040010// ListPage contains a single page of results from a List operation.
Ash Wilson9ccf9b62014-09-17 10:07:52 -040011// Use ExtractImages to convert it into a slice of usable structs.
Ash Wilsonfd043792014-09-17 10:40:17 -040012type ListPage struct {
Ash Wilson71ff2fe2014-09-25 13:39:27 -040013 pagination.LinkedPageBase
Ash Wilson9ccf9b62014-09-17 10:07:52 -040014}
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080015
Ash Wilson9ccf9b62014-09-17 10:07:52 -040016// IsEmpty returns true if a page contains no Image results.
Ash Wilsonfd043792014-09-17 10:40:17 -040017func (page ListPage) IsEmpty() (bool, error) {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040018 images, err := ExtractImages(page)
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080019 if err != nil {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040020 return true, err
21 }
22 return len(images) == 0, nil
23}
24
Ash Wilson71ff2fe2014-09-25 13:39:27 -040025// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
26func (page ListPage) NextPageURL() (string, error) {
27 type link struct {
28 Href string `mapstructure:"href"`
29 Rel string `mapstructure:"rel"`
30 }
31 type resp struct {
32 Links []link `mapstructure:"images_links"`
33 }
34
35 var r resp
36 err := mapstructure.Decode(page.Body, &r)
Ash Wilson9ccf9b62014-09-17 10:07:52 -040037 if err != nil {
38 return "", err
39 }
Ash Wilson71ff2fe2014-09-25 13:39:27 -040040
41 var url string
42 for _, l := range r.Links {
43 if l.Rel == "next" {
44 url = l.Href
45 }
46 }
47 if url == "" {
Ash Wilson9ccf9b62014-09-17 10:07:52 -040048 return "", nil
49 }
Ash Wilson71ff2fe2014-09-25 13:39:27 -040050
51 return url, nil
Ash Wilson9ccf9b62014-09-17 10:07:52 -040052}
53
54// List enumerates the available images.
55func List(client *gophercloud.ServiceClient) pagination.Pager {
56 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilson71ff2fe2014-09-25 13:39:27 -040057 return ListPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080058 }
59
Ash Wilson31f6bde2014-09-25 14:52:12 -040060 return pagination.NewPager(client, listURL(client), createPage)
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080061}
Ash Wilson7ddf0362014-09-17 10:59:09 -040062
63// Get acquires additional detail about a specific image by ID.
64// Use ExtractImage() to intepret the result as an openstack Image.
Ash Wilsond2f87032014-09-25 11:34:41 -040065func Get(client *gophercloud.ServiceClient, id string) GetResult {
Ash Wilson7ddf0362014-09-17 10:59:09 -040066 var result GetResult
Ash Wilson31f6bde2014-09-25 14:52:12 -040067 _, result.Err = perigee.Request("GET", imageURL(client, id), perigee.Options{
Ash Wilson7ddf0362014-09-17 10:59:09 -040068 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Ash Wilsond2f87032014-09-25 11:34:41 -040069 Results: &result.Resp,
Ash Wilson7ddf0362014-09-17 10:59:09 -040070 OkCodes: []int{200},
71 })
Ash Wilsond2f87032014-09-25 11:34:41 -040072 return result
Ash Wilson7ddf0362014-09-17 10:59:09 -040073}