Skip to content

Commit

Permalink
✨ add function to remove VSphere VM object from VM Group
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinandanbaheti committed Aug 22, 2024
1 parent 75a5e72 commit b1d1567
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/services/govmomi/cluster/vmgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,25 @@ func (vg VMGroup) HasVM(vmObj types.ManagedObjectReference) (bool, error) {
func (vg VMGroup) listVMs() []types.ManagedObjectReference {
return vg.ClusterVmGroup.Vm
}

// Remove a VSphere VM object from the VM Group.
func (vg VMGroup) Remove(ctx context.Context, vmObj types.ManagedObjectReference) (*object.Task, error) {
for i, vm := range vg.ClusterVmGroup.Vm {
if vm == vmObj {
vg.ClusterVmGroup.Vm = append(vg.ClusterVmGroup.Vm[:i], vg.ClusterVmGroup.Vm[i+1:]...)
break
}
}

spec := &types.ClusterConfigSpecEx{
GroupSpec: []types.ClusterGroupSpec{
{
ArrayUpdateSpec: types.ArrayUpdateSpec{
Operation: types.ArrayUpdateOperationEdit,
},
Info: vg.ClusterVmGroup,
},
},
}
return vg.ClusterComputeResource.Reconfigure(ctx, spec, true)
}
9 changes: 9 additions & 0 deletions pkg/services/govmomi/cluster/vmgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ func Test_VMGroup(t *testing.T) {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(hasVM).To(BeTrue())

task, err = vmGrp.Remove(ctx, vmRef)
g.Expect(task.Wait(ctx)).To(Succeed())
g.Expect(err).NotTo(HaveOccurred())
g.Expect(vmGrp.listVMs()).To(HaveLen(2))

hasVM, err = vmGrp.HasVM(vmRef)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(hasVM).To(BeFalse())

vmGroupName = "incorrect-vm-group"
_, err = FindVMGroup(ctx, computeClusterCtx, computeClusterName, vmGroupName)
g.Expect(err).To(HaveOccurred())
Expand Down

0 comments on commit b1d1567

Please sign in to comment.