blob: 05de97e8901096ef0c3954222173c5359147fcee [file] [log] [blame]
Ash Wilson61dcb022014-10-03 08:15:47 -04001package extensions
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Ash Wilson61dcb022014-10-03 08:15:47 -04006)
7
Jamie Hannaford35c91a62014-10-06 15:50:08 +02008// GetResult temporarily stores the result of a Get call.
Ash Wilson61dcb022014-10-03 08:15:47 -04009// Use its Extract() method to interpret it as an Extension.
10type GetResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040011 gophercloud.Result
Ash Wilson61dcb022014-10-03 08:15:47 -040012}
13
14// Extract interprets a GetResult as an Extension.
15func (r GetResult) Extract() (*Extension, error) {
Jon Perritt12395212016-02-24 10:41:17 -060016 var s struct {
Ash Wilson61dcb022014-10-03 08:15:47 -040017 Extension *Extension `json:"extension"`
18 }
Jon Perritt12395212016-02-24 10:41:17 -060019 err := r.ExtractInto(&s)
20 return s.Extension, err
Ash Wilson61dcb022014-10-03 08:15:47 -040021}
22
23// Extension is a struct that represents an OpenStack extension.
24type Extension struct {
Jon Perritt12395212016-02-24 10:41:17 -060025 Updated string `json:"updated"`
26 Name string `json:"name"`
27 Links []interface{} `json:"links"`
28 Namespace string `json:"namespace"`
29 Alias string `json:"alias"`
30 Description string `json:"description"`
Ash Wilson61dcb022014-10-03 08:15:47 -040031}
32
33// ExtensionPage is the page returned by a pager when traversing over a collection of extensions.
34type ExtensionPage struct {
35 pagination.SinglePageBase
36}
37
38// IsEmpty checks whether an ExtensionPage struct is empty.
39func (r ExtensionPage) IsEmpty() (bool, error) {
40 is, err := ExtractExtensions(r)
41 if err != nil {
42 return true, err
43 }
44 return len(is) == 0, nil
45}
46
47// ExtractExtensions accepts a Page struct, specifically an ExtensionPage struct, and extracts the
48// elements into a slice of Extension structs.
49// In other words, a generic collection is mapped into a relevant slice.
50func ExtractExtensions(page pagination.Page) ([]Extension, error) {
Jon Perritt12395212016-02-24 10:41:17 -060051 r := page.(ExtensionPage)
52 var s struct {
53 Extensions []Extension `json:"extensions"`
Ash Wilson61dcb022014-10-03 08:15:47 -040054 }
Jon Perritt12395212016-02-24 10:41:17 -060055 err := r.ExtractInto(&s)
56 return s.Extensions, err
Ash Wilson61dcb022014-10-03 08:15:47 -040057}