blob: 2b8408d7c69cb473712e46635f037414cb1665f0 [file] [log] [blame]
Jamie Hannaford4721abc2014-09-16 16:29:04 +02001package extensions
2
3import (
Jamie Hannafordd9036422014-09-23 17:50:24 +02004 "fmt"
5
Jamie Hannaford4721abc2014-09-16 16:29:04 +02006 "github.com/mitchellh/mapstructure"
Jamie Hannafordd9036422014-09-23 17:50:24 +02007 "github.com/rackspace/gophercloud"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02008 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford4721abc2014-09-16 16:29:04 +02009)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020010
Jamie Hannafordd9036422014-09-23 17:50:24 +020011type GetResult struct {
12 gophercloud.CommonResult
13}
14
15func (r GetResult) Extract() (*Extension, error) {
16 if r.Err != nil {
17 return nil, r.Err
18 }
19
20 var res struct {
21 Extension *Extension `json:"extension"`
22 }
23
24 err := mapstructure.Decode(r.Resp, &res)
25 if err != nil {
26 return nil, fmt.Errorf("Error decoding Neutron extension: %v", err)
27 }
28
29 return res.Extension, nil
30}
31
Jamie Hannafordc65e1922014-09-22 13:20:58 +020032// Extension is a struct that represents a Neutron extension.
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020033type Extension struct {
34 Updated string `json:"updated"`
35 Name string `json:"name"`
36 Links []interface{} `json:"links"`
37 Namespace string `json:"namespace"`
38 Alias string `json:"alias"`
39 Description string `json:"description"`
40}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020041
Jamie Hannafordc65e1922014-09-22 13:20:58 +020042// ExtensionPage is the page returned by a pager when traversing over a
43// collection of extensions.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020044type ExtensionPage struct {
45 pagination.SinglePageBase
46}
47
Jamie Hannafordc65e1922014-09-22 13:20:58 +020048// IsEmpty checks whether an ExtensionPage struct is empty.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020049func (r ExtensionPage) IsEmpty() (bool, error) {
50 is, err := ExtractExtensions(r)
51 if err != nil {
52 return true, err
53 }
54 return len(is) == 0, nil
55}
56
Jamie Hannafordc65e1922014-09-22 13:20:58 +020057// ExtractExtensions accepts a Page struct, specifically an ExtensionPage
58// struct, and extracts the elements into a slice of Extension structs. In other
59// words, a generic collection is mapped into a relevant slice.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020060func ExtractExtensions(page pagination.Page) ([]Extension, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020061 var resp struct {
62 Extensions []Extension `mapstructure:"extensions"`
63 }
64
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020065 err := mapstructure.Decode(page.(ExtensionPage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020066 if err != nil {
67 return nil, err
68 }
69
70 return resp.Extensions, nil
71}