blob: 02b3c809fb976d359b759c6a8058ddc170956152 [file] [log] [blame]
Ash Wilson61dcb022014-10-03 08:15:47 -04001package extensions
2
3import (
Ash Wilson61dcb022014-10-03 08:15:47 -04004 "github.com/mitchellh/mapstructure"
Jon Perritt27249f42016-02-18 10:35:59 -06005 "github.com/gophercloud/gophercloud"
6 "github.com/gophercloud/gophercloud/pagination"
Ash Wilson61dcb022014-10-03 08:15:47 -04007)
8
Jamie Hannaford35c91a62014-10-06 15:50:08 +02009// GetResult temporarily stores the result of a Get call.
Ash Wilson61dcb022014-10-03 08:15:47 -040010// Use its Extract() method to interpret it as an Extension.
11type GetResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040012 gophercloud.Result
Ash Wilson61dcb022014-10-03 08:15:47 -040013}
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
Ash Wilsond3dc2542014-10-20 10:10:48 -040025 err := mapstructure.Decode(r.Body, &res)
Ash Wilson61dcb022014-10-03 08:15:47 -040026
Jamie Hannaford1926e9c2014-10-08 17:14:02 +020027 return res.Extension, err
Ash Wilson61dcb022014-10-03 08:15:47 -040028}
29
30// Extension is a struct that represents an OpenStack extension.
31type Extension struct {
Ash Wilson07a25bf2014-10-13 12:00:32 -040032 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"`
Ash Wilson61dcb022014-10-03 08:15:47 -040038}
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)
Ash Wilson61dcb022014-10-03 08:15:47 -040063
Jamie Hannaford1926e9c2014-10-08 17:14:02 +020064 return resp.Extensions, err
Ash Wilson61dcb022014-10-03 08:15:47 -040065}