cloudapi/v2: minsize can be a string or integer
Define a reusable minsize property in the cloud API schema. The minsize type is either a uint64 or a string with data units. Internally, this creates a union (json.RawMessage) type called Minsize and optional uses of the property are of type *Minsize. We define an internal decodeMinsize() function to convert *Minsize to uint64. This is similar to the decodeSize() function in the blueprint, but made specifically to handle *Minsize.
This commit is contained in:
parent
82ac185e7c
commit
ad0cb80bed
4 changed files with 452 additions and 253 deletions
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/google/uuid"
|
||||
|
||||
"github.com/osbuild/images/pkg/customizations/subscription"
|
||||
"github.com/osbuild/images/pkg/datasizes"
|
||||
"github.com/osbuild/images/pkg/disk"
|
||||
"github.com/osbuild/images/pkg/distrofactory"
|
||||
"github.com/osbuild/images/pkg/reporegistry"
|
||||
|
|
@ -190,10 +191,14 @@ func (rbp *Blueprint) GetCustomizationsFromBlueprintRequest() (*blueprint.Custom
|
|||
if rbpc.Filesystem != nil {
|
||||
var fsCustomizations []blueprint.FilesystemCustomization
|
||||
for _, f := range *rbpc.Filesystem {
|
||||
minSize, err := decodeMinsize(&f.Minsize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fsCustomizations = append(fsCustomizations,
|
||||
blueprint.FilesystemCustomization{
|
||||
Mountpoint: f.Mountpoint,
|
||||
MinSize: f.Minsize,
|
||||
MinSize: minSize,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
@ -500,6 +505,11 @@ func (rbp *Blueprint) GetCustomizationsFromBlueprintRequest() (*blueprint.Custom
|
|||
return nil, err
|
||||
}
|
||||
c.Disk = disk
|
||||
bpDisk, err := convertDiskCustomizations(rbpc.Disk)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Disk = bpDisk
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
|
@ -1294,8 +1304,12 @@ func convertDiskCustomizations(disk *Disk) (*blueprint.DiskCustomization, error)
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
diskSize, err := decodeMinsize(disk.Minsize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bpDisk := &blueprint.DiskCustomization{
|
||||
MinSize: common.DerefOrDefault(disk.Minsize),
|
||||
MinSize: diskSize,
|
||||
Type: string(common.DerefOrDefault(disk.Type)),
|
||||
}
|
||||
|
||||
|
|
@ -1314,10 +1328,14 @@ func convertDiskCustomizations(disk *Disk) (*blueprint.DiskCustomization, error)
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to deserialize disk customization partition %d with type %q", idx, partType)
|
||||
}
|
||||
fsSize, err := decodeMinsize(fs.Minsize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bpPartition = blueprint.PartitionCustomization{
|
||||
Type: string(common.DerefOrDefault(fs.Type)),
|
||||
PartType: common.DerefOrDefault(fs.PartType),
|
||||
MinSize: common.DerefOrDefault(fs.Minsize),
|
||||
MinSize: fsSize,
|
||||
FilesystemTypedCustomization: blueprint.FilesystemTypedCustomization{
|
||||
Mountpoint: fs.Mountpoint,
|
||||
Label: common.DerefOrDefault(fs.Label),
|
||||
|
|
@ -1329,11 +1347,15 @@ func convertDiskCustomizations(disk *Disk) (*blueprint.DiskCustomization, error)
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to deserialize disk customization partition %d with type %q", idx, partType)
|
||||
}
|
||||
volSize, err := decodeMinsize(btrfsVol.Minsize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bpPartition = blueprint.PartitionCustomization{
|
||||
Type: string(common.DerefOrDefault(btrfsVol.Type)),
|
||||
PartType: common.DerefOrDefault(btrfsVol.PartType),
|
||||
MinSize: common.DerefOrDefault(btrfsVol.Minsize),
|
||||
MinSize: volSize,
|
||||
}
|
||||
|
||||
for _, subvol := range btrfsVol.Subvolumes {
|
||||
|
|
@ -1348,19 +1370,27 @@ func convertDiskCustomizations(disk *Disk) (*blueprint.DiskCustomization, error)
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to deserialize disk customization partition %d with type %q", idx, partType)
|
||||
}
|
||||
vgSize, err := decodeMinsize(vg.Minsize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bpPartition = blueprint.PartitionCustomization{
|
||||
Type: string(common.DerefOrDefault(vg.Type)),
|
||||
PartType: common.DerefOrDefault(vg.PartType),
|
||||
MinSize: common.DerefOrDefault(vg.Minsize),
|
||||
MinSize: vgSize,
|
||||
VGCustomization: blueprint.VGCustomization{
|
||||
Name: common.DerefOrDefault(vg.Name),
|
||||
},
|
||||
}
|
||||
|
||||
for _, lv := range vg.LogicalVolumes {
|
||||
lvSize, err := decodeMinsize(lv.Minsize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bpLV := blueprint.LVCustomization{
|
||||
Name: common.DerefOrDefault(lv.Name),
|
||||
MinSize: common.DerefOrDefault(lv.Minsize),
|
||||
MinSize: lvSize,
|
||||
FilesystemTypedCustomization: blueprint.FilesystemTypedCustomization{
|
||||
Mountpoint: lv.Mountpoint,
|
||||
Label: common.DerefOrDefault(lv.Label),
|
||||
|
|
@ -1378,3 +1408,19 @@ func convertDiskCustomizations(disk *Disk) (*blueprint.DiskCustomization, error)
|
|||
|
||||
return bpDisk, nil
|
||||
}
|
||||
|
||||
func decodeMinsize(size *Minsize) (uint64, error) {
|
||||
if size == nil {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
if sizeStr, err := size.AsMinsize1(); err == nil {
|
||||
return datasizes.Parse(sizeStr)
|
||||
}
|
||||
|
||||
if sizeUint, err := size.AsMinsize0(); err == nil {
|
||||
return sizeUint, err
|
||||
}
|
||||
|
||||
return 0, fmt.Errorf("failed to convert value \"%v\" to number", size)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue