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