github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/bms/v2/flavors/results.go (about)

     1  package flavors
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  )
    10  
    11  // Flavor represent (virtual) hardware configurations for server resources
    12  // in a region.
    13  type Flavor struct {
    14  	// ID is the flavor's unique ID.
    15  	ID string `json:"id"`
    16  
    17  	// Disk is the amount of root disk, measured in GB.
    18  	Disk int `json:"disk"`
    19  
    20  	// MinDisk and MinRAM, if provided, elides flavors which do not meet your
    21  	// criteria.
    22  	MinDisk int `json:"minDisk"`
    23  
    24  	MinRAM int `json:"minRam"`
    25  
    26  	// RAM is the amount of memory, measured in MB.
    27  	RAM int `json:"ram"`
    28  
    29  	// Name is the name of the flavor.
    30  	Name string `json:"name"`
    31  
    32  	// RxTxFactor describes bandwidth alterations of the flavor.
    33  	RxTxFactor float64 `json:"rxtx_factor"`
    34  
    35  	// Swap is the amount of swap space, measured in MB.
    36  	Swap int `json:"-"`
    37  
    38  	// VCPUs indicates how many (virtual) CPUs are available for this flavor.
    39  	VCPUs int `json:"vcpus"`
    40  
    41  	// IsPublic indicates whether the flavor is public.
    42  	IsPublic bool `json:"os-flavor-access:is_public"`
    43  
    44  	// Ephemeral is the amount of ephemeral disk space, measured in GB.
    45  	Ephemeral int `json:"OS-FLV-EXT-DATA:ephemeral"`
    46  
    47  	// Whether or not the flavor has been administratively disabled
    48  	Disabled bool `json:"OS-FLV-DISABLED:disabled"`
    49  
    50  	// Specifies the shortcut link of the BMS flavor.
    51  	Links []golangsdk.Link `json:"links"`
    52  
    53  	SortKey string `json:"sort_key"`
    54  
    55  	//SortDir sets the direction, and is either `asc' or `desc'
    56  	SortDir string `json:"sort_dir"`
    57  }
    58  
    59  func (r *Flavor) UnmarshalJSON(b []byte) error {
    60  	type tmp Flavor
    61  	var s struct {
    62  		tmp
    63  		Swap interface{} `json:"swap"`
    64  	}
    65  	err := json.Unmarshal(b, &s)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	*r = Flavor(s.tmp)
    71  
    72  	switch t := s.Swap.(type) {
    73  	case float64:
    74  		r.Swap = int(t)
    75  	case string:
    76  		switch t {
    77  		case "":
    78  			r.Swap = 0
    79  		default:
    80  			swap, err := strconv.ParseFloat(t, 64)
    81  			if err != nil {
    82  				return err
    83  			}
    84  			r.Swap = int(swap)
    85  		}
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  // FlavorPage contains a single page of all flavors from a List call.
    92  type FlavorPage struct {
    93  	pagination.LinkedPageBase
    94  }
    95  
    96  // IsEmpty determines if a FlavorPage contains any results.
    97  func (page FlavorPage) IsEmpty() (bool, error) {
    98  	flavors, err := ExtractFlavors(page)
    99  	return len(flavors) == 0, err
   100  }
   101  
   102  // NextPageURL uses the response's embedded link reference to navigate to the
   103  // next page of results.
   104  func (page FlavorPage) NextPageURL() (string, error) {
   105  	var s struct {
   106  		Links []golangsdk.Link `json:"flavors_links"`
   107  	}
   108  	err := page.ExtractInto(&s)
   109  	if err != nil {
   110  		return "", err
   111  	}
   112  	return golangsdk.ExtractNextURL(s.Links)
   113  }
   114  
   115  // ExtractFlavors provides access to the list of flavors in a page acquired
   116  // from the List operation.
   117  func ExtractFlavors(r pagination.Page) ([]Flavor, error) {
   118  	var s struct {
   119  		Flavors []Flavor `json:"flavors"`
   120  	}
   121  	err := (r.(FlavorPage)).ExtractInto(&s)
   122  	return s.Flavors, err
   123  }