blob: 4827072c5833bbcc2fff1573d757406457f5fda9 [file] [log] [blame]
Jamie Hannaford8c072a32014-10-16 14:33:32 +02001package extensions
2
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
7)
8
9// GetResult temporarily stores the result of a Get call.
10// Use its Extract() method to interpret it as an Extension.
11type GetResult struct {
12 gophercloud.CommonResult
13}
14
15// Extract interprets a GetResult as an Extension.
16func (r GetResult) Extract() (*Extension, error) {
17 if r.Err != nil {
18 return nil, r.Err
19 }
20
21 var res struct {
22 Extension *Extension `json:"extension"`
23 }
24
25 err := mapstructure.Decode(r.Resp, &res)
26
27 return res.Extension, err
28}
29
30// Extension is a struct that represents an OpenStack extension.
31type Extension struct {
32 Updated string `json:"updated" mapstructure:"updated"`
33 Name string `json:"name" mapstructure:"name"`
34 Links []interface{} `json:"links" mapstructure:"links"`
35 Namespace string `json:"namespace" mapstructure:"namespace"`
36 Alias string `json:"alias" mapstructure:"alias"`
37 Description string `json:"description" mapstructure:"description"`
38}
39
40// ExtensionPage is the page returned by a pager when traversing over a collection of extensions.
41type ExtensionPage struct {
42 pagination.SinglePageBase
43}
44
45// IsEmpty checks whether an ExtensionPage struct is empty.
46func (r ExtensionPage) IsEmpty() (bool, error) {
47 is, err := ExtractExtensions(r)
48 if err != nil {
49 return true, err
50 }
51 return len(is) == 0, nil
52}
53
54// ExtractExtensions accepts a Page struct, specifically an ExtensionPage struct, and extracts the
55// elements into a slice of Extension structs.
56// In other words, a generic collection is mapped into a relevant slice.
57func ExtractExtensions(page pagination.Page) ([]Extension, error) {
58 var resp struct {
59 Extensions []Extension `mapstructure:"extensions"`
60 }
61
62 err := mapstructure.Decode(page.(ExtensionPage).Body, &resp)
63
64 return resp.Extensions, err
65}