Skip to content

Commit

Permalink
refactor StorageVolume generation
Browse files Browse the repository at this point in the history
This part contains lots of code duplications, clean that up a bit.
  • Loading branch information
rck committed Jul 7, 2020
1 parent 76c77aa commit 41318e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
43 changes: 13 additions & 30 deletions internal/virter/libvirtxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,51 +194,34 @@ func (v *Virter) vmXML(poolName string, vm VMConfig, mac string) (string, error)
}

func (v *Virter) ciDataVolumeXML(name string) (string, error) {
volume := &lx.StorageVolume{
Name: name,
Capacity: &lx.StorageVolumeSize{Value: 0, Unit: "B"},
Target: &lx.StorageVolumeTarget{
Format: &lx.StorageVolumeTargetFormat{Type: "raw"},
},
}
return volume.Marshal()
return v.diskVolumeXML(name, 0, "B", "raw")
}

func (v *Virter) vmVolumeXML(name string, backingPath string) (string, error) {
volume := &lx.StorageVolume{
Name: name,
Capacity: &lx.StorageVolumeSize{Value: 10, Unit: "GiB"},
Target: &lx.StorageVolumeTarget{
Format: &lx.StorageVolumeTargetFormat{Type: "qcow2"},
},
BackingStore: &lx.StorageVolumeBackingStore{
Path: backingPath,
Format: &lx.StorageVolumeTargetFormat{Type: "qcow2"},
},
volume := v.diskVolume(name, 10, "GiB", "qcow2")
volume.BackingStore = &lx.StorageVolumeBackingStore{
Path: backingPath,
Format: &lx.StorageVolumeTargetFormat{Type: "qcow2"},
}
return volume.Marshal()
}

func (v *Virter) diskVolumeXML(name string, sizeKiB uint64, format string) (string, error) {
volume := &lx.StorageVolume{
func (v *Virter) diskVolume(name string, size uint64, unit, format string) *lx.StorageVolume {
return &lx.StorageVolume{
Name: name,
Capacity: &lx.StorageVolumeSize{Value: sizeKiB, Unit: "KiB"},
Capacity: &lx.StorageVolumeSize{Value: size, Unit: unit},
Target: &lx.StorageVolumeTarget{
Format: &lx.StorageVolumeTargetFormat{Type: format},
},
}
return volume.Marshal()
}

func (v *Virter) diskVolumeXML(name string, size uint64, unit, format string) (string, error) {
return v.diskVolume(name, size, unit, format).Marshal()
}

func (v *Virter) imageVolumeXML(name string) (string, error) {
volume := &lx.StorageVolume{
Name: name,
Capacity: &lx.StorageVolumeSize{Value: 0, Unit: "B"},
Target: &lx.StorageVolumeTarget{
Format: &lx.StorageVolumeTargetFormat{Type: "qcow2"},
},
}
return volume.Marshal()
return v.diskVolumeXML(name, 0, "B", "qcow2")
}

func libvirtConsole(vm VMConfig) lx.DomainConsole {
Expand Down
2 changes: 1 addition & 1 deletion internal/virter/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (v *Virter) createVMVolume(sp libvirt.StoragePool, vmConfig VMConfig) error
}

func (v *Virter) createDiskVolume(sp libvirt.StoragePool, vmName string, disk Disk) error {
xml, err := v.diskVolumeXML(diskVolumeName(vmName, disk.GetName()), disk.GetSizeKiB(), disk.GetFormat())
xml, err := v.diskVolumeXML(diskVolumeName(vmName, disk.GetName()), disk.GetSizeKiB(), "KiB", disk.GetFormat())
if err != nil {
return err
}
Expand Down

0 comments on commit 41318e7

Please sign in to comment.