github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/bms/v2/flavors/results.go (about)

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