blob: eff44855d7e08929ada60cb83329c50bb176fe52 [file] [log] [blame]
Jamie Hannaford4721abc2014-09-16 16:29:04 +02001package apiversions
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford4721abc2014-09-16 16:29:04 +02005)
6
Jamie Hannaforda311f182014-09-19 11:19:10 +02007// APIVersion represents an API version for Neutron. It contains the status of
8// the API, and its unique ID.
Jamie Hannaford4721abc2014-09-16 16:29:04 +02009type APIVersion struct {
Jon Perritt3c166472016-02-25 03:07:41 -060010 Status string `son:"status"`
11 ID string `json:"id"`
Jamie Hannaford4721abc2014-09-16 16:29:04 +020012}
13
Jamie Hannaforda311f182014-09-19 11:19:10 +020014// APIVersionPage is the page returned by a pager when traversing over a
15// collection of API versions.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020016type APIVersionPage struct {
17 pagination.SinglePageBase
18}
19
Jamie Hannafordc65e1922014-09-22 13:20:58 +020020// IsEmpty checks whether an APIVersionPage struct is empty.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020021func (r APIVersionPage) IsEmpty() (bool, error) {
22 is, err := ExtractAPIVersions(r)
Jon Perritt3c166472016-02-25 03:07:41 -060023 return len(is) == 0, err
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020024}
25
Jamie Hannafordc65e1922014-09-22 13:20:58 +020026// ExtractAPIVersions takes a collection page, extracts all of the elements,
Jamie Hannaforda311f182014-09-19 11:19:10 +020027// and returns them a slice of APIVersion structs. It is effectively a cast.
Jon Perritt3c166472016-02-25 03:07:41 -060028func ExtractAPIVersions(r pagination.Page) ([]APIVersion, error) {
29 var s struct {
30 Versions []APIVersion `json:"versions"`
Jamie Hannaford4721abc2014-09-16 16:29:04 +020031 }
Jon Perritt3c166472016-02-25 03:07:41 -060032 err := (r.(APIVersionPage)).ExtractInto(&s)
33 return s.Versions, err
Jamie Hannaford4721abc2014-09-16 16:29:04 +020034}
35
Jamie Hannaforda311f182014-09-19 11:19:10 +020036// APIVersionResource represents a generic API resource. It contains the name
37// of the resource and its plural collection name.
Jamie Hannaford4721abc2014-09-16 16:29:04 +020038type APIVersionResource struct {
Jon Perritt3c166472016-02-25 03:07:41 -060039 Name string `json:"name"`
40 Collection string `json:"collection"`
Jamie Hannaford4721abc2014-09-16 16:29:04 +020041}
42
Jamie Hannafordc65e1922014-09-22 13:20:58 +020043// APIVersionResourcePage is a concrete type which embeds the common
44// SinglePageBase struct, and is used when traversing API versions collections.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020045type APIVersionResourcePage struct {
46 pagination.SinglePageBase
47}
48
Jamie Hannafordc65e1922014-09-22 13:20:58 +020049// IsEmpty is a concrete function which indicates whether an
50// APIVersionResourcePage is empty or not.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020051func (r APIVersionResourcePage) IsEmpty() (bool, error) {
52 is, err := ExtractVersionResources(r)
Jon Perritt3c166472016-02-25 03:07:41 -060053 return len(is) == 0, err
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020054}
55
Jamie Hannafordc65e1922014-09-22 13:20:58 +020056// ExtractVersionResources accepts a Page struct, specifically a
57// APIVersionResourcePage struct, and extracts the elements into a slice of
58// APIVersionResource structs. In other words, the collection is mapped into
59// a relevant slice.
Jon Perritt3c166472016-02-25 03:07:41 -060060func ExtractVersionResources(r pagination.Page) ([]APIVersionResource, error) {
61 var s struct {
62 APIVersionResources []APIVersionResource `json:"resources"`
Jamie Hannaford4721abc2014-09-16 16:29:04 +020063 }
Jon Perritt3c166472016-02-25 03:07:41 -060064 err := (r.(APIVersionResourcePage)).ExtractInto(&s)
65 return s.APIVersionResources, err
Jamie Hannaford4721abc2014-09-16 16:29:04 +020066}