osbuild2: use aliased type for QEMU format, instead of string
Define a new aliased type `QEMUFormat` for the format type used by the osbuild2 QEMU stage and define constants for all allowed values. Use QEMU format type constants in all relevant places, instead of string literals. Not using string literals minimizes the room for making a typo.
This commit is contained in:
parent
e30fa53c50
commit
0e512e97d2
10 changed files with 71 additions and 61 deletions
|
|
@ -55,7 +55,7 @@ func qcow2Pipelines(t *imageType, customizations *blueprint.Customizations, opti
|
|||
imagePipeline := liveImagePipeline(treePipeline.Name, diskfile, partitionTable, t.arch, kernelVer)
|
||||
pipelines = append(pipelines, *imagePipeline)
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "qcow2", "0.10")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatQCOW2, "0.10")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
|
||||
return pipelines, nil
|
||||
|
|
@ -99,7 +99,7 @@ func vhdPipelines(t *imageType, customizations *blueprint.Customizations, option
|
|||
return nil, err
|
||||
}
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "vpc", "")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatVPC, "")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
return pipelines, nil
|
||||
}
|
||||
|
|
@ -130,7 +130,7 @@ func vmdkPipelines(t *imageType, customizations *blueprint.Customizations, optio
|
|||
return nil, err
|
||||
}
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "vmdk", "")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatVMDK, "")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
return pipelines, nil
|
||||
}
|
||||
|
|
@ -161,7 +161,7 @@ func openstackPipelines(t *imageType, customizations *blueprint.Customizations,
|
|||
return nil, err
|
||||
}
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "qcow2", "")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatQCOW2, "")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
return pipelines, nil
|
||||
}
|
||||
|
|
@ -1271,9 +1271,9 @@ func xzArchivePipeline(inputPipelineName, inputFilename, outputFilename string)
|
|||
return p
|
||||
}
|
||||
|
||||
func qemuPipeline(inputPipelineName, inputFilename, outputFilename, format, qcow2Compat string) *osbuild.Pipeline {
|
||||
func qemuPipeline(inputPipelineName, inputFilename, outputFilename string, format osbuild.QEMUFormat, qcow2Compat string) *osbuild.Pipeline {
|
||||
p := new(osbuild.Pipeline)
|
||||
p.Name = format
|
||||
p.Name = string(format)
|
||||
p.Build = "name:build"
|
||||
|
||||
qemuStage := osbuild.NewQEMUStage(qemuStageOptions(outputFilename, format, qcow2Compat), osbuild.NewQemuStagePipelineFilesInputs(inputPipelineName, inputFilename))
|
||||
|
|
|
|||
|
|
@ -229,21 +229,21 @@ func xorrisofsStageOptions(filename string, arch string, isolinux bool) *osbuild
|
|||
return options
|
||||
}
|
||||
|
||||
func qemuStageOptions(filename, format, compat string) *osbuild.QEMUStageOptions {
|
||||
func qemuStageOptions(filename string, format osbuild.QEMUFormat, compat string) *osbuild.QEMUStageOptions {
|
||||
var options osbuild.QEMUFormatOptions
|
||||
switch format {
|
||||
case "qcow2":
|
||||
case osbuild.QEMUFormatQCOW2:
|
||||
options = osbuild.Qcow2Options{
|
||||
Type: "qcow2",
|
||||
Type: format,
|
||||
Compat: compat,
|
||||
}
|
||||
case "vpc":
|
||||
case osbuild.QEMUFormatVPC:
|
||||
options = osbuild.VPCOptions{
|
||||
Type: "vpc",
|
||||
Type: format,
|
||||
}
|
||||
case "vmdk":
|
||||
case osbuild.QEMUFormatVMDK:
|
||||
options = osbuild.VMDKOptions{
|
||||
Type: "vmdk",
|
||||
Type: format,
|
||||
}
|
||||
default:
|
||||
panic("unknown format in qemu stage: " + format)
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ func qcow2Pipelines(t *imageType, customizations *blueprint.Customizations, opti
|
|||
imagePipeline := liveImagePipeline(treePipeline.Name, diskfile, partitionTable, t.arch, kernelVer)
|
||||
pipelines = append(pipelines, *imagePipeline)
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "qcow2", "0.10")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatQCOW2, "0.10")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
|
||||
return pipelines, nil
|
||||
|
|
@ -75,7 +75,7 @@ func vhdPipelines(t *imageType, customizations *blueprint.Customizations, option
|
|||
return nil, err
|
||||
}
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "vpc", "")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatVPC, "")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
return pipelines, nil
|
||||
}
|
||||
|
|
@ -103,7 +103,7 @@ func vmdkPipelines(t *imageType, customizations *blueprint.Customizations, optio
|
|||
return nil, err
|
||||
}
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "vmdk", "")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatVMDK, "")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
return pipelines, nil
|
||||
}
|
||||
|
|
@ -131,7 +131,7 @@ func openstackPipelines(t *imageType, customizations *blueprint.Customizations,
|
|||
return nil, err
|
||||
}
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "qcow2", "")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatQCOW2, "")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
return pipelines, nil
|
||||
}
|
||||
|
|
@ -1033,9 +1033,9 @@ func xzArchivePipeline(inputPipelineName, inputFilename, outputFilename string)
|
|||
return p
|
||||
}
|
||||
|
||||
func qemuPipeline(inputPipelineName, inputFilename, outputFilename, format, qcow2Compat string) *osbuild.Pipeline {
|
||||
func qemuPipeline(inputPipelineName, inputFilename, outputFilename string, format osbuild.QEMUFormat, qcow2Compat string) *osbuild.Pipeline {
|
||||
p := new(osbuild.Pipeline)
|
||||
p.Name = format
|
||||
p.Name = string(format)
|
||||
p.Build = "name:build"
|
||||
|
||||
qemuStage := osbuild.NewQEMUStage(qemuStageOptions(outputFilename, format, qcow2Compat), osbuild.NewQemuStagePipelineFilesInputs(inputPipelineName, inputFilename))
|
||||
|
|
|
|||
|
|
@ -278,21 +278,21 @@ func xorrisofsStageOptions(filename, isolabel, arch string, isolinux bool) *osbu
|
|||
return options
|
||||
}
|
||||
|
||||
func qemuStageOptions(filename, format, compat string) *osbuild.QEMUStageOptions {
|
||||
func qemuStageOptions(filename string, format osbuild.QEMUFormat, compat string) *osbuild.QEMUStageOptions {
|
||||
var options osbuild.QEMUFormatOptions
|
||||
switch format {
|
||||
case "qcow2":
|
||||
case osbuild.QEMUFormatQCOW2:
|
||||
options = osbuild.Qcow2Options{
|
||||
Type: "qcow2",
|
||||
Type: format,
|
||||
Compat: compat,
|
||||
}
|
||||
case "vpc":
|
||||
case osbuild.QEMUFormatVPC:
|
||||
options = osbuild.VPCOptions{
|
||||
Type: "vpc",
|
||||
Type: format,
|
||||
}
|
||||
case "vmdk":
|
||||
case osbuild.QEMUFormatVMDK:
|
||||
options = osbuild.VMDKOptions{
|
||||
Type: "vmdk",
|
||||
Type: format,
|
||||
}
|
||||
default:
|
||||
panic("unknown format in qemu stage: " + format)
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ func qcow2Pipelines(t *imageType, customizations *blueprint.Customizations, opti
|
|||
imagePipeline := liveImagePipeline(treePipeline.Name, diskfile, partitionTable, t.arch, kernelVer)
|
||||
pipelines = append(pipelines, *imagePipeline)
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "qcow2", "1.1")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatQCOW2, "1.1")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
|
||||
return pipelines, nil
|
||||
|
|
@ -72,7 +72,7 @@ func vhdPipelines(t *imageType, customizations *blueprint.Customizations, option
|
|||
imagePipeline := liveImagePipeline(treePipeline.Name, diskfile, partitionTable, t.arch, kernelVer)
|
||||
pipelines = append(pipelines, *imagePipeline)
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "vpc", "")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatVPC, "")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
return pipelines, nil
|
||||
}
|
||||
|
|
@ -97,7 +97,7 @@ func vmdkPipelines(t *imageType, customizations *blueprint.Customizations, optio
|
|||
imagePipeline := liveImagePipeline(treePipeline.Name, diskfile, partitionTable, t.arch, kernelVer)
|
||||
pipelines = append(pipelines, *imagePipeline)
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "vmdk", "")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatVMDK, "")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
return pipelines, nil
|
||||
}
|
||||
|
|
@ -122,7 +122,7 @@ func openstackPipelines(t *imageType, customizations *blueprint.Customizations,
|
|||
imagePipeline := liveImagePipeline(treePipeline.Name, diskfile, partitionTable, t.arch, kernelVer)
|
||||
pipelines = append(pipelines, *imagePipeline)
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "qcow2", "")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatQCOW2, "")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
return pipelines, nil
|
||||
}
|
||||
|
|
@ -1025,9 +1025,9 @@ func xzArchivePipeline(inputPipelineName, inputFilename, outputFilename string)
|
|||
return p
|
||||
}
|
||||
|
||||
func qemuPipeline(inputPipelineName, inputFilename, outputFilename, format, qcow2Compat string) *osbuild.Pipeline {
|
||||
func qemuPipeline(inputPipelineName, inputFilename, outputFilename string, format osbuild.QEMUFormat, qcow2Compat string) *osbuild.Pipeline {
|
||||
p := new(osbuild.Pipeline)
|
||||
p.Name = format
|
||||
p.Name = string(format)
|
||||
p.Build = "name:build"
|
||||
|
||||
qemuStage := osbuild.NewQEMUStage(qemuStageOptions(outputFilename, format, qcow2Compat), osbuild.NewQemuStagePipelineFilesInputs(inputPipelineName, inputFilename))
|
||||
|
|
|
|||
|
|
@ -278,21 +278,21 @@ func xorrisofsStageOptions(filename, isolabel, arch string, isolinux bool) *osbu
|
|||
return options
|
||||
}
|
||||
|
||||
func qemuStageOptions(filename, format, compat string) *osbuild.QEMUStageOptions {
|
||||
func qemuStageOptions(filename string, format osbuild.QEMUFormat, compat string) *osbuild.QEMUStageOptions {
|
||||
var options osbuild.QEMUFormatOptions
|
||||
switch format {
|
||||
case "qcow2":
|
||||
case osbuild.QEMUFormatQCOW2:
|
||||
options = osbuild.Qcow2Options{
|
||||
Type: "qcow2",
|
||||
Type: format,
|
||||
Compat: compat,
|
||||
}
|
||||
case "vpc":
|
||||
case osbuild.QEMUFormatVPC:
|
||||
options = osbuild.VPCOptions{
|
||||
Type: "vpc",
|
||||
Type: format,
|
||||
}
|
||||
case "vmdk":
|
||||
case osbuild.QEMUFormatVMDK:
|
||||
options = osbuild.VMDKOptions{
|
||||
Type: "vmdk",
|
||||
Type: format,
|
||||
}
|
||||
default:
|
||||
panic("unknown format in qemu stage: " + format)
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ func qcow2Pipelines(t *imageType, customizations *blueprint.Customizations, opti
|
|||
imagePipeline := liveImagePipeline(treePipeline.Name, diskfile, partitionTable, t.arch, kernelVer)
|
||||
pipelines = append(pipelines, *imagePipeline)
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "qcow2", "1.1")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatQCOW2, "1.1")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
|
||||
return pipelines, nil
|
||||
|
|
@ -92,7 +92,7 @@ func vhdPipelines(t *imageType, customizations *blueprint.Customizations, option
|
|||
imagePipeline := liveImagePipeline(treePipeline.Name, diskfile, partitionTable, t.arch, kernelVer)
|
||||
pipelines = append(pipelines, *imagePipeline)
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "vpc", "")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatVPC, "")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
return pipelines, nil
|
||||
}
|
||||
|
|
@ -121,7 +121,7 @@ func vmdkPipelines(t *imageType, customizations *blueprint.Customizations, optio
|
|||
imagePipeline := liveImagePipeline(treePipeline.Name, diskfile, partitionTable, t.arch, kernelVer)
|
||||
pipelines = append(pipelines, *imagePipeline)
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "vmdk", "")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatVMDK, "")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
return pipelines, nil
|
||||
}
|
||||
|
|
@ -149,7 +149,7 @@ func openstackPipelines(t *imageType, customizations *blueprint.Customizations,
|
|||
imagePipeline := liveImagePipeline(treePipeline.Name, diskfile, partitionTable, t.arch, kernelVer)
|
||||
pipelines = append(pipelines, *imagePipeline)
|
||||
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, "qcow2", "")
|
||||
qemuPipeline := qemuPipeline(imagePipeline.Name, diskfile, t.filename, osbuild.QEMUFormatQCOW2, "")
|
||||
pipelines = append(pipelines, *qemuPipeline)
|
||||
return pipelines, nil
|
||||
}
|
||||
|
|
@ -1087,9 +1087,9 @@ func xzArchivePipeline(inputPipelineName, inputFilename, outputFilename string)
|
|||
return p
|
||||
}
|
||||
|
||||
func qemuPipeline(inputPipelineName, inputFilename, outputFilename, format, qcow2Compat string) *osbuild.Pipeline {
|
||||
func qemuPipeline(inputPipelineName, inputFilename, outputFilename string, format osbuild.QEMUFormat, qcow2Compat string) *osbuild.Pipeline {
|
||||
p := new(osbuild.Pipeline)
|
||||
p.Name = format
|
||||
p.Name = string(format)
|
||||
p.Build = "name:build"
|
||||
|
||||
qemuStage := osbuild.NewQEMUStage(qemuStageOptions(outputFilename, format, qcow2Compat), osbuild.NewQemuStagePipelineFilesInputs(inputPipelineName, inputFilename))
|
||||
|
|
|
|||
|
|
@ -208,21 +208,21 @@ func xorrisofsStageOptions(filename string, arch string) *osbuild.XorrisofsStage
|
|||
}
|
||||
}
|
||||
|
||||
func qemuStageOptions(filename, format, compat string) *osbuild.QEMUStageOptions {
|
||||
func qemuStageOptions(filename string, format osbuild.QEMUFormat, compat string) *osbuild.QEMUStageOptions {
|
||||
var options osbuild.QEMUFormatOptions
|
||||
switch format {
|
||||
case "qcow2":
|
||||
case osbuild.QEMUFormatQCOW2:
|
||||
options = osbuild.Qcow2Options{
|
||||
Type: "qcow2",
|
||||
Type: format,
|
||||
Compat: compat,
|
||||
}
|
||||
case "vpc":
|
||||
case osbuild.QEMUFormatVPC:
|
||||
options = osbuild.VPCOptions{
|
||||
Type: "vpc",
|
||||
Type: format,
|
||||
}
|
||||
case "vmdk":
|
||||
case osbuild.QEMUFormatVMDK:
|
||||
options = osbuild.VMDKOptions{
|
||||
Type: "vmdk",
|
||||
Type: format,
|
||||
}
|
||||
default:
|
||||
panic("unknown format in qemu stage: " + format)
|
||||
|
|
|
|||
|
|
@ -20,13 +20,23 @@ type QEMUStageOptions struct {
|
|||
|
||||
func (QEMUStageOptions) isStageOptions() {}
|
||||
|
||||
type QEMUFormat string
|
||||
|
||||
const (
|
||||
QEMUFormatQCOW2 QEMUFormat = "qcow2"
|
||||
QEMUFormatVDI QEMUFormat = "vdi"
|
||||
QEMUFormatVMDK QEMUFormat = "vmdk"
|
||||
QEMUFormatVPC QEMUFormat = "vpc"
|
||||
QEMUFormatVHDX QEMUFormat = "vhdx"
|
||||
)
|
||||
|
||||
type QEMUFormatOptions interface {
|
||||
isQEMUFormatOptions()
|
||||
}
|
||||
|
||||
type Qcow2Options struct {
|
||||
// The type of the format must be 'qcow2'
|
||||
Type string `json:"type"`
|
||||
Type QEMUFormat `json:"type"`
|
||||
|
||||
// The qcow2-compatibility-version to use
|
||||
Compat string `json:"compat"`
|
||||
|
|
@ -36,14 +46,14 @@ func (Qcow2Options) isQEMUFormatOptions() {}
|
|||
|
||||
type VPCOptions struct {
|
||||
// The type of the format must be 'vpc'
|
||||
Type string `json:"type"`
|
||||
Type QEMUFormat `json:"type"`
|
||||
}
|
||||
|
||||
func (VPCOptions) isQEMUFormatOptions() {}
|
||||
|
||||
type VMDKOptions struct {
|
||||
// The type of the format must be 'vpc'
|
||||
Type string `json:"type"`
|
||||
Type QEMUFormat `json:"type"`
|
||||
}
|
||||
|
||||
func (VMDKOptions) isQEMUFormatOptions() {}
|
||||
|
|
@ -88,15 +98,15 @@ type qemuStageOptions QEMUStageOptions
|
|||
func (options QEMUStageOptions) MarshalJSON() ([]byte, error) {
|
||||
switch o := options.Format.(type) {
|
||||
case Qcow2Options:
|
||||
if o.Type != "qcow2" {
|
||||
if o.Type != QEMUFormatQCOW2 {
|
||||
return nil, fmt.Errorf("invalid format type %q for qcow2 options", o.Type)
|
||||
}
|
||||
case VPCOptions:
|
||||
if o.Type != "vpc" {
|
||||
if o.Type != QEMUFormatVPC {
|
||||
return nil, fmt.Errorf("invalid format type %q for vpc options", o.Type)
|
||||
}
|
||||
case VMDKOptions:
|
||||
if o.Type != "vmdk" {
|
||||
if o.Type != QEMUFormatVMDK {
|
||||
return nil, fmt.Errorf("invalid format type %q for vmdk options", o.Type)
|
||||
}
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -10,14 +10,14 @@ func TestNewQemuStage(t *testing.T) {
|
|||
|
||||
formatOptionsList := []QEMUFormatOptions{
|
||||
Qcow2Options{
|
||||
Type: "qcow2",
|
||||
Type: QEMUFormatQCOW2,
|
||||
Compat: "0.10",
|
||||
},
|
||||
VPCOptions{
|
||||
Type: "vpc",
|
||||
Type: QEMUFormatVPC,
|
||||
},
|
||||
VMDKOptions{
|
||||
Type: "vmdk",
|
||||
Type: QEMUFormatVMDK,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue