github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/extensions/schedulerstats/results.go (about)

     1  package schedulerstats
     2  
     3  import (
     4  	"encoding/json"
     5  	"math"
     6  	"strconv"
     7  
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // Capabilities represents the information of an individual StoragePool.
    12  type Capabilities struct {
    13  	// The following fields should be present in all storage drivers.
    14  	DriverVersion     string  `json:"driver_version"`
    15  	FreeCapacityGB    float64 `json:"-"`
    16  	StorageProtocol   string  `json:"storage_protocol"`
    17  	TotalCapacityGB   float64 `json:"-"`
    18  	VendorName        string  `json:"vendor_name"`
    19  	VolumeBackendName string  `json:"volume_backend_name"`
    20  
    21  	// The following fields are optional and may have empty values depending
    22  	// on the storage driver in use.
    23  	ReservedPercentage       int64   `json:"reserved_percentage"`
    24  	LocationInfo             string  `json:"location_info"`
    25  	QoSSupport               bool    `json:"QoS_support"`
    26  	ProvisionedCapacityGB    float64 `json:"provisioned_capacity_gb"`
    27  	MaxOverSubscriptionRatio string  `json:"-"`
    28  	ThinProvisioningSupport  bool    `json:"thin_provisioning_support"`
    29  	ThickProvisioningSupport bool    `json:"thick_provisioning_support"`
    30  	TotalVolumes             int64   `json:"total_volumes"`
    31  	FilterFunction           string  `json:"filter_function"`
    32  	GoodnessFunction         string  `json:"goodness_function"`
    33  	Multiattach              bool    `json:"multiattach"`
    34  	SparseCopyVolume         bool    `json:"sparse_copy_volume"`
    35  	AllocatedCapacityGB      float64 `json:"-"`
    36  }
    37  
    38  // StoragePool represents an individual StoragePool retrieved from the
    39  // schedulerstats API.
    40  type StoragePool struct {
    41  	Name         string       `json:"name"`
    42  	Capabilities Capabilities `json:"capabilities"`
    43  }
    44  
    45  func (r *Capabilities) UnmarshalJSON(b []byte) error {
    46  	type tmp Capabilities
    47  	var s struct {
    48  		tmp
    49  		AllocatedCapacityGB      interface{} `json:"allocated_capacity_gb"`
    50  		FreeCapacityGB           interface{} `json:"free_capacity_gb"`
    51  		MaxOverSubscriptionRatio interface{} `json:"max_over_subscription_ratio"`
    52  		TotalCapacityGB          interface{} `json:"total_capacity_gb"`
    53  	}
    54  	err := json.Unmarshal(b, &s)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	*r = Capabilities(s.tmp)
    59  
    60  	// Generic function to parse a capacity value which may be a numeric
    61  	// value, "unknown", or "infinite"
    62  	parseCapacity := func(capacity interface{}) float64 {
    63  		if capacity != nil {
    64  			switch capacity.(type) {
    65  			case float64:
    66  				return capacity.(float64)
    67  			case string:
    68  				if capacity.(string) == "infinite" {
    69  					return math.Inf(1)
    70  				}
    71  			}
    72  		}
    73  		return 0.0
    74  	}
    75  
    76  	r.AllocatedCapacityGB = parseCapacity(s.AllocatedCapacityGB)
    77  	r.FreeCapacityGB = parseCapacity(s.FreeCapacityGB)
    78  	r.TotalCapacityGB = parseCapacity(s.TotalCapacityGB)
    79  
    80  	if s.MaxOverSubscriptionRatio != nil {
    81  		switch t := s.MaxOverSubscriptionRatio.(type) {
    82  		case float64:
    83  			r.MaxOverSubscriptionRatio = strconv.FormatFloat(t, 'f', -1, 64)
    84  		case string:
    85  			r.MaxOverSubscriptionRatio = t
    86  		}
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  // StoragePoolPage is a single page of all List results.
    93  type StoragePoolPage struct {
    94  	pagination.SinglePageBase
    95  }
    96  
    97  // IsEmpty satisfies the IsEmpty method of the Page interface. It returns true
    98  // if a List contains no results.
    99  func (page StoragePoolPage) IsEmpty() (bool, error) {
   100  	if page.StatusCode == 204 {
   101  		return true, nil
   102  	}
   103  
   104  	va, err := ExtractStoragePools(page)
   105  	return len(va) == 0, err
   106  }
   107  
   108  // ExtractStoragePools takes a List result and extracts the collection of
   109  // StoragePools returned by the API.
   110  func ExtractStoragePools(p pagination.Page) ([]StoragePool, error) {
   111  	var s struct {
   112  		StoragePools []StoragePool `json:"pools"`
   113  	}
   114  	err := (p.(StoragePoolPage)).ExtractInto(&s)
   115  	return s.StoragePools, err
   116  }