github.com/gophercloud/gophercloud@v1.11.0/openstack/common/extensions/results.go (about) 1 package extensions 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // GetResult temporarily stores the result of a Get call. 9 // Use its Extract() method to interpret it as an Extension. 10 type GetResult struct { 11 gophercloud.Result 12 } 13 14 // Extract interprets a GetResult as an Extension. 15 func (r GetResult) Extract() (*Extension, error) { 16 var s struct { 17 Extension *Extension `json:"extension"` 18 } 19 err := r.ExtractInto(&s) 20 return s.Extension, err 21 } 22 23 // Extension is a struct that represents an OpenStack extension. 24 type Extension struct { 25 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"` 31 } 32 33 // ExtensionPage is the page returned by a pager when traversing over a collection of extensions. 34 type ExtensionPage struct { 35 pagination.SinglePageBase 36 } 37 38 // IsEmpty checks whether an ExtensionPage struct is empty. 39 func (r ExtensionPage) IsEmpty() (bool, error) { 40 if r.StatusCode == 204 { 41 return true, nil 42 } 43 44 is, err := ExtractExtensions(r) 45 return len(is) == 0, err 46 } 47 48 // ExtractExtensions accepts a Page struct, specifically an ExtensionPage 49 // struct, and extracts the elements into a slice of Extension structs. 50 // In other words, a generic collection is mapped into a relevant slice. 51 func ExtractExtensions(r pagination.Page) ([]Extension, error) { 52 var s struct { 53 Extensions []Extension `json:"extensions"` 54 } 55 err := (r.(ExtensionPage)).ExtractInto(&s) 56 return s.Extensions, err 57 }