2020-08-05 19:12:44 +00:00
|
|
|
// +build !luminous,!mimic
|
|
|
|
|
2020-08-04 21:00:16 +00:00
|
|
|
package admin
|
|
|
|
|
2020-09-02 17:51:13 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2020-08-04 21:00:16 +00:00
|
|
|
// ByteCount represents the size of a volume in bytes.
|
|
|
|
type ByteCount uint64
|
|
|
|
|
|
|
|
// SI byte size constants. keep these private for now.
|
|
|
|
const (
|
|
|
|
kibiByte ByteCount = 1024
|
|
|
|
mebiByte = 1024 * kibiByte
|
|
|
|
gibiByte = 1024 * mebiByte
|
|
|
|
tebiByte = 1024 * gibiByte
|
|
|
|
)
|
2020-08-05 17:43:26 +00:00
|
|
|
|
2020-09-02 12:29:48 +00:00
|
|
|
// resizeValue returns a size value as a string, as needed by the subvolume
|
2020-08-05 17:43:26 +00:00
|
|
|
// resize command json.
|
2020-09-02 12:29:48 +00:00
|
|
|
func (bc ByteCount) resizeValue() string {
|
2020-08-05 17:43:26 +00:00
|
|
|
return uint64String(uint64(bc))
|
|
|
|
}
|
|
|
|
|
2020-09-02 12:29:48 +00:00
|
|
|
// QuotaSize interface values can be used to change the size of a volume.
|
|
|
|
type QuotaSize interface {
|
|
|
|
resizeValue() string
|
2020-08-05 17:43:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 12:29:48 +00:00
|
|
|
// specialSize is a custom non-numeric quota size value.
|
2020-08-05 17:43:26 +00:00
|
|
|
type specialSize string
|
|
|
|
|
2020-09-02 12:29:48 +00:00
|
|
|
// resizeValue for a specialSize returns the original string value.
|
|
|
|
func (s specialSize) resizeValue() string {
|
2020-08-05 17:43:26 +00:00
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
2020-09-02 12:29:48 +00:00
|
|
|
// Infinite is a special QuotaSize value that can be used to clear size limits
|
|
|
|
// on a subvolume.
|
2020-08-05 17:43:26 +00:00
|
|
|
const Infinite = specialSize("infinite")
|
2020-09-02 17:51:13 +00:00
|
|
|
|
|
|
|
// quotaSizePlaceholder types are helpful to extract QuotaSize typed values
|
|
|
|
// from JSON responses.
|
|
|
|
type quotaSizePlaceholder struct {
|
|
|
|
Value QuotaSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *quotaSizePlaceholder) UnmarshalJSON(b []byte) error {
|
|
|
|
var val interface{}
|
|
|
|
if err := json.Unmarshal(b, &val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch v := val.(type) {
|
|
|
|
case string:
|
|
|
|
if v == string(Infinite) {
|
|
|
|
p.Value = Infinite
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("quota size: invalid string value: %q", v)
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
p.Value = ByteCount(v)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("quota size: invalid type, string or number required: %v (%T)", val, val)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|