Skip to content

Commit

Permalink
OCPRHV-794: added soundcard enabled flag to builder and VM
Browse files Browse the repository at this point in the history
  • Loading branch information
engelmi committed Jul 20, 2022
1 parent e5a6f17 commit 9a9565c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 19 deletions.
86 changes: 67 additions & 19 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@ type VM interface {

// SerialConsole returns true if the VM has a serial console.
SerialConsole() bool

// SoundcardEnabled returns true if a soundcard for the VM is enabled.
SoundcardEnabled() bool
}

// VMSearchParameters declares the parameters that can be passed to a VM search. Each parameter
Expand Down Expand Up @@ -694,6 +697,9 @@ type OptionalVMParameters interface {

// SerialConsole returns if a serial console should be created or not.
SerialConsole() *bool

// SoundcardEnabled returns if a soundcard should be created or not.
SoundcardEnabled() *bool
}

// BuildableVMParameters is a variant of OptionalVMParameters that can be changed using the supplied
Expand Down Expand Up @@ -766,6 +772,9 @@ type BuildableVMParameters interface {

// WithSerialConsole adds or removes a serial console to the VM.
WithSerialConsole(serialConsole bool) BuildableVMParameters

// WithSoundcardEnabled enables or disables a soundcard for the VM.
WithSoundcardEnabled(soundcardEnabled bool) BuildableVMParameters
}

// VMCPUParams contain the CPU parameters for a VM.
Expand Down Expand Up @@ -1538,7 +1547,8 @@ type vmParams struct {
os VMOSParameters
osSet bool

serialConsole *bool
serialConsole *bool
soundcardEnabled *bool
}

func (v *vmParams) SerialConsole() *bool {
Expand All @@ -1550,6 +1560,15 @@ func (v *vmParams) WithSerialConsole(serialConsole bool) BuildableVMParameters {
return v
}

func (v *vmParams) SoundcardEnabled() *bool {
return v.soundcardEnabled
}

func (v *vmParams) WithSoundcardEnabled(soundcardEnabled bool) BuildableVMParameters {
v.soundcardEnabled = &soundcardEnabled
return v
}

func (v *vmParams) OS() (VMOSParameters, bool) {
return v.os, v.osSet
}
Expand Down Expand Up @@ -1808,24 +1827,29 @@ func (v vmParams) Comment() string {
type vm struct {
client Client

id VMID
name string
comment string
clusterID ClusterID
templateID TemplateID
status VMStatus
cpu *vmCPU
memory int64
tagIDs []TagID
hugePages *VMHugePages
initialization Initialization
hostID *HostID
placementPolicy *vmPlacementPolicy
memoryPolicy *memoryPolicy
instanceTypeID *InstanceTypeID
vmType VMType
os *vmOS
serialConsole bool
id VMID
name string
comment string
clusterID ClusterID
templateID TemplateID
status VMStatus
cpu *vmCPU
memory int64
tagIDs []TagID
hugePages *VMHugePages
initialization Initialization
hostID *HostID
placementPolicy *vmPlacementPolicy
memoryPolicy *memoryPolicy
instanceTypeID *InstanceTypeID
vmType VMType
os *vmOS
serialConsole bool
soundcardEnabled bool
}

func (v *vm) SoundcardEnabled() bool {
return v.soundcardEnabled
}

func (v *vm) SerialConsole() bool {
Expand Down Expand Up @@ -1951,6 +1975,7 @@ func (v *vm) withName(name string) *vm {
v.vmType,
v.os,
v.serialConsole,
v.soundcardEnabled,
}
}

Expand All @@ -1977,6 +2002,7 @@ func (v *vm) withComment(comment string) *vm {
v.vmType,
v.os,
v.serialConsole,
v.soundcardEnabled,
}
}

Expand Down Expand Up @@ -2142,6 +2168,28 @@ func vmSerialConsoleConverter(object *ovirtsdk.Vm, v *vm, logger Logger, action
return nil
}

func vmSoundcardEnabledConverter(object *ovirtsdk.Vm, v *vm, logger Logger, action string) error {
soundcardEnabled, ok := object.SoundcardEnabled()
if !ok {
// This sometimes (?) happens, and we don't know why. We will assume the serial console does not exist,
// otherwise we create a flaky behavior.
//
// See these issues:
// - https://github.com/oVirt/terraform-provider-ovirt/issues/411
// - https://github.com/oVirt/go-ovirt-client/issues/211
logger.Warningf(
"If you see this message, please open a Bugzilla entry with your oVirt version! The virtual machine object was returned from the oVirt Engine while %s without a console sub-object. VM: %s status: %s",
action,
object.MustId(),
object.MustStatus(),
)
v.soundcardEnabled = false
return nil
}
v.soundcardEnabled = soundcardEnabled
return nil
}

func vmOSConverter(object *ovirtsdk.Vm, v *vm) error {
sdkOS, ok := object.Os()
if !ok {
Expand Down
15 changes: 15 additions & 0 deletions vm_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func createSDKVM(
vmTypeCreator,
vmOSCreator,
vmSerialConsoleCreator,
vmSoundcardEnabledCreator,
}

for _, part := range parts {
Expand Down Expand Up @@ -204,6 +205,14 @@ func vmSerialConsoleCreator(params OptionalVMParameters, builder *ovirtsdk.VmBui
builder.ConsoleBuilder(ovirtsdk.NewConsoleBuilder().Enabled(*serial))
}

func vmSoundcardEnabledCreator(params OptionalVMParameters, builder *ovirtsdk.VmBuilder) {
soundcardEnabled := params.SoundcardEnabled()
if soundcardEnabled == nil {
return
}
builder.SoundcardEnabled(*soundcardEnabled)
}

func vmOSCreator(params OptionalVMParameters, builder *ovirtsdk.VmBuilder) {
if os, ok := params.OS(); ok {
osBuilder := ovirtsdk.NewOperatingSystemBuilder()
Expand Down Expand Up @@ -397,6 +406,11 @@ func (m *mockClient) createVM(
console = *serialConsole
}

soundcardEnabled := false
if isEnabled := params.SoundcardEnabled(); isEnabled != nil {
soundcardEnabled = *isEnabled
}

vm := &vm{
m,
VMID(id),
Expand All @@ -417,6 +431,7 @@ func (m *mockClient) createVM(
vmType,
m.createVMOS(params),
console,
soundcardEnabled,
}
m.vms[VMID(id)] = vm
return vm
Expand Down

0 comments on commit 9a9565c

Please sign in to comment.