github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/common/extensions/results.go (about)

     1  package extensions
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/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  	golangsdk.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  	is, err := ExtractExtensions(r)
    41  	return len(is) == 0, err
    42  }
    43  
    44  // ExtractExtensions accepts a Page struct, specifically an ExtensionPage
    45  // struct, and extracts the elements into a slice of Extension structs.
    46  // In other words, a generic collection is mapped into a relevant slice.
    47  func ExtractExtensions(r pagination.Page) ([]Extension, error) {
    48  	var s struct {
    49  		Extensions []Extension `json:"extensions"`
    50  	}
    51  	err := (r.(ExtensionPage)).ExtractInto(&s)
    52  	return s.Extensions, err
    53  }