blob: 42e7fc3a51a79f13d0e6d4294bc2304779d726e2 [file] [log] [blame]
Jamie Hannaford4721abc2014-09-16 16:29:04 +02001package extensions
2
3import (
4 "github.com/mitchellh/mapstructure"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02005 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford4721abc2014-09-16 16:29:04 +02006)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +02007
Jamie Hannafordc65e1922014-09-22 13:20:58 +02008// Extension is a struct that represents a Neutron extension.
Jamie Hannaford1ce30f22014-09-16 11:23:34 +02009type Extension struct {
10 Updated string `json:"updated"`
11 Name string `json:"name"`
12 Links []interface{} `json:"links"`
13 Namespace string `json:"namespace"`
14 Alias string `json:"alias"`
15 Description string `json:"description"`
16}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020017
Jamie Hannafordc65e1922014-09-22 13:20:58 +020018// ExtensionPage is the page returned by a pager when traversing over a
19// collection of extensions.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020020type ExtensionPage struct {
21 pagination.SinglePageBase
22}
23
Jamie Hannafordc65e1922014-09-22 13:20:58 +020024// IsEmpty checks whether an ExtensionPage struct is empty.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020025func (r ExtensionPage) IsEmpty() (bool, error) {
26 is, err := ExtractExtensions(r)
27 if err != nil {
28 return true, err
29 }
30 return len(is) == 0, nil
31}
32
Jamie Hannafordc65e1922014-09-22 13:20:58 +020033// ExtractExtensions accepts a Page struct, specifically an ExtensionPage
34// struct, and extracts the elements into a slice of Extension structs. In other
35// words, a generic collection is mapped into a relevant slice.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020036func ExtractExtensions(page pagination.Page) ([]Extension, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020037 var resp struct {
38 Extensions []Extension `mapstructure:"extensions"`
39 }
40
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020041 err := mapstructure.Decode(page.(ExtensionPage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020042 if err != nil {
43 return nil, err
44 }
45
46 return resp.Extensions, nil
47}