Skip to content

Commit

Permalink
add description in returned VMData
Browse files Browse the repository at this point in the history
Signed-off-by: amberDante <shelkovkinsn@live.ru>
  • Loading branch information
AmberDante authored and engelmi committed Jul 31, 2023
1 parent 429b64b commit 768132d
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 6 deletions.
108 changes: 103 additions & 5 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ type VMData interface {
Name() string
// Comment is the comment added to the VM.
Comment() string
// Description is the description added to the VM.
Description() string
// ClusterID returns the cluster this machine belongs to.
ClusterID() ClusterID
// TemplateID returns the ID of the base template for this machine.
Expand Down Expand Up @@ -661,6 +663,9 @@ type OptionalVMParameters interface {
// Comment returns the comment for the VM.
Comment() string

// Description returns the description for the VM.
Description() string

// CPU contains the CPU topology, if any.
CPU() VMCPUParams

Expand Down Expand Up @@ -712,6 +717,11 @@ type BuildableVMParameters interface {
// MustWithComment is identical to WithComment, but panics instead of returning an error.
MustWithComment(comment string) BuildableVMParameters

// WithDescriptiont adds a common to the VM.
WithDescription(description string) (BuildableVMParameters, error)
// MustWithDescription is identical to WithDescription, but panics instead of returning an error.
MustWithDescription(descriptiont string) BuildableVMParameters

// WithCPU adds a VMCPUTopo to the VM.
WithCPU(cpu VMCPUParams) (BuildableVMParameters, error)
// MustWithCPU adds a VMCPUTopo and panics if an error happens.
Expand Down Expand Up @@ -1376,6 +1386,8 @@ type UpdateVMParameters interface {
Name() *string
// Comment returns the comment for the VM. Return nil if the name should not be changed.
Comment() *string
// Description returns the description for the VM. Return nil if the name should not be changed.
Description() *string
}

// VMCPUTopo contains the CPU topology information about a VM.
Expand Down Expand Up @@ -1459,6 +1471,12 @@ type BuildableUpdateVMParameters interface {

// MustWithComment is identical to WithComment, but panics instead of returning an error.
MustWithComment(comment string) BuildableUpdateVMParameters

// WithDescription adds a description to the request
WithDescription(description string) (BuildableUpdateVMParameters, error)

// MustWithDescription is identical to WithDescription, but panics instead of returning an error.
MustWithDescription(comment string) BuildableUpdateVMParameters
}

// UpdateVMParams returns a buildable set of update parameters.
Expand All @@ -1467,8 +1485,9 @@ func UpdateVMParams() BuildableUpdateVMParameters {
}

type updateVMParams struct {
name *string
comment *string
name *string
comment *string
description *string
}

func (u *updateVMParams) MustWithName(name string) BuildableUpdateVMParameters {
Expand All @@ -1487,6 +1506,14 @@ func (u *updateVMParams) MustWithComment(comment string) BuildableUpdateVMParame
return builder
}

func (u *updateVMParams) MustWithDescription(description string) BuildableUpdateVMParameters {
builder, err := u.WithDescription(description)
if err != nil {
panic(err)
}
return builder
}

func (u *updateVMParams) Name() *string {
return u.name
}
Expand All @@ -1495,6 +1522,10 @@ func (u *updateVMParams) Comment() *string {
return u.comment
}

func (u *updateVMParams) Description() *string {
return u.description
}

func (u *updateVMParams) WithName(name string) (BuildableUpdateVMParameters, error) {
if err := validateVMName(name); err != nil {
return nil, err
Expand All @@ -1508,6 +1539,11 @@ func (u *updateVMParams) WithComment(comment string) (BuildableUpdateVMParameter
return u, nil
}

func (u *updateVMParams) WithDescription(description string) (BuildableUpdateVMParameters, error) {
u.description = &description
return u, nil
}

// NewCreateVMParams creates a set of BuildableVMParameters that can be used to construct the optional VM parameters.
func NewCreateVMParams() BuildableVMParameters {
return &vmParams{
Expand All @@ -1524,9 +1560,10 @@ func CreateVMParams() BuildableVMParameters {
type vmParams struct {
lock *sync.Mutex

name string
comment string
cpu VMCPUParams
name string
comment string
description string
cpu VMCPUParams

hugePages *VMHugePages

Expand Down Expand Up @@ -1802,6 +1839,13 @@ func (v *vmParams) MustWithComment(comment string) BuildableVMParameters {
}
return builder
}
func (v *vmParams) MustWithDescription(description string) BuildableVMParameters {
builder, err := v.WithDescription(description)
if err != nil {
panic(err)
}
return builder
}

func (v *vmParams) WithName(name string) (BuildableVMParameters, error) {
if err := validateVMName(name); err != nil {
Expand All @@ -1816,6 +1860,11 @@ func (v *vmParams) WithComment(comment string) (BuildableVMParameters, error) {
return v, nil
}

func (v *vmParams) WithDescription(description string) (BuildableVMParameters, error) {
v.description = description
return v, nil
}

func (v vmParams) Name() string {
return v.name
}
Expand All @@ -1824,12 +1873,17 @@ func (v vmParams) Comment() string {
return v.comment
}

func (v vmParams) Description() string {
return v.description
}

type vm struct {
client Client

id VMID
name string
comment string
description string
clusterID ClusterID
templateID TemplateID
status VMStatus
Expand Down Expand Up @@ -1960,6 +2014,7 @@ func (v *vm) withName(name string) *vm {
v.id,
name,
v.comment,
v.description,
v.clusterID,
v.templateID,
v.status,
Expand Down Expand Up @@ -1987,6 +2042,35 @@ func (v *vm) withComment(comment string) *vm {
v.id,
v.name,
comment,
v.description,
v.clusterID,
v.templateID,
v.status,
v.cpu,
v.memory,
v.tagIDs,
v.hugePages,
v.initialization,
v.hostID,
v.placementPolicy,
v.memoryPolicy,
v.instanceTypeID,
v.vmType,
v.os,
v.serialConsole,
v.soundcardEnabled,
}
}

// withDescription returns a copy of the VM with the new comment. It does not change the original copy to avoid
// shared state issues.
func (v *vm) withDescription(description string) *vm {
return &vm{
v.client,
v.id,
v.name,
v.comment,
description,
v.clusterID,
v.templateID,
v.status,
Expand Down Expand Up @@ -2058,6 +2142,10 @@ func (v *vm) Comment() string {
return v.comment
}

func (v *vm) Description() string {
return v.description
}

func (v *vm) ClusterID() ClusterID {
return v.clusterID
}
Expand Down Expand Up @@ -2114,6 +2202,7 @@ func convertSDKVM(sdkObject *ovirtsdk.Vm, client Client) (VM, error) {
vmIDConverter,
vmNameConverter,
vmCommentConverter,
vmDescriptionConverter,
vmClusterConverter,
vmStatusConverter,
vmTemplateConverter,
Expand Down Expand Up @@ -2283,6 +2372,15 @@ func vmCommentConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
return nil
}

func vmDescriptionConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
description, ok := sdkObject.Description()
if !ok {
return newError(EFieldMissing, "description field missing from VM object")
}
v.description = description
return nil
}

func vmClusterConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
cluster, ok := sdkObject.Cluster()
if !ok {
Expand Down
8 changes: 8 additions & 0 deletions vm_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ func vmBuilderComment(params OptionalVMParameters, builder *ovirtsdk.VmBuilder)
}
}

func vmBuilderDescription(params OptionalVMParameters, builder *ovirtsdk.VmBuilder) {
if description := params.Description(); description != "" {
builder.Description(description)
}
}

func vmBuilderCPU(params OptionalVMParameters, builder *ovirtsdk.VmBuilder) {
if cpu := params.CPU(); cpu != nil {
cpuBuilder := ovirtsdk.NewCpuBuilder()
Expand Down Expand Up @@ -148,6 +154,7 @@ func createSDKVM(
builder.Name(name)
parts := []vmBuilderComponent{
vmBuilderComment,
vmBuilderDescription,
vmBuilderCPU,
vmBuilderHugePages,
vmBuilderInitialization,
Expand Down Expand Up @@ -416,6 +423,7 @@ func (m *mockClient) createVM(
VMID(id),
name,
params.Comment(),
params.Description(),
clusterID,
templateID,
VMStatusDown,
Expand Down
27 changes: 26 additions & 1 deletion vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestAfterVMCreationShouldBePresent(t *testing.T) {
}

updatedVM, err := fetchedVM.Update(
ovirtclient.UpdateVMParams().MustWithName("new_name").MustWithComment("new comment"),
ovirtclient.UpdateVMParams().MustWithName("new_name").MustWithComment("new comment").MustWithDescription("new description"),
)
if err != nil {
t.Fatal(err)
Expand All @@ -80,6 +80,9 @@ func TestAfterVMCreationShouldBePresent(t *testing.T) {
if updatedVM.Comment() != "new comment" {
t.Fatalf("updated VM comment %s does not match update parameters", updatedVM.Comment())
}
if updatedVM.Description() != "new description" {
t.Fatalf("updated VM description %s does not match update parameters", updatedVM.Description())
}

fetchedVM, err = client.GetVM(vm.ID())
if err != nil {
Expand All @@ -94,6 +97,9 @@ func TestAfterVMCreationShouldBePresent(t *testing.T) {
if fetchedVM.Comment() != "new comment" {
t.Fatalf("updated VM comment %s does not match update parameters", fetchedVM.Comment())
}
if fetchedVM.Description() != "new description" {
t.Fatalf("updated VM description %s does not match update parameters", fetchedVM.Description())
}
}

func TestVMCreationWithCPU(t *testing.T) {
Expand All @@ -104,6 +110,7 @@ func TestVMCreationWithCPU(t *testing.T) {
}
for name, param := range params {
t.Run(name, func(t *testing.T) {
param := param
t.Parallel()
helper := getHelper(t)
vm := assertCanCreateVM(
Expand Down Expand Up @@ -230,6 +237,24 @@ func TestVMCreationWithInit(t *testing.T) {
}
}

func TestVMCreationWithDescription(t *testing.T) {
t.Parallel()
testDescription := "test description"
helper := getHelper(t)
vm := assertCanCreateVM(
t,
helper,
fmt.Sprintf("test-%s", helper.GenerateRandomID(5)),
ovirtclient.CreateVMParams().MustWithDescription(testDescription),
)

description := vm.Description()
if description != testDescription {
t.Fatalf("Creating a VM with Description settings did not return a VM with expected Description , %s , %s.", description, testDescription)
}

}

// TestVMStartStop creates a micro VM with a tiny operating system, starts it and then stops it. The OS doesn't support
// ACPI, so shutdown cannot be tested.
func TestVMStartStop(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions vm_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func (o *oVirtClient) UpdateVM(
if comment := params.Comment(); comment != nil {
vm.SetComment(*comment)
}
if description := params.Description(); description != nil {
vm.SetDescription(*description)
}

err = retry(
fmt.Sprintf("updating vm %s", id),
Expand Down Expand Up @@ -71,6 +74,9 @@ func (m *mockClient) UpdateVM(id VMID, params UpdateVMParameters, _ ...RetryStra
if comment := params.Comment(); comment != nil {
vm = vm.withComment(*comment)
}
if description := params.Description(); description != nil {
vm = vm.withDescription(*description)
}
m.vms[id] = vm

return vm, nil
Expand Down

0 comments on commit 768132d

Please sign in to comment.