blob: d5f86509135d283eeddb99a6d829d5efe11291cc [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)
Jon Perritt31b66462016-02-25 22:25:30 -060041 return len(is) == 0, err
Ash Wilson61dcb022014-10-03 08:15:47 -040042}
43
44// ExtractExtensions accepts a Page struct, specifically an ExtensionPage struct, and extracts the
45// elements into a slice of Extension structs.
46// In other words, a generic collection is mapped into a relevant slice.
Jon Perritt31b66462016-02-25 22:25:30 -060047func ExtractExtensions(r pagination.Page) ([]Extension, error) {
Jon Perritt12395212016-02-24 10:41:17 -060048 var s struct {
49 Extensions []Extension `json:"extensions"`
Ash Wilson61dcb022014-10-03 08:15:47 -040050 }
Jon Perritt31b66462016-02-25 22:25:30 -060051 err := (r.(ExtensionPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -060052 return s.Extensions, err
Ash Wilson61dcb022014-10-03 08:15:47 -040053}